Skip to content

refactor: move base branch filter to on: block #4932

refactor: move base branch filter to on: block

refactor: move base branch filter to on: block #4932

Workflow file for this run

# Automatically fix license files on PRs that need updates
# Instead of just failing, this workflow pushes the fix and comments on the PR
name: License Check
on:
pull_request:
branches:
- main # Only run when PR targets main
paths:
- "**.go"
- go.mod
- go.sum
- ".github/licenses.tmpl"
- "script/licenses*"
- "third-party-licenses.*.md"
- "third-party/**"
permissions:
contents: write
pull-requests: write
jobs:
license-check:
runs-on: ubuntu-latest
# Don't run on forks (they can't push back)
if: github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Check out code
uses: actions/checkout@v6
with:
ref: ${{ github.head_ref }}
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version-file: "go.mod"
# actions/setup-go does not setup the installed toolchain to be preferred over the system install,
# which causes go-licenses to raise "Package ... does not have module info" errors.
# For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633
- name: Regenerate licenses
env:
CI: "true"
run: |
export GOROOT=$(go env GOROOT)
export PATH=${GOROOT}/bin:$PATH
./script/licenses
- name: Check for changes
id: changes
run: |
if git diff --exit-code; then
echo "changed=false" >> $GITHUB_OUTPUT
echo "✅ License files are up to date"
else
echo "changed=true" >> $GITHUB_OUTPUT
echo "📝 License files need updating"
git diff --stat
fi
- name: Commit and push fixes
if: steps.changes.outputs.changed == 'true'
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add third-party third-party-licenses.*.md
git commit -m "chore: regenerate third-party licenses"
git push
- name: Comment on PR
if: steps.changes.outputs.changed == 'true'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## 📜 License files updated
I noticed the third-party license files were out of date and pushed a fix to this PR.

Check failure on line 81 in .github/workflows/license-check.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/license-check.yml

Invalid workflow file

You have an error in your yaml syntax on line 81
**What changed:** Dependencies were added, removed, or updated, which requires regenerating the license documentation.
**What I did:** Ran \`./script/licenses\` and committed the result.
Please pull the latest changes before pushing again.`
})
# After pushing the fix, check if PR now has no functional changes (just license updates)
# This handles the case where the PR only needed license updates and nothing else
- name: Check if PR is now empty
if: steps.changes.outputs.changed == 'true'
id: empty_check
uses: actions/github-script@v7
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
// Check if ALL changes are just license files
const nonLicenseFiles = files.filter(f =>
!f.filename.startsWith('third-party-licenses.') &&
!f.filename.startsWith('third-party/')
);
const isEmpty = nonLicenseFiles.length === 0;
// Only close if the PR title/body suggests it wasn't meant to be about licenses
// or if it was created by a bot (which might auto-update dependencies)
const isLicenseFocused =
pr.title.toLowerCase().includes('licen') ||
pr.title.toLowerCase().includes('third-party') ||
pr.body?.toLowerCase().includes('update.*licen');
const shouldClose = isEmpty && !isLicenseFocused && pr.user.type !== 'Bot';
core.setOutput('should_close', shouldClose);
if (isEmpty && isLicenseFocused) {
core.info('PR only has license files but appears to be intentionally about licenses - keeping open');
} else if (isEmpty && pr.user.type === 'Bot') {
core.info('PR is from a bot and only has license files - keeping open (might be dependabot)');
} else if (shouldClose) {
core.info('PR only contains license file changes and appears stale - will close');
} else {
core.info(`PR has ${nonLicenseFiles.length} non-license file changes - keeping open`);
}
- name: Close stale license-only PR
if: steps.changes.outputs.changed == 'true' && steps.empty_check.outputs.should_close == 'true'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## 🤖 Auto-closing stale PR
This PR now only contains license file updates with no other functional changes. The license updates have been applied, so closing this PR as complete.
If this PR should have had other changes, please reopen it and add the intended changes.`
});
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
state: 'closed'
});