Skip to content

Commit

Permalink
WIP: Try
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Dec 21, 2023
1 parent e34e43e commit f5066c3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
68 changes: 68 additions & 0 deletions .github/actions/rename_towncrier/rename_towncrier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3

# Adapted from action-towncrier-changelog
import json
import os
import re
import subprocess
import sys
from pathlib import Path

from github import Github
from tomllib import loads

event_name = os.getenv('GITHUB_EVENT_NAME', 'pull_request')
if not event_name.startswith('pull_request'):
print(f'No-op for {event_name}')
sys.exit(0)
if 'GITHUB_EVENT_PATH' in os.environ:
with open(os.environ['GITHUB_EVENT_PATH'], encoding='utf-8') as fin:
event = json.load(fin)
pr_num = event['number']
basereponame = event['pull_request']['base']['repo']['full_name']
real = True
else: # local testing
pr_num = 12318 # added some towncrier files
basereponame = "mne-tools/mne-python"
real = False

g = Github(os.environ.get('GITHUB_TOKEN'))
baserepo = g.get_repo(basereponame)

# Grab config from upstream's default branch
toml_cfg = loads(Path("pyproject.toml").read_text("utf-8"))

config = toml_cfg["tool"]["towncrier"]
pr = baserepo.get_pull(pr_num)
modified_files = [f.filename for f in pr.get_files()]

# Get types from config
types = [ent["directory"] for ent in toml_cfg["tool"]["towncrier"]["type"]]
type_pipe = "|".join(types)

# Get files that potentially match the types
directory = toml_cfg["tool"]["towncrier"]["directory"]
assert directory.endswith("/"), directory

file_re = re.compile(rf"^{directory}({type_pipe})\.rst$")
found_stubs = [
f for f in modified_files if file_re.match(f)
]
for stub in found_stubs:
fro = stub
to = file_re.sub(rf"{directory}{pr_num}.\1.rst", fro)
print(f"Renaming {fro} to {to}")
if real:
subprocess.check_call(["git", "mv", fro, to])
else:
exit(0)
if real:
subprocess.check_call(
["git", "commit", "-am", f"DOC: Rename towncrier file(s) for PR {pr_num}"]
)
subprocess.check_call(["git", "push"])
print(
"Pushed commit to GitHub, exiting with failing status to prevent further "
"checks"
)
exit(1) # don't proceed with other checks
20 changes: 17 additions & 3 deletions .github/workflows/check_changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ on: # yamllint disable-line rule:truthy
pull_request:
types: [opened, synchronize, labeled, unlabeled]

# https://joht.github.io/johtizen/build/2022/01/20/github-actions-push-into-repository.html#git-commit-within-a-pull-request#example-4
jobs:
changelog_checker:
name: Check towncrier entry in doc/changes/devel/
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BOT_USERNAME: changelog-bot
GIT_COMMITTER_NAME: "MNE CI Bot"
GIT_COMMITTER_EMAIL: "[email protected]"
defaults:
run:
shell: bash -el {0}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
- run: actions/setup-python@v5
- run: |
pip install --upgrade towncrier pygithub
python ./.github/actions/rename_towncrier/rename_towncrier.py
- uses: larsoner/action-towncrier-changelog@co # revert to scientific-python @ 0.1.1 once bug is fixed
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BOT_USERNAME: changelog-bot

0 comments on commit f5066c3

Please sign in to comment.