-
Notifications
You must be signed in to change notification settings - Fork 390
/
powershell.ps1
72 lines (63 loc) · 2.38 KB
/
powershell.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Configuration
$organization = "YOUR_ORG"
$project = "YOUR_PROJECT"
$pat = "YOUR_PERSONAL_ACCESS_TOKEN"
$templatePath = "C:\path\to\your\pull_request_template.md"
# Read the template content
$templateContent = Get-Content -Raw -Path $templatePath
# Base64 encode the PAT for HTTP Basic Auth
$encodedPat = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
# Function to get all repositories
function Get-Repos {
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories?api-version=6.0"
$headers = @{
Authorization = "Basic $encodedPat"
}
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
return $response.value
}
# Function to create or update pull_request_template.md
function Upload-Template($repoId) {
$templatePath = "pull_request_template.md"
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repoId/pushes?api-version=6.0"
$headers = @{
Authorization = "Basic $encodedPat"
Content-Type = "application/json"
}
# Get the latest commit ID of the main branch
$branchUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repoId/refs?filter=heads/main&api-version=6.0"
$branchResponse = Invoke-RestMethod -Uri $branchUrl -Headers $headers -Method Get
$latestCommitId = $branchResponse.value[0].objectId
# Create the push object
$pushData = @{
refUpdates = @(@{
name = "refs/heads/main"
oldObjectId = $latestCommitId
})
commits = @(@{
comment = "Add pull request template"
changes = @(@{
changeType = "add"
item = @{
path = $templatePath
}
newContent = @{
content = $templateContent
contentType = "rawtext"
}
})
})
} | ConvertTo-Json -Depth 4
# Push the template to the repository
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $pushData
if ($response -ne $null) {
Write-Output "Template uploaded to repo $repoId"
} else {
Write-Output "Failed to upload template to repo $repoId"
}
}
# Get all repositories and upload the template
$repos = Get-Repos
foreach ($repo in $repos) {
Upload-Template -repoId $repo.id
}