Skip to content

Commit e7d8d87

Browse files
authored
Add CI for news update (#79)
* Add CI for news update * Fix line lenght for flake8
1 parent 0b9a087 commit e7d8d87

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

.github/workflows/check-news-item.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Check News Item
2+
3+
on:
4+
pull_request_target:
5+
branches:
6+
- main
7+
8+
permissions:
9+
pull-requests: write
10+
contents: read
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
name: Check News item
16+
steps:
17+
18+
# note: the checkout will pull code from the base branch. This step should not pull code from the merge commit
19+
- uses: actions/checkout@v4
20+
21+
- name: Setup Python
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: '3.9'
25+
cache: 'pip'
26+
cache-dependency-path: 'pyproject.toml'
27+
- run: pip install PyGithub
28+
- run: python .github/workflows/check-news.py
29+
env:
30+
PR_NUMBER: "${{ github.event.number }}"
31+
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
32+
GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}"

.github/workflows/check-news.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)