fix: pr commentor

This commit is contained in:
Noortheen Raja 2022-04-09 12:05:01 +05:30 committed by Noorhteen Raja NJ
parent b1fe9e49d4
commit ea51a8a37f
2 changed files with 24 additions and 11 deletions

View file

@ -14,7 +14,10 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Check News item name: Check News item
steps: steps:
# note: the checkout will pull code from the base branch. This step should not pull code from the merge commit
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Setup Python - name: Setup Python
uses: actions/setup-python@v2 uses: actions/setup-python@v2
with: with:
@ -24,6 +27,6 @@ jobs:
- run: pip install -r requirements/ci.txt - run: pip install -r requirements/ci.txt
- run: python .github/workflows/check-news.py - run: python .github/workflows/check-news.py
env: env:
GITHUB_REF: "${{ env.GITHUB_REF }}" PR_NUMBER: "${{ github.event.number }}"
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}" GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}"

View file

@ -4,7 +4,6 @@ Put a warning comment if it doesn't.
""" """
import os import os
from github import Github, PullRequest from github import Github, PullRequest
import re
from fnmatch import fnmatch from fnmatch import fnmatch
@ -21,14 +20,18 @@ def check_news_file(pr):
def get_pr_number(): def get_pr_number():
pattern = re.compile(r"pull/(\d+)/") number = os.environ["PR_NUMBER"]
matches = pattern.findall(os.environ["GITHUB_REF"]) if not number:
return int(matches[0]) raise Exception(f"Pull request number is not found `PR_NUMBER={number}")
return int(number)
def check_issue_comment(pr: PullRequest.PullRequest): def get_old_comment(pr: PullRequest.PullRequest):
for comment in pr.get_issue_comments(): for comment in pr.get_issue_comments():
print(comment.user, comment.id) if ("github-actions" in comment.user.login) and (
"No news item is found" in comment.body
):
return comment
def main(): def main():
@ -37,14 +40,21 @@ def main():
repo = gh.get_repo(os.environ["GITHUB_REPOSITORY"]) repo = gh.get_repo(os.environ["GITHUB_REPOSITORY"])
pr = repo.get_pull(get_pr_number()) pr = repo.get_pull(get_pr_number())
has_news_added = check_news_file(pr) has_news_added = check_news_file(pr)
check_issue_comment(pr) old_comment = get_old_comment(pr)
if not has_news_added: if old_comment:
print("Found an existing comment from bot")
if has_news_added:
print("Delete warning from bot, since news items is added.")
if (not has_news_added) and (not old_comment):
print("No news item found") print("No news item found")
pr.create_issue_comment( pr.create_issue_comment(
"Warning! No news item is found. " """\
"If this is user facing change, please add a news item 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`.
"""
) )