Skip to content

Commit 0ff0c6d

Browse files
authored
Add copyright check to PR checks. (#504)
Added script based on a script from the Firebase iOS repo, with an option to format output for the GitHub log. Missing copyrights added in a separate PR.
1 parent d9946cd commit 0ff0c6d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.github/workflows/checks.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ jobs:
7676
grep -E "error:|warning:|^ parameter" doxygen_errors.txt | sed ':a;N;$!ba;s/\n/%0A/g' | sed 's/^/::error ::DOXYGEN ERRORS: %0A/'
7777
exit 1
7878
fi
79+
80+
copyright_check:
81+
# Check for Google copyright in each file.
82+
runs-on: ubuntu-latest
83+
steps:
84+
- uses: actions/checkout@v2
85+
with:
86+
submodules: false
87+
- name: Run check_copyright.sh
88+
run: |
89+
set -e
90+
bash scripts/gha/check_copyright.sh -g

scripts/gha/check_copyright.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
usage(){
16+
echo "Usage: $0 [options]
17+
options:
18+
-g enable github log format"
19+
}
20+
21+
github_log=0
22+
IFS=',' # split options on ',' characters
23+
while getopts "hg" opt; do
24+
case $opt in
25+
h)
26+
usage
27+
exit 0
28+
;;
29+
g)
30+
github_log=1
31+
;;
32+
*)
33+
echo "unknown parameter"
34+
exit 2
35+
;;
36+
esac
37+
done
38+
39+
40+
# Check source files for copyright notices
41+
42+
options=(
43+
-E # Use extended regexps
44+
-I # Exclude binary files
45+
-L # Show files that don't have a match
46+
'Copyright.*[0-9]{4}.*Google'
47+
)
48+
49+
result=$(git grep "${options[@]}" -- \
50+
'*.'{c,cc,cmake,h,js,m,mm,py,rb,sh,swift} \
51+
CMakeLists.txt '**/CMakeLists.txt' \
52+
':(exclude)**/third_party/**' \
53+
':(exclude)**/external/**')
54+
55+
if [[ $result ]]; then
56+
if [[ $github_log -eq 1 ]]; then
57+
echo -n "::error ::Missing copyright notices in the following files:%0A"
58+
# Print all files with %0A instead of newline.
59+
echo "$result" | sed ':a;N;$!ba;s/\n/%0A/g'
60+
else
61+
echo "$result"
62+
echo "ERROR: Missing copyright notices in the files above. Please fix."
63+
fi
64+
exit 1
65+
fi

0 commit comments

Comments
 (0)