|
| 1 | +name: Backport |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [closed, labeled] |
| 6 | + # TODO: Remove before merging — manual trigger for testing from any branch |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + pr_number: |
| 10 | + description: 'Merged PR number to test backporting' |
| 11 | + required: true |
| 12 | + type: number |
| 13 | + |
| 14 | +permissions: |
| 15 | + actions: write |
| 16 | + contents: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + backport: |
| 21 | + name: Backport |
| 22 | + runs-on: ubuntu-latest |
| 23 | + # Run when a labeled PR is merged, or when a backport label is added to an already-merged PR. |
| 24 | + # Uses pull_request_target so the token has write access even for PRs from forks. |
| 25 | + if: >- |
| 26 | + github.repository_owner == 'npm' && |
| 27 | + github.event.pull_request.merged == true && |
| 28 | + ( |
| 29 | + (github.event.action == 'closed' && |
| 30 | + contains(join(github.event.pull_request.labels.*.name, ','), 'backport:')) |
| 31 | + || |
| 32 | + (github.event.action == 'labeled' && |
| 33 | + startsWith(github.event.label.name, 'backport:')) |
| 34 | + ) |
| 35 | + defaults: |
| 36 | + run: |
| 37 | + shell: bash |
| 38 | + steps: |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@v6 |
| 41 | + with: |
| 42 | + fetch-depth: 0 |
| 43 | + - name: Setup Git User |
| 44 | + run: | |
| 45 | + git config --global user.email "npm-cli+bot@github.com" |
| 46 | + git config --global user.name "npm CLI robot" |
| 47 | + - name: Create Backports |
| 48 | + uses: actions/github-script@v7 |
| 49 | + env: |
| 50 | + MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }} |
| 51 | + with: |
| 52 | + script: | |
| 53 | + const backport = require('./scripts/backport.js') |
| 54 | + await backport({ github, context, core }) |
| 55 | +
|
| 56 | + # TODO: Remove before merging — manual test job |
| 57 | + backport-test: |
| 58 | + name: Backport (Test) |
| 59 | + if: github.event_name == 'workflow_dispatch' |
| 60 | + runs-on: ubuntu-latest |
| 61 | + defaults: |
| 62 | + run: |
| 63 | + shell: bash |
| 64 | + steps: |
| 65 | + - name: Checkout |
| 66 | + uses: actions/checkout@v6 |
| 67 | + with: |
| 68 | + fetch-depth: 0 |
| 69 | + - name: Setup Git User |
| 70 | + run: | |
| 71 | + git config --global user.email "npm-cli+bot@github.com" |
| 72 | + git config --global user.name "npm CLI robot" |
| 73 | + - name: Create Backports |
| 74 | + uses: actions/github-script@v7 |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const { data: pr } = await github.rest.pulls.get({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + pull_number: ${{ inputs.pr_number }}, |
| 81 | + }) |
| 82 | +
|
| 83 | + if (!pr.merged) { |
| 84 | + return core.setFailed('PR #${{ inputs.pr_number }} is not merged') |
| 85 | + } |
| 86 | +
|
| 87 | + process.env.MERGE_COMMIT_SHA = pr.merge_commit_sha |
| 88 | +
|
| 89 | + const backport = require('./scripts/backport.js') |
| 90 | + await backport({ |
| 91 | + github, |
| 92 | + core, |
| 93 | + context: { |
| 94 | + ...context, |
| 95 | + payload: { action: 'closed', pull_request: pr }, |
| 96 | + }, |
| 97 | + }) |
0 commit comments