forked from xamarin/backport-bot-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
134 lines (123 loc) · 5.71 KB
/
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
name: Backport Pull Request from Comment
description: Launches a Backport Job from a GitHub Comment on an Issue
author: Connor Adsit <[email protected]>
inputs:
pull_request_url:
description: URL of the pull request from the issue comment event
required: true
comment_author:
description: Author of the PR comment
required: true
comment_body:
description: Message of the comment on a PR
required: true
github_repository:
description: GitHub Repository where the comment was written
required: true
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
use_fork:
description: The action should create a PR from a fork
required: false
default: 'false'
runs:
using: "composite"
steps:
- name: Gather Parameters
id: get_parameters
run: |
$statusCode = 0
$message = ""
try {
($repoOwner, $repoName) = "${{ inputs.github_repository }}".Split("/")
$headers = $headers = @{ Authorization = "token ${{ inputs.github_account_pat }}"}
$uri = "https://api.github.com/repos/$repoOwner/$repoName/collaborators/${{ inputs.comment_author }}/permission"
Write-Host "Checking $repoOwner membership for ${{ inputs.comment_author }} 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 "${{ inputs.comment_author }}/permission does not have sufficient permissions for ${{ inputs.github_repository }}"
}
Write-Host "Parsing ${{ inputs.comment_body }}"
($botName, $backport, $backportTargetBranch) = [System.Text.RegularExpressions.Regex]::Split("${{ inputs.comment_body }}", "\s+")
Write-Host "Grabbing PR details from ${{ inputs.pull_request_url }}"
$response = Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri "${{ inputs.pull_request_url }}" | ConvertFrom-Json
$backportPRNumber = $response.number
$parameters = @{
BackportRepoName = "$repoName";
BackportRepoOrg = "$repoOwner";
BackportTargetBranch = "$backportTargetBranch";
BackportPRNumber = "$backportPRNumber";
BackportHeadSHA = "$($response.head.sha)";
UseFork = "${{ inputs.use_fork }}" -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
- name: Launch ADO Build
id: ado_build
run: |
$message = ""
$statusCode = 0
try {
$launchURI = "https://dev.azure.com/${{ inputs.ado_organization }}/${{ inputs.ado_project }}/_apis/pipelines/${{ inputs.backport_pipeline_id }}/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/dev/cadsit/backport-bot"
}
}
};
} | ConvertTo-Json -Depth 10
Write-Host "Posting to $launchURI"
Write-Host $jsonBody
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":${{ inputs.ado_build_pat }}"))
$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/${{ inputs.ado_organization }}/${{ inputs.ado_project }}/_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 ${{ inputs.github_account_pat }}"}
$uri = "https://api.github.com/repos/${{ inputs.github_repository }}/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