Skip to content

Commit 681f562

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 - Remove unnecessary fetch-depth: 0 from checkout - Fix comment body indentation in PR comment - Add code-scanning exclusion for third-party files - Simplify licenses-check to regenerate and compare
1 parent a27f96f commit 681f562

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

.github/workflows/license-check.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ permissions:
1919
jobs:
2020
license-check:
2121
runs-on: ubuntu-latest
22-
# Don't run on forks (they can't push back) or dependabot (has its own token)
23-
if: github.event.pull_request.head.repo.full_name == github.repository
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]'
2424

2525
steps:
2626
- name: Check out code
2727
uses: actions/checkout@v6
2828
with:
2929
ref: ${{ github.head_ref }}
30-
# Need full history for push
31-
fetch-depth: 0
3230

3331
- name: Set up Go
3432
uses: actions/setup-go@v6
@@ -39,6 +37,8 @@ jobs:
3937
# which causes go-licenses to raise "Package ... does not have module info" errors.
4038
# For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633
4139
- name: Regenerate licenses
40+
env:
41+
CI: "true"
4242
run: |
4343
export GOROOT=$(go env GOROOT)
4444
export PATH=${GOROOT}/bin:$PATH
@@ -76,12 +76,12 @@ jobs:
7676
issue_number: context.issue.number,
7777
body: `## 📜 License files updated
7878
79-
I noticed the third-party license files were out of date and pushed a fix to this PR.
79+
I noticed the third-party license files were out of date and pushed a fix to this PR.
8080

81-
**What changed:** Dependencies were added, removed, or updated, which requires regenerating the license documentation.
81+
**What changed:** Dependencies were added, removed, or updated, which requires regenerating the license documentation.
8282

83-
**What I did:** Ran \`./script/licenses\` and committed the result.
83+
**What I did:** Ran \`./script/licenses\` and committed the result.
8484

85-
Please pull the latest changes before pushing again.`
85+
Please pull the latest changes before pushing again.`
8686
})
8787

script/licenses-check

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
#!/bin/bash
2+
#
3+
# Check that license files are up to date.
4+
# This script regenerates the license files and compares them with the committed versions.
5+
# If there are differences, it exits with an error.
26

3-
# Pinned version for CI reproducibility, latest for local development
4-
# See: https://github.com/cli/cli/pull/11161
5-
if [ "$CI" = "true" ]; then
6-
go install github.com/google/go-licenses@5348b744d0983d85713295ea08a20cca1654a45e # v2.0.1
7-
else
8-
go install github.com/google/go-licenses@latest
9-
fi
7+
set -e
8+
9+
# Store original files for comparison
10+
TEMPDIR="$(mktemp -d)"
11+
trap "rm -fr ${TEMPDIR}" EXIT
12+
13+
for goos in darwin linux windows; do
14+
cp "third-party-licenses.${goos}.md" "${TEMPDIR}/"
15+
done
16+
17+
# Regenerate using the same script
18+
./script/licenses
1019

11-
for goos in linux darwin windows ; do
12-
# Note: we ignore warnings because we want the command to succeed, however the output should be checked
13-
# for any new warnings, and potentially we may need to add license information.
14-
#
15-
# Normally these warnings are packages containing non go code, which may or may not require explicit attribution,
16-
# depending on the license.
17-
GOOS="${goos}" GOFLAGS=-mod=mod go-licenses report ./... --template .github/licenses.tmpl > third-party-licenses.${goos}.copy.md || echo "Ignore warnings"
18-
if ! diff -s third-party-licenses.${goos}.copy.md third-party-licenses.${goos}.md; then
19-
printf "License check failed for %s.\n\nPlease update the license file by running \`./script/licenses\` and committing the output.\n" "${goos}"
20-
rm -f third-party-licenses.${goos}.copy.md
21-
exit 1
20+
# Compare with originals
21+
has_diff=0
22+
for goos in darwin linux windows; do
23+
if ! diff -q "${TEMPDIR}/third-party-licenses.${goos}.md" "third-party-licenses.${goos}.md" >/dev/null 2>&1; then
24+
echo "License file for ${goos} is out of date:"
25+
diff "${TEMPDIR}/third-party-licenses.${goos}.md" "third-party-licenses.${goos}.md" || true
26+
has_diff=1
2227
fi
23-
rm -f third-party-licenses.${goos}.copy.md
2428
done
2529

30+
# Restore original files (check shouldn't modify anything)
31+
for goos in darwin linux windows; do
32+
cp "${TEMPDIR}/third-party-licenses.${goos}.md" "./"
33+
done
34+
35+
if [ $has_diff -eq 1 ]; then
36+
printf "\nLicense check failed.\n\nPlease update the license files by running \`./script/licenses\` and committing the output.\n"
37+
exit 1
38+
fi
39+
2640
echo "License check passed for all platforms."
2741

2842

0 commit comments

Comments
 (0)