Skip to content

Instantly share code, notes, and snippets.

@blink1073
Created November 24, 2021 15:57
Show Gist options
  • Save blink1073/a13c65f9920f1e5f848211516b36f428 to your computer and use it in GitHub Desktop.
Save blink1073/a13c65f9920f1e5f848211516b36f428 to your computer and use it in GitHub Desktop.
from subprocess import getoutput
import os
from datetime import datetime
from github_activity import generate_activity_md
import re
import sys
from ghapi.core import GhApi
name = sys.argv[-1]
os.chdir(name)
getoutput('git fetch upstream --tags')
url = getoutput('git remote get-url upstream')
_, target = url.replace('.git', '').split(':')
data = getoutput('git log --tags --simplify-by-decoration --pretty="format:%h | %D"').splitlines()
pattern = 'tag: (v?\d+\.\d+\.\d+)$'
def filter(datum):
sha, tag = datum
return re.match(pattern, tag) is not None
data = [d.split(' | ') for (i, d) in enumerate(data)]
data = [d for d in data if filter(d)]
print(data, file=sys.stderr)
output = ''
auth = os.environ['GITHUB_ACCESS_TOKEN']
def format_pr_entry(target, number):
"""Format a PR entry in the style used by our changelogs.
Parameters
----------
target : str
The GitHub owner/repo
number : int
The PR number to resolve
auth : str, optional
The GitHub authorization token
Returns
-------
str
A formatted PR entry
"""
owner, repo = target.split("/")
gh = GhApi(owner=owner, repo=repo, token=auth)
pull = gh.pulls.get(number)
title = pull.title
url = pull.url
user_name = pull.user.login
user_url = pull.user.html_url
return f"- {title} [#{number}]({url}) [@{user_name}]({user_url})"
for i in range(len(data) - 1):
curr_data = data[i]
prev_data = data[i + 1]
since = prev_data[0]
until = curr_data[0]
match = re.search(pattern, curr_data[1])
tag = match.groups()[0]
print(f'\n({i + 1}/{len(data)})', since, until, tag, file=sys.stderr)
md = generate_activity_md(
target,
since=since,
heading_level=2,
until=until,
auth=auth,
kind="pr"
)
if not md:
continue
entry = md.replace("[full changelog]", "[Full Changelog]")
entry = entry.splitlines()[2:]
for (ind, line) in enumerate(entry):
if re.search(r"\[@meeseeksmachine\]", line) is not None:
match = re.search(r"Backport PR #(\d+)", line)
if match:
print('resolving', match.groups()[0], file=sys.stderr)
entry[ind] = format_pr_entry(target, match.groups()[0])
entry = "\n".join(entry).strip()
output += f"""
## {tag}
{entry}
"""
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment