From cae4d78dbf5144a5d6e4a12df39e7e68419e89fc Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 16:20:10 +0530 Subject: T6315: added scripts for pr check --- scripts/check-pr-title-and-commit-messages.py | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 scripts/check-pr-title-and-commit-messages.py (limited to 'scripts/check-pr-title-and-commit-messages.py') diff --git a/scripts/check-pr-title-and-commit-messages.py b/scripts/check-pr-title-and-commit-messages.py new file mode 100644 index 0000000..001f6cf --- /dev/null +++ b/scripts/check-pr-title-and-commit-messages.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import re +import sys +import time + +import requests + +# Use the same regex for PR title and commit messages for now +title_regex = r'^(([a-zA-Z\-_.]+:\s)?)T\d+:\s+[^\s]+.*' +commit_regex = title_regex + +def check_pr_title(title): + if not re.match(title_regex, title): + print("PR title '{}' does not match the required format!".format(title)) + print("Valid title example: T99999: make IPsec secure") + sys.exit(1) + +def check_commit_message(title): + if not re.match(commit_regex, title): + print("Commit title '{}' does not match the required format!".format(title)) + print("Valid title example: T99999: make IPsec secure") + sys.exit(1) + +if __name__ == '__main__': + if len(sys.argv) < 2: + print("Please specify pull request URL!") + sys.exit(1) + + # There seems to be a race condition that causes this scripts to receive + # an incomplete PR object that is missing certain fields, + # which causes temporary CI failures that require re-running the script + # + # It's probably better to add a small delay to prevent that + time.sleep(5) + + # Get the pull request object + pr = requests.get(sys.argv[1]).json() + if "title" not in pr: + print("The PR object does not have a title field!") + print("Did not receive a valid pull request object, please check the URL!") + sys.exit(1) + + check_pr_title(pr["title"]) + + # Get the list of commits + commits = requests.get(pr["commits_url"]).json() + for c in commits: + # Retrieve every individual commit and check its title + co = requests.get(c["url"]).json() + check_commit_message(co["commit"]["message"]) -- cgit v1.2.3 From d8bc71a13f19258270364dc7b3d54fb7c52a67b7 Mon Sep 17 00:00:00 2001 From: kumvijaya Date: Wed, 8 May 2024 16:25:18 +0530 Subject: T6315: updated scripts permissions --- scripts/check-pr-title-and-commit-messages.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/check-pr-title-and-commit-messages.py (limited to 'scripts/check-pr-title-and-commit-messages.py') diff --git a/scripts/check-pr-title-and-commit-messages.py b/scripts/check-pr-title-and-commit-messages.py old mode 100644 new mode 100755 -- cgit v1.2.3