|
1 | | -# Create a github action that runs the license check script and fails if it exits with a non-zero status |
| 1 | +# Automatically fix license files on PRs that need updates |
| 2 | +# Tries to auto-commit the fix, or comments with instructions if push fails |
2 | 3 |
|
3 | 4 | name: License Check |
4 | | -on: [push, pull_request] |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + branches: |
| 8 | + - main # Only run when PR targets main |
| 9 | + paths: |
| 10 | + - "**.go" |
| 11 | + - go.mod |
| 12 | + - go.sum |
| 13 | + - ".github/licenses.tmpl" |
| 14 | + - "script/licenses*" |
| 15 | + - "third-party-licenses.*.md" |
| 16 | + - "third-party/**" |
5 | 17 | permissions: |
6 | | - contents: read |
| 18 | + contents: write |
| 19 | + pull-requests: write |
7 | 20 |
|
8 | 21 | jobs: |
9 | 22 | license-check: |
|
13 | 26 | - name: Check out code |
14 | 27 | uses: actions/checkout@v6 |
15 | 28 |
|
| 29 | + # Check out the actual PR branch so we can push changes back if needed |
| 30 | + - name: Check out PR branch |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ github.token }} |
| 33 | + run: gh pr checkout ${{ github.event.pull_request.number }} |
| 34 | + |
16 | 35 | - name: Set up Go |
17 | 36 | uses: actions/setup-go@v6 |
18 | 37 | with: |
19 | 38 | go-version-file: "go.mod" |
20 | | - - name: check licenses |
21 | | - run: ./script/licenses-check |
| 39 | + |
| 40 | + # actions/setup-go does not setup the installed toolchain to be preferred over the system install, |
| 41 | + # which causes go-licenses to raise "Package ... does not have module info" errors. |
| 42 | + # For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633 |
| 43 | + - name: Regenerate licenses |
| 44 | + env: |
| 45 | + CI: "true" |
| 46 | + run: | |
| 47 | + export GOROOT=$(go env GOROOT) |
| 48 | + export PATH=${GOROOT}/bin:$PATH |
| 49 | + ./script/licenses |
| 50 | +
|
| 51 | + - name: Check for changes |
| 52 | + id: changes |
| 53 | + continue-on-error: true |
| 54 | + run: script/licenses-check |
| 55 | + |
| 56 | + - name: Commit and push fixes |
| 57 | + if: steps.changes.outcome == 'failure' |
| 58 | + continue-on-error: true |
| 59 | + id: push |
| 60 | + run: | |
| 61 | + git config user.name "github-actions[bot]" |
| 62 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 63 | + git add third-party-licenses.*.md third-party/ |
| 64 | + git commit -m "chore: regenerate license files" -m "Auto-generated by license-check workflow" |
| 65 | + git push |
| 66 | +
|
| 67 | + - name: Check if already commented |
| 68 | + if: steps.changes.outcome == 'failure' && steps.push.outcome == 'failure' |
| 69 | + id: check_comment |
| 70 | + uses: actions/github-script@v8 |
| 71 | + with: |
| 72 | + script: | |
| 73 | + const { data: comments } = await github.rest.issues.listComments({ |
| 74 | + owner: context.repo.owner, |
| 75 | + repo: context.repo.repo, |
| 76 | + issue_number: context.issue.number |
| 77 | + }); |
| 78 | + |
| 79 | + const alreadyCommented = comments.some(comment => |
| 80 | + comment.user.login === 'github-actions[bot]' && |
| 81 | + comment.body.includes('## ⚠️ License files need updating') |
| 82 | + ); |
| 83 | + |
| 84 | + core.setOutput('already_commented', alreadyCommented ? 'true' : 'false'); |
| 85 | +
|
| 86 | + - name: Comment with instructions if cannot push |
| 87 | + if: steps.changes.outcome == 'failure' && steps.push.outcome == 'failure' && steps.check_comment.outputs.already_commented == 'false' |
| 88 | + uses: actions/github-script@v8 |
| 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: `## ⚠️ License files need updating |
| 96 | + |
| 97 | + The license files are out of date. I tried to fix them automatically but don't have permission to push to this branch. |
| 98 | + |
| 99 | + **Please run:** |
| 100 | + \`\`\`bash |
| 101 | + script/licenses |
| 102 | + git add third-party-licenses.*.md third-party/ |
| 103 | + git commit -m "chore: regenerate license files" |
| 104 | + git push |
| 105 | + \`\`\` |
| 106 | + |
| 107 | + Alternatively, enable "Allow edits by maintainers" in the PR settings so I can fix it automatically.` |
| 108 | + }); |
| 109 | +
|
| 110 | + - name: Fail check if changes needed |
| 111 | + if: steps.changes.outcome == 'failure' |
| 112 | + run: exit 1 |
| 113 | + |
0 commit comments