Skip to content

Commit d2e7b2c

Browse files
committed
Migrate to addlicense
1 parent af4c190 commit d2e7b2c

File tree

5 files changed

+133
-112
lines changed

5 files changed

+133
-112
lines changed

.github/workflows/ci.yml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,50 @@ jobs:
150150
go-version: '1.25.x'
151151

152152
- name: Install govulncheck
153-
run: go install golang.org/x/vuln/cmd/govulncheck@latest
153+
run: go install golang.org/x/vuln/cmd/govulncheck@v1.1.3
154154

155155
- name: Run govulncheck
156156
run: govulncheck ./...
157+
158+
# License header check job
159+
# Security: pull_request_target grants write access and secrets to fork PRs
160+
# Malicious PR could exfiltrate secrets via modified Makefile targets
161+
# Gated by security_check requiring 'safe-to-test' label from maintainer
162+
# See security_check job for fork PR protection details
163+
license:
164+
name: License Check
165+
runs-on: ubuntu-latest
166+
needs: [security_check]
167+
if: |
168+
always() &&
169+
(github.event_name == 'push' ||
170+
(github.event_name == 'pull_request_target' && needs.security_check.outputs.is_safe == 'true'))
171+
permissions:
172+
contents: read
173+
174+
steps:
175+
- name: Checkout code
176+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
177+
with:
178+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
179+
180+
- name: Set up Go
181+
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v5.2.0
182+
with:
183+
go-version: '1.25.x'
184+
185+
- name: Cache Go modules
186+
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
187+
with:
188+
path: |
189+
~/.cache/go-build
190+
~/go/pkg/mod
191+
key: ${{ runner.os }}-go-1.25.x-addlicense-v1.1.1-${{ hashFiles('**/go.sum') }}
192+
restore-keys: |
193+
${{ runner.os }}-go-1.25.x-addlicense-
194+
195+
- name: Install addlicense
196+
run: go install github.com/google/[email protected]
197+
198+
- name: Check license headers
199+
run: make check-license

CONTRIBUTING.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Feature suggestions are welcome! Please:
3131
1. Fork the repository and create a branch from `main`
3232
2. Make your changes following the coding guidelines below
3333
3. Add tests for any new functionality
34-
4. Add SPDX headers to all new Go files (see Headers section)
34+
4. Add SPDX headers to new Go files - see Headers section (contributors: add manually with YOUR copyright, do NOT run `make license`)
3535
5. Ensure all checks pass: `make test`, `make lint`, `make security`
3636
6. Update documentation as needed
3737
7. Write clear commit messages following conventional commits
@@ -73,7 +73,40 @@ All Go source files must include SPDX license identifier and copyright notice:
7373
package netconf
7474
```
7575

76-
Use provided scripts: `bash scripts/add-headers.sh` or `bash scripts/check-headers.sh`
76+
#### Adding Headers
77+
78+
The project uses [Google's addlicense](https://github.com/google/addlicense) tool for license header management.
79+
80+
**Install addlicense:**
81+
```bash
82+
go install github.com/google/[email protected]
83+
# Or use: make tools
84+
```
85+
86+
**Version Note**: Use the exact version shown above (`@v1.1.1`) to match the CI environment. Using `@latest` may cause version mismatches between local development and CI checks.
87+
88+
**Add headers to new files:**
89+
```bash
90+
make license
91+
```
92+
93+
**Verify headers:**
94+
```bash
95+
make check-license
96+
```
97+
98+
**Important Notes:**
99+
- **For Maintainers**: Use `make license` to add headers with project default copyright holder
100+
- **For Contributors**: Do NOT use `make license` - it will overwrite your copyright holder
101+
- **For Contributors**: Manually add headers to new files with YOUR organization's copyright:
102+
```go
103+
// SPDX-License-Identifier: MPL-2.0
104+
// Copyright (c) 2025 Your Organization Name
105+
106+
package netconf
107+
```
108+
- Then run `make check-license` to verify format compliance (accepts any copyright holder)
109+
- Both SPDX-first and Copyright-first formats are accepted by the check command
77110

78111
### Testing
79112

Makefile

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
.PHONY: help test lint security coverage benchmark clean fmt verify tools ci
1+
.PHONY: help test lint security coverage benchmark clean fmt verify tools ci license check-license
22

33
# Default target
44
help:
55
@echo "Available targets:"
6-
@echo " test - Run all tests"
7-
@echo " lint - Run linters (golangci-lint with gosec)"
8-
@echo " security - Run vulnerability check (govulncheck)"
9-
@echo " coverage - Run tests with coverage report"
10-
@echo " benchmark - Run benchmarks"
11-
@echo " clean - Clean build artifacts"
12-
@echo " fmt - Format code"
13-
@echo " verify - Run all checks (test, lint, security)"
14-
@echo " tools - Install development tools"
15-
@echo " ci - Run CI pipeline checks"
6+
@echo " test - Run all tests"
7+
@echo " lint - Run linters (golangci-lint with gosec)"
8+
@echo " security - Run vulnerability check (govulncheck)"
9+
@echo " coverage - Run tests with coverage report"
10+
@echo " benchmark - Run benchmarks"
11+
@echo " clean - Clean build artifacts"
12+
@echo " fmt - Format code"
13+
@echo " license - Add license headers to all Go files"
14+
@echo " check-license - Verify license headers are present"
15+
@echo " verify - Run all checks (test, lint, security, license)"
16+
@echo " tools - Install development tools"
17+
@echo " ci - Run CI pipeline checks (test, lint, security, license)"
1618

1719
# Run tests
1820
test:
@@ -60,15 +62,52 @@ fmt:
6062
gofmt -s -w .
6163

6264
# Run all checks
63-
verify: fmt test lint security
65+
verify: fmt test lint security check-license
6466
@echo "All checks passed!"
6567

6668
# Install development tools
6769
tools:
6870
@echo "Installing development tools..."
69-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
70-
go install golang.org/x/vuln/cmd/govulncheck@latest
71+
go install github.com/golangci/golangci-lint/cmd/[email protected]
72+
go install golang.org/x/vuln/cmd/[email protected]
73+
go install github.com/google/[email protected]
74+
75+
# Add license headers to all Go files
76+
# Uses Google's addlicense tool (github.com/google/addlicense)
77+
# Adds MPL-2.0 + SPDX headers with your name as copyright holder
78+
# Existing headers are preserved (no re-processing)
79+
# Note: Requires addlicense in PATH or ~/go/bin - install with 'make tools'
80+
license:
81+
@echo "Adding license headers to Go files..."
82+
@if [ -z "$(HOME)" ] || [ ! -d "$(HOME)" ]; then \
83+
echo "Error: Invalid HOME directory (required for ~/go/bin tool installation)"; \
84+
echo " If running in Docker/container, ensure HOME is set and ~/go/bin exists"; exit 1; \
85+
fi
86+
@PATH="$(HOME)/go/bin:$$PATH" && \
87+
command -v addlicense >/dev/null 2>&1 || { echo "Error: addlicense not found. Install with: make tools"; exit 1; } && \
88+
find . -name "*.go" -not -path "./vendor/*" -not -path "./examples/*" -print0 | \
89+
xargs -0 addlicense -c "Daniel Schmidt" -l mpl -s=only -y 2025 -v
90+
@echo "License header addition complete!"
91+
92+
# Check that all Go files have license headers
93+
# Uses addlicense in check mode - accepts ANY copyright holder name
94+
# Only verifies that MPL-2.0 + SPDX headers exist
95+
# Note: Requires addlicense in PATH or ~/go/bin - install with 'make tools'
96+
check-license:
97+
@echo "Checking license headers..."
98+
@if [ -z "$(HOME)" ] || [ ! -d "$(HOME)" ]; then \
99+
echo "Error: Invalid HOME directory (required for ~/go/bin tool installation)"; \
100+
echo " If running in Docker/container, ensure HOME is set and ~/go/bin exists"; exit 1; \
101+
fi
102+
@PATH="$(HOME)/go/bin:$$PATH" && \
103+
command -v addlicense >/dev/null 2>&1 || { echo "Error: addlicense not found. Install with: make tools"; exit 1; } && \
104+
find . -name "*.go" -not -path "./vendor/*" -not -path "./examples/*" -print0 | \
105+
if xargs -0 addlicense -check -l mpl -s=only -y 2025; then \
106+
echo "✓ All Go files have license headers!"; \
107+
else \
108+
echo "✗ Some files are missing license headers. Run 'make license' to add them."; exit 1; \
109+
fi
71110

72111
# CI pipeline checks (used in GitHub Actions)
73-
ci: test lint security
112+
ci: test lint security check-license
74113
@echo "CI checks passed!"

scripts/add-headers.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

scripts/check-headers.sh

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)