Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions license-header-check-v2/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright (c) 2024-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'License Header Check V2'
description: 'check license/copyright header v2'
inputs:
included_file_patterns:
description: "included file pattern"
required: true
type: string
excluded_file_patterns:
description: "excluded file pattern"
required: false
type: string

runs:
using: "composite"
steps:
- name: Get changed files
shell: bash
run: |
set -e

# Get license pattern with year
CURRENT_YEAR=$(date +%Y)
LICENSE_PATTERN="Copyright \(c\) .*?${CURRENT_YEAR},? NVIDIA CORPORATION."

# Format file patterns
IFS="," read -r -a INCLUDE_PATTERNS <<< "$(echo "${{ inputs.included_file_patterns }}" | tr -d ' ' | tr -d '\n')"
IFS="," read -r -a EXCLUDE_PATTERNS <<< "$(echo "${{ inputs.excluded_file_patterns }}" | tr -d ' ' | tr -d '\n')"
echo "Included file patterns: ${INCLUDE_PATTERNS[@]}"
echo "Excluded file patterns: ${EXCLUDE_PATTERNS[@]}"

# Get changed files
BASE_REF=$(git --no-pager log --oneline -1 | awk '{ print $NF }')
echo "Base REF: ${BASE_REF}"
FILES=$(git diff --name-only --diff-filter=AM ${BASE_REF} HEAD) || (echo "Your base commit ID is too old, please try upmerge first." && exit 1)
RENAME_FILES=$(git diff --name-status ${BASE_REF} HEAD | grep "^R" | grep -v "R100" | awk '{print $3}' || echo "")
echo "${RENAME_FILES[@]}"
FILES=($FILES $RENAME_FILES)
echo "Files to be detected: ${FILES[@]}"

# Check license header
NO_LICENSE_FILES=""
for FILE in "${FILES[@]}"; do
INCLUDE=false
for INCLUDE_PATTERN in "${INCLUDE_PATTERNS[@]}"; do
if [[ $FILE == $INCLUDE_PATTERN ]]; then
INCLUDE=true
break
fi
done
EXCLUDE=false
for EXCLUDE_PATTERN in "${EXCLUDE_PATTERNS[@]}"; do
if [[ $FILE == $EXCLUDE_PATTERN ]]; then
EXCLUDE=true
break
fi
done
if [[ $INCLUDE == true && $EXCLUDE == false ]]; then
echo "Checking $FILE"
if !(grep -Eiq "$LICENSE_PATTERN" "$FILE"); then
NO_LICENSE_FILES+="$FILE "
fi
fi
done

# Output result
echo "--------- RESULT ---------"
ERROR_MESSAGE="If the feature branch is not based on the latest target branch, \
git diff may include unexpected files marked as modified in the check that are not from the feature branch. \
This is because it tries to find a common commit between the two branches \
and can get confused if it is too old and there are lots of changes. \
If this happens please UPMERGE your PR to the latest development branch."
if [ ! -z "$NO_LICENSE_FILES" ]; then
echo "Following files missed copyright/license header or expired:"
echo $NO_LICENSE_FILES | tr ' ' '\n'
echo "--------- NOTICE ---------"
echo "${ERROR_MESSAGE}"
exit 1
else
echo "All files passed the check"
exit 0
fi