Skip to content

Commit 67ad197

Browse files
authored
Merge pull request #93 from manics/dot-config
Load argument defaults from `.githubactivity.json`
2 parents ec172e8 + e9bfe5e commit 67ad197

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

github_activity/cli.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
import argparse
2+
import json
23
import os
34
import sys
45
from subprocess import PIPE
56
from subprocess import run
67

78
from .git import _git_installed_check
9+
from .git import _git_toplevel_path
810
from .github_activity import _parse_target
911
from .github_activity import generate_activity_md
1012
from .github_activity import generate_all_activity_md
1113

1214
DESCRIPTION = "Generate a markdown changelog of GitHub activity within a date window."
15+
16+
# These defaults are managed by load_config_and_defaults so that they can be
17+
# overridden by a config file
18+
ARG_DEFAULTS = {
19+
"heading-level": 1,
20+
"include-issues": False,
21+
"include-opened": False,
22+
"strip-brackets": False,
23+
"all": False,
24+
}
25+
1326
parser = argparse.ArgumentParser(description=DESCRIPTION)
1427
parser.add_argument(
1528
"-t",
@@ -71,19 +84,19 @@
7184
)
7285
parser.add_argument(
7386
"--include-issues",
74-
default=False,
87+
default=None,
7588
action="store_true",
7689
help="Include Issues in the markdown output",
7790
)
7891
parser.add_argument(
7992
"--include-opened",
80-
default=False,
93+
default=None,
8194
action="store_true",
8295
help="Include a list of opened items in the markdown output",
8396
)
8497
parser.add_argument(
8598
"--strip-brackets",
86-
default=False,
99+
default=None,
87100
action="store_true",
88101
help=(
89102
"If True, strip any text between brackets at the beginning of the issue/PR title. "
@@ -92,7 +105,7 @@
92105
)
93106
parser.add_argument(
94107
"--heading-level",
95-
default=1,
108+
default=None,
96109
type=int,
97110
help=(
98111
"""Base heading level to add when generating markdown.
@@ -113,7 +126,7 @@
113126
)
114127
parser.add_argument(
115128
"--all",
116-
default=False,
129+
default=None,
117130
action="store_true",
118131
help=("""Whether to include all the GitHub tags"""),
119132
)
@@ -127,6 +140,29 @@
127140
)
128141

129142

143+
def load_config_and_defaults(args):
144+
"""
145+
Load .githubactivity.json from the Git top-level directory,
146+
override unset args with values from .githubactivity.json,
147+
and set defaults for remaining args.
148+
"""
149+
config = {}
150+
git_toplevel = _git_toplevel_path()
151+
if git_toplevel:
152+
try:
153+
with open(os.path.join(git_toplevel, ".githubactivity.json")) as f:
154+
config = json.load(f)
155+
except FileNotFoundError:
156+
pass
157+
158+
# Treat args as a dict
159+
# https://docs.python.org/3/library/argparse.html#the-namespace-object
160+
for argname in vars(args):
161+
configname = argname.replace("_", "-")
162+
if getattr(args, argname) is None:
163+
setattr(args, argname, config.get(configname, ARG_DEFAULTS.get(configname)))
164+
165+
130166
def main():
131167
if not _git_installed_check():
132168
print("git is required to run github-activity", file=sys.stderr)
@@ -140,6 +176,8 @@ def main():
140176
if not args.target:
141177
args.target = args._target
142178

179+
load_config_and_defaults(args)
180+
143181
tags = args.tags.split(",") if args.tags is not None else args.tags
144182
# Automatically detect the target from remotes if we haven't had one passed.
145183
if not args.target:

github_activity/git.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ def _git_installed_check():
88
return True
99
except subprocess.CalledProcessError:
1010
return False
11+
12+
13+
def _git_toplevel_path():
14+
"""Fetch the top-level of the local Git repository"""
15+
cmd = ["git", "rev-parse", "--show-toplevel"]
16+
try:
17+
top = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
18+
return top.strip().decode()
19+
except subprocess.CalledProcessError:
20+
return None
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"heading-level": 5
3+
}

tests/test_cli.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import shutil
13
from pathlib import Path
24
from subprocess import run
35

@@ -43,6 +45,41 @@ def test_cli(tmpdir, file_regression, cmd, basename):
4345
file_regression.check(md, basename=basename, extension=".md")
4446

4547

48+
def test_cli_dot_config(tmp_path, monkeypatch, file_regression):
49+
"""Test that pyproject.toml config is loaded"""
50+
cmd = "github-activity -s 2019-09-01 -u 2019-11-01 -o {path_output}"
51+
basename = "cli_no_target_pyproject"
52+
53+
path_output = tmp_path / "out.md"
54+
55+
# We need to augment the repository with a custom .githubactivity.json
56+
# but since a local git repo is only needed to get the origin a shallow
57+
# clone is enough
58+
run(
59+
[
60+
"git",
61+
"clone",
62+
"--depth=1",
63+
"https://github.com/executablebooks/github-activity",
64+
str(tmp_path / "repo"),
65+
],
66+
check=True,
67+
)
68+
tests_dir = Path(__file__).parent
69+
shutil.copyfile(
70+
str(tests_dir / "resources" / "cli_no_target.githubactivity.json"),
71+
str(tmp_path / "repo" / ".githubactivity.json"),
72+
)
73+
74+
# cd into a subdirectory so we test the lookup of .githubactivity.json
75+
monkeypatch.chdir(tmp_path / "repo" / "tests")
76+
77+
command = cmd.format(path_output=path_output)
78+
run(command.split(), check=True)
79+
md = path_output.read_text()
80+
file_regression.check(md, basename=basename, extension=".md")
81+
82+
4683
def test_cli_nonexistent_branch(tmpdir):
4784
path_tmp = Path(tmpdir)
4885
path_output = path_tmp.joinpath("out.md")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
##### master@{2019-09-01}...master@{2019-11-01}
2+
3+
([full changelog](https://github.com/executablebooks/github-activity/compare/479cc4b2f5504945021e3c4ee84818a10fabf810...ed7f1ed78b523c6b9fe6b3ac29e834087e299296))
4+
5+
###### Merged PRs
6+
7+
- defining contributions [#14](https://github.com/executablebooks/github-activity/pull/14) ([@choldgraf](https://github.com/choldgraf), [@betatim](https://github.com/betatim))
8+
- updating CLI for new tags [#12](https://github.com/executablebooks/github-activity/pull/12) ([@choldgraf](https://github.com/choldgraf))
9+
- fixing link to changelog with refs [#11](https://github.com/executablebooks/github-activity/pull/11) ([@choldgraf](https://github.com/choldgraf))
10+
- adding contributors list [#10](https://github.com/executablebooks/github-activity/pull/10) ([@choldgraf](https://github.com/choldgraf), [@consideRatio](https://github.com/consideRatio))
11+
- some improvements to `since` and opened issues list [#8](https://github.com/executablebooks/github-activity/pull/8) ([@choldgraf](https://github.com/choldgraf), [@consideRatio](https://github.com/consideRatio))
12+
- Support git references etc. [#6](https://github.com/executablebooks/github-activity/pull/6) ([@consideRatio](https://github.com/consideRatio), [@choldgraf](https://github.com/choldgraf))
13+
- adding authentication information [#2](https://github.com/executablebooks/github-activity/pull/2) ([@choldgraf](https://github.com/choldgraf))
14+
- Mention the required GITHUB_ACCESS_TOKEN [#1](https://github.com/executablebooks/github-activity/pull/1) ([@consideRatio](https://github.com/consideRatio), [@choldgraf](https://github.com/choldgraf))
15+
16+
###### Contributors to this release
17+
18+
The following people contributed discussions, new ideas, code and documentation contributions, and review.
19+
See [our definition of contributors](https://github-activity.readthedocs.io/en/latest/#how-does-this-tool-define-contributions-in-the-reports).
20+
21+
([GitHub contributors page for this release](https://github.com/executablebooks/github-activity/graphs/contributors?from=2019-09-01&to=2019-11-01&type=c))
22+
23+
@betatim ([activity](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Abetatim+updated%3A2019-09-01..2019-11-01&type=Issues)) | @choldgraf ([activity](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3Acholdgraf+updated%3A2019-09-01..2019-11-01&type=Issues)) | @consideRatio ([activity](https://github.com/search?q=repo%3Aexecutablebooks%2Fgithub-activity+involves%3AconsideRatio+updated%3A2019-09-01..2019-11-01&type=Issues))

0 commit comments

Comments
 (0)