-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-BranchPolicies.ps1
58 lines (46 loc) · 2.11 KB
/
Get-BranchPolicies.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[CmdLetBinding()]
param(
$organization = "Microsoft",
$project = "OSGTools",
$repositoryName = "ES.Build.Rings.Configuration",
$branchName = "refs/heads/main",
$filenamePattern = "/rings/coreBuildTeamTools/*",
$policyType = "pullRequest",
$azureDevopsResourceId = "499b84ac-1321-427f-aa17-267ca6975798")
Write-Verbose "repositoryName: $repositoryName"
$token = az account get-access-token --resource $azureDevopsResourceId | ConvertFrom-Json
$personalAccessToken = $token.accessToken
Write-Verbose "$personalAccessToken"
$authValue = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":" + $personalAccessToken))
$headers = @{
Authorization = "Basic $authValue"
'X-VSS-ForceMsaPassThrough' = $true
}
Write-Verbose "repositoryName: $repositoryName"
# Get the repository ID
$url = "https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repositoryName}?api-version=6.0"
Write-Verbose "$url"
$global:repositoryResponse = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
$repositoryId = $repositoryResponse.id
Write-Verbose "repositoryId: $repositoryId"
# Get the branch policies
$url = "https://dev.azure.com/${organization}/${project}/_apis/policy/configurations?repositoryId=${repositoryId}&refName=${branchName}&policyType=${$policyType}&api-version=6.0"
Write-Verbose "$url"
$policiesResponse = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
# Extract policy IDs
$policyIds = $policiesResponse.value | ForEach-Object { $_ }
$policyIds = $policyIds | Where-Object {
$_.settings.scope.refName -eq $branchName -and
$_.settings.scope.repositoryId -eq $repositoryId -and
$_.isEnabled -and $_.isBlocking -and
(-not $_.isDeleted) }
# $policyIds
$policyIds = $policyIds |
Select-Object @{Name="Id";Expression={$_.Id}},
@{Name="Name";Expression={$_.type.displayName}},
@{Name="Type";Expression={$_.type.id}},
@{Name="FileNamePatterns";Expression={$_.settings.fileNamePatterns}},
@{Name="Rest";Expression={$_}}
$policyIds |
Where-Object { $_.FileNamePatterns -eq $null -or $_.FileNamePatterns -contains $fileNamePattern } |
Select-Object -Property Id,Name