2

I'm successfully creating PR requests in Azure DevOps using API-call. However, I would like to add the reviewer's name to my PR. As per the sample in the link, I have to add the reviewer id in the body. So, my question is how to dynamically find the reviewer's id prior to submitting the PR from my project? I was following Pull Request Reviewers and nothing seems coming up to provide me the id based on name. As per branch policy, I have to add 2 reviewers' name.

{
  "sourceRefName": "refs/heads/npaulk/my_work",
  "targetRefName": "refs/heads/new_feature",
  "title": "A new feature",
  "description": "Adding a new feature",
  "reviewers": [
    {
      "id": "d6245f20-2af8-44f4-9451-8107cb2767db"
    }
  ]
}
change198
  • 1,647
  • 3
  • 21
  • 60

3 Answers3

1

Like @Krzysztof Madej suggested in his answer, you can use the Subject Query endpoint to search and get the GraphSubject response.

However, the Id values in the GraphSubject response does not work for the IdentityRef Id used as parameter for the Pull Request Reviewers endpoint (used to add Reviewers to an existing pull request).

To get the correct IdentityRef Id, you need to do a GET on the URL from the storageKey.href value in the GraphSubject Response. E.g.:

"storageKey": {
                "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/StorageKeys/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
            },

The response should look something like this:

"value": "73b67dcb-6969-62f2-8075-99834ae11234",
"_links": {
    "self": {
        "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/StorageKeys/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
    },
    "descriptor": {
        "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/Descriptors/73b67dcb-6969-62f2-8075-99834ae11234"
    }
}   

The GUID for value is what you use for IdentityRef.Id. The payload to POST to the Pull Request Reviewers endpoint would look something like this:

[
  {
    "id": "73b67dcb-6969-62f2-8075-99834ae11234"
  }
]
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Hungvuong
  • 11
  • 1
  • Disappointing it seems the API has changed because this doesn't hold true anymore. The storage key no longer has a value in the returned object. – Shawn Melton Jun 15 '23 at 16:30
0

You can use Subject Query Endpoint

POST https://vssps.dev.azure.com/{organization}/_apis/graph/subjectquery?api-version=6.0-preview.1

Body should look like this:

{
    "query": "Term to search (e.g. Krzysztof)",
    "subjectKind": [ "User" ]
}

and then you will get response like this:

{
    "count": 3,
    "value": [
        {
            "subjectKind": "user",
            "metaType": "member",
            "domain": "Windows Live ID",
            "principalName": "mail@mail.com,
            "mailAddress": "mail@mail.com",
            "origin": "msa",
            "originId": "0006BFFDBC3FE9A1",
            "displayName": "Krzysztof Madej",
            "_links": {
                "self": {
                    "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/Users/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
                },
                "memberships": {
                    "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/Memberships/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
                },
                "membershipState": {
                    "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/MembershipStates/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
                },
                "storageKey": {
                    "href": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/StorageKeys/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
                },
                "avatar": {
                    "href": "https://dev.azure.com/thecodemanual/_apis/GraphProfile/MemberAvatars/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
                }
            },
            "url": "https://vssps.dev.azure.com/thecodemanual/_apis/Graph/Users/msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3",
            "descriptor": "msa.MDQ5MGM0N2ItODNiNC03MmEzLTk2MzgtZTJhMmNjOTY3NWQ3"
        },

as next ise originId in reviewers collection.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
0

You can use Identities - Read Identities API to get user id. For example:

Get https://vssps.dev.azure.com/{org}/_apis/identities?searchFilter=General&filterValue=cece dong&api-version=6.1-preview.1
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • I tried to use `https://vssps.dev.azure.com//_apis/identities?searchFilter=General&filterValue=random user name&queryMembership=None&api-version=6.0` --> This throws me error `"Message":"Either descriptors or identityIds or searchFactor/factorValue must be specified"` – change198 Nov 03 '20 at 11:25
  • Did you run the api from a script? Could you share more about how you use this api? I've tested this api in Postman, it works as expected. – Cece Dong - MSFT Nov 04 '20 at 01:32
  • I will update a bit later haven't got time further to look into. Will look into this again very soon. thanks. – change198 Nov 06 '20 at 11:52
  • @ Cece Dong - MSFT has not got a chance to take a look at this because stuck with some another AzDo issue, maybe you can help? https://stackoverflow.com/questions/64824802/trigger-another-build-exist-in-project-in-azure-devops/64852348#64852348 – change198 Nov 17 '20 at 23:52
  • I've noticed the issue is solved in that link. If there is any update for this case, kindly let us know. – Cece Dong - MSFT Nov 20 '20 at 07:36
  • 1
    It's working now, if I give the filterValue=`firstname lastname` --> space in between it doesn't work also `firstname, lastname` also doesn't work so i search using email address it works fine. like this `newurl="https://vssps.dev.azure.com/organisation/_apis/identities?searchFilter=General&filterValue=a.b@random.io&queryMembership=None&api-version=6.1-preview.1" ret=$(curl --silent -X GET -H "Authorization:Bearer $(System.AccessToken)" -H "Content-Type:application/json" ${newurl} --write-out "%{http_code}" --output /tmp/response1.json)` – change198 Nov 26 '20 at 18:06
  • Values of `searchFilter` can be AccountName (domain\alias), DisplayName, MailAddress, General (display name, account name, or unique name), or LocalGroupName (only search Azure Devops groups). And `filterValue` is the search value, as specified by the `searchFilter`. You may check whether you have the correct syntax. Any way, great to see your issue is solved, you could [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), this can be beneficial to other community members reading this thread. – Cece Dong - MSFT Nov 27 '20 at 01:57