|
| 1 | +name: Backport PR to branch |
| 2 | +on: |
| 3 | + issue_comment: |
| 4 | + types: [created] |
| 5 | + schedule: |
| 6 | + # once a day at 13:00 UTC |
| 7 | + - cron: '0 13 * * *' |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + issues: write |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + cleanup_old_runs: |
| 16 | + if: github.event.schedule == '0 13 * * *' |
| 17 | + runs-on: ubuntu-20.04 |
| 18 | + permissions: |
| 19 | + actions: write |
| 20 | + env: |
| 21 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + steps: |
| 23 | + - name: Delete old workflow runs |
| 24 | + run: | |
| 25 | + _UrlPath="/repos/$GITHUB_REPOSITORY/actions/workflows" |
| 26 | + _CurrentWorkflowID="$(gh api -X GET "$_UrlPath" | jq '.workflows[] | select(.name == '\""$GITHUB_WORKFLOW"\"') | .id')" |
| 27 | +
|
| 28 | + # delete workitems which are 'completed'. (other candidate values of status field are: 'queued' and 'in_progress') |
| 29 | +
|
| 30 | + gh api -X GET "$_UrlPath/$_CurrentWorkflowID/runs" --paginate \ |
| 31 | + | jq '.workflow_runs[] | select(.status == "completed") | .id' \ |
| 32 | + | xargs -I{} gh api -X DELETE "/repos/$GITHUB_REPOSITORY/actions/runs"/{} |
| 33 | +
|
| 34 | + backport: |
| 35 | + if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/backport to') |
| 36 | + runs-on: ubuntu-20.04 |
| 37 | + steps: |
| 38 | + - name: Extract backport target branch |
| 39 | + uses: actions/github-script@v3 |
| 40 | + id: target-branch-extractor |
| 41 | + with: |
| 42 | + result-encoding: string |
| 43 | + script: | |
| 44 | + if (context.eventName !== "issue_comment") throw "Error: This action only works on issue_comment events."; |
| 45 | +
|
| 46 | + // extract the target branch name from the trigger phrase containing these characters: a-z, A-Z, digits, forward slash, dot, hyphen, underscore |
| 47 | + const regex = /^\/backport to ([a-zA-Z\d\/\.\-\_]+)/; |
| 48 | + target_branch = regex.exec(context.payload.comment.body); |
| 49 | + if (target_branch == null) throw "Error: No backport branch found in the trigger phrase."; |
| 50 | +
|
| 51 | + return target_branch[1]; |
| 52 | + - name: Post backport started comment to pull request |
| 53 | + uses: actions/github-script@v3 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const backport_start_body = `Started backporting to ${{ steps.target-branch-extractor.outputs.result }}: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 57 | + await github.issues.createComment({ |
| 58 | + issue_number: context.issue.number, |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + body: backport_start_body |
| 62 | + }); |
| 63 | + - name: Checkout repo |
| 64 | + uses: actions/checkout@v2 |
| 65 | + with: |
| 66 | + fetch-depth: 0 |
| 67 | + - name: Run backport |
| 68 | + uses: ./eng/actions/backport |
| 69 | + with: |
| 70 | + target_branch: ${{ steps.target-branch-extractor.outputs.result }} |
| 71 | + auth_token: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + pr_description_template: | |
| 73 | + Backport of #%source_pr_number% to %target_branch% |
| 74 | +
|
| 75 | + /cc %cc_users% |
| 76 | +
|
| 77 | + ## Customer Impact |
| 78 | +
|
| 79 | + ## Testing |
| 80 | +
|
| 81 | + ## Risk |
| 82 | +
|
| 83 | + IMPORTANT: Is this backport for a servicing release? If so and this change touches code that ships in a NuGet package, please make certain that you have added any necessary [package authoring](https://github.com/dotnet/runtime/blob/main/docs/project/library-servicing.md) and gotten it explicitly reviewed. |
0 commit comments