This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Backport Assistant Prerunner | ||
|
Check failure on line 1 in .github/workflows/backport-assistant-prerun.yml
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, labeled] | ||
| # Runs on PRs to main and all release branches | ||
| branches: | ||
| - main | ||
| - release/* | ||
| env: | ||
| BACKPORT_ERROR_LABEL: backport-error/merge-commit-found | ||
| jobs: | ||
| # checks that a backport label is present for a PR | ||
| backport-check: | ||
| # If there's a `pr/no-backport` label we ignore this check. Also, we ignore PRs created by the bot assigned to `backport-assistant` | ||
| if: "! ( contains(github.event.pull_request.labels.*.name, 'pr/no-backport') || github.event.pull_request.user.login == 'hc-github-team-consul-core' )" | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| has_backport_label: ${{ steps.check_backport.outputs.has_backport }} | ||
| steps: | ||
| - name: Check for Backport Label | ||
| id: check_backport | ||
| run: | | ||
| labels="${{join(github.event.pull_request.labels.*.name, ', ') }}" | ||
| if [[ "$labels" =~ .*"backport/".* ]]; then | ||
| echo "Found backport label!" | ||
| exit 0 | ||
| fi | ||
| # Fail status check when no backport label was found on the PR | ||
| echo "Did not find a backport label matching the pattern 'backport/*' and the 'pr/no-backport' label was not applied. Reference - https://github.com/hashicorp/consul/pull/16567" | ||
| exit 1 | ||
| get-pr-info: | ||
| runs-on: ubuntu-latest | ||
| needs: backport-check | ||
| outputs: | ||
| has_no_backport: ${{ steps.check_label.outputs.has_no_backport }} | ||
| pr_number: ${{ steps.check_label.outputs.pr_number }} | ||
| pull_request_ref: ${{ steps.check_label.outputs.pull_request_ref }} | ||
| pull_request_repo: ${{ steps.check_label.outputs.pull_request_repo }} | ||
| steps: | ||
| - name: Get PR information and check labels | ||
| id: check_label | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| const prNumber = context.payload.pull_request.number; | ||
| if (!prNumber) { | ||
| console.log('No PR associated with this workflow run, skipping checks.'); | ||
| return; | ||
| } | ||
| const { data: pr } = await github.rest.pulls.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: prNumber | ||
| }); | ||
| const hasNoBackport = pr.labels.some(label => label.name === 'no-backport'); | ||
| console.log('PR has no-backport label:', hasNoBackport); | ||
| core.setOutput('has_no_backport', hasNoBackport); | ||
| core.setOutput('pr_number', prNumber); | ||
| core.setOutput('pull_request_ref', pr.head.ref); | ||
| core.setOutput('pull_request_repo', `${pr.head.repo.owner.login}/${pr.head.repo.name}`); | ||
| check-pr-condition: | ||
| runs-on: ubuntu-latest | ||
| needs: get-pr-info | ||
| steps: | ||
| - name: Fetch repository history | ||
| if: needs.get-pr-info.outputs.has_no_backport == 'false' | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| ref: ${{ needs.get-pr-info.outputs.pull_request_ref }} | ||
| repository: ${{ needs.get-pr-info.outputs.pull_request_repo }} | ||
| - name: Check for merge commits | ||
| id: check_merges | ||
| if: needs.get-pr-info.outputs.has_no_backport == 'false' | ||
| run: | | ||
| BASE_REF="${{ github.event.pull_request.base.ref }}" | ||
| MERGE_COMMITS=$(git log --merges --oneline origin/$BASE_REF.HEAD) | ||
| if [ -n "$MERGE_COMMITS" ]; then | ||
| echo "::error::Merge commits found." | ||
| echo "merge_commit_found=true" >> $GITHUB_OUTPUT | ||
| echo "MERGE_COMMIT_IDS<<EOF" >> $GITHUB_OUTPUT | ||
| echo "$MERGE_COMMITS" >> $GITHUB_OUTPUT | ||
| echo "EOF" >> $GITHUB_OUTPUT | ||
| fi | ||
| echo "merge_commit_found=${MERGE_COMMITS:+'true'}" >> $GITHUB_OUTPUT | ||
| - name: Add 'backport-error' label and comment | ||
| if: steps.check_merges.outputs.merge_commit_found == 'true' && needs.get-pr-info.outputs.has_no_backport == 'false' | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const prNumber = ${{ needs.get-pr-info.outputs.pr_number }}; | ||
| const labelName = ${{ env.BACKPORT_ERROR_LABEL }}; | ||
| const commentBody = ` | ||
| 🚨 **AUTOMATED CHECK: MERGE COMMITS FOUND** 🚨 | ||
| This pull request contains merge commits. The **\`${labelName}\`** label has been added. Please rebase your branch. | ||
| **Found Commits:** | ||
| \`\`\` | ||
| ${{ steps.check_merges.outputs.MERGE_COMMIT_IDS }} | ||
| \`\`\` | ||
| `; | ||
| await github.rest.issues.addLabels({ owner: context.repo.owner, repo: context.repo.repo, issue_number: prNumber, labels: [labelName] }); | ||
| await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: prNumber, body: commentBody }); | ||
| - name: | ||
| - name: Remove 'backport-error' label and comment | ||
| if: steps.check_merges.outputs.merge_commit_found != 'true' && needs.get-pr-info.outputs.has_no_backport == 'false' | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const prNumber = ${{ needs.get-pr-info.outputs.pr_number }}; | ||
| const commentPrefix = '🚨 **AUTOMATED CHECK: MERGE COMMITS FOUND** 🚨'; | ||
| const labelName = ${{ env.BACKPORT_ERROR_LABEL }}; | ||
| try { | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber | ||
| }); | ||
| const botComment = comments.find(comment => comment.body.includes(commentPrefix)); | ||
| if (botComment) { | ||
| await github.rest.issues.deleteComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: botComment.id | ||
| }); | ||
| console.log('Merge commit comment successfully removed.'); | ||
| } | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| name: labelName | ||
| }); | ||
| console.log('do not merge label successfully removed.'); | ||
| } catch (error) { | ||
| console.log('Comment or label not found, skipping.'); | ||
| } | ||