From f5b69f163092b3e6f31e225a87c7a6873abc02e8 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 31 Aug 2024 17:30:00 -0400 Subject: [PATCH 1/2] Add CI for news update --- .github/workflows/check-news-item.yml | 32 ++++++++++++++ .github/workflows/check-news.py | 64 +++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 .github/workflows/check-news-item.yml create mode 100644 .github/workflows/check-news.py diff --git a/.github/workflows/check-news-item.yml b/.github/workflows/check-news-item.yml new file mode 100644 index 00000000..cb409145 --- /dev/null +++ b/.github/workflows/check-news-item.yml @@ -0,0 +1,32 @@ +name: Check News Item + +on: + pull_request_target: + branches: + - main + +permissions: + pull-requests: write + contents: read + +jobs: + build: + runs-on: ubuntu-latest + name: Check News item + steps: + + # note: the checkout will pull code from the base branch. This step should not pull code from the merge commit + - uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.9' + cache: 'pip' + cache-dependency-path: 'pyproject.toml' + - run: pip install PyGithub + - run: python .github/workflows/check-news.py + env: + PR_NUMBER: "${{ github.event.number }}" + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}" \ No newline at end of file diff --git a/.github/workflows/check-news.py b/.github/workflows/check-news.py new file mode 100644 index 00000000..6d6acf4b --- /dev/null +++ b/.github/workflows/check-news.py @@ -0,0 +1,64 @@ +"""Check if the PR has a news item. + +Put a warning comment if it doesn't. +""" +import os +from fnmatch import fnmatch + +from github import Github, PullRequest + + +def get_added_files(pr: PullRequest.PullRequest): + print(pr, pr.number) + for file in pr.get_files(): + if file.status == "added": + yield file.filename + + +def check_news_file(pr): + return any( + map(lambda file_name: fnmatch(file_name, "news/*.rst"), get_added_files(pr)) + ) + + +def get_pr_number(): + number = os.environ["PR_NUMBER"] + if not number: + raise Exception(f"Pull request number is not found `PR_NUMBER={number}") + return int(number) + + +def get_old_comment(pr: PullRequest.PullRequest): + for comment in pr.get_issue_comments(): + if ("github-actions" in comment.user.login) and ( + "No news item is found" in comment.body + ): + return comment + + +def main(): + # using an access token + gh = Github(os.environ["GITHUB_TOKEN"]) + repo = gh.get_repo(os.environ["GITHUB_REPOSITORY"]) + pr = repo.get_pull(get_pr_number()) + has_news_added = check_news_file(pr) + old_comment = get_old_comment(pr) + + if old_comment: + print("Found an existing comment from bot") + if has_news_added: + print("Delete warning from bot, since news items is added.") + old_comment.delete() + elif not has_news_added: + print("No news item found") + + pr.create_issue_comment( + """\ +**Warning!** No news item is found for this PR. +If this is an user facing change/feature/fix, please add a news item by copying the format from `news/TEMPLATE.rst`. +""" + ) + + +if __name__ == "__main__": + main() \ No newline at end of file From 2985dfe34da913af39b74b9f7b2c1b43b7b7a356 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 31 Aug 2024 17:34:09 -0400 Subject: [PATCH 2/2] Fix line lenght for flake8 --- .github/workflows/check-news-item.yml | 2 +- .github/workflows/check-news.py | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/check-news-item.yml b/.github/workflows/check-news-item.yml index cb409145..02bb6470 100644 --- a/.github/workflows/check-news-item.yml +++ b/.github/workflows/check-news-item.yml @@ -29,4 +29,4 @@ jobs: env: PR_NUMBER: "${{ github.event.number }}" GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}" \ No newline at end of file + GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}" diff --git a/.github/workflows/check-news.py b/.github/workflows/check-news.py index 6d6acf4b..acf58378 100644 --- a/.github/workflows/check-news.py +++ b/.github/workflows/check-news.py @@ -2,6 +2,7 @@ Put a warning comment if it doesn't. """ + import os from fnmatch import fnmatch @@ -16,9 +17,7 @@ def get_added_files(pr: PullRequest.PullRequest): def check_news_file(pr): - return any( - map(lambda file_name: fnmatch(file_name, "news/*.rst"), get_added_files(pr)) - ) + return any(map(lambda file_name: fnmatch(file_name, "news/*.rst"), get_added_files(pr))) def get_pr_number(): @@ -30,9 +29,7 @@ def get_pr_number(): def get_old_comment(pr: PullRequest.PullRequest): for comment in pr.get_issue_comments(): - if ("github-actions" in comment.user.login) and ( - "No news item is found" in comment.body - ): + if ("github-actions" in comment.user.login) and ("No news item is found" in comment.body): return comment @@ -54,11 +51,11 @@ def main(): pr.create_issue_comment( """\ -**Warning!** No news item is found for this PR. -If this is an user facing change/feature/fix, please add a news item by copying the format from `news/TEMPLATE.rst`. +**Warning!** No news item is found for this PR. If this is an user facing change/feature/fix, + please add a news item by copying the format from `news/TEMPLATE.rst`. """ ) if __name__ == "__main__": - main() \ No newline at end of file + main()