最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure devops - How to add reviewer to AD Pull request with API call - Stack Overflow

programmeradmin6浏览0评论

I'm trying to add a reviewer to a pull request in Azure DevOps using the REST API, but it doesn't seem to work. I attempted to use both the username and email, but neither worked.

Here is the relevant part of my Groovy script:

if (codeReviewer) {
def reviewerId = getUserGUID(codeReviewer)
println("reviewerId: " + reviewerId)

if (reviewerId) {
    callAzureDevopsRestAPI([
        'azureRestUrlSuffix': "${GIT_PROJECT}/_apis/git/repositories/${GIT_REPO}/pullrequests/${pr.pullRequestId}/reviewers",
        'httpMode': 'POST',
        'requestBody': "{ \"id\": \"$reviewerId\" }"
    ])
}

def getUserGUID(String reviewer) {
    def response = callAzureDevopsRestAPI([
        'azureRestUrlSuffix': "_apis/graph/userentitlements?api-version=7.1-preview.1",
        'httpMode': 'GET'
    ])

    def jsonResponse = new groovy.json.JsonSlurper().parseText(response.data.toString())

    def user = jsonResponse.value.find {
        it.principalName?.equalsIgnoreCase(reviewer) ||
        it.displayName?.equalsIgnoreCase(reviewer) ||
        it.mail?.equalsIgnoreCase(reviewer)
    }

    return user ? user.descriptor : null
}

I tried using both the email and display name to fetch the user descriptor. The API call to get the GUID fails no matter what I try.

Has anyone successfully added a reviewer to a pull request via the Azure DevOps API? Am I missing something in the request body or using the wrong user identifier?

Any help would be greatly appreciated!

I'm trying to add a reviewer to a pull request in Azure DevOps using the REST API, but it doesn't seem to work. I attempted to use both the username and email, but neither worked.

Here is the relevant part of my Groovy script:

if (codeReviewer) {
def reviewerId = getUserGUID(codeReviewer)
println("reviewerId: " + reviewerId)

if (reviewerId) {
    callAzureDevopsRestAPI([
        'azureRestUrlSuffix': "${GIT_PROJECT}/_apis/git/repositories/${GIT_REPO}/pullrequests/${pr.pullRequestId}/reviewers",
        'httpMode': 'POST',
        'requestBody': "{ \"id\": \"$reviewerId\" }"
    ])
}

def getUserGUID(String reviewer) {
    def response = callAzureDevopsRestAPI([
        'azureRestUrlSuffix': "_apis/graph/userentitlements?api-version=7.1-preview.1",
        'httpMode': 'GET'
    ])

    def jsonResponse = new groovy.json.JsonSlurper().parseText(response.data.toString())

    def user = jsonResponse.value.find {
        it.principalName?.equalsIgnoreCase(reviewer) ||
        it.displayName?.equalsIgnoreCase(reviewer) ||
        it.mail?.equalsIgnoreCase(reviewer)
    }

    return user ? user.descriptor : null
}

I tried using both the email and display name to fetch the user descriptor. The API call to get the GUID fails no matter what I try.

Has anyone successfully added a reviewer to a pull request via the Azure DevOps API? Am I missing something in the request body or using the wrong user identifier?

Any help would be greatly appreciated!

Share Improve this question edited Mar 12 at 23:05 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 12 at 10:51 BernanaBernana 2472 silver badges13 bronze badges 1
  • Hi @Bernana, Good day. Have you got a chance to test the answer by haakonsollund to retrieve the reviewer' GUID first? Hope it helps resolve your issue in this post. Thx for the sharing. – Alvin Zhao Commented Mar 13 at 5:10
Add a comment  | 

1 Answer 1

Reset to default 0

To call the Azure DevOps REST API to add a user as a reviewer of a pull request, you can do like as below:

  1. Use the API "Identities - Read Identities" to get the id of the user by email.

    GET https://vssps.dev.azure/{anization}/_apis/identities?searchFilter=General&filterValue={user-email-address}&queryMembership=None&api-version=7.1
    
  2. Then use the API "Pull Request Reviewers - Create Pull Request Reviewer" to add the user as a reviewer to the specified pull request. you need to pass the user's id to request URI and the request body of this API.

    • Request URI: Replace {reviewerId} with the user's id you get above.
      PUT https://dev.azure/{anization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/reviewers/{reviewerId}?api-version=7.1
      
    • Request Body: Replace {reviewerId} with the user's id you get above.
      {
          "id": "{reviewerId}",
          "isRequired": true,
          "vote": 0
      }
  3. You also can use the API "Pull Request Reviewers - Create Pull Request Reviewers" to add multiple users as the reviewers to the specified pull request at once, if you have gotten the id of each user.

    • Request URI:
      POST https://dev.azure/{anization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/reviewers?api-version=7.1
      
    • Request Body:
      [
          {
              "id": "{reviewerId-01}",  // Id of user01.
               "isRequired": true,
               "vote": 0
          },
          {
              "id": "{reviewerId-02}",  // Id of user02.
               "isRequired": true,
               "vote": 0
          }
      ]
发布评论

评论列表(0)

  1. 暂无评论