azureazure-devopsazure-active-directoryazure-functionsazure-rest-api

Invalid argument error when adding Required Reviewer via Azure DevOps API


I can add myself as a Required Reviewer on a Pull Request in Azure DevOps without any issues. However, when I attempt to do this using the API, I encounter the following error:

{"$id":"1","innerException":null,"message":"Invalid argument value.\r\nParameter name: A valid reviewer must be supplied.","typeName":"Microsoft.TeamFoundation.SourceControl.WebServer.InvalidArgumentValueException, Microsoft.TeamFoundation.SourceControl.WebServer","typeKey":"InvalidArgumentValueException","errorCode":0,"eventId":0}

I tried the following reviewerId:


Solution

  • You can do like as below:

    1. Use the Azure DevOps REST API "Identities - Read Identities" to get the ID of user that needs to be added as a PR reviewer.

    2. Use the Azure DevOps REST API "Pull Request Reviewers - Create Pull Request Reviewer" to add the user as a required reviewer on the specified PR.

    Below is a sample of PowerShell script to show how to call these two REST API.

    $organization = "xxxx"  # The name of Azure DevOps organization.
    $userMailAddress = "xxxx"  # The user email address that needs to be added as a PR reviewer.
    $project = "xxxx"  # The name of project where the git repository is.
    $repository = "xxxx"  # The name of git repository where the PR is.
    $pullRequestId = 1111  # The ID of PR.
    
    $pat = "xxxx"  # The Personal Access Token.
    $base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
    $headers = @{
        Authorization = "Basic $base64Token"
        "Content-Type" = "application/json"
    }
    
    # Get the user ID.
    $uri_read_id = "https://vssps.dev.azure.com/${organization}/_apis/identities?searchFilter=General&filterValue=${userMailAddress}&queryMembership=None&api-version=7.1"
    $user_id = (Invoke-RestMethod -Method GET -Uri $uri_read_id -Headers $headers).value[0].id
    
    # Add the user as a required reviewer on the specified PR.
    $uri_add_pr_reviewer = "https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repository}/pullRequests/${pullRequestId}/reviewers/${user_id}?api-version=7.1"
    
    $body_add_pr_reviewer = @{
        id = $user_id
        isRequired = $True
        vote = 0
    } | ConvertTo-Json -Depth 5
    
    Invoke-RestMethod -Method PUT -Uri $uri_add_pr_reviewer -Headers $headers -Body $body_add_pr_reviewer
    

    EDIT:

    To get add a group/team as the reviewer on a PR, you also can use the API "Identities - Read Identities" to get the ID of the group/team, and use the API "Pull Request Reviewers - Create Pull Request Reviewer" to add the it as PR reviewer.

    To get the ID of a group/team, you can pass the subject descriptor of it to the API "Identities - Read Identities". This method also can get the ID of a user.

    GET https://vssps.dev.azure.com/{organization}/_apis/identities?subjectDescriptors={subjectDescriptors}&queryMembership=None&api-version=7.1
    

    The sample of PowerShell script to call the API.

    $subjectDescriptor = "xxxx"  # The subject descriptor of user, group or team that needs to be added as a PR reviewer.
    $organization = "xxxx"  # The name of Azure DevOps organization.
    $project = "xxxx"  # The name of project where the git repository is.
    $repository = "xxxx"  # The name of git repository where the PR is.
    $pullRequestId = 1111  # The ID of PR.
    
    $pat = "xxxx"  # The Personal Access Token.
    $base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
    $headers = @{
        Authorization = "Basic $base64Token"
        "Content-Type" = "application/json"
    }
    
    # Get the ID of user, group or team.
    $uri_read_id = "https://vssps.dev.azure.com/${organization}/_apis/identities?subjectDescriptors=${subjectDescriptor}&queryMembership=None&api-version=7.1"
    $reviewer_id = (Invoke-RestMethod -Method GET -Uri $uri_read_id -Headers $headers).value[0].id
    
    # Add the user, group or team as a required reviewer on the specified PR.
    $uri_add_pr_reviewer = "https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repository}/pullRequests/${pullRequestId}/reviewers/${reviewer_id}?api-version=7.1"
    
    $body_add_pr_reviewer = @{
        id = $reviewer_id
        isRequired = $True
        vote = 0
    } | ConvertTo-Json -Depth 5
    
    Invoke-RestMethod -Method PUT -Uri $uri_add_pr_reviewer -Headers $headers -Body $body_add_pr_reviewer