From 76a4b6049cee6b7d2ba54c83a4dc26b9da59ad7a Mon Sep 17 00:00:00 2001 From: Sujoy Pal Date: Tue, 7 May 2024 16:11:55 +0530 Subject: [PATCH] feat: add pr title validation feature --- .github/workflows/pr_title_validation.yml | 16 +++++++++++++++ validate_title.py | 25 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 .github/workflows/pr_title_validation.yml create mode 100644 validate_title.py diff --git a/.github/workflows/pr_title_validation.yml b/.github/workflows/pr_title_validation.yml new file mode 100644 index 0000000..88d2192 --- /dev/null +++ b/.github/workflows/pr_title_validation.yml @@ -0,0 +1,16 @@ +name: PR Title Validation + +on: + pull_request: + types: [opened, reopened, edited] + +jobs: + validate_title: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Run validation script + run: python validate_title.py "${{ github.event.pull_request.title }}" diff --git a/validate_title.py b/validate_title.py new file mode 100644 index 0000000..eb16ae6 --- /dev/null +++ b/validate_title.py @@ -0,0 +1,25 @@ +import re +import sys + +def validate_title(title): + print("The title is:", title) + prefix_pattern = r'^(build|chore|ci|docs|feat|fix|perf|refactor|style|test|sample)[:\s].+' + max_length = 50 + + match = re.match(prefix_pattern, title, re.IGNORECASE) + print("Match:", match) + + if not match: + print("PR title must start with one of the following prefixes: build, chore, ci, docs, feat, fix, perf, refactor, style, test, sample", file=sys.stderr) + sys.exit(1) + + title_content = title.split(':', 1)[-1].strip() + print("Title Content:", title_content) + + if len(title_content) > max_length: + print(f"PR title content must not exceed {max_length} characters", file=sys.stderr) + sys.exit(1) + +if __name__ == "__main__": + title = sys.argv[1] + validate_title(title)