Skip to content

Commit fca825e

Browse files
feat: auto-fix license files on PRs and improve CI reliability
Changes: - Pin go-licenses version in CI for reproducibility (commit 5348b744) - Add GOROOT/PATH setup for 'Package does not have module info' fix - Update license-check.yml to auto-fix and push to PR branches - Add CI=true env var to use pinned go-licenses version - Add dependabot exclusion from auto-fix workflow - Add code-scanning exclusion for third-party files
1 parent eb7d73c commit fca825e

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

.github/workflows/code-scanning.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ jobs:
4646
queries: "" # Default query suite
4747
packs: github/ccr-${{ matrix.language }}-queries
4848
config: |
49+
paths-ignore:
50+
- third-party
51+
- third-party-licenses.*.md
4952
default-setup:
5053
org:
5154
model-packs: [ ${{ github.event.inputs.code_scanning_codeql_packs }} ]
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,87 @@
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
23

34
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/**"
515
permissions:
6-
contents: read
16+
contents: write
17+
pull-requests: write
718

819
jobs:
920
license-check:
1021
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]'
1124

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+
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+

script/licenses

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919

2020
set -e
2121

22-
go install github.com/google/go-licenses@latest
22+
# Pinned version for CI reproducibility, latest for local development
23+
# See: https://github.com/cli/cli/pull/11161
24+
if [ "$CI" = "true" ]; then
25+
go install github.com/google/go-licenses@5348b744d0983d85713295ea08a20cca1654a45e # v2.0.1
26+
else
27+
go install github.com/google/go-licenses@latest
28+
fi
2329

2430
# actions/setup-go does not setup the installed toolchain to be preferred over the system install,
2531
# which causes go-licenses to raise "Package ... does not have module info" errors in CI.

0 commit comments

Comments
 (0)