-
Notifications
You must be signed in to change notification settings - Fork 3
180 lines (167 loc) · 7.76 KB
/
backport-action.yml
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
name: Launch Backport Job
on:
workflow_call:
inputs:
pull_request_url:
description: URL of the pull request from the issue comment event
type: string
required: true
target_branch:
description: Branch to backport the changes
type: string
required: true
comment_author:
description: Author of the PR comment
type: string
required: true
github_repository:
description: GitHub Repository where the comment was written
type: string
required: true
use_fork:
description: The action should create a PR from a fork
type: boolean
required: false
default: false
secrets:
ado_organization:
description: Azure DevOps organization that hosts the backport job
required: true
ado_project:
description: Azure DevOps project that hosts the backport job
required: true
backport_pipeline_id:
description: ID of the backport yaml pipeline in Azure DevOps
required: true
ado_build_pat:
description: PAT of an account that can launch builds on the backport pipeline in Azure DevOps
required: true
github_account_pat:
description: PAT for a github account that has write access to source repository
required: true
jobs:
launch_ado_build:
runs-on: ubuntu-latest
# GITHUB_TOKEN change from read-write to read-only on 2024-02-01 requiring permissions block
# https://docs.opensource.microsoft.com/github/apps/permission-changes/
# https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
permissions:
actions: none
contents: read
security-events: none
env:
# Protect against script injection attacks via input variables (i.e., the content of the variables could be executed at the time of evaluation/expansion within a script)
# Scripts must consume the environment variable settings instead
GITHUB_REPOSITORY: "${{ inputs.github_repository }}"
GITHUB_ACCOUNT_PAT: "${{ secrets.github_account_pat }}"
COMMENT_AUTHOR: "${{ inputs.comment_author }}"
PULL_REQUEST_URL: "${{ inputs.pull_request_url }}"
TARGET_BRANCH: "${{ inputs.target_branch }}"
USE_FORK: "${{ inputs.use_fork }}"
ADO_ORGANIZATION: "${{ secrets.ado_organization }}"
ADO_PROJECT: "${{ secrets.ado_project }}"
ADO_PIPELINE_ID: "${{ secrets.backport_pipeline_id }}"
ADO_BUILD_PAT: "${{ secrets.ado_build_pat }}"
steps:
- name: Gather Parameters
id: get_parameters
run: |
$statusCode = 0
$message = ""
try {
$gitHubRepository = $env:GITHUB_REPOSITORY
$gitHubAccountPAT = $env:GITHUB_ACCOUNT_PAT
$commentAuthor = $env:COMMENT_AUTHOR
$pullRequestUrl = $env:PULL_REQUEST_URL
$backportTargetBranch = $env:TARGET_BRANCH
$useFork = $env:USE_FORK
Write-Host "GITHUB_REPOSITORY: ${gitHubRepository}"
Write-Host "COMMENT_AUTHOR: ${commentAuthor}"
Write-Host "PULL_REQUEST_URL: ${pullRequestUrl}"
Write-Host "TARGET_BRANCH: ${backportTargetBranch}"
Write-Host "USE_FORK: ${useFork}"
($repoOwner, $repoName) = $gitHubRepository.Split("/")
$headers = $headers = @{ Authorization = "token ${gitHubAccountPAT}"}
$uri = "https://api.github.com/repos/${repoOwner}/${repoName}/collaborators/${commentAuthor}/permission"
Write-Host "Checking ${repoOwner} membership for ${commentAuthor} via $uri"
$response = Invoke-WebRequest -Headers $headers -Uri $uri
$content = $response.Content | ConvertFrom-Json
$accessType = $content.permission
Write-Host "Found membership: $accessType"
if (-not ($accessType.Equals("admin") -or $accessType.Equals("write"))) {
throw "${commentAuthor}/permission does not have sufficient permissions for ${gitHubRepository}"
}
Write-Host "Grabbing PR details from ${pullRequestUrl}"
$response = Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri "${pullRequestUrl}" | ConvertFrom-Json
$backportPRNumber = $response.number
$parameters = @{
BackportRepoName = "$repoName";
BackportRepoOrg = "$repoOwner";
BackportTargetBranch = "$backportTargetBranch";
BackportPRNumber = "$backportPRNumber";
BackportHeadSHA = "$($response.head.sha)";
UseFork = $useFork -eq "true";
} | ConvertTo-Json -Compress
$json = $parameters.Replace("`"","'")
Write-Host "Setting output variables"
echo "::set-output name=parameters::$json"
echo "::set-output name=pr_number::$backportPRNumber"
} catch {
Write-Host $_.Exception.Message
$statusCode = 1
}
return $statusCode
shell: pwsh
- run: |
$adoOrganization = $env:ADO_ORGANIZATION
$adoProject = $env:ADO_PROJECT
$adoPipelineId = $env:ADO_PIPELINE_ID
$adoBuildPAT = $env:ADO_BUILD_PAT
$gitHubRepository = $env:GITHUB_REPOSITORY
$gitHubAccountPAT = $env:GITHUB_ACCOUNT_PAT
Write-Host "ADO_ORGANIZATION: ${adoOrganization}"
Write-Host "ADO_PROJECT: ${adoProject}"
Write-Host "ADO_PIPELINE_ID: ${adoPipelineId}"
Write-Host "GITHUB_REPOSITORY: ${gitHubRepository}"
$message = ""
$statusCode = 0
try {
$launchURI = "https://dev.azure.com/${adoOrganization}/${adoProject}/_apis/pipelines/${adoPipelineId}/runs?api-version=6.0-preview.1"
Write-Host "Grabbing parameters from prior step"
$parameters = ConvertFrom-Json "${{ steps.get_parameters.outputs.parameters }}"
Write-Host "$parameters"
$jsonBody = @{
previewRun = false;
templateParameters = $parameters;
resources = @{
repositories = @{
self = @{
refName = "refs/heads/main"
}
}
};
} | ConvertTo-Json -Depth 10
Write-Host "Posting to $launchURI"
Write-Host $jsonBody
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":${adoBuildPAT}"))
$headers = @{ Authorization = "Basic $encoded"}
$response = Invoke-WebRequest -UseBasicParsing -Method POST -Headers $headers -ContentType "application/json" -Uri $launchURI -Body $jsonBody
$responseJson = ConvertFrom-Json $response.Content
echo "Job successfully launched"
$message = "Backport Job to branch **$($parameters.BackportTargetBranch)** Created! The magic is happening [here](https://dev.azure.com/${adoOrganization}/${adoProject}/_build/results?buildId=$($responseJson.id))"
} catch {
Write-Host $_.Exception.Message
$message = "I couldn't create a backport to **$($parameters.BackportTargetBranch)** for you. :( Please check the Action logs for more details."
$statusCode = 1
} finally {
$jsonBody = @{
body = $message
} | ConvertTo-Json
$headers = @{ Authorization = "token ${gitHubAccountPAT}"}
$uri = "https://api.github.com/repos/${gitHubRepository}/issues/${{ steps.get_parameters.outputs.pr_number }}/comments"
Write-Host "Posting to $uri"
Write-Host "$jsonBody"
Invoke-WebRequest -UseBasicParsing -Headers $headers -Method POST -Uri $uri -Body $jsonBody
}
return $statusCode
shell: pwsh