|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | +# Check that the files in a PR include license headers. |
| 17 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 18 | + |
| 19 | +name: License checker |
| 20 | +run-name: >- |
| 21 | + Check license headers in the files of PR |
| 22 | + #${{github.event.inputs.pr-number || github.event.pull_request.number}} |
| 23 | + by ${{github.actor}} |
| 24 | +
|
| 25 | +on: |
| 26 | + pull_request: |
| 27 | + types: |
| 28 | + - opened |
| 29 | + - reopened |
| 30 | + - synchronize |
| 31 | + branches: |
| 32 | + - main |
| 33 | + - master |
| 34 | + |
| 35 | + # Support merge queues. |
| 36 | + merge_group: |
| 37 | + types: |
| 38 | + - checks_requested |
| 39 | + |
| 40 | + # Allow manual invocation. |
| 41 | + workflow_dispatch: |
| 42 | + inputs: |
| 43 | + pr-number: |
| 44 | + description: 'The PR number of the PR to check:' |
| 45 | + type: string |
| 46 | + required: true |
| 47 | + debug: |
| 48 | + description: 'Run with debugging options' |
| 49 | + type: boolean |
| 50 | + default: true |
| 51 | + |
| 52 | +env: |
| 53 | + # Location of a file containing path patterns to be ignored when checking |
| 54 | + # files for license headers. The file uses `doublestar` file pattern syntax. |
| 55 | + ignore_patterns_file: .github/workflows/config/license-checker-ignore.txt |
| 56 | + |
| 57 | +# Declare default workflow permissions as read only. |
| 58 | +permissions: read-all |
| 59 | + |
| 60 | +concurrency: |
| 61 | + # Cancel any previously-started but still active runs on the same branch. |
| 62 | + cancel-in-progress: true |
| 63 | + group: ${{github.workflow}}-${{github.event.pull_request.number||github.ref}} |
| 64 | + |
| 65 | +jobs: |
| 66 | + check-license: |
| 67 | + if: github.repository_owner == 'quantumlib' |
| 68 | + name: Check files for license headers |
| 69 | + runs-on: ubuntu-24.04 |
| 70 | + timeout-minutes: 5 |
| 71 | + env: |
| 72 | + PR_NUMBER: ${{inputs.pr-number || github.event.pull_request.number}} |
| 73 | + # The next var is used by Bash. Add 'xtrace' to the options for all Bash |
| 74 | + # scripts if this is a workflow_dispatch run & the user set debug=true. |
| 75 | + SHELLOPTS: ${{inputs.debug && 'xtrace' || '' }} |
| 76 | + steps: |
| 77 | + - if: github.event_name == 'workflow_dispatch' |
| 78 | + name: Check out a copy of the git repository (manual invocation) |
| 79 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 80 | + with: |
| 81 | + ref: refs/pull/${{env.PR_NUMBER}}/head |
| 82 | + repository: ${{github.repository}} |
| 83 | + |
| 84 | + - if: github.event_name != 'workflow_dispatch' |
| 85 | + name: Check out a copy of the git repository (pull requests) |
| 86 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 87 | + |
| 88 | + - name: Check files |
| 89 | + env: |
| 90 | + GH_TOKEN: ${{github.token}} |
| 91 | + VERBOSE: ${{inputs.debug && '-v' || '' }} |
| 92 | + run: | |
| 93 | + changed_files=$(gh pr diff "$PR_NUMBER" --name-only) |
| 94 | + if [[ -z "$changed_files" ]]; then |
| 95 | + echo "::notice::No files changed in PR #$PR_NUMBER." |
| 96 | + exit 0 |
| 97 | + fi |
| 98 | +
|
| 99 | + # As of v.1.2.0 addlicense doesn't have a config file option or a way |
| 100 | + # to read ignore patterns from a file. This our homegrown solution. |
| 101 | + ignore_patterns=$(awk '!/^#/ && NF' ${{env.ignore_patterns_file}}) |
| 102 | + ignore_args=$(printf -- "-ignore='%s' " "${ignore_patterns[@]}") |
| 103 | +
|
| 104 | + unlicensed=$(go run github.com/google/addlicense@v1.2.0 \ |
| 105 | + -check "$ignore_args" "$VERBOSE" "${changed_files[@]}") |
| 106 | +
|
| 107 | + if [[ -n "$unlicensed" ]]; then |
| 108 | + echo '::error::Found files missing licenses.' |
| 109 | + echo '::error::Please visit the workflow summary.' |
| 110 | + { |
| 111 | + echo "## Some files in PR #$PR_NUMBER lack license headers" |
| 112 | + echo "" |
| 113 | + echo "$unlicensed" |
| 114 | + } >> "$GITHUB_STEP_SUMMARY" |
| 115 | + exit 1 |
| 116 | + fi |
0 commit comments