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
23
34name : 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/**"
517permissions :
6- contents : read
18+ contents : write
19+ pull-requests : write
720
821jobs :
922 license-check :
@@ -12,10 +25,87 @@ jobs:
1225 steps :
1326 - name : Check out code
1427 uses : actions/checkout@v6
28+ with :
29+ ref : ${{ github.head_ref }}
1530
1631 - name : Set up Go
1732 uses : actions/setup-go@v6
1833 with :
1934 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+ continue-on-error : true
50+ run : script/licenses-check
51+
52+ - name : Commit and push fixes
53+ if : steps.changes.outcome == 'failure'
54+ continue-on-error : true
55+ id : push
56+ run : |
57+ git config user.name "github-actions[bot]"
58+ git config user.email "github-actions[bot]@users.noreply.github.com"
59+ git add third-party-licenses.*.md third-party/
60+ git commit -m "chore: regenerate license files
61+
62+ Auto-generated by license-check workflow"
63+ git push
64+
65+ - name : Check if already commented
66+ if : steps.changes.outcome == 'failure' && steps.push.outcome == 'failure'
67+ id : check_comment
68+ uses : actions/github-script@v7
69+ with :
70+ script : |
71+ const { data: comments } = await github.rest.issues.listComments({
72+ owner: context.repo.owner,
73+ repo: context.repo.repo,
74+ issue_number: context.issue.number
75+ });
76+
77+ const alreadyCommented = comments.some(comment =>
78+ comment.user.login === 'github-actions[bot]' &&
79+ comment.body.includes('## ⚠️ License files need updating')
80+ );
81+
82+ core.setOutput('already_commented', alreadyCommented ? 'true' : 'false');
83+
84+ - name : Comment with instructions if cannot push
85+ if : steps.changes.outcome == 'failure' && steps.push.outcome == 'failure' && steps.check_comment.outputs.already_commented == 'false'
86+ uses : actions/github-script@v7
87+ with :
88+ script : |
89+ await github.rest.issues.createComment({
90+ owner: context.repo.owner,
91+ repo: context.repo.repo,
92+ issue_number: context.issue.number,
93+ body: `## ⚠️ License files need updating
94+
95+ The license files are out of date. I tried to fix them automatically but don't have permission to push to this branch.
96+
97+ **Please run:**
98+ \`\`\`bash
99+ script/licenses
100+ git add third-party-licenses.*.md third-party/
101+ git commit -m "chore : regenerate license files"
102+ git push
103+ \`\`\`
104+
105+ Alternatively, enable "Allow edits by maintainers" in the PR settings so I can fix it automatically.`
106+ });
107+
108+ - name : Fail check if changes needed
109+ if : steps.changes.outcome == 'failure'
110+ run : exit 1
111+
0 commit comments