From 90e27b542e79fdf100e5531ad1c2c75a6a42b549 Mon Sep 17 00:00:00 2001
From: "Yasin, Siraj (SY495P)" <sirajudeen.yasin@gmail.com>
Date: Tue, 5 May 2020 22:37:18 -0500
Subject: [PATCH] Add check_copyright as part of gate script

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

Change-Id: I8d9f3510db9cb5cbc510748a2f71ad95aa8d366b
---
 Makefile              | 13 ++++++++++++-
 tools/add_license.sh  |  8 +++++---
 tools/check_copyright | 45 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 4 deletions(-)
 create mode 100755 tools/check_copyright

diff --git a/Makefile b/Makefile
index a51d11064..fd969df70 100644
--- a/Makefile
+++ b/Makefile
@@ -61,6 +61,7 @@ install:
 .PHONY: test
 test: lint
 test: cover
+test: check-copyright
 
 .PHONY: unit-tests
 unit-tests: TESTFLAGS += -race -v
@@ -129,7 +130,7 @@ print-docker-image-tag:
 	@echo "$(DOCKER_IMAGE)"
 
 .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-image
 
@@ -195,3 +196,13 @@ delete-golden:
 .PHONY: check-git-diff
 check-git-diff:
 	@./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
diff --git a/tools/add_license.sh b/tools/add_license.sh
index c06b9e3c0..7aae2b854 100755
--- a/tools/add_license.sh
+++ b/tools/add_license.sh
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-set -ex
+set -e
 
 # Find all files of given format and add license if missing
 add_license() {
@@ -23,8 +23,9 @@ add_license() {
 
   for each in $FILES
   do
-    if ! grep -q 'Apache License' $each
+    if ! grep -Eq 'Apache License|License-Identifier: Apache' $each
     then
+      echo "Adding license header to $each"
       cat tools/${template} $each >$each.new
       mv $each.new $each
     fi
@@ -38,10 +39,11 @@ add_license_to_bash() {
 
   for each in $FILES
   do
-    if ! grep -q 'Apache License' $each
+    if ! grep -Eq 'Apache License|License-Identifier: Apache' $each
     then
       if head -1 $each | grep '^#!' > /dev/null
       then
+        echo "Adding license header to $each"
         head -n 1 $each >>$each.new
         head -n $NUM_OF_LINES tools/$template >>$each.new
         tail -n+2 $each >>$each.new
diff --git a/tools/check_copyright b/tools/check_copyright
new file mode 100755
index 000000000..eb99ca89f
--- /dev/null
+++ b/tools/check_copyright
@@ -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
+