Skip to content

Add CI check for code generated files #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ jobs:
python3 -m pip install sphinx
python3 -m pip install --verbose .
./make-docs.py

check-codegen-edits:
runs-on: ubuntu-20.04 # latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Check for edits to code-generated files
run: |
./utils/check_codegen_edits.py
80 changes: 80 additions & 0 deletions utils/check_codegen_edits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
import argparse
import fnmatch
from genericpath import isfile
import os
import subprocess
import sys

CODEGEN_PHRASES = [
'This file is generated',
]

IGNORE_PATTERNS = [
'utils/*', # this script has the phrase in it
]

ERROR_MSG = """
ERROR: You have changed code-generated files.

If you edited these files by hand, your changes will be erased when the
code-generator is run again. An SDK team member MUST update the code-gen
templates with corresponding changes before merging this Pull Request.

You can ignore this error if you are in fact running the code generator.
"""


def main():
parser = argparse.ArgumentParser(
description="Detect edits to code-generated files")
parser.add_argument('--diff-branch', default='main',
help="Branch/commit to diff against")
parser.add_argument('--diff-repo', default='origin',
help="Repository to diff against")
args = parser.parse_args()

# chdir to project root
os.chdir(os.path.join(os.path.dirname(__file__), '..'))

# get all files with diffs
git_cmd = ['git', 'diff', '--name-only',
f"{args.diff_repo}/{args.diff_branch}"]
git_result = subprocess.run(git_cmd, check=True, stdout=subprocess.PIPE)
diff_files = git_result.stdout.decode().splitlines()

# figure out which files were code-generated
print('Checking files with diffs...')
any_codegen = False
for filepath in diff_files:
is_codegen = False
ignore = False
if not os.path.isfile(filepath):
ignore = True
if any([fnmatch.fnmatch(filepath, pat)
for pat in IGNORE_PATTERNS]):
ignore = True
if not ignore:
with open(filepath) as f:
text = f.read()
for phrase in CODEGEN_PHRASES:
if phrase in text:
is_codegen = True
any_codegen = True
break
if is_codegen:
print(f" ⛔️ GENERATED - {filepath}")
elif ignore:
print(f" ✅ ignored - {filepath}")
else:
print(f" ✅ normal - {filepath}")

if any_codegen:
print(ERROR_MSG)
sys.exit(-1)
else:
print("No code-generated files were changed.")


if __name__ == '__main__':
main()