|
1 | 1 | import argparse |
2 | 2 | import os |
3 | 3 | import sys |
| 4 | +from subprocess import run, PIPE |
4 | 5 |
|
5 | 6 | from .github_activity import generate_activity_md |
6 | 7 | from .git import _git_installed_check |
7 | 8 |
|
8 | 9 | DESCRIPTION = "Generate a markdown changelog of GitHub activity within a date window." |
9 | 10 | parser = argparse.ArgumentParser(description=DESCRIPTION) |
10 | 11 | parser.add_argument( |
11 | | - "target", |
| 12 | + "-t", |
| 13 | + "--target", |
| 14 | + nargs="?", |
| 15 | + default=None, |
12 | 16 | help="""The GitHub organization/repo for which you want to grab recent issues/PRs. |
13 | 17 | Can either be *just* an organization (e.g., `jupyter`), or a combination |
14 | 18 | organization and repo (e.g., `jupyter/notebook`). If the former, all |
15 | 19 | 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`.""", |
17 | 22 | ) |
18 | 23 | parser.add_argument( |
19 | 24 | "-s", |
@@ -111,8 +116,34 @@ def main(): |
111 | 116 | print("git is required to run github-activity") |
112 | 117 | sys.exit(1) |
113 | 118 |
|
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 | + |
115 | 126 | 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 | + |
116 | 147 | md = generate_activity_md( |
117 | 148 | args.target, |
118 | 149 | since=args.since, |
|
0 commit comments