Skip to content

Commit 93312f1

Browse files
committed
Auto-detecting the target
1 parent 5f8a4fc commit 93312f1

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

github_activity/cli.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import argparse
22
import os
33
import sys
4+
from subprocess import run, PIPE
45

56
from .github_activity import generate_activity_md
67
from .git import _git_installed_check
78

89
DESCRIPTION = "Generate a markdown changelog of GitHub activity within a date window."
910
parser = argparse.ArgumentParser(description=DESCRIPTION)
1011
parser.add_argument(
11-
"target",
12+
"-t",
13+
"--target",
14+
nargs="?",
15+
default=None,
1216
help="""The GitHub organization/repo for which you want to grab recent issues/PRs.
1317
Can either be *just* an organization (e.g., `jupyter`), or a combination
1418
organization and repo (e.g., `jupyter/notebook`). If the former, all
1519
repositories for that org will be used. If the latter, only the specified
16-
repository will be used. Can also be a GitHub URL to an organization or repo.""",
20+
repository will be used. Can also be a GitHub URL to an organization or repo. If
21+
None, the org/repo will attempt to be inferred from `git remote -v`.""",
1722
)
1823
parser.add_argument(
1924
"-s",
@@ -111,8 +116,34 @@ def main():
111116
print("git is required to run github-activity")
112117
sys.exit(1)
113118

114-
args = parser.parse_args(sys.argv[1:])
119+
args, unknown = parser.parse_known_args()
120+
# If we have unknown, it is the target
121+
# TODO: this feels sub-optimal, we should be able to just treat positional args
122+
# as optional.
123+
if unknown and not args.target:
124+
args.target = unknown[0]
125+
115126
tags = args.tags.split(",") if args.tags is not None else args.tags
127+
# Automatically detect the target from remotes if we haven't had one passed.
128+
if not args.target:
129+
err = "Could not automatically detect remote, and none was given."
130+
try:
131+
out = run("git remote -v".split(), stdout=PIPE)
132+
remotes = out.stdout.decode().split('\n')
133+
remotes = [ii for ii in remotes if ii]
134+
remotes = {ii.split("\t")[0]: ii.split("\t")[1].split()[0] for ii in remotes}
135+
if "upstream" in remotes:
136+
ref = remotes["upstream"]
137+
elif "origin" in remotes:
138+
ref = remotes["origin"]
139+
else:
140+
ref = None
141+
if not ref:
142+
raise ValueError(err)
143+
args.target = ref.split("/", 3)[-1]
144+
except Exception:
145+
raise ValueError(err)
146+
116147
md = generate_activity_md(
117148
args.target,
118149
since=args.since,

github_activity/github_activity.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import urllib
88
from pathlib import Path
9+
from subprocess import run, PIPE
910

1011
from .graphql import GitHubGraphQlQuery
1112
from .cache import _cache_data
@@ -530,8 +531,6 @@ def _get_datetime_from_git_ref(org, repo, ref):
530531

531532
def _get_latest_tag(org, repo):
532533
"""Return the latest tag name for a given repository."""
533-
response = requests.get(f"https://api.github.com/repos/{org}/{repo}/git/refs/tags")
534-
response.raise_for_status()
535-
tags = response.json()
536-
latest_tag = list(tags)[-1]
537-
return latest_tag["ref"].split("/tags/")[-1]
534+
out = run("git describe --tags".split(), stdout=PIPE)
535+
tag = out.stdout.decode().rsplit('-', 2)[0]
536+
return tag

0 commit comments

Comments
 (0)