|
| 1 | +name: License Header Check |
| 2 | + |
| 3 | +on: [pull_request, workflow_dispatch] |
| 4 | + |
| 5 | +defaults: |
| 6 | + run: |
| 7 | + shell: bash |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-license-headers: |
| 11 | + name: Check License Headers |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + lfs: false |
| 20 | + |
| 21 | + - name: Check license headers |
| 22 | + run: | |
| 23 | + expected_header_1="# This file is part of sbi, a toolkit for simulation-based inference. sbi is licensed" |
| 24 | + expected_header_2="# under the Apache License Version 2.0, see <https://www.apache.org/licenses/>" |
| 25 | +
|
| 26 | + invalid_files=() |
| 27 | +
|
| 28 | + while IFS= read -r file; do |
| 29 | + file_header_1=$(head -n 1 "$file") |
| 30 | + file_header_2=$(head -n 2 "$file" | tail -n 1) |
| 31 | +
|
| 32 | + invalid=false |
| 33 | +
|
| 34 | + # Check the first line |
| 35 | + if [ "$file_header_1" != "$expected_header_1" ]; then |
| 36 | + invalid=true |
| 37 | + fi |
| 38 | +
|
| 39 | + # Check the second line |
| 40 | + if [ "$file_header_2" != "$expected_header_2" ]; then |
| 41 | + invalid=true |
| 42 | + fi |
| 43 | +
|
| 44 | + # If either line is invalid, add to the list |
| 45 | + if [ "$invalid" = true ]; then |
| 46 | + invalid_files+=("$file") |
| 47 | + fi |
| 48 | +
|
| 49 | + done < <(find sbi tests -name "*.py" -type f) |
| 50 | +
|
| 51 | + # Report results |
| 52 | + if [ ${#invalid_files[@]} -ne 0 ]; then |
| 53 | + echo "❌ Missing or incorrect license headers in the following files:" |
| 54 | + for file in "${invalid_files[@]}"; do |
| 55 | + echo " $file" |
| 56 | + done |
| 57 | + echo "Make sure the following two lines are provided and correctly spelled at the very start of all above listed files" |
| 58 | + echo "$expected_header_1" |
| 59 | + echo "$expected_header_2" |
| 60 | + exit 1 |
| 61 | + else |
| 62 | + echo "✅ All files have the correct license headers." |
| 63 | + exit 0 |
| 64 | + fi |
0 commit comments