codeownerinsights 0.3.1__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/PKG-INFO +1 -1
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/cli.py +141 -14
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/git.py +32 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/pyproject.toml +1 -1
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/LICENSE +0 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/README-pypi.md +0 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/__init__.py +0 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/__main__.py +0 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/codeowners.py +0 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/coercion.py +0 -0
- {codeownerinsights-0.3.1 → codeownerinsights-0.4.0}/codeownerinsights/status_text.py +0 -0
|
@@ -3,7 +3,7 @@ import json
|
|
|
3
3
|
import os
|
|
4
4
|
import sys
|
|
5
5
|
from pathlib import Path, PurePath
|
|
6
|
-
from typing import Dict, Iterable, List, Optional, Sequence, TextIO
|
|
6
|
+
from typing import Dict, Iterable, List, Mapping, Optional, Sequence, TextIO, Tuple
|
|
7
7
|
|
|
8
8
|
from .codeowners import (
|
|
9
9
|
CodeOwnerSpecification,
|
|
@@ -16,6 +16,8 @@ from .git import (
|
|
|
16
16
|
exec_command,
|
|
17
17
|
get_git_changed_files_compared_to_branch,
|
|
18
18
|
get_git_changed_files_compared_to_default_branch,
|
|
19
|
+
get_git_staged_files,
|
|
20
|
+
get_git_unstaged_files,
|
|
19
21
|
)
|
|
20
22
|
|
|
21
23
|
PR_DIFF_FILTER = 'ACMRD'
|
|
@@ -30,6 +32,19 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
30
32
|
"default branch, which are the files GitHub requests a pull request's reviewers for."
|
|
31
33
|
),
|
|
32
34
|
)
|
|
35
|
+
parser.add_argument(
|
|
36
|
+
'--staged',
|
|
37
|
+
action='store_true',
|
|
38
|
+
help='use the files changed in the index instead of the branch changes',
|
|
39
|
+
)
|
|
40
|
+
parser.add_argument(
|
|
41
|
+
'--unstaged',
|
|
42
|
+
action='store_true',
|
|
43
|
+
help=(
|
|
44
|
+
'use the files changed in the working tree, and untracked files, instead of the '
|
|
45
|
+
'branch changes; combine with --staged for every uncommitted change'
|
|
46
|
+
),
|
|
47
|
+
)
|
|
33
48
|
parser.add_argument(
|
|
34
49
|
'paths',
|
|
35
50
|
nargs='*',
|
|
@@ -54,6 +69,24 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
54
69
|
action='store_true',
|
|
55
70
|
help='also list every rule that matches each file, not only the one that applies',
|
|
56
71
|
)
|
|
72
|
+
parser.add_argument(
|
|
73
|
+
'--group-by',
|
|
74
|
+
choices=('file', 'owner'),
|
|
75
|
+
default='file',
|
|
76
|
+
help=(
|
|
77
|
+
'text output only: list one file per line (file, the default), or list the files '
|
|
78
|
+
'under each set of owners that applies to them (owner)'
|
|
79
|
+
),
|
|
80
|
+
)
|
|
81
|
+
parser.add_argument(
|
|
82
|
+
'--color',
|
|
83
|
+
choices=('auto', 'always', 'never'),
|
|
84
|
+
default='auto',
|
|
85
|
+
help=(
|
|
86
|
+
'colour text output: always, never, or auto (the default) when stdout is a terminal '
|
|
87
|
+
'and NO_COLOR is not set'
|
|
88
|
+
),
|
|
89
|
+
)
|
|
57
90
|
parser.add_argument('--json', action='store_true', help='print JSON instead of text')
|
|
58
91
|
parser.add_argument('--git', default=DEFAULT_GIT, help='git executable (default: git)')
|
|
59
92
|
return parser
|
|
@@ -71,9 +104,30 @@ def _repo_relative(path: str, cwd: Path, repo_root: Path) -> Path:
|
|
|
71
104
|
return Path(os.path.relpath(os.path.normpath(str(absolute)), str(repo_root)))
|
|
72
105
|
|
|
73
106
|
|
|
107
|
+
def _check_sources(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None:
|
|
108
|
+
working_tree = [
|
|
109
|
+
flag for flag, on in (('--staged', args.staged), ('--unstaged', args.unstaged)) if on
|
|
110
|
+
]
|
|
111
|
+
if not working_tree:
|
|
112
|
+
return
|
|
113
|
+
if args.paths:
|
|
114
|
+
parser.error(f'{working_tree[0]} cannot be combined with paths')
|
|
115
|
+
if args.base or args.no_fetch:
|
|
116
|
+
other = '--base' if args.base else '--no-fetch'
|
|
117
|
+
parser.error(f'{working_tree[0]} cannot be combined with {other}')
|
|
118
|
+
|
|
119
|
+
|
|
74
120
|
def _collect_paths(
|
|
75
121
|
args: argparse.Namespace, cwd: Path, repo_root: Path, stdin: TextIO
|
|
76
122
|
) -> List[Path]:
|
|
123
|
+
if args.staged or args.unstaged:
|
|
124
|
+
changed: List[Path] = []
|
|
125
|
+
if args.staged:
|
|
126
|
+
changed += get_git_staged_files(repo_root, git=args.git, diff_filter=PR_DIFF_FILTER)
|
|
127
|
+
if args.unstaged:
|
|
128
|
+
changed += get_git_unstaged_files(repo_root, git=args.git, diff_filter=PR_DIFF_FILTER)
|
|
129
|
+
return sorted(set(changed))
|
|
130
|
+
|
|
77
131
|
if args.paths:
|
|
78
132
|
raw: List[str] = []
|
|
79
133
|
for path in args.paths:
|
|
@@ -140,28 +194,94 @@ def _describe_rule(rule: Dict[str, object]) -> str:
|
|
|
140
194
|
return f'{rule["codeowners_file"]}:{rule["line"]} {rule["pattern"]}'
|
|
141
195
|
|
|
142
196
|
|
|
143
|
-
|
|
197
|
+
class _Style:
|
|
198
|
+
def __init__(self, enabled: bool) -> None:
|
|
199
|
+
self.enabled = enabled
|
|
200
|
+
|
|
201
|
+
def _wrap(self, code: str, text: str) -> str:
|
|
202
|
+
return f'\033[{code}m{text}\033[0m' if self.enabled and text else text
|
|
203
|
+
|
|
204
|
+
def owners(self, text: str) -> str:
|
|
205
|
+
return self._wrap('1;36', text)
|
|
206
|
+
|
|
207
|
+
def rule(self, text: str) -> str:
|
|
208
|
+
return self._wrap('2', text)
|
|
209
|
+
|
|
210
|
+
def unowned(self, text: str) -> str:
|
|
211
|
+
return self._wrap('33', text)
|
|
212
|
+
|
|
213
|
+
def heading(self, text: str) -> str:
|
|
214
|
+
return self._wrap('1', text)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _use_color(choice: str, stdout: TextIO, environ: Mapping[str, str]) -> bool:
|
|
218
|
+
if choice != 'auto':
|
|
219
|
+
return choice == 'always'
|
|
220
|
+
isatty = getattr(stdout, 'isatty', None)
|
|
221
|
+
return bool(isatty and isatty()) and not environ.get('NO_COLOR')
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _files(count: int) -> str:
|
|
225
|
+
return f'{count} file{"s" if count != 1 else ""}'
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _print_matching_rules(
|
|
229
|
+
result: Dict[str, object], indent: str, style: _Style, out: TextIO
|
|
230
|
+
) -> None:
|
|
231
|
+
for matching in result['matching_rules']: # type: ignore[union-attr]
|
|
232
|
+
line = f'{_describe_rule(matching)} -> {" ".join(matching["owners"])}'
|
|
233
|
+
if matching == result['rule']:
|
|
234
|
+
out.write(f'{indent}* {line}\n')
|
|
235
|
+
else:
|
|
236
|
+
out.write(f'{indent} {style.rule(line)}\n')
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _print_text(
|
|
240
|
+
results: List[Dict[str, object]], all_rules: bool, style: _Style, out: TextIO
|
|
241
|
+
) -> None:
|
|
144
242
|
for result in results:
|
|
145
243
|
rule = result['rule']
|
|
146
244
|
if rule is None:
|
|
147
|
-
out.write(f'{result["path"]}\t(no owner)\n')
|
|
245
|
+
out.write(f'{result["path"]}\t{style.unowned("(no owner)")}\n')
|
|
148
246
|
else:
|
|
149
|
-
owners = ' '.join(result['owners']) # type: ignore[arg-type]
|
|
150
|
-
out.write(f'{result["path"]}\t{owners}\t{_describe_rule(rule)}\n') # type: ignore[arg-type]
|
|
247
|
+
owners = style.owners(' '.join(result['owners'])) # type: ignore[arg-type]
|
|
248
|
+
out.write(f'{result["path"]}\t{owners}\t{style.rule(_describe_rule(rule))}\n') # type: ignore[arg-type]
|
|
151
249
|
if all_rules:
|
|
152
|
-
|
|
153
|
-
marker = '*' if matching == rule else ' '
|
|
154
|
-
owners = ' '.join(matching['owners'])
|
|
155
|
-
out.write(f' {marker} {_describe_rule(matching)} -> {owners}\n')
|
|
250
|
+
_print_matching_rules(result, ' ', style, out)
|
|
156
251
|
|
|
157
252
|
summary = _owners_summary(results)
|
|
158
253
|
if summary:
|
|
159
|
-
out.write('\
|
|
254
|
+
out.write(f'\n{style.heading("Owners:")}\n')
|
|
160
255
|
for owner, paths in summary.items():
|
|
161
|
-
out.write(f' {owner} ({
|
|
256
|
+
out.write(f' {style.owners(owner)} ({_files(len(paths))})\n')
|
|
162
257
|
unowned = [result['path'] for result in results if result['rule'] is None]
|
|
163
258
|
if unowned:
|
|
164
|
-
out.write(f'\
|
|
259
|
+
out.write(f'\n{style.unowned("No owner:")} {_files(len(unowned))}\n')
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _print_text_by_owner(
|
|
263
|
+
results: List[Dict[str, object]], all_rules: bool, style: _Style, out: TextIO
|
|
264
|
+
) -> None:
|
|
265
|
+
groups: Dict[Tuple[str, ...], List[Dict[str, object]]] = {}
|
|
266
|
+
for result in results:
|
|
267
|
+
groups.setdefault(tuple(result['owners']), []).append(result) # type: ignore[arg-type]
|
|
268
|
+
width = max(len(str(result['path'])) for result in results)
|
|
269
|
+
|
|
270
|
+
for index, owners in enumerate(sorted(groups, key=lambda owners: (not owners, owners))):
|
|
271
|
+
members = groups[owners]
|
|
272
|
+
heading = style.owners(' '.join(owners)) if owners else style.unowned('(no owner)')
|
|
273
|
+
if index:
|
|
274
|
+
out.write('\n')
|
|
275
|
+
out.write(f'{heading} ({_files(len(members))})\n')
|
|
276
|
+
for result in members:
|
|
277
|
+
rule = result['rule']
|
|
278
|
+
if rule is None:
|
|
279
|
+
out.write(f' {result["path"]}\n')
|
|
280
|
+
else:
|
|
281
|
+
path = f'{str(result["path"]):<{width}}'
|
|
282
|
+
out.write(f' {path} {style.rule(_describe_rule(rule))}\n') # type: ignore[arg-type]
|
|
283
|
+
if all_rules:
|
|
284
|
+
_print_matching_rules(result, ' ', style, out)
|
|
165
285
|
|
|
166
286
|
|
|
167
287
|
def main(
|
|
@@ -170,8 +290,11 @@ def main(
|
|
|
170
290
|
stdout: TextIO = sys.stdout,
|
|
171
291
|
stderr: TextIO = sys.stderr,
|
|
172
292
|
cwd: Optional[Path] = None,
|
|
293
|
+
environ: Optional[Mapping[str, str]] = None,
|
|
173
294
|
) -> int:
|
|
174
|
-
|
|
295
|
+
parser = _build_parser()
|
|
296
|
+
args = parser.parse_args(argv)
|
|
297
|
+
_check_sources(parser, args)
|
|
175
298
|
cwd = (cwd or Path.cwd()).resolve()
|
|
176
299
|
repo_root = (args.repo if args.repo.is_absolute() else cwd / args.repo) if args.repo else None
|
|
177
300
|
repo_root = (repo_root or _find_repo_root(cwd, args.git)).resolve()
|
|
@@ -196,6 +319,10 @@ def main(
|
|
|
196
319
|
if args.json:
|
|
197
320
|
json.dump({'files': results, 'owners': _owners_summary(results)}, stdout, indent=2)
|
|
198
321
|
stdout.write('\n')
|
|
322
|
+
elif results:
|
|
323
|
+
style = _Style(_use_color(args.color, stdout, os.environ if environ is None else environ))
|
|
324
|
+
printer = _print_text_by_owner if args.group_by == 'owner' else _print_text
|
|
325
|
+
printer(results, args.all_rules, style, stdout)
|
|
199
326
|
else:
|
|
200
|
-
|
|
327
|
+
stderr.write('No files to resolve\n')
|
|
201
328
|
return 0
|
|
@@ -78,6 +78,38 @@ def get_git_changed_files_compared_to_default_branch(
|
|
|
78
78
|
)
|
|
79
79
|
|
|
80
80
|
|
|
81
|
+
def _null_separated_paths(summary: ShellExecutionSummary) -> List[Path]:
|
|
82
|
+
if not summary or summary.process.returncode != 0:
|
|
83
|
+
return []
|
|
84
|
+
return [Path(file) for file in summary.process.stdout.split('\0') if file]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def get_git_staged_files(
|
|
88
|
+
folder_path: Path, git: str = DEFAULT_GIT, diff_filter: str = 'ACMR'
|
|
89
|
+
) -> List[Path]:
|
|
90
|
+
return _null_separated_paths(
|
|
91
|
+
exec_command(
|
|
92
|
+
folder_path,
|
|
93
|
+
_git_cmd(git, 'diff', '--cached', '--name-only', '-z', f'--diff-filter={diff_filter}'),
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def get_git_unstaged_files(
|
|
99
|
+
folder_path: Path, git: str = DEFAULT_GIT, diff_filter: str = 'ACMR'
|
|
100
|
+
) -> List[Path]:
|
|
101
|
+
"""Return tracked files changed in the working tree, then untracked files that are not ignored."""
|
|
102
|
+
tracked = _null_separated_paths(
|
|
103
|
+
exec_command(
|
|
104
|
+
folder_path, _git_cmd(git, 'diff', '--name-only', '-z', f'--diff-filter={diff_filter}')
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
untracked = _null_separated_paths(
|
|
108
|
+
exec_command(folder_path, _git_cmd(git, 'ls-files', '--others', '--exclude-standard', '-z'))
|
|
109
|
+
)
|
|
110
|
+
return tracked + untracked
|
|
111
|
+
|
|
112
|
+
|
|
81
113
|
def get_default_branch(folder_path: Path, git: str = DEFAULT_GIT) -> Optional[str]:
|
|
82
114
|
result = exec_command(folder_path, _git_cmd(git, 'rev-parse', '--abbrev-ref', 'origin/HEAD'))
|
|
83
115
|
if result and result.process.returncode == 0:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "codeownerinsights"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
4
4
|
description = "Parse GitHub's CODEOWNERS file and resolve code owners for a path, with optional git integration."
|
|
5
5
|
authors = [{ name = "Keith Hall", email = "keith.hall@available.systems" }]
|
|
6
6
|
license = "MIT"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|