11name : Backport Pull Request from Comment
22description : Launches a Backport Job from a GitHub Comment on an Issue
3+ author : Connor Adsit <cadsit@microsoft.com>
34inputs :
45 pull_request_url :
56 description : URL of the pull request from the issue comment event
@@ -33,63 +34,32 @@ inputs:
3334 required : false
3435 default : ' false'
3536
36- # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs
3737runs :
3838 using : " composite"
39- # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsenv
40- env :
41- # 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)
42- # Scripts must consume the environment variable settings instead
43- GITHUB_REPOSITORY : " ${{ inputs.github_repository }}"
44- GITHUB_ACCOUNT_PAT : " ${{ inputs.github_account_pat }}"
45- COMMENT_AUTHOR : " ${{ inputs.comment_author }}"
46- COMMENT_BODY : " ${{ inputs.comment_body }}"
47- PULL_REQUEST_URL : " ${{ inputs.pull_request_url }}"
48- USE_FORK : " ${{ inputs.use_fork }}"
49- ADO_ORGANIZATION : " ${{ inputs.ado_organization }}"
50- ADO_PROJECT : " ${{ inputs.ado_project }}"
51- ADO_PIPELINE_ID : " ${{ inputs.backport_pipeline_id }}"
52- ADO_BUILD_PAT : " ${{ inputs.ado_build_pat }}"
5339 steps :
5440 - name : Gather Parameters
5541 id : get_parameters
5642 run : |
5743 $statusCode = 0
5844 $message = ""
5945 try {
60- $gitHubRepository = $env:GITHUB_REPOSITORY
61- $gitHubAccountPAT = $env:GITHUB_ACCOUNT_PAT
62- $commentAuthor = $env:COMMENT_AUTHOR
63- $pullRequestUrl = $env:PULL_REQUEST_URL
64- $useFork = $env:USE_FORK
65-
66- Write-Host "GITHUB_REPOSITORY: ${gitHubRepository}"
67- Write-Host "COMMENT_AUTHOR: ${commentAuthor}"
68- Write-Host "PULL_REQUEST_URL: ${pullRequestUrl}"
69- Write-Host "USE_FORK: ${useFork}"
70-
71- ($repoOwner, $repoName) = $gitHubRepository.Split("/")
72- $headers = $headers = @{ Authorization = "token ${gitHubAccountPAT}"}
73- $uri = "https://api.github.com/repos/${repoOwner}/${repoName}/collaborators/${commentAuthor}/permission"
74- Write-Host "Checking ${repoOwner} membership for ${commentAuthor} via $uri"
46+ ($repoOwner, $repoName) = "${{ inputs.github_repository }}".Split("/")
47+ $headers = $headers = @{ Authorization = "token ${{ inputs.github_account_pat }}"}
48+ $uri = "https://api.github.com/repos/$repoOwner/$repoName/collaborators/${{ inputs.comment_author }}/permission"
49+ Write-Host "Checking $repoOwner membership for ${{ inputs.comment_author }} via $uri"
7550 $response = Invoke-WebRequest -Headers $headers -Uri $uri
7651 $content = $response.Content | ConvertFrom-Json
7752 $accessType = $content.permission
7853 Write-Host "Found membership: $accessType"
7954 if (-not ($accessType.Equals("admin") -or $accessType.Equals("write"))) {
80- throw "${commentAuthor} /permission does not have sufficient permissions for ${gitHubRepository }"
55+ throw "${{ inputs.comment_author }} /permission does not have sufficient permissions for ${{ inputs.github_repository } }"
8156 }
8257
83- $commentBody = $env:COMMENT_BODY
84- Write-Host "--------------------------------------------------------------------------------"
85- Write-Host "COMMENT_BODY"
86- Write-Host "--------------------------------------------------------------------------------"
87- Write-Host $commentBody
88-
89- ($botName, $backport, $backportTargetBranch) = [System.Text.RegularExpressions.Regex]::Split($commentBody, "\s+")
58+ Write-Host "Parsing ${{ inputs.comment_body }}"
59+ ($botName, $backport, $backportTargetBranch) = [System.Text.RegularExpressions.Regex]::Split("${{ inputs.comment_body }}", "\s+")
9060
91- Write-Host "Grabbing PR details from ${pullRequestUrl }"
92- $response = Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri "${pullRequestUrl }" | ConvertFrom-Json
61+ Write-Host "Grabbing PR details from ${{ inputs.pull_request_url } }"
62+ $response = Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri "${{ inputs.pull_request_url } }" | ConvertFrom-Json
9363 $backportPRNumber = $response.number
9464
9565 $parameters = @{
9868 BackportTargetBranch = "$backportTargetBranch";
9969 BackportPRNumber = "$backportPRNumber";
10070 BackportHeadSHA = "$($response.head.sha)";
101- UseFork = $useFork -eq "true";
71+ UseFork = "${{ inputs.use_fork }}" -eq "true";
10272 } | ConvertTo-Json -Compress
10373
10474 $json = $parameters.Replace("`"","'")
@@ -115,22 +85,10 @@ runs:
11585 - name : Launch ADO Build
11686 id : ado_build
11787 run : |
118- $adoOrganization = $env:ADO_ORGANIZATION
119- $adoProject = $env:ADO_PROJECT
120- $adoPipelineId = $env:ADO_PIPELINE_ID
121- $adoBuildPAT = $env:ADO_BUILD_PAT
122- $gitHubRepository = $env:GITHUB_REPOSITORY
123- $gitHubAccountPAT = $env:GITHUB_ACCOUNT_PAT
124-
125- Write-Host "ADO_ORGANIZATION: ${adoOrganization}"
126- Write-Host "ADO_PROJECT: ${adoProject}"
127- Write-Host "ADO_PIPELINE_ID: ${adoPipelineId}"
128- Write-Host "GITHUB_REPOSITORY: ${gitHubRepository}"
129-
13088 $message = ""
13189 $statusCode = 0
13290 try {
133- $launchURI = "https://dev.azure.com/${adoOrganization} /${adoProject} /_apis/pipelines/${adoPipelineId }/runs?api-version=6.0-preview.1"
91+ $launchURI = "https://dev.azure.com/${{ inputs.ado_organization }} /${{ inputs.ado_project }} /_apis/pipelines/${{ inputs.backport_pipeline_id } }/runs?api-version=6.0-preview.1"
13492 Write-Host "Grabbing parameters from prior step"
13593 $parameters = ConvertFrom-Json "${{ steps.get_parameters.outputs.parameters }}"
13694 Write-Host "$parameters"
@@ -148,13 +106,13 @@ runs:
148106
149107 Write-Host "Posting to $launchURI"
150108 Write-Host $jsonBody
151- $encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":${adoBuildPAT }"))
109+ $encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":${{ inputs.ado_build_pat } }"))
152110 $headers = @{ Authorization = "Basic $encoded"}
153111
154112 $response = Invoke-WebRequest -UseBasicParsing -Method POST -Headers $headers -ContentType "application/json" -Uri $launchURI -Body $jsonBody
155113 $responseJson = ConvertFrom-Json $response.Content
156114 echo "Job successfully launched"
157- $message = "Backport Job to branch **$($parameters.BackportTargetBranch)** Created! The magic is happening [here](https://dev.azure.com/${adoOrganization} /${adoProject }/_build/results?buildId=$($responseJson.id))"
115+ $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))"
158116 } catch {
159117 Write-Host $_.Exception.Message
160118 $message = "I couldn't create a backport to **$($parameters.BackportTargetBranch)** for you. :( Please check the Action logs for more details."
@@ -164,8 +122,8 @@ runs:
164122 body = $message
165123 } | ConvertTo-Json
166124
167- $headers = @{ Authorization = "token ${gitHubAccountPAT }"}
168- $uri = "https://api.github.com/repos/${gitHubRepository }/issues/${{ steps.get_parameters.outputs.pr_number }}/comments"
125+ $headers = @{ Authorization = "token ${{ inputs.github_account_pat } }"}
126+ $uri = "https://api.github.com/repos/${{ inputs.github_repository } }/issues/${{ steps.get_parameters.outputs.pr_number }}/comments"
169127
170128
171129 Write-Host "Posting to $uri"
0 commit comments