|
| 1 | +name: Rebuild Compact-JS Bundle (Bot Action) |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr_number: |
| 9 | + description: 'PR number to rebuild the compact-js bundle for' |
| 10 | + required: true |
| 11 | + |
| 12 | +jobs: |
| 13 | + rebuild-compact-js-bundle: |
| 14 | + if: | |
| 15 | + github.event_name == 'workflow_dispatch' || |
| 16 | + (github.event.issue.pull_request && contains(github.event.comment.body, '/bot rebuild-compact-js-bundle')) |
| 17 | + runs-on: [self-hosted, "tier:medium"] |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + steps: |
| 22 | + - name: Check user permission |
| 23 | + if: github.event_name == 'issue_comment' |
| 24 | + id: check_permission |
| 25 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 26 | + with: |
| 27 | + script: | |
| 28 | + const association = context.payload.comment.author_association; |
| 29 | + if (!['MEMBER', 'OWNER'].includes(association)) { |
| 30 | + core.setFailed(`User ${context.actor} is not an org member or owner (association: ${association})`); |
| 31 | + return; |
| 32 | + } |
| 33 | + const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + username: context.actor |
| 37 | + }); |
| 38 | + const allowed = ['admin', 'write'].includes(permission.permission); |
| 39 | + if (!allowed) { |
| 40 | + core.setFailed(`User ${context.actor} does not have write permission`); |
| 41 | + } |
| 42 | +
|
| 43 | + - name: Add reaction to comment |
| 44 | + if: github.event_name == 'issue_comment' |
| 45 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 46 | + with: |
| 47 | + script: | |
| 48 | + 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 | + env: |
| 59 | + PR_NUMBER: ${{ github.event.inputs.pr_number }} |
| 60 | + with: |
| 61 | + script: | |
| 62 | + const prNumber = context.eventName === 'workflow_dispatch' |
| 63 | + ? process.env.PR_NUMBER |
| 64 | + : context.issue.number; |
| 65 | + const pr = await github.rest.pulls.get({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + pull_number: prNumber |
| 69 | + }); |
| 70 | + if (pr.data.head.repo === null || pr.data.head.repo.fork) { |
| 71 | + core.setFailed('Bot commands are not allowed on fork PRs for security reasons'); |
| 72 | + return; |
| 73 | + } |
| 74 | + core.setOutput('ref', pr.data.head.ref); |
| 75 | + core.setOutput('sha', pr.data.head.sha); |
| 76 | + core.setOutput('number', prNumber); |
| 77 | +
|
| 78 | + - name: Checkout PR branch |
| 79 | + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 80 | + with: |
| 81 | + submodules: recursive |
| 82 | + ref: ${{ steps.pr.outputs.sha }} |
| 83 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 84 | + fetch-depth: 0 |
| 85 | + |
| 86 | + - name: Setup Earthly |
| 87 | + uses: EarthBuild/actions-setup@cae2d9ab68894d8402751fe42e07c7cca0272f7f |
| 88 | + with: |
| 89 | + version: v0.8.16 |
| 90 | + github-token: ${{ github.token }} |
| 91 | + use-cache: false |
| 92 | + |
| 93 | + - name: Login to GHCR |
| 94 | + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee #v4.2.0 |
| 95 | + with: |
| 96 | + registry: ghcr.io |
| 97 | + username: MidnightCI |
| 98 | + password: ${{ secrets.MIDNIGHTCI_PACKAGES_READ }} |
| 99 | + |
| 100 | + - name: Run compact-js-bundle |
| 101 | + env: |
| 102 | + EARTHLY_CONFIG: .earthly/config.yml |
| 103 | + MIDNIGHTCI_PACKAGES_READ: ${{ secrets.MIDNIGHTCI_PACKAGES_READ }} |
| 104 | + run: | |
| 105 | + mkdir -p "$HOME"/.cargo |
| 106 | + echo "[net]" >> "$HOME"/.cargo/config |
| 107 | + echo "git-fetch-with-cli = true" >> "$HOME"/.cargo/config |
| 108 | + earthly -P --secret MIDNIGHTCI_PACKAGES_READ="$MIDNIGHTCI_PACKAGES_READ" +compact-js-bundle |
| 109 | +
|
| 110 | + - name: Commit and push changes |
| 111 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 112 | + id: commit |
| 113 | + env: |
| 114 | + REPO: ${{ github.repository }} |
| 115 | + PR_REF: ${{ steps.pr.outputs.ref }} |
| 116 | + with: |
| 117 | + script: | |
| 118 | + const fs = require('fs'); |
| 119 | + const { execSync } = require('child_process'); |
| 120 | +
|
| 121 | + // Check if there are changes |
| 122 | + const status = execSync('git status --porcelain').toString(); |
| 123 | + if (!status.trim()) { |
| 124 | + core.exportVariable('CHANGES_MADE', 'false'); |
| 125 | + console.log('No changes to commit'); |
| 126 | + return; |
| 127 | + } |
| 128 | +
|
| 129 | + // Get changed vendored tarballs |
| 130 | + const prefix = 'util/toolkit-js/compact-0.31.108/vendor/'; |
| 131 | + const changedFiles = status.split('\n') |
| 132 | + .filter(line => line.trim()) |
| 133 | + .map(line => line.substring(3)) |
| 134 | + .filter(file => file.startsWith(prefix) && file.endsWith('.tgz')); |
| 135 | +
|
| 136 | + if (changedFiles.length === 0) { |
| 137 | + core.exportVariable('CHANGES_MADE', 'false'); |
| 138 | + console.log('No vendored bundle changes to commit'); |
| 139 | + return; |
| 140 | + } |
| 141 | +
|
| 142 | + // Prepare file additions (tarballs are binary; base64 handles them). |
| 143 | + const additions = changedFiles.map(filePath => ({ |
| 144 | + path: filePath, |
| 145 | + contents: fs.readFileSync(filePath).toString('base64') |
| 146 | + })); |
| 147 | +
|
| 148 | + // Get current HEAD |
| 149 | + const headOid = execSync('git rev-parse HEAD').toString().trim(); |
| 150 | +
|
| 151 | + // Create commit via GraphQL API (produces a verified github-actions[bot] commit). DCO is |
| 152 | + // enforced only by the local commit-msg hook, not server-side, but we add Signed-off-by |
| 153 | + // in the body by convention. |
| 154 | + const result = await github.graphql(` |
| 155 | + mutation($input: CreateCommitOnBranchInput!) { |
| 156 | + createCommitOnBranch(input: $input) { |
| 157 | + commit { |
| 158 | + oid |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + `, { |
| 163 | + input: { |
| 164 | + branch: { |
| 165 | + repositoryNameWithOwner: process.env.REPO, |
| 166 | + branchName: process.env.PR_REF |
| 167 | + }, |
| 168 | + message: { |
| 169 | + headline: 'chore: rebuild compact-js vendored bundle', |
| 170 | + body: 'Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>' |
| 171 | + }, |
| 172 | + expectedHeadOid: headOid, |
| 173 | + fileChanges: { |
| 174 | + additions: additions |
| 175 | + } |
| 176 | + } |
| 177 | + }); |
| 178 | +
|
| 179 | + console.log('Commit created:', result.createCommitOnBranch.commit.oid); |
| 180 | + core.exportVariable('CHANGES_MADE', 'true'); |
| 181 | +
|
| 182 | + - name: Post success comment |
| 183 | + if: env.CHANGES_MADE == 'true' |
| 184 | + uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 |
| 185 | + with: |
| 186 | + message: | |
| 187 | + :white_check_mark: Compact-JS bundle rebuild complete! Vendored tarballs have been committed. Remember to regenerate `util/toolkit-js/package-lock.json` (`npm install`) and commit it. |
| 188 | + pr-number: ${{ steps.pr.outputs.number }} |
| 189 | + |
| 190 | + - name: Post no-changes comment |
| 191 | + if: env.CHANGES_MADE == 'false' |
| 192 | + uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 |
| 193 | + with: |
| 194 | + message: | |
| 195 | + :white_check_mark: Compact-JS bundle rebuild complete. No changes detected. |
| 196 | + pr-number: ${{ steps.pr.outputs.number }} |
| 197 | + |
| 198 | + - name: Post failure comment |
| 199 | + if: failure() |
| 200 | + uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 |
| 201 | + with: |
| 202 | + message: | |
| 203 | + :x: Compact-JS bundle rebuild failed. Check the [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details. |
| 204 | + pr-number: ${{ steps.pr.outputs.number }} |
0 commit comments