From 757a388e1219954a87fb63615d908d4a54bc7754 Mon Sep 17 00:00:00 2001 From: Dalton Bohning Date: Tue, 3 Dec 2024 16:40:53 +0000 Subject: [PATCH] DAOS-15677 cq: add copyright GHA Add GHA to check for copyright update. Move core logic from update-copyright githook into check_update_copyright.sh so the logic is shared between the githook and GHA. Skip-build: true Required-githooks: true Signed-off-by: Dalton Bohning --- .github/workflows/linting.yml | 12 ++ utils/cq/check_update_copyright.sh | 110 ++++++++++++++++++ .../githooks/pre-commit.d/10-update-copyright | 81 ------------- .../pre-commit.d/10-update-copyright.sh | 17 +++ .../{30-Jenkinsfile => 30-Jenkinsfile.sh} | 1 + .../{50-clang-format => 50-clang-format.sh} | 0 6 files changed, 140 insertions(+), 81 deletions(-) create mode 100755 utils/cq/check_update_copyright.sh delete mode 100755 utils/githooks/pre-commit.d/10-update-copyright create mode 100755 utils/githooks/pre-commit.d/10-update-copyright.sh rename utils/githooks/pre-commit.d/{30-Jenkinsfile => 30-Jenkinsfile.sh} (96%) rename utils/githooks/pre-commit.d/{50-clang-format => 50-clang-format.sh} (100%) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 6d611b21351..7524667f484 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -197,6 +197,18 @@ jobs: - name: Run check run: yamllint --format github . + copyright: + name: Copyright check + runs-on: ubuntu-24.04 + steps: + - name: Check out source repository + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 2 + - name: Run check + run: ./utils/cq/check_update_copyright.sh HEAD^1 gha + linting-summary: name: Linting Summary runs-on: ubuntu-22.04 diff --git a/utils/cq/check_update_copyright.sh b/utils/cq/check_update_copyright.sh new file mode 100755 index 00000000000..436457c879d --- /dev/null +++ b/utils/cq/check_update_copyright.sh @@ -0,0 +1,110 @@ +#!/bin/bash +# +# Copyright 2024 Intel Corporation. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# Check or update copyright date in modified files. +# Usage: check_update_copyright.sh +# mode "githook" will update copyright dates in place. +# mode "gha" will just print a warning in a GHA-compatible format. + +set -e + +git_target="$1" +mode="$2" +case "$mode" in + "githook" | "gha") + ;; + *) + echo "Usage: check_update_copyright.sh " + exit 1 +esac + +# Navigate to repo root +PARENT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +cd "$PARENT_DIR"/../../ + + +regex='(^[[:blank:]]*[\*/]*.*)((Copyright[[:blank:]]*)([0-9]{4})(-([0-9]{4}))?)([[:blank:]]*(Intel.*$))' +year=$(date +%Y) +errors=0 +targets=( + # Entries with wildcard. These must be first and start with '*' or + # older versions of git will return files that were not changed. + '*.c' + '*.h' + '*.go' + '*.py' + '*.proto' + '*.java' + '*.yml' + '*.yaml' + '*.sh' + '*.bash' + '*Dockerfile*' + '*README*' + '*LICENSE*' + '*NOTICE*' + '*.txt' + '*.md' + # Entries without a wildcard + 'Makefile' + 'Jenkinsfile' + 'SConscript' + 'SConstruct' + 'copyright' + '.env' +) + +if [ -z "$files" ]; then + files=$(git diff "$git_target" --cached --diff-filter=AM --name-only -- "${targets[@]}") +else + echo " Checking against custom files" +fi + +os=$(uname -s) + +. utils/githooks/git-version.sh + +for file in $files; do + echo "DEBUG: Checking file $file" + if [[ "$file" == *vendor* ]] || [[ "$file" == *pb.go ]] || + [[ "$file" == *_string.go ]] || [[ "$file" == *pb-c* ]] || + { [ "$git_vercode" -ge 2030000 ] && + [ "$(git diff --cached -I Copyright "$file")" = '' ]; }; then + continue + fi + read -r y1 y2 <<< "$(sed -nre "s/^.*$regex.*$/\4 \6/p" "$file")" + if [[ -z $y1 ]] ; then + # Print warning but don't error on non-existent copyright + echo " Copyright Information not found in: $file" + elif [[ $y1 -ne $year && $year -ne $y2 ]] ; then + if [[ "$mode" == "githook" ]]; then + # Update copyright in place + if ! git reset "$file"; then + echo " Unable to un-stage $file" + errors=$((errors + 1)) + fi + if [[ "$os" == 'Linux' ]]; then + sed -i -re "s/$regex/\1Copyright $y1-$year \8/" "$file" + else + sed -i '' -re "s/$regex/\1Copyright $y1-$year \8/" "$file" + fi + + if ! git add "$file"; then + echo " Unable to re-stage $file" + errors=$((errors + 1)) + fi + elif [[ "$mode" == "gha" ]]; then + # Print error but do not update + echo "::error file=$file::Copyright out of date" + errors=$((errors + 1)) + fi + fi +done + +if [[ $errors -ne 0 ]]; then + echo " $errors errors while checking/fixing copyrights." + exit 1 +fi diff --git a/utils/githooks/pre-commit.d/10-update-copyright b/utils/githooks/pre-commit.d/10-update-copyright deleted file mode 100755 index e2641848cd7..00000000000 --- a/utils/githooks/pre-commit.d/10-update-copyright +++ /dev/null @@ -1,81 +0,0 @@ -#!/bin/bash -# -# Copyright 2022-2024 Intel Corporation. -# -# SPDX-License-Identifier: BSD-2-Clause-Patent -# -# A git hook to validate and correct the copyright date in source files. - -_print_githook_header "Copyright" -if [ -e .git/MERGE_HEAD ]; then - echo "Merge commit. Skipping" - exit 0 -fi - -echo "Updating copyright headers" - -regex='(^[[:blank:]]*[\*/]*.*)((Copyright[[:blank:]]*)([0-9]{4})(-([0-9]{4}))?)([[:blank:]]*(Intel.*$))' -year=$(date +%Y) -errors=0 -targets=( - # Entries with wildcard. These must be first and start with '*' or - # older versions of git will return files that were not changed. - '*.c' - '*.h' - '*.go' - '*.py' - '*.proto' - '*.java' - '*.yml' - '*.yaml' - '*.sh' - '*.bash' - '*Dockerfile*' - '*README*' - '*LICENSE*' - '*NOTICE*' - '*.txt' - '*.md' - # Entries without a wildcard - 'Makefile' - 'Jenkinsfile' - 'SConscript' - 'SConstruct' - 'copyright' - '.env' -) - -if [ -z "$files" ]; then - files=$(git diff "$TARGET" --cached --diff-filter=AM --name-only -- "${targets[@]}") -else - echo " Checking against custom files" -fi - -os=$(uname -s) - -. utils/githooks/git-version.sh - -for file in $files; do - if [[ "$file" == *vendor* ]] || [[ "$file" == *pb.go ]] || - [[ "$file" == *_string.go ]] || [[ "$file" == *pb-c* ]] || - { [ "$git_vercode" -ge 2030000 ] && - [ "$(git diff --cached -I Copyright "$file")" = '' ]; }; then - continue - fi - read -r y1 y2 <<< "$(sed -nre "s/^.*$regex.*$/\4 \6/p" "$file")" - if [[ -z $y1 ]] ; then - echo " Copyright Information not found in: $file" - errors=$((errors + 1)) - elif [[ $y1 -ne $year && $year -ne $y2 ]] ; then - git reset "$file" || (echo " Unable to un-stage $file" && exit 1) - if [ "$os" == 'Linux' ] - then - sed -i -re "s/$regex/\1Copyright $y1-$year \8/" "$file" - else - sed -i '' -re "s/$regex/\1Copyright $y1-$year \8/" "$file" - fi - - git add "$file" || (echo " Unable to re-stage $file" && exit 1) - fi -done -[[ $errors = 0 ]] || (echo " $errors errors while checking/fixing copyrights.") diff --git a/utils/githooks/pre-commit.d/10-update-copyright.sh b/utils/githooks/pre-commit.d/10-update-copyright.sh new file mode 100755 index 00000000000..b88cce8e634 --- /dev/null +++ b/utils/githooks/pre-commit.d/10-update-copyright.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# +# Copyright 2022-2024 Intel Corporation. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# A git hook to validate and correct the copyright date in source files. + +_print_githook_header "Copyright" +if [ -e .git/MERGE_HEAD ]; then + echo "Merge commit. Skipping" + exit 0 +fi + +echo "Updating copyright headers" + +utils/cq/check_update_copyright.sh "$TARGET" githook diff --git a/utils/githooks/pre-commit.d/30-Jenkinsfile b/utils/githooks/pre-commit.d/30-Jenkinsfile.sh similarity index 96% rename from utils/githooks/pre-commit.d/30-Jenkinsfile rename to utils/githooks/pre-commit.d/30-Jenkinsfile.sh index dd482c744e0..0348e2a8edd 100755 --- a/utils/githooks/pre-commit.d/30-Jenkinsfile +++ b/utils/githooks/pre-commit.d/30-Jenkinsfile.sh @@ -25,6 +25,7 @@ CURL_VERBOSE=${CURL_VERBOSE:-""} CURL_PROXY="${CURL_PROXY:+-x }${CURL_PROXY:-}" CURL_OPTS="$CURL_PROXY $CURL_VERBOSE -s" URL="https://$HOST/pipeline-model-converter/validate" +# shellcheck disable=SC2086 if ! output=$(curl $CURL_OPTS -s -X POST -F "jenkinsfile=<${1:-Jenkinsfile}" "$URL"); then echo " Failed to access $URL. Skipping" exit 0 diff --git a/utils/githooks/pre-commit.d/50-clang-format b/utils/githooks/pre-commit.d/50-clang-format.sh similarity index 100% rename from utils/githooks/pre-commit.d/50-clang-format rename to utils/githooks/pre-commit.d/50-clang-format.sh