|
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 | +# Instead of just failing, this workflow pushes the fix and comments on the PR |
2 | 3 |
|
3 | 4 | name: License Check |
4 | | -on: [push, pull_request] |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + paths: |
| 8 | + - "**.go" |
| 9 | + - go.mod |
| 10 | + - go.sum |
| 11 | + - ".github/licenses.tmpl" |
| 12 | + - "script/licenses*" |
| 13 | + - "third-party-licenses.*.md" |
| 14 | + - "third-party/**" |
5 | 15 | permissions: |
6 | | - contents: read |
| 16 | + contents: write |
| 17 | + pull-requests: write |
7 | 18 |
|
8 | 19 | jobs: |
9 | 20 | license-check: |
10 | 21 | runs-on: ubuntu-latest |
| 22 | + # Don't run on forks (they can't push back) or dependabot |
| 23 | + if: github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' |
11 | 24 |
|
12 | 25 | steps: |
13 | 26 | - name: Check out code |
14 | 27 | uses: actions/checkout@v6 |
| 28 | + with: |
| 29 | + ref: ${{ github.head_ref }} |
15 | 30 |
|
16 | 31 | - name: Set up Go |
17 | 32 | uses: actions/setup-go@v6 |
18 | 33 | with: |
19 | 34 | go-version-file: "go.mod" |
20 | | - - name: check licenses |
21 | | - run: ./script/licenses-check |
| 35 | + |
| 36 | + # actions/setup-go does not setup the installed toolchain to be preferred over the system install, |
| 37 | + # which causes go-licenses to raise "Package ... does not have module info" errors. |
| 38 | + # For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633 |
| 39 | + - name: Regenerate licenses |
| 40 | + env: |
| 41 | + CI: "true" |
| 42 | + run: | |
| 43 | + export GOROOT=$(go env GOROOT) |
| 44 | + export PATH=${GOROOT}/bin:$PATH |
| 45 | + ./script/licenses |
| 46 | +
|
| 47 | + - name: Check for changes |
| 48 | + id: changes |
| 49 | + run: | |
| 50 | + if git diff --exit-code; then |
| 51 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 52 | + echo "✅ License files are up to date" |
| 53 | + else |
| 54 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 55 | + echo "📝 License files need updating" |
| 56 | + git diff --stat |
| 57 | + fi |
| 58 | +
|
| 59 | + - name: Commit and push fixes |
| 60 | + if: steps.changes.outputs.changed == 'true' |
| 61 | + run: | |
| 62 | + git config --local user.name "github-actions[bot]" |
| 63 | + git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 64 | + git add third-party third-party-licenses.*.md |
| 65 | + git commit -m "chore: regenerate third-party licenses" |
| 66 | + git push |
| 67 | +
|
| 68 | + - name: Comment on PR |
| 69 | + if: steps.changes.outputs.changed == 'true' |
| 70 | + uses: actions/github-script@v7 |
| 71 | + with: |
| 72 | + script: | |
| 73 | + github.rest.issues.createComment({ |
| 74 | + owner: context.repo.owner, |
| 75 | + repo: context.repo.repo, |
| 76 | + issue_number: context.issue.number, |
| 77 | + body: `## 📜 License files updated |
| 78 | +
|
| 79 | +I noticed the third-party license files were out of date and pushed a fix to this PR. |
| 80 | + |
| 81 | +**What changed:** Dependencies were added, removed, or updated, which requires regenerating the license documentation. |
| 82 | + |
| 83 | +**What I did:** Ran \`./script/licenses\` and committed the result. |
| 84 | + |
| 85 | +Please pull the latest changes before pushing again.` |
| 86 | + }) |
| 87 | + |
0 commit comments