|
| 1 | +name: Repository lockdown check |
| 2 | +on: |
| 3 | + pull_request_target: |
| 4 | + types: [opened, synchronize, reopened] |
| 5 | + branches: |
| 6 | + - 'main' |
| 7 | + - 'release/*' |
| 8 | +permissions: |
| 9 | + issues: write |
| 10 | + pull-requests: write |
| 11 | +jobs: |
| 12 | + repository_lockdown: |
| 13 | + permissions: |
| 14 | + issues: write |
| 15 | + pull-requests: write |
| 16 | + env: |
| 17 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Check if lockdown is in place |
| 21 | + run: | |
| 22 | + set -e |
| 23 | + if [[ ${{ vars.LOCKDOWN }} == "true" ]]; then |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | + # Did bot already comment the PR? |
| 27 | + - name: Find Comment |
| 28 | + if: (success() || failure()) |
| 29 | + uses: peter-evans/find-comment@v2.4.0 |
| 30 | + id: fc |
| 31 | + with: |
| 32 | + issue-number: ${{github.event.pull_request.number}} |
| 33 | + comment-author: 'github-actions[bot]' |
| 34 | + body-includes: '<!-- DO_NOT_REMOVE: repository_lockdown -->' |
| 35 | + # If not, create a new comment |
| 36 | + - name: Create comment |
| 37 | + if: steps.fc.outputs.comment-id == '' && failure() |
| 38 | + uses: actions/github-script@v6 |
| 39 | + with: |
| 40 | + github-token: ${{ github.token }} |
| 41 | + script: | |
| 42 | + let body = "<!-- DO_NOT_REMOVE: repository_lockdown -->\n\n> [!CAUTION]\n>Repository is on lockdown for maintenance, all merges are on hold." |
| 43 | + const comment = await github.rest.issues.createComment({ |
| 44 | + issue_number: context.issue.number, |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + body: body |
| 48 | + }); |
| 49 | + return comment.data.id; |
| 50 | + # If yes, update the comment |
| 51 | + - name: Update comment |
| 52 | + if: steps.fc.outputs.comment-id != '' && failure() |
| 53 | + uses: actions/github-script@v6 |
| 54 | + with: |
| 55 | + github-token: ${{ github.token }} |
| 56 | + script: | |
| 57 | + let body = "<!-- DO_NOT_REMOVE: repository_lockdown -->\n\n> [!CAUTION]\n>Repository is on lockdown for maintenance, all merges are on hold." |
| 58 | + const comment = await github.rest.issues.updateComment({ |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + comment_id: ${{steps.fc.outputs.comment-id}}, |
| 62 | + body: body |
| 63 | + }); |
| 64 | + return comment.data.id; |
| 65 | + # If comment exists, but we are no longer in maintenance mode, delete the comment. |
| 66 | + - name: Delete comment |
| 67 | + if: steps.fc.outputs.comment-id != '' && success() |
| 68 | + uses: actions/github-script@v6 |
| 69 | + with: |
| 70 | + github-token: ${{ github.token }} |
| 71 | + script: | |
| 72 | + let body = "<!-- DO_NOT_REMOVE: repository_lockdown -->\n\n> [!CAUTION]\n>Repository is on lockdown for maintenance, all merges are on hold." |
| 73 | + const comment = await github.rest.issues.deleteComment({ |
| 74 | + owner: context.repo.owner, |
| 75 | + repo: context.repo.repo, |
| 76 | + comment_id: ${{steps.fc.outputs.comment-id}} |
| 77 | + }); |
| 78 | + return 0; |
0 commit comments