Azure / azure-devops-cli-extension

Azure DevOps Extension for Azure CLI
https://docs.microsoft.com/en-us/cli/azure/ext/azure-devops/?view=azure-cli-latest
MIT License
628 stars 241 forks source link

Cannot pass values to `devops invoke` with --route-parameters #1300

Closed jsuddsjr closed 2 years ago

jsuddsjr commented 2 years ago

Describe the bug

Cannot pass values to the route with --route-parameters.

Command Name az devops invoke Extension Name: azure-devops. Version: 0.25.0.

Debugging INFO: cli.azext_devops.dev.team.invoke: inputList is ['project=', 'MAX', 'wikiIdentifier=', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx']

Note that the arguments from --route-parameters are not sent as 'key=value' pairs. Rather, they are sent as separate arguments for key=, value. The current code tries to split these elements into two values at the '=' but fails when the second item in the list isn't in the expected format.

Here's the code from the latest version of azext_devops\dev\team\invoke.py.

def stringToDict(inputList):
    if not inputList:
        return {}

    result = {}

    for inputSet in inputList:
        parts = inputSet.split('=', 1)
        if len(parts) != 2:
            raise CLIError('%s is not valid it needs to be in format param=value' % (inputSet))
        key = parts[0]
        value = parts[1]
        result[key] = value

    return result

Expected Behavior

This code has been updated to handle the input format.

def stringToDict(inputList):
    if not inputList:
        return {}

    logger.info('inputList is %s', inputList)

    if (len(inputList) % 2) != 0:
        raise CLIError('Please provide even number of arguments.')

    keys = inputList[::2]
    values = inputList[1::2]

    result = {}

    for key, value in zip(keys, values):
        result[key.rstrip('=')] = value

    logger.info('stringToDict result is %s', result)

    return result

Environment Summary

Windows-10-10.0.25197-SP0
Python 3.10.5
Installer: MSI

azure-cli 2.40.0

Extensions:
arcdata 1.4.5
azure-devops 0.25.0

Dependencies:
msal 1.18.0b1
azure-mgmt-resource 21.1.0b1

Additional Context

jsuddsjr commented 2 years ago

After further investigation, this bug is caused by the idiosyncrasies of PowerShell. When setting variables in the command line, these values are passed as separate arguments.

--route-parameters project=$($projectId) wikiIdentifier=$($wikiId)

To work around this, you must quote the value as strings.

--route-parameters "project=$projectId" "wikiIdentifier=$wikiId"