Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add news item CI #28

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/check-news-item.yml
Original file line number Diff line number Diff line change
@@ -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.12'
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 }}"
62 changes: 62 additions & 0 deletions .github/workflows/check-news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""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`.
"""
)
assert False


if __name__ == "__main__":
main()
23 changes: 23 additions & 0 deletions news/TEMPLATE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
Loading