|
| 1 | +#/ |
| 2 | +# @license Apache-2.0 |
| 3 | +# |
| 4 | +# Copyright (c) 2024 The Stdlib Authors. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +#/ |
| 18 | + |
| 19 | +# Workflow name: |
| 20 | +name: 'Lint Copyright Years in Newly Added Files' |
| 21 | + |
| 22 | +# Workflow triggers: |
| 23 | +on: |
| 24 | + pull_request: |
| 25 | + types: [opened, synchronize, reopened] |
| 26 | + |
| 27 | +# Global permissions: |
| 28 | +permissions: |
| 29 | + # Allow read-only access to the repository contents: |
| 30 | + contents: read |
| 31 | + |
| 32 | +# Workflow jobs: |
| 33 | +jobs: |
| 34 | + # Define a job for linting copyright years in newly added files: |
| 35 | + lint: |
| 36 | + name: 'Lint Copyright Years' |
| 37 | + runs-on: ubuntu-latest |
| 38 | + |
| 39 | + steps: |
| 40 | + # Checkout the repository: |
| 41 | + - name: 'Checkout repository' |
| 42 | + # Pin action to full length commit SHA |
| 43 | + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 |
| 44 | + with: |
| 45 | + # Specify whether to remove untracked files before checking out the repository: |
| 46 | + clean: true |
| 47 | + |
| 48 | + # Limit clone depth to the last 1000 commits: |
| 49 | + fetch-depth: 100 |
| 50 | + |
| 51 | + # Specify whether to download Git-LFS files: |
| 52 | + lfs: false |
| 53 | + timeout-minutes: 10 |
| 54 | + |
| 55 | + # Get list of newly added files: |
| 56 | + - name: 'Get list of newly added files' |
| 57 | + id: added-files |
| 58 | + run: | |
| 59 | + page=1 |
| 60 | + files="" |
| 61 | + while true; do |
| 62 | + new_files=$(curl -s \ |
| 63 | + -H "Accept: application/vnd.github.v3+json" \ |
| 64 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 65 | + "${{ github.api_url }}/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files?page=$page&per_page=100" \ |
| 66 | + | jq -r '.[] | select(.status == "added") | .filename') |
| 67 | + if [ -z "$new_files" ]; then |
| 68 | + break |
| 69 | + fi |
| 70 | + files="$files $new_files" |
| 71 | + page=$((page+1)) |
| 72 | + done |
| 73 | + files=$(echo "$files" | tr '\n' ' ' | sed 's/^ //;s/ $//') |
| 74 | + echo "files=${files}" >> $GITHUB_OUTPUT |
| 75 | +
|
| 76 | + # Check copyright years in newly added files: |
| 77 | + - name: 'Check copyright years' |
| 78 | + run: | |
| 79 | + current_year=$(date +"%Y") |
| 80 | + files="${{ steps.added-files.outputs.files }}" |
| 81 | + exit_code=0 |
| 82 | +
|
| 83 | + echo "## 📄 Copyright Year Check Results" >> $GITHUB_STEP_SUMMARY |
| 84 | + echo "_Only newly added files are checked for the current year in the copyright notice._" >> $GITHUB_STEP_SUMMARY |
| 85 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 86 | +
|
| 87 | + if [[ -z "$files" ]]; then |
| 88 | + echo "No newly added files to check." >> $GITHUB_STEP_SUMMARY |
| 89 | + exit 0 |
| 90 | + fi |
| 91 | +
|
| 92 | + for file in $files; do |
| 93 | + if [[ ! -f "$file" ]]; then |
| 94 | + continue |
| 95 | + fi |
| 96 | +
|
| 97 | + # Check if file contains stdlib copyright notice: |
| 98 | + year=$(grep -oP \ |
| 99 | + 'Copyright \(c\) \K[0-9]{4}(?= The Stdlib Authors\.)' \ |
| 100 | + "$file") |
| 101 | +
|
| 102 | + if [[ -n "$year" ]]; then |
| 103 | + if [[ "$year" == "$current_year" ]]; then |
| 104 | + echo "- ✅ \`$file\`: Year is correct (**$year**)" >> $GITHUB_STEP_SUMMARY |
| 105 | + else |
| 106 | + echo "- ❌ **Error**: \`$file\` — Expected year **$current_year**, found **$year**" >> $GITHUB_STEP_SUMMARY |
| 107 | + exit_code=1 |
| 108 | + fi |
| 109 | + else |
| 110 | + echo "- ⚠️ No copyright notice found in \`$file\`" >> $GITHUB_STEP_SUMMARY |
| 111 | + fi |
| 112 | + done |
| 113 | +
|
| 114 | + exit $exit_code |
0 commit comments