| 1 | name: Format PR |
| 2 | |
| 3 | on: |
| 4 | issue_comment: |
| 5 | types: [created] |
| 6 | workflow_dispatch: |
| 7 | inputs: |
| 8 | pr_number: |
| 9 | description: "PR number to format" |
| 10 | required: true |
| 11 | type: number |
| 12 | |
| 13 | permissions: |
| 14 | contents: write |
| 15 | pull-requests: write |
| 16 | |
| 17 | jobs: |
| 18 | format: |
| 19 | if: | |
| 20 | github.event.issue.pull_request && |
| 21 | github.event.comment.body == '/format' |
| 22 | runs-on: ubuntu-latest |
| 23 | steps: |
| 24 | - name: Check if user has write access |
| 25 | uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 26 | with: |
| 27 | script: | |
| 28 | const permission = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 29 | owner: context.repo.owner, |
| 30 | repo: context.repo.repo, |
| 31 | username: context.payload.comment.user.login, |
| 32 | }); |
| 33 | |
| 34 | if (!['admin', 'write'].includes(permission.data.permission)) { |
| 35 | await github.rest.issues.createComment({ |
| 36 | owner: context.repo.owner, |
| 37 | repo: context.repo.repo, |
| 38 | issue_number: context.issue.number, |
| 39 | body: '❌ You need write access to run this command.' |
| 40 | }); |
| 41 | core.setFailed('User lacks write permission'); |
| 42 | } |
| 43 | |
| 44 | - name: React to comment |
| 45 | uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 46 | with: |
| 47 | script: | |
| 48 | await github.rest.reactions.createForIssueComment({ |
| 49 | owner: context.repo.owner, |
| 50 | repo: context.repo.repo, |
| 51 | comment_id: context.payload.comment.id, |
| 52 | content: 'rocket' |
| 53 | }); |
| 54 | |
| 55 | - name: Get PR branch |
| 56 | id: pr |
| 57 | uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 58 | with: |
| 59 | script: | |
| 60 | const pr = await github.rest.pulls.get({ |
| 61 | owner: context.repo.owner, |
| 62 | repo: context.repo.repo, |
| 63 | pull_number: context.issue.number, |
| 64 | }); |
| 65 | core.setOutput('head_ref', pr.data.head.ref); |
| 66 | core.setOutput('head_sha', pr.data.head.sha); |
| 67 | |
| 68 | - name: Checkout PR |
| 69 | uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 70 | with: |
| 71 | ref: ${{ steps.pr.outputs.head_ref }} |
| 72 | fetch-depth: 0 |
| 73 | |
| 74 | - name: Install Nix |
| 75 | uses: cachix/install-nix-action@630ae543ea3a38a9a4166f03376c02c50f408342 # v31 |
| 76 | |
| 77 | - name: Setup Cachix |
| 78 | uses: cachix/cachix-action@5f2d7c5294214f71b873db4b969586b980625e71 # v17 |
| 79 | with: |
| 80 | name: nixos-infra-dev |
| 81 | authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}" |
| 82 | |
| 83 | - name: Run format and absorb |
| 84 | run: ./.github/scripts/format-and-absorb.sh |
| 85 | |
| 86 | - name: Comment on success |
| 87 | if: success() |
| 88 | uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 89 | with: |
| 90 | script: | |
| 91 | await github.rest.issues.createComment({ |
| 92 | owner: context.repo.owner, |
| 93 | repo: context.repo.repo, |
| 94 | issue_number: context.issue.number, |
| 95 | body: '✅ Successfully formatted and absorbed changes!' |
| 96 | }); |
| 97 | |
| 98 | - name: Comment on failure |
| 99 | if: failure() |
| 100 | uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 101 | with: |
| 102 | script: | |
| 103 | await github.rest.issues.createComment({ |
| 104 | owner: context.repo.owner, |
| 105 | repo: context.repo.repo, |
| 106 | issue_number: context.issue.number, |
| 107 | body: '❌ Failed to format and absorb changes. Check the workflow logs for details.' |
| 108 | }); |