azure-devopsazure-pipelines

How to add/update approvers for environments through REST API on Azure Devops


enter image description here

I am trying to make hundreds of environments on Azure Devops using a PowerShell script and REST API. I need to include approvers on some of it.
I can already create those environments, I just need to add/update the approvers for it.
How can I do that? I can't seem to find anything on the documentation


Solution

  • To add /update the approvers for environments, you can use the following Rest API:

    Add approvers for environment:

    Rest API:

    Post https://dev.azure.com/Orgname/Project/_apis/pipelines/checks/configurations?api-version=api-version=7.1-preview.1
    

    Request Body:

    {
        "type":{
            "id":"8C6F20A7-A545-4486-9777-F762FAFE0D4D",
            "name":"Approval"
        },
        "settings":{
             "approvers":[{"displayName":"{UserName}","id":"{UserID}"}],
             "executionOrder":1,
             "blockedApprovers":[],
             "minRequiredApprovers":0,
             "requesterCannotBeApprover":false
        },
         "resource":
             {
                 "type":"environment",
                 "id":"{EnvironmentID}",
                 "name":"{EnvironmentName}"
             }
    }
    

    Update approvers for environment:

    Rest API:

    PATCH https://dev.azure.com/{organization}/{project}/_apis/pipelines/checks/configurations/{Configurationid}?api-version=7.1-preview.1
    

    Request Body:

    {
        "type":{
            "id":"8C6F20A7-A545-4486-9777-F762FAFE0D4D",
            "name":"Approval"
        },
        "settings":{
             "approvers":[
                 {
                 "displayName":"{Username}",
                 "id":"{UserID}"
                 },
                 {
                 "displayName":"{Username}",
                 "id":"{UserID}"
                 }
                 ],
             "executionOrder":1,
             "blockedApprovers":[],
             "minRequiredApprovers":0,
             "requesterCannotBeApprover":false
        },
         "resource":
             {
                 "type":"environment",
                 "id":"{EnvironmentID}",
                 "name":"{EnvironmentName}"
             }
    
    }
    

    For the configuartionID, you can get the ID in the following Rest API: Check Configurations - List

    Since the requirement has no official sample, you can check the NetWork trace for more detailed info.

    For example: Manually add/update the approvers and Check network tab in Browser Developer tool.

    enter image description here