Skip to content

Commit ca8303d

Browse files
Add CI check for code generated files (#356)
Adds a CI check to make sure code-generated files are not modified, as a safety check.
1 parent 384d035 commit ca8303d

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ jobs:
5757
python3 -m pip install sphinx
5858
python3 -m pip install --verbose .
5959
./make-docs.py
60+
61+
check-codegen-edits:
62+
runs-on: ubuntu-20.04 # latest
63+
steps:
64+
- uses: actions/checkout@v2
65+
with:
66+
fetch-depth: 0
67+
- name: Check for edits to code-generated files
68+
run: |
69+
./utils/check_codegen_edits.py

utils/check_codegen_edits.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import fnmatch
4+
from genericpath import isfile
5+
import os
6+
import subprocess
7+
import sys
8+
9+
CODEGEN_PHRASES = [
10+
'This file is generated',
11+
]
12+
13+
IGNORE_PATTERNS = [
14+
'utils/*', # this script has the phrase in it
15+
]
16+
17+
ERROR_MSG = """
18+
ERROR: You have changed code-generated files.
19+
20+
If you edited these files by hand, your changes will be erased when the
21+
code-generator is run again. An SDK team member MUST update the code-gen
22+
templates with corresponding changes before merging this Pull Request.
23+
24+
You can ignore this error if you are in fact running the code generator.
25+
"""
26+
27+
28+
def main():
29+
parser = argparse.ArgumentParser(
30+
description="Detect edits to code-generated files")
31+
parser.add_argument('--diff-branch', default='main',
32+
help="Branch/commit to diff against")
33+
parser.add_argument('--diff-repo', default='origin',
34+
help="Repository to diff against")
35+
args = parser.parse_args()
36+
37+
# chdir to project root
38+
os.chdir(os.path.join(os.path.dirname(__file__), '..'))
39+
40+
# get all files with diffs
41+
git_cmd = ['git', 'diff', '--name-only',
42+
f"{args.diff_repo}/{args.diff_branch}"]
43+
git_result = subprocess.run(git_cmd, check=True, stdout=subprocess.PIPE)
44+
diff_files = git_result.stdout.decode().splitlines()
45+
46+
# figure out which files were code-generated
47+
print('Checking files with diffs...')
48+
any_codegen = False
49+
for filepath in diff_files:
50+
is_codegen = False
51+
ignore = False
52+
if not os.path.isfile(filepath):
53+
ignore = True
54+
if any([fnmatch.fnmatch(filepath, pat)
55+
for pat in IGNORE_PATTERNS]):
56+
ignore = True
57+
if not ignore:
58+
with open(filepath) as f:
59+
text = f.read()
60+
for phrase in CODEGEN_PHRASES:
61+
if phrase in text:
62+
is_codegen = True
63+
any_codegen = True
64+
break
65+
if is_codegen:
66+
print(f" ⛔️ GENERATED - {filepath}")
67+
elif ignore:
68+
print(f" ✅ ignored - {filepath}")
69+
else:
70+
print(f" ✅ normal - {filepath}")
71+
72+
if any_codegen:
73+
print(ERROR_MSG)
74+
sys.exit(-1)
75+
else:
76+
print("No code-generated files were changed.")
77+
78+
79+
if __name__ == '__main__':
80+
main()

0 commit comments

Comments
 (0)