Add check_copyright as part of gate script

* Added check_copyright to validate if all source files has
  expected copyright header

Change-Id: I8d9f3510db9cb5cbc510748a2f71ad95aa8d366b
This commit is contained in:
Yasin, Siraj (SY495P) 2020-05-05 22:37:18 -05:00 committed by Sirajudeen
parent 1bc62bb55f
commit 90e27b542e
3 changed files with 62 additions and 4 deletions

View File

@ -61,6 +61,7 @@ install:
.PHONY: test .PHONY: test
test: lint test: lint
test: cover test: cover
test: check-copyright
.PHONY: unit-tests .PHONY: unit-tests
unit-tests: TESTFLAGS += -race -v unit-tests: TESTFLAGS += -race -v
@ -129,7 +130,7 @@ print-docker-image-tag:
@echo "$(DOCKER_IMAGE)" @echo "$(DOCKER_IMAGE)"
.PHONY: docker-image-test-suite .PHONY: docker-image-test-suite
docker-image-test-suite: DOCKER_MAKE_TARGET = "lint cover update-golden check-git-diff" docker-image-test-suite: DOCKER_MAKE_TARGET = "lint cover update-golden check-git-diff check-copyright"
docker-image-test-suite: DOCKER_TARGET_STAGE = builder docker-image-test-suite: DOCKER_TARGET_STAGE = builder
docker-image-test-suite: docker-image docker-image-test-suite: docker-image
@ -195,3 +196,13 @@ delete-golden:
.PHONY: check-git-diff .PHONY: check-git-diff
check-git-diff: check-git-diff:
@./tools/git_diff_check @./tools/git_diff_check
# add-copyright is a utility to add copyright header to missing files
.PHONY: add-copyright
add-copyright:
@./tools/add_license.sh
# check-copyright is a utility to check if copyright header is present on all files
.PHONY: check-copyright
check-copyright:
@./tools/check_copyright

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
set -ex set -e
# Find all files of given format and add license if missing # Find all files of given format and add license if missing
add_license() { add_license() {
@ -23,8 +23,9 @@ add_license() {
for each in $FILES for each in $FILES
do do
if ! grep -q 'Apache License' $each if ! grep -Eq 'Apache License|License-Identifier: Apache' $each
then then
echo "Adding license header to $each"
cat tools/${template} $each >$each.new cat tools/${template} $each >$each.new
mv $each.new $each mv $each.new $each
fi fi
@ -38,10 +39,11 @@ add_license_to_bash() {
for each in $FILES for each in $FILES
do do
if ! grep -q 'Apache License' $each if ! grep -Eq 'Apache License|License-Identifier: Apache' $each
then then
if head -1 $each | grep '^#!' > /dev/null if head -1 $each | grep '^#!' > /dev/null
then then
echo "Adding license header to $each"
head -n 1 $each >>$each.new head -n 1 $each >>$each.new
head -n $NUM_OF_LINES tools/$template >>$each.new head -n $NUM_OF_LINES tools/$template >>$each.new
tail -n+2 $each >>$each.new tail -n+2 $each >>$each.new

45
tools/check_copyright Executable file
View File

@ -0,0 +1,45 @@
#!/bin/bash
# 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.
declare FILES_MISSING_COPYRIGHT=()
# Find all files of given format and add license if missing
check_license() {
ext=$1
# skipping license for testdata and manifests folders
FILES=$(find -L . -name "*.${ext}" | grep -v "testdata" | grep -v "manifests")
for each in $FILES
do
if ! grep -Eq 'Apache License|License-Identifier: Apache' $each
then
FILES_MISSING_COPYRIGHT+=($each)
fi
done
}
check_license 'go'
check_license 'yaml'
check_license 'yml'
check_license 'sh'
if [ ${#FILES_MISSING_COPYRIGHT[@]} -gt 0 ]
then
echo "Copyright header missing for these files: ${FILES_MISSING_COPYRIGHT[@]}"
echo "please run make add-copyright"
exit 1
else
echo "no file with missing copyright header detected, make target completed successfully"
fi