diff --git a/.github/verify_header.sh b/.github/verify_header.sh new file mode 100755 index 0000000..d17f64d --- /dev/null +++ b/.github/verify_header.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# +# Copyright(c) 2022 Intel Corporation +# SPDX-License-Identifier: BSD-3-Clause +# + +# COPYRIGHT_REGEX is lowercase, because the whole line is +# converted to lowercase before test against this regex. +COPYRIGHT_REGEX="(copyright|\(c\))\s*([0-9]{4}(\s*-\s*([0-9]{4}))?)" +LICENSE_REGEX="SPDX-License-Identifier: BSD-3-Clause$" +YEAR=$(date +"%Y") + +unset copyright_header license_header + +# Read lines until proper copyright and license headers are found. +while read -r line && [[ ! "$copyright_header" || ! "$license_header" ]]; do + if [[ "${line,,}" =~ $COPYRIGHT_REGEX ]]; then + # If the fourth regex group (from year range) doesn't exist, + # use the second regex group instead (from a single year). + copyright_year=${BASH_REMATCH[4]:-${BASH_REMATCH[2]}} + + if [[ $copyright_year == $YEAR ]]; then + copyright_header="correct_copyright_header_found" + fi + elif [[ "$line" =~ $LICENSE_REGEX ]]; then + license_header="correct_license_header_found" + fi +done < "$1" + +# Proper copyright and license info were found - all good. +[[ "$copyright_header" && "$license_header" ]] && exit 0 + +[[ ! "$copyright_header" ]] && echo >&2 "error: file '$1' does not contain any appropriate copyright info" +[[ ! "$license_header" ]] && echo >&2 "error: file '$1' does not contain appropriate license identifier" +exit 1 diff --git a/.github/workflows/license-check.yml b/.github/workflows/license-check.yml new file mode 100644 index 0000000..6d30d10 --- /dev/null +++ b/.github/workflows/license-check.yml @@ -0,0 +1,43 @@ +name: Licence-date-verification +permissions: + contents: read + pull-requests: read +on: + pull_request: + branches: + - master + - v* + +env: + EXTENSIONS: "c h cpp py go sh mk spec service" + FILES: "configure configure.d/* *Makefile utils/casctl tools/pckgen.d/deb/debian/rules" +jobs: + verify-date: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v47.0.0 + with: + files_ignore: '.github/**' + - name: List all changed files + run: | + files_to_check=(${{ steps.changed-files.outputs.added_files }}) + files_to_check+=(${{ steps.changed-files.outputs.modified_files }}) + + for file in ${files_to_check[@]}; do + for file_in_list in $FILES; do + if [[ "$file" == $file_in_list ]]; then + .github/verify_header.sh "$file" + continue 2 + fi + done + + extension=${file##*.} + if [[ "$EXTENSIONS" =~ $extension ]]; then + .github/verify_header.sh "$file" + fi + done