nb1
nb1

Reputation: 21

VSTS REST API - GIT - Get Branch Policies

Is it possible to get a list of (git) branches using the REST API in VSTS that have a policy?

https://learn.microsoft.com/en-us/rest/api/vsts/git/refs/list

The response returns if the branch is locked, but not if it contains a policy.

Upvotes: 2

Views: 2103

Answers (1)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

You can use the Policy API and get a list with all the branches with policies.

Get a list of policy configurations in a project.

GET https://dev.azure.com/{organization}/{project}/_apis/policy/configurations?api-version=4.1

Sample response:

{
  "count": 3,
  "value": [
    {
      "createdBy": {
        "id": "d6245f20-2af8-44f4-9451-8107cb2767db",
        "displayName": "Normal Paulk",
        "uniqueName": "[email protected]",
        "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db",
        "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db"
      },
      "createdDate": "2015-02-23T12:51:06.85754Z",
      "isEnabled": true,
      "isBlocking": true,
      "isDeleted": false,
      "settings": {
        "requiredReviewerIds": [
          "1d1dad71-f27c-4370-810d-838ec41efd41",
          "13272ea3-92ef-46d1-b77e-608ebbf3428b"
        ],
        "filenamePatterns": [
          "*/API*.cs",
          "sql/tables/*"
        ],
        "addedFilesOnly": false,
        "message": null,
        "scope": [
          {
            "refName": "refs/heads/master",
            "matchKind": "Exact",
            "repositoryId": null
          },
          {
            "refName": "refs/heads/releases/",
            "matchKind": "Prefix",
            "repositoryId": null
          },
          {
            "refName": "refs/heads/adventureworks",
            "matchKind": "Exact",
            "repositoryId": "49c1d4d0-be28-4f20-9e0a-4ecfafc39257"
          }
        ]
      },
      "_links": {
        "self": {
          "href": "https://dev.azure.com/fabrikam/_apis/policy/configurations/17"
        },
        "policyType": {
          "href": "https://dev.azure.com/fabrikam/1be3fc5b-c58c-4173-8fd7-6647d11eccd1/_apis/policy/types/fd2167ab-b0be-447a-8ec8-39368250530e"
        }
      },
      "revision": 2,
      "id": 17,
      "url": "https://dev.azure.com/fabrikam/_apis/policy/configurations/17",
      "type": {
        "id": "fd2167ab-b0be-447a-8ec8-39368250530e",
        "url": "https://dev.azure.com/fabrikam/1be3fc5b-c58c-4173-8fd7-6647d11eccd1/_apis/policy/types/fd2167ab-b0be-447a-8ec8-39368250530e",
        "displayName": "Required reviewers"
      }
    },
    {
      "createdBy": {
        "id": "d6245f20-2af8-44f4-9451-8107cb2767db",
        "displayName": "Normal Paulk",
        "uniqueName": "[email protected]",
        "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db",
        "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db"
      },
      "createdDate": "2015-02-23T12:51:06.8887894Z",
      "isEnabled": true,
      "isBlocking": false,
      "settings": {
        "minimumApproverCount": 1,
        "creatorVoteCounts": false,
        "scope": [
          {
            "refName": "refs/heads/master",
            "matchKind": "Exact",
            "repositoryId": null
          }
        ]
      },
      "_links": {
        "self": {
          "href": "https://dev.azure.com/fabrikam/_apis/policy/configurations/18"
        },
        "policyType": {
          "href": "https://dev.azure.com/fabrikam/1be3fc5b-c58c-4173-8fd7-6647d11eccd1/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd"
        }
      },
      "revision": 1,
      "id": 18,
      "url": "https://dev.azure.com/fabrikam/_apis/policy/configurations/18",
      "type": {
        "id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
        "url": "https://dev.azure.com/fabrikam/1be3fc5b-c58c-4173-8fd7-6647d11eccd1/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
        "displayName": "Minimum approval count"
      }
    },
    {
      "createdBy": {
        "id": "d6245f20-2af8-44f4-9451-8107cb2767db",
        "displayName": "Normal Paulk",
        "uniqueName": "[email protected]",
        "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db",
        "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db"
      },
      "createdDate": "2015-02-23T12:51:06.935666Z",
      "isEnabled": true,
      "isBlocking": false,
      "settings": {
        "buildDefinitionId": 5,
        "scope": [
          {
            "refName": "refs/heads/features/",
            "matchKind": "Prefix",
            "repositoryId": null
          }
        ]
      },
      "_links": {
        "self": {
          "href": "https://dev.azure.com/fabrikam/_apis/policy/configurations/19"
        },
        "policyType": {
          "href": "https://dev.azure.com/fabrikam/1be3fc5b-c58c-4173-8fd7-6647d11eccd1/_apis/policy/types/0609b952-1397-4640-95ec-e00a01b2c241"
        }
      },
      "revision": 1,
      "id": 19,
      "url": "https://dev.azure.com/fabrikam/_apis/policy/configurations/19",
      "type": {
        "id": "0609b952-1397-4640-95ec-e00a01b2c241",
        "url": "https://dev.azure.com/fabrikam/1be3fc5b-c58c-4173-8fd7-6647d11eccd1/_apis/policy/types/0609b952-1397-4640-95ec-e00a01b2c241",
        "displayName": "Build"
      }
    }
  ]
}

In the refName in the scope section you can see the branch name with the policy, in the policyType you have a link to get

the policy type description.

Upvotes: 3

Related Questions