-
-
Save knight42/6843c4377eb6db0927cfdff004e4698b to your computer and use it in GitHub Desktop.
A Python Script for checking Pull Request title conventions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
from typing import Callable, List | |
VALID_PR_TAGS = { | |
# this pr implements new features | |
"Feature", | |
# this pr fixes bugs | |
"Bugfix", | |
# this pr updates documentation | |
"Doc", | |
# this pr updates or implements ci, cd or devops related stuff | |
"DevOps", | |
# this pr adds or updates tests | |
"Test", | |
# this pr focuses on code refactor | |
"Refactor", | |
# this pr is a release | |
"Release", | |
} | |
registered_checkers = [] | |
def register(func: Callable) -> Callable: | |
registered_checkers.append(func) | |
return func | |
@register | |
def check_pr_tags(title: str) -> List[str]: | |
"""Make sure PR title has valid tags.""" | |
error_array = [] | |
if not title.startswith("[") and not title.startswith("Revert "): | |
error_array.append(f"🚫 Pull Request title doesn't start with any tag: {title}") | |
if title.endswith(" "): | |
error_array.append(f"🚫 Pull Request title should not end with a space: {title}") | |
if title.endswith("]"): | |
error_array.append( | |
f"🚫 Pull Request title should have content besides tags: {title}" | |
) | |
for x in title.split("]")[1:]: | |
if x[0] != " ": | |
error_array.append(f"🚫 No space found before: {x}") | |
if x[1] == " ": | |
error_array.append(f"🚫 Extra space found before: {x[2:]}") | |
for tag in re.findall(r"\[([^]]+)]", title): | |
if tag not in VALID_PR_TAGS: | |
error_array.append( | |
f"🚫 Pull Request title contains invalid tag: [{tag}], allowed tags are: {VALID_PR_TAGS}" | |
) | |
return error_array | |
@register | |
def check_pr_length(title: str) -> List[str]: | |
"""Make sure PR title is descriptive enough.""" | |
error_array = [] | |
if len(title) <= 34: | |
error_array.append( | |
f"🚫 Pull Request title is too short, please add more description (> 35 characters)!: {title}" | |
) | |
return error_array | |
@register | |
def check_invalid_chars(title: str) -> List[str]: | |
"""Make sure PR title has no invalid characters.""" | |
error_array = [] | |
if "`" in title: | |
error_array.append( | |
f"🚫 Pull Request title should not contain backquotes (`): {title}" | |
) | |
return error_array | |
def main(title) -> None: | |
"""Main function.""" | |
print(f"Checking your Pull Request title: {title}") | |
errors = "" | |
for checker in registered_checkers: | |
if len(error_array := checker(title)) > 0: | |
for error in error_array: | |
errors += f"{error} \n" | |
if errors != "": | |
exit(errors) | |
print("🎉 Your Pull Request looks good!") | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment