Skip to content

Commit

Permalink
Create kdoc_check.py
Browse files Browse the repository at this point in the history
GitHub action test for verifying Kdocs
  • Loading branch information
AIprototype authored Feb 14, 2024
1 parent e1cb794 commit a0a7485
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions kdoc_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import subprocess
import re


def run_command(command):
"""Runs a shell command and returns the output."""
result = subprocess.run(command, stdout=subprocess.PIPE, shell=True, check=True)
return result.stdout.decode('utf-8')


def get_changed_files():
"""Returns a list of changed .kt files in the PR."""
files = run_command("git diff --name-only origin/main").splitlines()
return [f for f in files if f.endswith('.kt')]


def get_file_diff(file_path):
"""Returns the diff of the given file."""
return run_command(f"git diff origin/main -- {file_path}")


def check_kdoc_in_function(function):
"""Checks if a function has an associated KDoc comment."""
return re.match(r'\s*/\*\*\n(\s*\*.*\n)+\s*\*/\n\s*fun\s', function)


def main():
changed_files = get_changed_files()
missing_kdoc = False

for file in changed_files:
diff = get_file_diff(file)
functions = re.findall(r'(\s*/\*\*.*?fun\s.*?\))', diff, re.DOTALL)

for function in functions:
if not check_kdoc_in_function(function):
print(f"Missing KDoc for function in file: {file}")
print(function)
missing_kdoc = True

if missing_kdoc:
raise Exception("Some functions are missing KDoc comments.")


if __name__ == "__main__":
main()

0 comments on commit a0a7485

Please sign in to comment.