Skip to content

Commit a73faf1

Browse files
authored
Merge pull request #180 from executablebooks/prek
Update pre commit config and moved to an active mirror for prettier
2 parents 53e8fe8 + 83a38bb commit a73faf1

12 files changed

Lines changed: 101 additions & 95 deletions

File tree

.github/workflows/tests.yaml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,19 @@ jobs:
2525

2626
steps:
2727
- uses: actions/checkout@v7
28-
- uses: actions/setup-python@v7
29-
with:
30-
python-version: "3.14"
3128

32-
# ref: https://github.com/pre-commit/action
33-
- uses: pre-commit/action@v3.0.1
29+
# ref: https://github.com/j178/prek-action
30+
- uses: j178/prek-action@4e14d07f9231acabce116ccfca13b13dd9755ece # v3.0.0
3431
- name: Help message if pre-commit fail
3532
if: ${{ failure() }}
3633
run: |
37-
echo "You can install pre-commit hooks to automatically run formatting"
38-
echo "on each commit with:"
39-
echo " pre-commit install"
34+
echo "You can install pre-commit hooks (with prek or pre-commit) to"
35+
echo "automatically run formatting on each commit with:"
36+
echo " prek install"
4037
echo "or you can run by hand on staged files with"
41-
echo " pre-commit run"
38+
echo " prek run"
4239
echo "or after-the-fact on already committed files with"
43-
echo " pre-commit run --all-files"
40+
echo " prek run --all-files"
4441
4542
tests:
4643
runs-on: ubuntu-24.04

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
repos:
1212
# Autoformat: Python code
1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: f0fe93c067104b76ffb58852abe79673a8429bd1 # frozen: v0.11.8
14+
rev: v0.16.7
1515
hooks:
16-
- id: ruff
16+
- id: ruff-check
1717
args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"]
1818
- id: ruff-format
1919

2020
# Autoformat: markdown, yaml
21-
- repo: https://github.com/pre-commit/mirrors-prettier
22-
rev: 787fb9f542b140ba0b2aced38e6a3e68021647a3 # frozen: v3.5.3
21+
- repo: https://github.com/rbubley/mirrors-prettier
22+
rev: v3.9.6
2323
hooks:
2424
- id: prettier
2525
exclude: tests/.*
2626

2727
# Misc...
2828
- repo: https://github.com/pre-commit/pre-commit-hooks
29-
rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # v6.0.0
29+
rev: v6.0.0 # v6.0.0
3030
# ref: https://github.com/pre-commit/pre-commit-hooks#hooks-available
3131
hooks:
3232
# Sanity checks

docs/example_cache.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
21+
"from github_activity.cache import get_cache_stats, load_from_cache\n",
2122
"from github_activity.github_activity import get_activity\n",
22-
"from github_activity.cache import load_from_cache, get_cache_stats\n",
2323
"\n",
2424
"activity = get_activity(\"jupyter/repo2docker\", \"2019\", cache=True)"
2525
]

docs/use.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ df = get_activity(
266266
until="2023-12-31",
267267
auth="your-github-token",
268268
kind=None,
269-
cache=None
269+
cache=None,
270270
)
271271
```
272272

@@ -277,5 +277,5 @@ There are some helper functions for this. For example, to extract nested comment
277277
from github_activity import get_activity, extract_comments
278278

279279
df = get_activity(...)
280-
comments_df = extract_comments(df['comments'])
280+
comments_df = extract_comments(df["comments"])
281281
```

github_activity/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__version__ = "1.1.7"
2-
__all__ = ["get_activity", "generate_activity_md"]
2+
__all__ = ["generate_activity_md", "get_activity"]
33

4-
from .github_activity import get_activity, generate_activity_md
4+
from .github_activity import generate_activity_md, get_activity

github_activity/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _cache_data(query_data, path_cache):
1919
path_repo_cache.mkdir(parents=True, exist_ok=True)
2020

2121
# First pull issues
22-
def _categorize_item(item):
22+
def _categorize_item(item, repo=repo):
2323
if f"{repo}/issues" in item:
2424
out = "issue"
2525
else:

github_activity/cli.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import json
33
import os
44
import sys
5-
from subprocess import PIPE
6-
from subprocess import run
5+
from subprocess import PIPE, run
76

8-
from .git import _git_installed_check
9-
from .git import _git_toplevel_path
10-
from .github_activity import _parse_target
11-
from .github_activity import generate_activity_md
12-
from .github_activity import generate_all_activity_md
7+
from .git import _git_installed_check, _git_toplevel_path
8+
from .github_activity import (
9+
_parse_target,
10+
generate_activity_md,
11+
generate_all_activity_md,
12+
)
1313

1414
DESCRIPTION = "Generate a markdown changelog of GitHub activity within a date window."
1515

@@ -191,7 +191,7 @@ def main():
191191
if not args.target:
192192
err = "Could not automatically detect remote, and none was given."
193193
try:
194-
out = run("git remote -v".split(), stdout=PIPE)
194+
out = run(["git", "remote", "-v"], stdout=PIPE, check=False)
195195
remotes = out.stdout.decode().split("\n")
196196
remotes = [ii for ii in remotes if ii]
197197
remotes = {
@@ -214,16 +214,16 @@ def main():
214214
except Exception:
215215
raise ValueError(err)
216216

217-
common_kwargs = dict(
218-
kind=args.kind,
219-
auth=args.auth,
220-
tags=tags,
221-
include_issues=bool(args.include_issues),
222-
include_opened=bool(args.include_opened),
223-
strip_brackets=bool(args.strip_brackets),
224-
branch=args.branch,
225-
ignored_contributors=args.ignore_contributor,
226-
)
217+
common_kwargs = {
218+
"kind": args.kind,
219+
"auth": args.auth,
220+
"tags": tags,
221+
"include_issues": bool(args.include_issues),
222+
"include_opened": bool(args.include_opened),
223+
"strip_brackets": bool(args.strip_brackets),
224+
"branch": args.branch,
225+
"ignored_contributors": args.ignore_contributor,
226+
}
227227

228228
# Wrap in a try/except so we don't have an ugly stack trace if there's an error
229229
try:

github_activity/github_activity.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import sys
1111
from collections import OrderedDict
1212
from json import loads
13-
from subprocess import CalledProcessError
14-
from subprocess import PIPE
15-
from subprocess import run
13+
from subprocess import PIPE, CalledProcessError, run
1614
from tempfile import TemporaryDirectory
1715

1816
import dateutil.parser
@@ -25,7 +23,6 @@
2523
from .cache import _cache_data
2624
from .graphql import GitHubGraphQlQuery
2725

28-
2926
# The tags and description to use in creating subsets of PRs
3027
TAGS_METADATA_BASE = OrderedDict(
3128
[
@@ -175,7 +172,9 @@ def get_activity(
175172
if auth is None:
176173
# Attempt to use the gh cli if installed
177174
try:
178-
p = run(["gh", "auth", "token"], text=True, capture_output=True)
175+
p = run(
176+
["gh", "auth", "token"], text=True, capture_output=True, check=False
177+
)
179178
auth = p.stdout.strip()
180179
except CalledProcessError:
181180
print(
@@ -270,7 +269,7 @@ def generate_all_activity_md(
270269
include_opened=False,
271270
strip_brackets=False,
272271
branch=None,
273-
ignored_contributors: list[str] = None,
272+
ignored_contributors: list[str] | None = None,
274273
):
275274
"""Generate a full markdown changelog of GitHub activity of a repo based on release tags.
276275
@@ -316,10 +315,12 @@ def generate_all_activity_md(
316315
# Get the sha and tag name for each tag in the target repo
317316
with TemporaryDirectory() as td:
318317
subprocess.run(
319-
shlex.split(f"git clone https://github.com/{target} repo"), cwd=td
318+
shlex.split(f"git clone https://github.com/{target} repo"),
319+
cwd=td,
320+
check=False,
320321
)
321322
repo = os.path.join(td, "repo")
322-
subprocess.run(shlex.split("git fetch origin --tags"), cwd=repo)
323+
subprocess.run(shlex.split("git fetch origin --tags"), cwd=repo, check=False)
323324

324325
cmd = 'git log --tags --simplify-by-decoration --pretty="format:%h | %D"'
325326
data = (
@@ -402,8 +403,7 @@ def add(self, contributor):
402403
def __iter__(self):
403404
if self.author:
404405
yield self.author
405-
for item in sorted(self.other - {self.author}):
406-
yield item
406+
yield from sorted(self.other - {self.author})
407407

408408

409409
def generate_activity_md(
@@ -418,7 +418,7 @@ def generate_activity_md(
418418
strip_brackets=False,
419419
heading_level=1,
420420
branch=None,
421-
ignored_contributors: list[str] = None,
421+
ignored_contributors: list[str] | None = None,
422422
):
423423
"""Generate a markdown changelog of GitHub activity within a date window.
424424
@@ -536,12 +536,10 @@ def ignored_user(username):
536536
return True
537537

538538
# Check against user-specified ignored contributors
539-
if ignored_contributors and any(
540-
fnmatch.fnmatch(username, user) for user in ignored_contributors
541-
):
542-
return True
543-
544-
return False
539+
return bool(
540+
ignored_contributors
541+
and any(fnmatch.fnmatch(username, user) for user in ignored_contributors)
542+
)
545543

546544
def filter_ignored(userlist):
547545
return {user for user in userlist if not ignored_user(user)}
@@ -617,7 +615,7 @@ def filter_ignored(userlist):
617615
comment_contributors = comment_contributor_counts[
618616
comment_contributor_counts >= comment_others_cutoff
619617
].index.tolist()
620-
all_contributors |= set(c for c in comment_contributors if isinstance(c, str))
618+
all_contributors |= {c for c in comment_contributors if isinstance(c, str)}
621619

622620
closed_mask, opened_mask = _activity_window_masks(
623621
data, data.since_dt_str, data.until_dt_str, data.since_is_git_ref
@@ -646,7 +644,7 @@ def filter_ignored(userlist):
646644
# Add any contributors to a merged PR to our contributors list
647645
# Filter out NaN values and non-strings
648646
pr_contributors = closed_prs["contributors"].explode().unique().tolist()
649-
all_contributors |= set(c for c in pr_contributors if isinstance(c, str))
647+
all_contributors |= {c for c in pr_contributors if isinstance(c, str)}
650648

651649
# Define categories for a few labels
652650
if tags is None:
@@ -660,7 +658,7 @@ def filter_ignored(userlist):
660658
tags_metadata = {key: val for key, val in TAGS_METADATA_BASE.items() if key in tags}
661659

662660
# Initialize our tags with empty metadata
663-
for key, vals in tags_metadata.items():
661+
for vals in tags_metadata.values():
664662
vals.update(
665663
{
666664
"mask": None,
@@ -673,14 +671,18 @@ def filter_ignored(userlist):
673671
# Track which PRs have already been assigned to prevent duplicates
674672
assigned_prs = set()
675673

676-
for kind, kindmeta in tags_metadata.items():
674+
for kindmeta in tags_metadata.values():
677675
# First find the PRs based on tag
678676
mask = closed_prs["labels"].map(
679-
lambda a: any(ii == jj for ii in kindmeta["tags"] for jj in a)
677+
lambda a, kindmeta=kindmeta: any(
678+
ii == jj for ii in kindmeta["tags"] for jj in a
679+
)
680680
)
681681
# Now find PRs based on prefix
682682
mask_pre = closed_prs["title"].map(
683-
lambda title: any(f"{ipre}:" in title for ipre in kindmeta["pre"])
683+
lambda title, kindmeta=kindmeta: any(
684+
f"{ipre}:" in title for ipre in kindmeta["pre"]
685+
)
684686
)
685687
mask = mask | mask_pre
686688

@@ -705,39 +707,39 @@ def filter_ignored(userlist):
705707

706708
# Add some optional kinds of PRs / issues
707709
tags_metadata.update(
708-
dict(others={"description": other_description, "md": [], "data": others})
710+
{"others": {"description": other_description, "md": [], "data": others}}
709711
)
710712
if include_issues:
711713
tags_metadata.update(
712-
dict(
713-
closed_issues={
714+
{
715+
"closed_issues": {
714716
"description": "Closed issues",
715717
"md": [],
716718
"data": closed_issues,
717719
}
718-
)
720+
}
719721
)
720722
if include_opened:
721723
tags_metadata.update(
722-
dict(
723-
opened_issues={
724+
{
725+
"opened_issues": {
724726
"description": "Opened issues",
725727
"md": [],
726728
"data": opened_issues,
727729
}
728-
)
730+
}
729731
)
730732
if include_opened:
731733
tags_metadata.update(
732-
dict(opened_prs={"description": "Opened PRs", "md": [], "data": opened_prs})
734+
{"opened_prs": {"description": "Opened PRs", "md": [], "data": opened_prs}}
733735
)
734736

735737
# Generate the markdown
736738
prs = tags_metadata
737739

738740
extra_head = "#" * (heading_level - 1)
739741

740-
for kind, items in prs.items():
742+
for items in prs.values():
741743
n_orgs = len(items["data"]["org"].unique())
742744
for org, idata in items["data"].groupby("org"):
743745
if n_orgs > 1:
@@ -794,7 +796,7 @@ def filter_ignored(userlist):
794796
"",
795797
f"([full changelog]({changelog_url}))",
796798
]
797-
for kind, info in prs.items():
799+
for info in prs.values():
798800
if len(info["md"]) > 0:
799801
md += [""]
800802
md.append(f"{extra_head}## {info['description']}")
@@ -943,9 +945,7 @@ def _get_datetime_and_type(org, repo, datetime_or_git_ref, auth):
943945
return (dt, False)
944946
except Exception:
945947
raise ValueError(
946-
"{0} not found as a ref or valid date format".format(
947-
datetime_or_git_ref
948-
)
948+
f"{datetime_or_git_ref} not found as a ref or valid date format"
949949
)
950950

951951

@@ -981,7 +981,7 @@ def _get_latest_release_tag(org, repo):
981981
]
982982
print(f"Auto-detecting latest release tag for: {org}/{repo}", file=sys.stderr)
983983
print(f"Running command: {' '.join(cmd)}", file=sys.stderr)
984-
out = run(cmd, stdout=PIPE)
984+
out = run(cmd, stdout=PIPE, check=False)
985985
try:
986986
json = out.stdout.decode()
987987
release_data = loads(json)
@@ -998,6 +998,6 @@ def _get_latest_release_tag(org, repo):
998998
f"Error getting latest release tag for {org}/{repo}: {e}", file=sys.stderr
999999
)
10001000
print("Reverting to using latest local git tag...", file=sys.stderr)
1001-
out = run("git describe --tags".split(), stdout=PIPE)
1001+
out = run(["git", "describe", "--tags"], stdout=PIPE, check=False)
10021002
tag = out.stdout.decode().rsplit("-", 2)[0]
10031003
return tag

0 commit comments

Comments
 (0)