Skip to content

Commit 2a5f89e

Browse files
committed
add a bootstrap function for all changelog entries
1 parent f994a69 commit 2a5f89e

File tree

2 files changed

+134
-8
lines changed

2 files changed

+134
-8
lines changed

github_activity/cli.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .git import _git_installed_check
88
from .github_activity import _parse_target
99
from .github_activity import generate_activity_md
10+
from .github_activity import generate_all_activity_md
1011

1112
DESCRIPTION = "Generate a markdown changelog of GitHub activity within a date window."
1213
parser = argparse.ArgumentParser(description=DESCRIPTION)
@@ -109,6 +110,12 @@
109110
default=None,
110111
help=("""The branch or reference name to filter pull requests by"""),
111112
)
113+
parser.add_argument(
114+
"--all",
115+
default=False,
116+
action="store_true",
117+
help=("""Whether to include all the GitHub tags"""),
118+
)
112119

113120

114121
def main():
@@ -151,19 +158,28 @@ def main():
151158
except Exception:
152159
raise ValueError(err)
153160

154-
md = generate_activity_md(
155-
args.target,
156-
since=args.since,
157-
until=args.until,
161+
common_kwargs = dict(
158162
kind=args.kind,
159163
auth=args.auth,
160164
tags=tags,
161165
include_issues=bool(args.include_issues),
162166
include_opened=bool(args.include_opened),
163167
strip_brackets=bool(args.strip_brackets),
164-
heading_level=args.heading_level,
165168
branch=args.branch,
166169
)
170+
171+
if args.all:
172+
md = generate_all_activity_md(args.target, **common_kwargs)
173+
174+
else:
175+
md = generate_activity_md(
176+
args.target,
177+
since=args.since,
178+
until=args.until,
179+
heading_level=args.heading_level,
180+
**common_kwargs
181+
)
182+
167183
if not md:
168184
return
169185

github_activity/github_activity.py

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import sys
55
import urllib
66
from pathlib import Path
7+
import re
8+
import shlex
9+
import subprocess
10+
from tempfile import TemporaryDirectory
711
from subprocess import PIPE
812
from subprocess import run
913

@@ -153,6 +157,112 @@ def get_activity(
153157
return query_data
154158

155159

160+
def generate_all_activity_md(
161+
target,
162+
pattern=r'(v?\d+\.\d+\.\d+)$',
163+
kind=None,
164+
auth=None,
165+
tags=None,
166+
include_issues=False,
167+
include_opened=False,
168+
strip_brackets=False,
169+
branch=None,
170+
):
171+
"""Generate a full markdown changelog of GitHub activity of a repo based on release tags.
172+
173+
Parameters
174+
----------
175+
target : string
176+
The GitHub organization/repo for which you want to grab recent issues/PRs.
177+
Can either be *just* and organization (e.g., `jupyter`) or a combination
178+
organization and repo (e.g., `jupyter/notebook`). If the former, all
179+
repositories for that org will be used. If the latter, only the specified
180+
repository will be used. Can also be a URL to a GitHub org or repo.
181+
pattern: str
182+
The expression used to match a release tag
183+
kind : ["issue", "pr"] | None
184+
Return only issues or PRs. If None, both will be returned.
185+
auth : string | None
186+
An authentication token for GitHub. If None, then the environment
187+
variable `GITHUB_ACCESS_TOKEN` will be tried.
188+
tags : list of strings | None
189+
A list of the tags to use in generating subsets of PRs for the markdown report.
190+
Must be one of:
191+
192+
['enhancement', 'bugs', 'maintenance', 'documentation', 'api_change']
193+
194+
If None, all of the above tags will be used.
195+
include_issues : bool
196+
Include Issues in the markdown output. Default is False.
197+
include_opened : bool
198+
Include a list of opened items in the markdown output. Default is False.
199+
strip_brackets : bool
200+
If True, strip any text between brackets at the beginning of the issue/PR title.
201+
E.g., [MRG], [DOC], etc.
202+
branch : string | None
203+
The branch or reference name to filter pull requests by
204+
205+
Returns
206+
-------
207+
entry: str
208+
The markdown changelog entry for all of the release tags in the repo.
209+
"""
210+
with TemporaryDirectory() as td:
211+
212+
subprocess.run(shlex.split(f'git clone git@github.com:{target}.git repo'), cwd=td)
213+
repo = os.path.join(td, 'repo')
214+
subprocess.run(shlex.split('git fetch upstream --tags'), cwd=repo)
215+
216+
cmd = 'git log --tags --simplify-by-decoration --pretty="format:%h | %D"'
217+
data = subprocess.check_output(shlex.split(cmd), cwd=repo).decode('utf-8').splitlines()
218+
219+
pattern = f'tag: {pattern}'
220+
221+
def filter(datum):
222+
_, tag = datum
223+
return re.match(pattern, tag) is not None
224+
225+
data = [d.split(' | ') for (i, d) in enumerate(data)]
226+
data = [d for d in data if filter(d)]
227+
output = ""
228+
229+
for i in range(len(data) - 1):
230+
curr_data = data[i]
231+
prev_data = data[i + 1]
232+
233+
since = prev_data[0]
234+
until = curr_data[0]
235+
236+
match = re.search(pattern, curr_data[1])
237+
tag = match.groups()[0]
238+
239+
print(f'\n({i + 1}/{len(data)})', since, until, tag, file=sys.stderr)
240+
md = generate_activity_md(
241+
target,
242+
since=since,
243+
heading_level=2,
244+
until=until,
245+
auth=auth,
246+
kind=kind,
247+
include_issues=include_issues,
248+
include_opened=include_opened,
249+
strip_brackets=strip_brackets,
250+
branch=branch
251+
)
252+
253+
if not md:
254+
continue
255+
256+
# Replace the header line with our version tag
257+
md = '\n'.join(md.splitlines()[1:])
258+
259+
output += f"""
260+
## {tag}
261+
{md}
262+
"""
263+
return output
264+
265+
156266
def generate_activity_md(
157267
target,
158268
since=None,
@@ -212,9 +322,8 @@ def generate_activity_md(
212322
213323
Returns
214324
-------
215-
query_data : pandas DataFrame
216-
A munged collection of data returned from your query. This
217-
will be a combination of issues and PRs.
325+
entry: str
326+
The markdown changelog entry
218327
"""
219328
org, repo = _parse_target(target)
220329

@@ -574,3 +683,4 @@ def _get_latest_tag():
574683
out = run("git describe --tags".split(), stdout=PIPE)
575684
tag = out.stdout.decode().rsplit("-", 2)[0]
576685
return tag
686+

0 commit comments

Comments
 (0)