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