Skip to content

Commit 3eb41a4

Browse files
authored
Merge pull request #20 from alyssacgoins/py-script
Add python script.
2 parents 79d845b + 6e36197 commit 3eb41a4

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import re
4+
import sys
5+
6+
from github import Auth, Github
7+
8+
# removes hidden section of PR body.
9+
def clean_pr_body(body: str) -> str:
10+
if not body:
11+
return ""
12+
clean_text = re.sub(r'<!--.*?-->', '', body, flags=re.DOTALL)
13+
return clean_text.strip()
14+
15+
16+
def main() -> int:
17+
# Retrieve env vars provided by GH Action workflow
18+
token = os.getenv("GITHUB_TOKEN")
19+
pr_num = int(os.getenv("PR_NUMBER"))
20+
#todo: this var necessary for testing purposes and can utlimately be removed.
21+
repo_name = os.getenv("GITHUB_REPOSITORY")
22+
print(f"Verifying linked issues for PR #{pr_num} in {repo_name}")
23+
24+
g = Github(auth=Auth.Token(token))
25+
repo = g.get_repo(repo_name)
26+
pr = repo.get_pull(pr_num)
27+
28+
# 1. Parse PR body for linked issues (e.g., #123 or Fixes #123)
29+
pr_body_original = pr.body or ""
30+
pr_body = clean_pr_body(pr_body_original)
31+
32+
issue_numbers = re.findall(r"(?:#|issues\/)(\d+)", pr_body)
33+
print(f"Found issue numbers: {issue_numbers}")
34+
35+
if not issue_numbers:
36+
print('ERROR: No linked issues found in the PR description.')
37+
return 1
38+
39+
#2: Check each linked issue for the "ready" command.
40+
# If there is more than one issue linked, each issue must be marked /ready
41+
found_ready = False
42+
for issue_num in issue_numbers:
43+
issue = repo.get_issue(int(issue_num))
44+
comments = issue.get_comments()
45+
46+
# Check if the issue contains a comment with /ready command
47+
for comment in comments:
48+
if "/ready" in comment.body:
49+
print(f"Found '/ready' command in issue #{issue_num}")
50+
found_ready = True
51+
break
52+
#todo: is there ever a case in which "/ready" is canceled out by a later comment? (ie do not exit here)
53+
if found_ready: break
54+
55+
if not found_ready:
56+
print("ERROR: The linked issue(s) must have a '/ready' command.")
57+
return 1
58+
59+
print(f'Successfully verified linked issues: {issue_numbers}')
60+
return 0
61+
62+
if __name__ == "__main__":
63+
sys.exit(main())

0 commit comments

Comments
 (0)