lupaxa-github-intel 0.1.0__py3-none-any.whl

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.
Files changed (41) hide show
  1. lupaxa/github_intel/__init__.py +13 -0
  2. lupaxa/github_intel/__main__.py +8 -0
  3. lupaxa/github_intel/app.py +493 -0
  4. lupaxa/github_intel/checks/__init__.py +1 -0
  5. lupaxa/github_intel/checks/base.py +17 -0
  6. lupaxa/github_intel/checks/releases.py +59 -0
  7. lupaxa/github_intel/checks/workflows.py +34 -0
  8. lupaxa/github_intel/cli.py +699 -0
  9. lupaxa/github_intel/config/__init__.py +1 -0
  10. lupaxa/github_intel/config/defaults.py +150 -0
  11. lupaxa/github_intel/config/editor.py +316 -0
  12. lupaxa/github_intel/config/loader.py +412 -0
  13. lupaxa/github_intel/config/models.py +310 -0
  14. lupaxa/github_intel/config/paths.py +84 -0
  15. lupaxa/github_intel/config/profiles.py +143 -0
  16. lupaxa/github_intel/constants.py +54 -0
  17. lupaxa/github_intel/dates.py +179 -0
  18. lupaxa/github_intel/errors.py +72 -0
  19. lupaxa/github_intel/github/__init__.py +1 -0
  20. lupaxa/github_intel/github/auth.py +40 -0
  21. lupaxa/github_intel/github/client.py +239 -0
  22. lupaxa/github_intel/github/rate_limit.py +107 -0
  23. lupaxa/github_intel/layout.py +141 -0
  24. lupaxa/github_intel/logging_setup.py +51 -0
  25. lupaxa/github_intel/present.py +361 -0
  26. lupaxa/github_intel/reports/__init__.py +1 -0
  27. lupaxa/github_intel/reports/render.py +113 -0
  28. lupaxa/github_intel/storage/__init__.py +1 -0
  29. lupaxa/github_intel/storage/database.py +419 -0
  30. lupaxa/github_intel/sync/__init__.py +1 -0
  31. lupaxa/github_intel/sync/engine.py +671 -0
  32. lupaxa/github_intel/sync/summary.py +308 -0
  33. lupaxa/github_intel/tui/__init__.py +1 -0
  34. lupaxa/github_intel/tui/application.py +487 -0
  35. lupaxa/github_intel/views.py +896 -0
  36. lupaxa_github_intel-0.1.0.dist-info/METADATA +180 -0
  37. lupaxa_github_intel-0.1.0.dist-info/RECORD +41 -0
  38. lupaxa_github_intel-0.1.0.dist-info/WHEEL +5 -0
  39. lupaxa_github_intel-0.1.0.dist-info/entry_points.txt +2 -0
  40. lupaxa_github_intel-0.1.0.dist-info/licenses/LICENCE +21 -0
  41. lupaxa_github_intel-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,150 @@
1
+ """
2
+ Default configuration files created the first time a repository is added.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from pathlib import Path
8
+
9
+ __all__ = [
10
+ "generate_default_files",
11
+ "write_default_files",
12
+ ]
13
+
14
+ _CONFIG = """\
15
+ # GitHub Intel configuration.
16
+ # Authentication uses the GitHub CLI login (`gh auth token`), the same
17
+ # GitHub account used for SSH. Tokens are not stored in this file.
18
+
19
+ version: 1
20
+
21
+ github:
22
+ api_url: https://api.github.com
23
+
24
+ files:
25
+ profiles: profiles.yml
26
+ repositories: repositories.yml
27
+
28
+ defaults:
29
+ profile: upstream
30
+ releases: false
31
+ workflows: false
32
+ issues: false
33
+ pull_requests: true
34
+ quality: true
35
+
36
+ display:
37
+ date_format: DD/MM/YYYY HH:mm UTC
38
+ view: default
39
+ views:
40
+ popularity:
41
+ list:
42
+ - Organisation
43
+ - Repository
44
+ - Stars
45
+ - Watchers
46
+ - Forks
47
+
48
+ sync:
49
+ concurrency: 10
50
+ timeout: 15
51
+ retries: 3
52
+ interval: 0
53
+ cache:
54
+ enabled: true
55
+ metadata_ttl: 3600
56
+ releases_ttl: 1800
57
+ workflows_ttl: 900
58
+ issues_ttl: 1800
59
+
60
+ logging:
61
+ level: INFO
62
+ """
63
+
64
+ _PROFILES = """\
65
+ profiles:
66
+ base:
67
+ releases: true
68
+ workflows: false
69
+ issues: false
70
+
71
+ maintained:
72
+ extends: base
73
+ workflows: true
74
+ issues: true
75
+
76
+ upstream:
77
+ extends: base
78
+
79
+ watchlist:
80
+ extends: upstream
81
+ issues: true
82
+
83
+ critical:
84
+ extends: maintained
85
+ """
86
+
87
+ _REPOSITORIES = """\
88
+ organisations: []
89
+ """
90
+
91
+
92
+ def write_default_files(directory: Path) -> Path:
93
+ """
94
+ Create the default YAML files when the directory has no config.yml.
95
+
96
+ Args:
97
+ directory: Configuration directory.
98
+
99
+ Returns:
100
+ Path to ``config.yml``. Existing files are left unchanged.
101
+
102
+ """
103
+
104
+ config = directory / "config.yml"
105
+ generate_default_files(
106
+ (config, directory / "profiles.yml", directory / "repositories.yml"),
107
+ force=False,
108
+ )
109
+ return config
110
+
111
+
112
+ def generate_default_files(
113
+ paths: tuple[Path, Path, Path],
114
+ *,
115
+ force: bool,
116
+ ) -> tuple[tuple[Path, ...], tuple[Path, ...]]:
117
+ """
118
+ Write the shipped config, profiles, and repositories files.
119
+
120
+ Args:
121
+ paths: ``config.yml``, ``profiles.yml``, and ``repositories.yml``.
122
+ force: Replace files that already exist.
123
+
124
+ Returns:
125
+ The paths written, then the paths left unchanged.
126
+
127
+ """
128
+
129
+ directory = paths[0].parent
130
+ created = not directory.exists()
131
+ directory.mkdir(parents=True, exist_ok=True)
132
+ if created:
133
+ _restrict(directory)
134
+ written: list[Path] = []
135
+ kept: list[Path] = []
136
+ for path, text in zip(paths, (_CONFIG, _PROFILES, _REPOSITORIES), strict=True):
137
+ if path.exists() and not force:
138
+ kept.append(path)
139
+ continue
140
+ path.write_text(text, encoding="utf-8")
141
+ _restrict(path)
142
+ written.append(path)
143
+ return tuple(written), tuple(kept)
144
+
145
+
146
+ def _restrict(path: Path) -> None:
147
+ if path.is_dir():
148
+ path.chmod(0o700)
149
+ else:
150
+ path.chmod(0o600)
@@ -0,0 +1,316 @@
1
+ """
2
+ Atomic edits of the repository file.
3
+
4
+ YAML comments, key order, and unrelated content are preserved. The original
5
+ file is replaced only after the edited document validates. A file that
6
+ changes under us is left untouched.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from collections.abc import Callable
12
+ import json
13
+ import os
14
+ from pathlib import Path
15
+ from typing import Any, cast
16
+
17
+ from pydantic import ValidationError
18
+ from ruamel.yaml import YAML
19
+ from ruamel.yaml.comments import CommentedMap, CommentedSeq
20
+
21
+ from lupaxa.github_intel.config.loader import (
22
+ LoadedConfig,
23
+ load_config,
24
+ repository_key,
25
+ validate_repository_file,
26
+ )
27
+ from lupaxa.github_intel.config.models import RepositorySpec
28
+ from lupaxa.github_intel.errors import ConfigError, NotFound
29
+
30
+ __all__ = [
31
+ "add_repository",
32
+ "remove_repository",
33
+ "set_repository_fields",
34
+ ]
35
+
36
+
37
+ def add_repository(
38
+ config_path: Path,
39
+ repo: str,
40
+ *,
41
+ group: str | None = None,
42
+ profile: str | None = None,
43
+ tags: list[str] | None = None,
44
+ ) -> None:
45
+ """
46
+ Append one repository if it is not already configured.
47
+
48
+ Args:
49
+ config_path: Main configuration file.
50
+ repo: ``owner/name`` identifier.
51
+ group: Optional group. Omitted uses ``ungrouped``.
52
+ profile: Optional profile. Omitted uses the configured default.
53
+ tags: Optional tags.
54
+
55
+ Raises:
56
+ ConfigError: The identifier, profile, or resulting file is invalid.
57
+ NotFound: Never. Raised by the other editors.
58
+
59
+ """
60
+
61
+ def mutate(owners: CommentedSeq | list[Any]) -> None:
62
+ owner, name = _split(repo)
63
+ _reject_duplicate(owners, f"{owner}/{name}")
64
+ block = _find_owner(owners, owner)
65
+ if block is None:
66
+ block = _new_owner(owner, group, profile)
67
+ owners.append(block)
68
+ listed = block["repositories"]
69
+ else:
70
+ listed = _repository_list(block)
71
+ listed.append(_entry(name, block, group, profile, tags))
72
+
73
+ _edit(config_path, mutate)
74
+
75
+
76
+ def remove_repository(config_path: Path, repo: str) -> None:
77
+ """
78
+ Remove one repository from configuration. Cache rows are left in place.
79
+
80
+ Args:
81
+ config_path: Main configuration file.
82
+ repo: ``owner/name`` identifier.
83
+
84
+ Raises:
85
+ NotFound: The repository is not configured.
86
+
87
+ """
88
+
89
+ def mutate(owners: CommentedSeq | list[Any]) -> None:
90
+ owner, name = _split(repo)
91
+ block, index = _find(owners, owner, name)
92
+ del _repository_list(block)[index]
93
+ if len(_repository_list(block)) == 0:
94
+ owners.remove(block)
95
+
96
+ _edit(config_path, mutate)
97
+
98
+
99
+ def set_repository_fields(
100
+ config_path: Path,
101
+ repo: str,
102
+ **fields: bool | str | None,
103
+ ) -> None:
104
+ """
105
+ Set fields on one configured repository.
106
+
107
+ A string entry is promoted to an object when a field must be stored.
108
+
109
+ Args:
110
+ config_path: Main configuration file.
111
+ repo: ``owner/name`` identifier.
112
+ fields: Keys such as ``enabled``, ``profile``, or ``group``.
113
+
114
+ Raises:
115
+ NotFound: The repository is not configured.
116
+ ConfigError: The edited file does not validate.
117
+
118
+ """
119
+
120
+ def mutate(owners: CommentedSeq | list[Any]) -> None:
121
+ owner, name = _split(repo)
122
+ block, index = _find(owners, owner, name)
123
+ listed = _repository_list(block)
124
+ listed[index] = _with_fields(_as_mapping(listed[index], name), fields)
125
+
126
+ _edit(config_path, mutate)
127
+
128
+
129
+ def _edit(
130
+ config_path: Path,
131
+ mutate: Callable[[CommentedSeq | list[Any]], None],
132
+ ) -> None:
133
+ loaded = load_config(config_path)
134
+ path = loaded.repositories_path
135
+ before = path.read_bytes()
136
+ document, items = _load_editable(path)
137
+ mutate(items)
138
+ _write_validated(loaded, path, document, before)
139
+
140
+
141
+ def _load_editable(path: Path) -> tuple[Any, CommentedSeq | list[Any]]:
142
+ suffix = path.suffix.casefold()
143
+ if suffix == ".json":
144
+ document = json.loads(path.read_text(encoding="utf-8"))
145
+ items = document.get("organisations")
146
+ if not isinstance(items, list):
147
+ raise ConfigError(f"{path} organisations must be a list.")
148
+ return document, items
149
+ yaml = _round_trip()
150
+ document = yaml.load(path.read_text(encoding="utf-8"))
151
+ items = None
152
+ if isinstance(document, CommentedMap):
153
+ items = document.get("organisations")
154
+ if not isinstance(items, CommentedSeq):
155
+ raise ConfigError(f"{path} organisations must be a list.")
156
+ return document, items
157
+
158
+
159
+ def _write_validated(
160
+ loaded: LoadedConfig,
161
+ path: Path,
162
+ document: CommentedMap | dict[str, Any],
163
+ before: bytes,
164
+ ) -> None:
165
+ temporary = path.with_name(path.name + ".part")
166
+ try:
167
+ _dump(path, temporary, document)
168
+ if path.read_bytes() != before:
169
+ raise ConfigError(f"{path} changed while it was being edited.")
170
+ validate_repository_file(
171
+ temporary,
172
+ loaded.config.defaults.profile,
173
+ loaded.profiles,
174
+ )
175
+ if path.read_bytes() != before:
176
+ raise ConfigError(f"{path} changed while it was being edited.")
177
+ os.replace(temporary, path)
178
+ finally:
179
+ temporary.unlink(missing_ok=True)
180
+
181
+
182
+ def _dump(path: Path, temporary: Path, document: CommentedMap | dict[str, Any]) -> None:
183
+ if path.suffix.casefold() == ".json":
184
+ text = json.dumps(document, indent=2) + "\n"
185
+ temporary.write_text(text, encoding="utf-8")
186
+ return
187
+ yaml = _round_trip()
188
+ with temporary.open("w", encoding="utf-8") as handle:
189
+ yaml.dump(document, handle)
190
+
191
+
192
+ def _round_trip() -> YAML:
193
+ yaml = YAML(typ="rt")
194
+ yaml.preserve_quotes = True
195
+ yaml.indent(mapping=2, sequence=4, offset=2)
196
+ return yaml
197
+
198
+
199
+ def _split(repo: str) -> tuple[str, str]:
200
+ try:
201
+ canonical = RepositorySpec(repo=repo).repo
202
+ except ValidationError as exc:
203
+ raise ConfigError("repo must be owner/name.") from exc
204
+ owner, _, name = canonical.partition("/")
205
+ return owner, name
206
+
207
+
208
+ def _reject_duplicate(owners: CommentedSeq | list[Any], repo: str) -> None:
209
+ owner, name = _split(repo)
210
+ try:
211
+ _find(owners, owner, name)
212
+ except NotFound:
213
+ return
214
+ raise ConfigError(f"{repo} is already configured.")
215
+
216
+
217
+ def _find(
218
+ owners: CommentedSeq | list[Any],
219
+ owner: str,
220
+ name: str,
221
+ ) -> tuple[CommentedMap | dict[str, Any], int]:
222
+ wanted = repository_key(f"{owner}/{name}")
223
+ for block in owners:
224
+ if not isinstance(block, dict):
225
+ continue
226
+ raw_owner = block.get("name")
227
+ if not isinstance(raw_owner, str):
228
+ continue
229
+ listed = block.get("repositories")
230
+ if not isinstance(listed, list):
231
+ continue
232
+ for index, item in enumerate(listed):
233
+ if repository_key(f"{raw_owner}/{_item_name(item)}") == wanted:
234
+ return block, index
235
+ raise NotFound(f"{owner}/{name} is not configured.")
236
+
237
+
238
+ def _find_owner(
239
+ owners: CommentedSeq | list[Any],
240
+ owner: str,
241
+ ) -> CommentedMap | dict[str, Any] | None:
242
+ for block in owners:
243
+ if not isinstance(block, dict):
244
+ continue
245
+ raw_name = block.get("name")
246
+ if isinstance(raw_name, str) and raw_name.casefold() == owner.casefold():
247
+ return cast("dict[str, Any]", block)
248
+ return None
249
+
250
+
251
+ def _new_owner(owner: str, group: str | None, profile: str | None) -> CommentedMap:
252
+ block = CommentedMap()
253
+ block["name"] = owner
254
+ if group is not None:
255
+ block["group"] = group
256
+ if profile is not None:
257
+ block["profile"] = profile
258
+ block["repositories"] = CommentedSeq()
259
+ return block
260
+
261
+
262
+ def _repository_list(block: CommentedMap | dict[str, Any]) -> CommentedSeq | list[Any]:
263
+ listed = block.get("repositories")
264
+ if not isinstance(listed, list):
265
+ raise ConfigError(f"{block.get('name')} repositories must be a list.")
266
+ return listed
267
+
268
+
269
+ def _entry(
270
+ name: str,
271
+ block: CommentedMap | dict[str, Any],
272
+ group: str | None,
273
+ profile: str | None,
274
+ tags: list[str] | None,
275
+ ) -> str | CommentedMap:
276
+ mapping = CommentedMap()
277
+ mapping["name"] = name
278
+ if group is not None and group != block.get("group"):
279
+ mapping["group"] = group
280
+ if profile is not None and profile != block.get("profile"):
281
+ mapping["profile"] = profile
282
+ if tags:
283
+ mapping["tags"] = list(tags)
284
+ if len(mapping) == 1:
285
+ return name
286
+ return mapping
287
+
288
+
289
+ def _item_name(item: object) -> str:
290
+ if isinstance(item, str):
291
+ return item
292
+ if isinstance(item, dict) and isinstance(item.get("name"), str):
293
+ return cast("str", item["name"])
294
+ raise ConfigError("A repository entry must be a name or an object.")
295
+
296
+
297
+ def _as_mapping(item: object, name: str) -> CommentedMap | dict[str, Any]:
298
+ if isinstance(item, str):
299
+ mapping = CommentedMap()
300
+ mapping["name"] = name
301
+ return mapping
302
+ if isinstance(item, dict):
303
+ return item
304
+ raise ConfigError("A repository entry must be a name or an object.")
305
+
306
+
307
+ def _with_fields(
308
+ mapping: CommentedMap | dict[str, Any],
309
+ fields: dict[str, Any],
310
+ ) -> CommentedMap | dict[str, Any]:
311
+ for key, value in fields.items():
312
+ if value is None:
313
+ mapping.pop(key, None)
314
+ else:
315
+ mapping[key] = value
316
+ return mapping