Skip to content

Commit c38e325

Browse files
authoredApr 9, 2022
Merge pull request #64 from blink1073/all-changelog
2 parents f994a69 + 29c2d8e commit c38e325

File tree

5 files changed

+241
-14
lines changed

5 files changed

+241
-14
lines changed
 

‎.pre-commit-config.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111
repos:
1212
# Autoformat: Python code
1313
- repo: https://github.com/ambv/black
14-
rev: stable
14+
rev: 22.3.0
1515
hooks:
1616
- id: black
1717

1818
# Autoformat: markdown, yaml
1919
- repo: https://github.com/pre-commit/mirrors-prettier
20-
rev: v2.3.0
20+
rev: v2.6.2
2121
hooks:
2222
- id: prettier
2323
exclude: tests/.*
2424

2525
# Autoformat: https://github.com/asottile/reorder_python_imports
2626
- repo: https://github.com/asottile/reorder_python_imports
27-
rev: v2.5.0
27+
rev: v3.0.1
2828
hooks:
2929
- id: reorder-python-imports
3030

3131
# Misc...
3232
- repo: https://github.com/pre-commit/pre-commit-hooks
33-
rev: v3.4.0
33+
rev: v4.2.0
3434
# ref: https://github.com/pre-commit/pre-commit-hooks#hooks-available
3535
hooks:
3636
# Autoformat: Makes sure files end in a newline and only a newline.
@@ -49,6 +49,6 @@ repos:
4949

5050
# Lint: Python code
5151
- repo: https://github.com/pycqa/flake8
52-
rev: "3.9.2"
52+
rev: "4.0.1"
5353
hooks:
5454
- id: flake8

‎github_activity/cli.py

+21-5
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

+130-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""Use the GraphQL api to grab issues/PRs that match a query."""
22
import datetime
33
import os
4+
import re
5+
import shlex
6+
import subprocess
47
import sys
58
import urllib
69
from pathlib import Path
710
from subprocess import PIPE
811
from subprocess import run
12+
from tempfile import TemporaryDirectory
913

1014
import dateutil
1115
import numpy as np
@@ -153,6 +157,129 @@ 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+
# Get the sha and tag name for each tag in the target repo
211+
with TemporaryDirectory() as td:
212+
213+
subprocess.run(
214+
shlex.split(f"git clone https://github.com/{target} repo"), cwd=td
215+
)
216+
repo = os.path.join(td, "repo")
217+
subprocess.run(shlex.split("git fetch origin --tags"), cwd=repo)
218+
219+
cmd = 'git log --tags --simplify-by-decoration --pretty="format:%h | %D"'
220+
data = (
221+
subprocess.check_output(shlex.split(cmd), cwd=repo)
222+
.decode("utf-8")
223+
.splitlines()
224+
)
225+
226+
# Clean up the raw data
227+
pattern = f"tag: {pattern}"
228+
229+
def filter(datum):
230+
_, tag = datum
231+
# Handle the HEAD tag if it exists
232+
if "," in tag:
233+
tag = tag.split(", ")[1]
234+
return re.match(pattern, tag) is not None
235+
236+
data = [d.split(" | ") for (i, d) in enumerate(data)]
237+
data = [d for d in data if filter(d)]
238+
239+
# Generate a changelog entry for each version and sha range
240+
output = ""
241+
242+
for i in range(len(data) - 1):
243+
curr_data = data[i]
244+
prev_data = data[i + 1]
245+
246+
since = prev_data[0]
247+
until = curr_data[0]
248+
249+
# Handle the HEAD tag if it exists
250+
if "," in curr_data[1]:
251+
curr_data[1] = curr_data[1].split(",")[1]
252+
253+
match = re.search(pattern, curr_data[1])
254+
tag = match.groups()[0]
255+
256+
print(f"\n({i + 1}/{len(data)})", since, until, tag, file=sys.stderr)
257+
md = generate_activity_md(
258+
target,
259+
since=since,
260+
heading_level=2,
261+
until=until,
262+
auth=auth,
263+
kind=kind,
264+
include_issues=include_issues,
265+
include_opened=include_opened,
266+
strip_brackets=strip_brackets,
267+
branch=branch,
268+
)
269+
270+
if not md:
271+
continue
272+
273+
# Replace the header line with our version tag
274+
md = "\n".join(md.splitlines()[1:])
275+
276+
output += f"""
277+
## {tag}
278+
{md}
279+
"""
280+
return output
281+
282+
156283
def generate_activity_md(
157284
target,
158285
since=None,
@@ -208,13 +335,12 @@ def generate_activity_md(
208335
By default, top-level heading is h1, sections are h2.
209336
With heading_level=2 those are increased to h2 and h3, respectively.
210337
branch : string | None
211-
The branch or reference name to filter pull requests by
338+
The branch or reference name to filter pull requests by.
212339
213340
Returns
214341
-------
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.
342+
entry: str
343+
The markdown changelog entry.
218344
"""
219345
org, repo = _parse_target(target)
220346

‎tests/test_cli.py

+11
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ def test_pr_split(tmpdir, file_regression):
6969
md = path_output.read_text()
7070
md = md.split("## Contributors to this release")[0]
7171
file_regression.check(md, extension=".md")
72+
73+
74+
def test_cli_all(tmpdir, file_regression):
75+
"""Test that a full changelog is created"""
76+
path_tmp = Path(tmpdir)
77+
path_output = path_tmp.joinpath("out.md")
78+
cmd = f"github-activity executablebooks/github-activity --all -o {path_output}"
79+
run(cmd.split(), check=True)
80+
md = path_output.read_text()
81+
index = md.index("## v0.2.0")
82+
file_regression.check(md[index:], extension=".md")

‎tests/test_cli/test_cli_all.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## v0.2.0
2+
3+
([full changelog](https://github.com/executablebooks/github-activity/compare/ca2819b...f994a69))
4+
5+
### Enhancements made
6+
7+
- Use auth for all usages of requests [#60](https://github.com/executablebooks/github-activity/pull/60) ([@blink1073](https://github.com/blink1073))
8+
- Handle detection of target from SSH based remotes [#51](https://github.com/executablebooks/github-activity/pull/51) ([@consideRatio](https://github.com/consideRatio))
9+
- ✨ ENH: Auto-detecting the target [#45](https://github.com/executablebooks/github-activity/pull/45) ([@choldgraf](https://github.com/choldgraf))
10+
11+
### Bugs fixed
12+
13+
- 🐛 FIX: write status messages to sys.stderr [#47](https://github.com/executablebooks/github-activity/pull/47) ([@minrk](https://github.com/minrk))
14+
15+
### Maintenance and upkeep improvements
16+
17+
- 🔧 MAINT: Split test_cli using @pytest.mark.parameterize [#56](https://github.com/executablebooks/github-activity/pull/56) ([@manics](https://github.com/manics))
18+
- pre-commit configured and executed [#55](https://github.com/executablebooks/github-activity/pull/55) ([@consideRatio](https://github.com/consideRatio))
19+
- 🔧 MAINT: _get_latest_tag should use the remote repo [#52](https://github.com/executablebooks/github-activity/pull/52) ([@manics](https://github.com/manics))
20+
- 🔧 MAINT: hyphen instead of asterisk [#44](https://github.com/executablebooks/github-activity/pull/44) ([@choldgraf](https://github.com/choldgraf))
21+
22+
### Contributors to this release
23+
24+
([GitHub contributors page for this release](https://github.com/executablebooks/github-activity/graphs/contributors?from=2021-02-20&to=2021-12-01&type=c))
25+
26+
[@blink1073](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Ablink1073+updated%3A2021-02-20..2021-12-01&type=Issues) | [@choldgraf](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Acholdgraf+updated%3A2021-02-20..2021-12-01&type=Issues) | [@consideRatio](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3AconsideRatio+updated%3A2021-02-20..2021-12-01&type=Issues) | [@manics](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Amanics+updated%3A2021-02-20..2021-12-01&type=Issues) | [@minrk](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Aminrk+updated%3A2021-02-20..2021-12-01&type=Issues) | [@welcome](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Awelcome+updated%3A2021-02-20..2021-12-01&type=Issues) | [@wolfv](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Awolfv+updated%3A2021-02-20..2021-12-01&type=Issues)
27+
28+
## v0.1.3
29+
30+
([full changelog](https://github.com/executablebooks/github-activity/compare/60c7f06...ca2819b))
31+
32+
### New features added
33+
34+
- ✨NEW: heading_level argument for increasing heading level [#38](https://github.com/executablebooks/github-activity/pull/38) ([@minrk](https://github.com/minrk))
35+
36+
### Enhancements made
37+
38+
- 👌IMPROVE: add blank lines below headings [#36](https://github.com/executablebooks/github-activity/pull/36) ([@minrk](https://github.com/minrk))
39+
- 👌 IMPROVE: since/until: assume git reference, fallback to datetime [#33](https://github.com/executablebooks/github-activity/pull/33) ([@consideRatio](https://github.com/consideRatio))
40+
41+
### Bugs fixed
42+
43+
- 🐛 FIX: tags like 'doctor' would trigger 'doc' tag [#40](https://github.com/executablebooks/github-activity/pull/40) ([@consideRatio](https://github.com/consideRatio))
44+
45+
### Documentation improvements
46+
47+
- 📚 DOC: Minor docs changes [#43](https://github.com/executablebooks/github-activity/pull/43) ([@choldgraf](https://github.com/choldgraf))
48+
49+
### Other merged PRs
50+
51+
- Adding filtering by branch [#42](https://github.com/executablebooks/github-activity/pull/42) ([@choldgraf](https://github.com/choldgraf))
52+
- use tqdm for progress [#39](https://github.com/executablebooks/github-activity/pull/39) ([@minrk](https://github.com/minrk))
53+
- Remove no longer used code [#37](https://github.com/executablebooks/github-activity/pull/37) ([@consideRatio](https://github.com/consideRatio))
54+
- [FIX,TST] resolve refs when not run from a repo [#35](https://github.com/executablebooks/github-activity/pull/35) ([@minrk](https://github.com/minrk))
55+
56+
### Contributors to this release
57+
58+
([GitHub contributors page for this release](https://github.com/executablebooks/github-activity/graphs/contributors?from=2020-08-31&to=2021-02-20&type=c))
59+
60+
[@choldgraf](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Acholdgraf+updated%3A2020-08-31..2021-02-20&type=Issues) | [@consideRatio](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3AconsideRatio+updated%3A2020-08-31..2021-02-20&type=Issues) | [@minrk](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Aminrk+updated%3A2020-08-31..2021-02-20&type=Issues) | [@welcome](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Awelcome+updated%3A2020-08-31..2021-02-20&type=Issues)
61+
62+
## v0.1.2
63+
64+
([full changelog](https://github.com/executablebooks/github-activity/compare/32f89fd...60c7f06))
65+
66+
### Merged PRs
67+
68+
- adding thumbsup to query [#31](https://github.com/executablebooks/github-activity/pull/31) ([@choldgraf](https://github.com/choldgraf))
69+
70+
### Contributors to this release
71+
72+
([GitHub contributors page for this release](https://github.com/executablebooks/github-activity/graphs/contributors?from=2020-08-07&to=2020-08-31&type=c))
73+
74+
[@choldgraf](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Acholdgraf+updated%3A2020-08-07..2020-08-31&type=Issues)

0 commit comments

Comments
 (0)
Please sign in to comment.