Skip to content

Commit

Permalink
Add GH workflow for spdx header check
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Engel <[email protected]>
  • Loading branch information
engelmi committed Oct 9, 2024
1 parent 09ca78f commit a82de79
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Lint

on:
pull_request:
branches: [main]
workflow_dispatch:

jobs:
common:
name: Common
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v4
with:
submodules: "true"

- name: Check SPDX headers
run: |
./scripts/spdx-header-check.sh
63 changes: 63 additions & 0 deletions scripts/spdx-header-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/bash -e
#
# Copyright Contributors to the Eclipse BlueChi project
#
# SPDX-License-Identifier: MIT-0

#
# Checked files regex
#
CHECKED_FILES=".*\(\.c\|\.h\|\.go\|\.py\|\.rs\|\.sh\|\.xml\)"

#
# License which is OK when found in the project
#
LICENSE="MIT-0"

#
# List of files to check using the project license
#
FILES="$(find -type f \
-regex ${CHECKED_FILES})"

#
# Iterate over files within a path and check the allowed licenses
#
# Parameters:
# files - a list of files to check the license
# licences - a list of approved licenses
#
# Result:
# cl_result - 0 if all files contain valid license, otherwise 1
#
check_licenses() {
local files=$1
local licenses=$2
cl_result=0

for f in ${files} ; do
# scan for license only within the 1st 5 lines of each file
license_found=$(head -n 5 ${f} | grep -Po '.*SPDX-License-Identifier\: \K\S+' || echo "-1")
if [ "${license_found}" == "-1" ]; then
cl_result=1
echo "File '${f}' does not contain SPDX header!"
else
valid_license=1
for l in ${licenses} ; do
if [ "${license_found}" == "$l" ] ; then
valid_license=0
fi
done
if [ "${valid_license}" == "1" ] ; then
cl_result=1
echo "File '${f}' contains unapproved license '${license_found}'"
fi
fi
done
}


check_licenses "${FILES}" "${LICENSE}"
result=$cl_result

exit $((${result}))

0 comments on commit a82de79

Please sign in to comment.