Skip to content

Commit f088e4a

Browse files
authored
Improve branch filtering and raise error on empty dataframes (#162)
1 parent 247c2d7 commit f088e4a

File tree

3 files changed

+67
-38
lines changed

3 files changed

+67
-38
lines changed

docs/use.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,20 @@ github-activity jupyter/notebook -s 6.0.0 -u 6.0.1 -o sample_notebook_activity.m
4141

4242
You can find the [resulting markdown here](sample_notebook_activity).
4343

44-
```{tip}
45-
For repositories that use multiple branches, it may be necessary to filter PRs by a branch name. This can be done using the `--branch` parameter in the CLI. Other git references can be used as well in place of a branch name.
44+
## Filter pull requests by branch
45+
46+
Many repositories work with multiple active branches (e.g., `main`, `develop`, feature branches). When generating a changelog for a specific release, you typically only want to include pull requests that were merged into the release branch.
47+
48+
Use the `--branch` (or `-b`) parameter to filter pull requests by their target branch:
49+
50+
```bash
51+
github-activity org/repo --since v1.0.0 --until v2.0.0 --branch main
52+
```
53+
54+
This will **only include pull requests that targeted the `main` branch**, excluding any PRs merged to other branches like `develop` or feature branches.
55+
56+
```{note}
57+
You can use any git reference (tag, commit hash, etc.) in place of a branch name.
4658
```
4759

4860
## Choose a date or a tag to filter activity
@@ -217,7 +229,7 @@ This is not as well-documented as the CLI, but should have most functionality av
217229

218230
For generating markdown changelogs from Python, here's an example:
219231

220-
```
232+
```python
221233
from github_activity import generate_activity_md
222234

223235
markdown = generate_activity_md(
@@ -231,7 +243,7 @@ markdown = generate_activity_md(
231243
include_opened=True,
232244
strip_brackets=True,
233245
heading_level=1,
234-
branch=None,
246+
branch="main", # Filter PRs by target branch (optional, use None for all branches)
235247
)
236248

237249
# Print or save the markdown

github_activity/cli.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@
123123
"--branch",
124124
"-b",
125125
default=None,
126-
help=("""The branch or reference name to filter pull requests by"""),
126+
help=(
127+
"""Filter pull requests by their target branch. Only PRs merged into this branch will be included. """
128+
),
127129
)
128130
parser.add_argument(
129131
"--all",
@@ -223,31 +225,36 @@ def main():
223225
ignored_contributors=args.ignore_contributor,
224226
)
225227

226-
if args.all:
227-
md = generate_all_activity_md(args.target, **common_kwargs)
228+
# Wrap in a try/except so we don't have an ugly stack trace if there's an error
229+
try:
230+
if args.all:
231+
md = generate_all_activity_md(args.target, **common_kwargs)
228232

229-
else:
230-
md = generate_activity_md(
231-
args.target,
232-
since=args.since,
233-
until=args.until,
234-
heading_level=args.heading_level,
235-
**common_kwargs,
236-
)
233+
else:
234+
md = generate_activity_md(
235+
args.target,
236+
since=args.since,
237+
until=args.until,
238+
heading_level=args.heading_level,
239+
**common_kwargs,
240+
)
237241

238-
if not md:
239-
return
240-
241-
if args.output:
242-
output = os.path.abspath(args.output)
243-
output_dir = os.path.dirname(output)
244-
if not os.path.exists(output_dir):
245-
os.makedirs(output_dir)
246-
with open(args.output, "w") as ff:
247-
ff.write(md)
248-
print(f"Finished writing markdown to: {args.output}", file=sys.stderr)
249-
else:
250-
print(md)
242+
if not md:
243+
return
244+
245+
if args.output:
246+
output = os.path.abspath(args.output)
247+
output_dir = os.path.dirname(output)
248+
if not os.path.exists(output_dir):
249+
os.makedirs(output_dir)
250+
with open(args.output, "w") as ff:
251+
ff.write(md)
252+
print(f"Finished writing markdown to: {args.output}", file=sys.stderr)
253+
else:
254+
print(md)
255+
except ValueError as e:
256+
print(f"Error: {e}", file=sys.stderr)
257+
sys.exit(1)
251258

252259

253260
if __name__ == "__main__":

github_activity/github_activity.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,27 @@ def generate_activity_md(
462462
data = get_activity(
463463
target, since=since, until=until, kind=kind, auth=auth, cache=False
464464
)
465+
466+
# Raise error if GitHub API returned no activity at all
467+
# This happens when the repository has no issues/PRs in the date range
465468
if data.empty:
466-
return
469+
raise ValueError(
470+
f"No activity found for {org}/{repo} between {since} and {until}."
471+
)
472+
473+
# Filter the PRs by branch (or ref) if given
474+
if branch is not None:
475+
index_names = data[
476+
(data["kind"] == "pr") & (data["baseRefName"] != branch)
477+
].index
478+
data.drop(index_names, inplace=True)
479+
480+
# Raise error if branch filter removed all data
481+
# This happens when PRs exist but none targeted the specified branch
482+
if data.empty:
483+
raise ValueError(
484+
f"Found activity, but none for the --branch target specified for {org}/{repo} on branch '{branch}' between {since} and {until}.\n"
485+
)
467486

468487
# Collect authors of comments on issues/prs that they didn't open for our attribution list
469488
comment_response_cutoff = 6 # Comments on a single issue
@@ -581,15 +600,6 @@ def filter_ignored(userlist):
581600
].index.tolist()
582601
all_contributors |= set(c for c in comment_contributors if isinstance(c, str))
583602

584-
# Filter the PRs by branch (or ref) if given
585-
if branch is not None:
586-
index_names = data[
587-
(data["kind"] == "pr") & (data["baseRefName"] != branch)
588-
].index
589-
data.drop(index_names, inplace=True)
590-
if data.empty:
591-
return
592-
593603
# Extract datetime strings from data attributes for pandas query
594604
since_dt_str = data.since_dt_str # noqa: F841
595605
until_dt_str = data.until_dt_str # noqa: F841

0 commit comments

Comments
 (0)