Skip to content

Commit 33e953e

Browse files
authored
feat: add pre-commit hook for license header insertion (#1492)
This PR adds pre-commit hooks for license header validation and CI workflow integration.
1 parent 62d76cf commit 33e953e

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

.github/workflows/test.yml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# SPDX-License-Identifier: FSL-1.1-MIT
12
name: "CI"
23

34
# The workflow should be triggered on any push and release events.
@@ -27,7 +28,46 @@ jobs:
2728
- name: Install dependencies
2829
run: pip install pre-commit
2930
- name: Run pre-commit
30-
run: pre-commit run --all-files
31+
run: SKIP=license-headers pre-commit run --all-files
32+
33+
license-headers:
34+
if: github.event_name == 'push' || github.event_name == 'pull_request'
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
38+
with:
39+
fetch-depth: 0
40+
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
41+
with:
42+
python-version: "3.14.3"
43+
- run: pip install pre-commit
44+
45+
- name: Run pre-commit on changed files
46+
run: |
47+
if [ "${{ github.event_name }}" = "pull_request" ]; then
48+
BASE_REF="${{ github.event.pull_request.base.sha }}"
49+
else
50+
BASE_REF="${{ github.event.before }}"
51+
fi
52+
53+
if [ -z "$BASE_REF" ] || [ "$BASE_REF" = "0000000000000000000000000000000000000000" ]; then
54+
echo "No valid base ref; skipping."
55+
exit 0
56+
fi
57+
58+
if ! git cat-file -e "$BASE_REF^{commit}" 2>/dev/null; then
59+
echo "$BASE_REF is not available locally; falling back to HEAD^."
60+
BASE_REF="HEAD^"
61+
fi
62+
63+
CHANGED_FILES=$(git diff --name-only --diff-filter=AM "$BASE_REF" HEAD)
64+
65+
if [ -z "$CHANGED_FILES" ]; then
66+
echo "No added or modified files; skipping."
67+
exit 0
68+
fi
69+
70+
pre-commit run license-headers --files $CHANGED_FILES
3171
3272
django-check:
3373
runs-on: ubuntu-latest

.pre-commit-config.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# SPDX-License-Identifier: FSL-1.1-MIT
2+
13
# See https://pre-commit.com for more information
24
# See https://pre-commit.com/hooks.html for more hooks
35
repos:
@@ -14,3 +16,18 @@ repos:
1416
types: [python]
1517
- id: check-yaml
1618
- id: check-added-large-files
19+
20+
- repo: https://github.com/Lucas-C/pre-commit-hooks
21+
rev: v1.5.5
22+
hooks:
23+
- id: insert-license
24+
name: insert-license
25+
alias: license-headers
26+
files: \.(py|yaml|yml|md|sh|Dockerfile|ini)$
27+
stages: [pre-commit]
28+
args:
29+
- --license-filepath
30+
- LICENSE_HEADER.txt
31+
- --comment-style
32+
- "#"
33+
- --no-extra-eol

LICENSE_HEADER.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SPDX-License-Identifier: FSL-1.1-MIT

0 commit comments

Comments
 (0)