GitHub: Add action to check file headers.
The check_header.sh script checks a given file for changes
made to its file header, i.e. @date and @version. For each
file that did not receive a proper update a warning message
is generated.
diff --git a/.github/workflows/fileheader.sh b/.github/workflows/fileheader.sh
new file mode 100755
index 0000000..d2fdf63
--- /dev/null
+++ b/.github/workflows/fileheader.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+PR=$(echo ${GITHUB_REF} | cut -f3 -d/)
+
+echo "Scanning file headers"
+echo "GitHub API: ${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR}/comments"
+
+function add_comment_for() {
+ FILE=$(echo $1 | cut -f1 -d:)
+ LINE=$(echo $1 | cut -f2 -d:)
+ MSG=$(echo $1 | cut -f3 -d:)
+ echo "Adding comment '${MSG}' to ${FILE}:${LINE} ..."
+ curl \
+ -X POST \
+ -H "Authorization: token ${GITHUB_TOKEN}" \
+ -H "Accept: application/vnd.github.v3+json" \
+ ${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR}/comments \
+ -d "{
+ \"body\":\"${MSG}\",
+ \"path\":\"${FILE}\",
+ \"side\":\"RIGHT\",
+ \"line\":\"${LINE}\"
+ }"
+}
+
+rm comments
+for changed_file in ${GITHUB_CHANGED_FILES}; do
+ ${GITHUB_WORKSPACE}/CMSIS/Utilities/check_header.sh ${changed_file} >> comments
+done
+
+while IFS="" read -r p || [ -n "$p" ]; do
+ add_comment_for "$p"
+done < comments
+
+exit 0
diff --git a/.github/workflows/fileheader.yml b/.github/workflows/fileheader.yml
new file mode 100644
index 0000000..8e453cf
--- /dev/null
+++ b/.github/workflows/fileheader.yml
@@ -0,0 +1,32 @@
+name: File header
+
+on:
+ pull_request:
+ branches: [ develop ]
+ paths:
+ - 'CMSIS/Core/**'
+ - 'CMSIS/Core_A/**'
+ - 'CMSIS/RTOS2/Include/**'
+ - 'CMSIS/RTOS2/Source/**'
+ - 'Device/**'
+
+permissions:
+ contents: read
+ pull-requests: write
+
+jobs:
+ check:
+ name: Check file header
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v2
+ - id: files
+ uses: jitterbit/get-changed-files@v1
+ - name: Check changed files
+ run: |
+ RC=0
+ for changed_file in ${{ steps.files.outputs.all }}; do
+ ./CMSIS/Utilities/check_header.sh ${changed_file} || RC=1
+ done
+ exit $RC
diff --git a/CMSIS/Utilities/check_header.sh b/CMSIS/Utilities/check_header.sh
new file mode 100755
index 0000000..07909ad
--- /dev/null
+++ b/CMSIS/Utilities/check_header.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+FILE=$1
+RESULT=0
+
+FILE_DATE=$(grep -E '@date\s+([0-9]{2}\. \w+ [0-9]{4})' ${FILE} | sed -E 's/^.*@date\s+([0-9]{2}\. \w+ [0-9]{4}).*/\1/')
+if [[ ! -z $FILE_DATE ]]; then
+ AUTHOR_DATE=$(git log -1 --pretty="format:%ad" --date="format:%d. %B %Y" ${FILE})
+ if [[ $AUTHOR_DATE != $FILE_DATE ]]; then
+ FILE_DATE_LINE=$(grep -En "@date.*${FILE_DATE}" ${FILE} | cut -f1 -d:)
+ echo "${FILE}:${FILE_DATE_LINE}:Please update file date to '$AUTHOR_DATE'." >&2
+ RESULT=1
+ fi
+fi
+
+FILE_VERSION=$(grep -E '@version\s+V?([0-9]+\.[0-9]+(\.[0-9]+)?)' ${FILE} | sed -E 's/^.*@version\s+V?([0-9]+\.[0-9]+(\.[0-9]+)?).*/\1/')
+if [[ ! -z $FILE_VERSION ]]; then
+ AUTHOR_REV=$(git log -1 --pretty="format:%h")
+ PARENT_REV=$(git log -1 --pretty="format:%p")
+ VERSION_BLAME=$(git blame ${PARENT_REV}..${AUTHOR_REV} -L ${FILE_VERSION_LINE},${FILE_VERSION_LINE} ${FILE} | sed -E 's/^\^([[:alnum:]]+).*/\1/')
+ if [[ $AUTHOR_REV != $VERSION_BLAME ]]; then
+ FILE_VERSION_LINE=$(grep -En "@version.*${FILE_VERSION}" ${FILE} | cut -f1 -d:)
+ echo "${FILE}:${FILE_VERSION_LINE}:Please increment file version." >&2
+ RESULT=1
+ fi
+fi
+
+exit $RESULT