|
| 1 | +"""Check if the PR has a news item. |
| 2 | +
|
| 3 | +Put a warning comment if it doesn't. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +from fnmatch import fnmatch |
| 8 | + |
| 9 | +from github import Github, PullRequest |
| 10 | + |
| 11 | + |
| 12 | +def get_added_files(pr: PullRequest.PullRequest): |
| 13 | + print(pr, pr.number) |
| 14 | + for file in pr.get_files(): |
| 15 | + if file.status == "added": |
| 16 | + yield file.filename |
| 17 | + |
| 18 | + |
| 19 | +def check_news_file(pr): |
| 20 | + return any(map(lambda file_name: fnmatch(file_name, "news/*.rst"), get_added_files(pr))) |
| 21 | + |
| 22 | + |
| 23 | +def get_pr_number(): |
| 24 | + number = os.environ["PR_NUMBER"] |
| 25 | + if not number: |
| 26 | + raise Exception(f"Pull request number is not found `PR_NUMBER={number}") |
| 27 | + return int(number) |
| 28 | + |
| 29 | + |
| 30 | +def get_old_comment(pr: PullRequest.PullRequest): |
| 31 | + for comment in pr.get_issue_comments(): |
| 32 | + if ("github-actions" in comment.user.login) and ("No news item is found" in comment.body): |
| 33 | + return comment |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + # using an access token |
| 38 | + gh = Github(os.environ["GITHUB_TOKEN"]) |
| 39 | + repo = gh.get_repo(os.environ["GITHUB_REPOSITORY"]) |
| 40 | + pr = repo.get_pull(get_pr_number()) |
| 41 | + has_news_added = check_news_file(pr) |
| 42 | + old_comment = get_old_comment(pr) |
| 43 | + |
| 44 | + if old_comment: |
| 45 | + print("Found an existing comment from bot") |
| 46 | + if has_news_added: |
| 47 | + print("Delete warning from bot, since news items is added.") |
| 48 | + old_comment.delete() |
| 49 | + elif not has_news_added: |
| 50 | + print("No news item found") |
| 51 | + |
| 52 | + pr.create_issue_comment( |
| 53 | + """\ |
| 54 | +**Warning!** No news item is found for this PR. If this is an user facing change/feature/fix, |
| 55 | + please add a news item by copying the format from `news/TEMPLATE.rst`. |
| 56 | +""" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments