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,896 @@
1
+ """Repository rows shared by the CLI, reports, and TUI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ import time
7
+
8
+ from rich.text import Text
9
+
10
+ from lupaxa.github_intel.config.loader import LoadedConfig
11
+ from lupaxa.github_intel.config.models import RepositorySpec
12
+ from lupaxa.github_intel.config.profiles import check_defaults, resolve_profile
13
+ from lupaxa.github_intel.dates import (
14
+ DEFAULT_DATE_FORMAT,
15
+ format_date,
16
+ format_timestamp,
17
+ normalize_instant,
18
+ )
19
+ from lupaxa.github_intel.storage.database import Document, StoredCheck
20
+ from lupaxa.github_intel.sync.summary import (
21
+ ahead_count,
22
+ format_count,
23
+ latest_published,
24
+ workflow_label,
25
+ )
26
+
27
+ __all__ = [
28
+ "RepositoryView",
29
+ "build_view",
30
+ "detail_rows",
31
+ "field_text",
32
+ "metric_cells",
33
+ "metric_columns",
34
+ "quality_text",
35
+ "select_repositories",
36
+ "split_identifier",
37
+ "status_style",
38
+ "visibility_style",
39
+ "visibility_text",
40
+ "workflow_text",
41
+ ]
42
+
43
+
44
+ @dataclass(frozen=True)
45
+ class RepositoryView:
46
+ """One repository as shown in lists and reports."""
47
+
48
+ repo: str
49
+ group: str
50
+ profile: str
51
+ tags: tuple[str, ...]
52
+ enabled: bool
53
+ description: str
54
+ visibility: str
55
+ status: str
56
+ statuses: tuple[str, ...]
57
+ updated_at: float | None
58
+ checks: tuple[StoredCheck, ...]
59
+ metadata_error: str | None
60
+ version: str
61
+ open_pulls: int | None
62
+ open_issues: int | None
63
+ codeql: int | None
64
+ codeql_reason: str
65
+ archived: bool
66
+ last_push: str
67
+ default_branch: str
68
+ homepage: str
69
+ language: str
70
+ topics: tuple[str, ...]
71
+ fork: bool
72
+ forks: int | None
73
+ watchers: int | None
74
+ created: str
75
+ github_updated: str
76
+ size: int | None
77
+ stars: int | None
78
+ licence: str
79
+ release_date: str
80
+ ahead: int | None
81
+ workflow: str
82
+ workflows: tuple[tuple[str, str], ...] | None
83
+ workflows_enabled: bool
84
+ issues_enabled: bool
85
+ pull_requests_enabled: bool
86
+ quality_enabled: bool
87
+ dependencies: int | None
88
+ dependencies_reason: str
89
+
90
+
91
+ def build_view(
92
+ loaded: LoadedConfig,
93
+ repository: RepositorySpec,
94
+ documents: list[Document],
95
+ checks: list[StoredCheck],
96
+ ) -> RepositoryView:
97
+ """
98
+ Combine configuration with cached documents.
99
+
100
+ Args:
101
+ loaded: Configuration, used for the metadata TTL.
102
+ repository: Configured repository.
103
+ documents: Cached payloads.
104
+ checks: Stored check rows.
105
+
106
+ Returns:
107
+ Display row.
108
+
109
+ """
110
+
111
+ metadata = _document(documents, "metadata")
112
+ body = {} if metadata is None else metadata.json_body()
113
+ description = ""
114
+ if isinstance(body, dict) and isinstance(body.get("description"), str):
115
+ description = body["description"]
116
+ updated = None if metadata is None else metadata.fetched_at
117
+ statuses = _statuses(repository, metadata, loaded.config.sync.cache.metadata_ttl)
118
+ summary = _summary(documents)
119
+ profile_name = repository.profile or loaded.config.defaults.profile
120
+ resolved = resolve_profile(
121
+ loaded.profiles,
122
+ profile_name,
123
+ repository.checks,
124
+ **check_defaults(loaded.config.defaults),
125
+ )
126
+ checks = _shown_checks(checks)
127
+ ahead = _ahead(summary, documents)
128
+ return RepositoryView(
129
+ repo=repository.repo,
130
+ group=repository.group,
131
+ profile=profile_name,
132
+ tags=tuple(repository.tags),
133
+ enabled=repository.enabled,
134
+ description=description,
135
+ visibility=_visibility(body),
136
+ status=_release_label(_headline(repository, metadata, checks), ahead),
137
+ statuses=tuple(sorted(statuses)),
138
+ updated_at=updated,
139
+ checks=tuple(checks),
140
+ metadata_error=None if metadata is None else metadata.error,
141
+ version=_text(summary, "version"),
142
+ open_pulls=_whole(summary, "open_pulls"),
143
+ open_issues=_whole(summary, "open_issues"),
144
+ codeql=_quality(summary),
145
+ codeql_reason=_codeql_reason(summary),
146
+ archived=_archived(body),
147
+ last_push=_last_push(body),
148
+ default_branch=_default_branch(body),
149
+ homepage=_body_text(body, "homepage"),
150
+ language=_body_text(body, "language"),
151
+ topics=_topics(body),
152
+ fork=_flag(body, "fork"),
153
+ forks=_body_whole(body, "forks_count"),
154
+ watchers=_body_whole(body, "subscribers_count"),
155
+ created=_body_day(body, "created_at"),
156
+ github_updated=_body_day(body, "updated_at"),
157
+ size=_body_whole(body, "size"),
158
+ stars=_stars(body),
159
+ licence=_licence(body),
160
+ release_date=_release_date(summary, documents),
161
+ ahead=ahead,
162
+ workflow=_workflow(summary, documents),
163
+ workflows=_workflows(summary),
164
+ workflows_enabled=resolved.enabled["workflows"],
165
+ issues_enabled=resolved.enabled["issues"],
166
+ pull_requests_enabled=resolved.enabled["pull_requests"],
167
+ quality_enabled=resolved.enabled["quality"],
168
+ dependencies=_dependencies(summary),
169
+ dependencies_reason=_dependencies_reason(summary),
170
+ )
171
+
172
+
173
+ def field_text(
174
+ view: RepositoryView,
175
+ label: str,
176
+ date_format: str,
177
+ *,
178
+ detail: bool,
179
+ ) -> str:
180
+ """
181
+ Return one catalogue cell.
182
+
183
+ Args:
184
+ view: Repository to describe.
185
+ label: Catalogue field name.
186
+ date_format: Pattern such as ``DD/MM/YYYY HH:mm UTC``.
187
+ detail: Use the detail wording for a skipped check. Status is the
188
+ short word on the list and the release sentence on the detail.
189
+
190
+ Returns:
191
+ The cell text. A disabled list check is blank, except Workflows,
192
+ which stays ``SKIPPED — …`` on the list as well.
193
+
194
+ """
195
+
196
+ org, name = split_identifier(view.repo)
197
+ cells = {
198
+ "Organisation": org,
199
+ "Repository": name,
200
+ "Visibility": view.visibility,
201
+ "Description": view.description or "No description",
202
+ "Homepage": view.homepage,
203
+ "Language": view.language,
204
+ "Topics": ", ".join(view.topics),
205
+ "Group": view.group,
206
+ "Profile": view.profile,
207
+ "Tags": ", ".join(view.tags),
208
+ "Enabled": _yes_no(view.enabled),
209
+ "Status": view.status if not detail else _status_and_checks(view)[0],
210
+ "Cache": ", ".join(view.statuses),
211
+ "Archived": _yes_no(view.archived),
212
+ "Fork": _yes_no(view.fork),
213
+ "Forks": format_count(view.forks),
214
+ "Last push": format_date(view.last_push, date_format),
215
+ "Created": format_date(view.created, date_format),
216
+ "Updated": format_date(view.github_updated, date_format),
217
+ "Synchronised": format_timestamp(view.updated_at, date_format),
218
+ "Default branch": view.default_branch,
219
+ "Size": "" if view.size is None else f"{view.size} KB",
220
+ "Stars": format_count(view.stars),
221
+ "Watchers": format_count(view.watchers),
222
+ "Licence": view.licence,
223
+ "Version": view.version,
224
+ "Release date": format_date(view.release_date, date_format),
225
+ "Ahead": format_count(view.ahead),
226
+ "Workflows": _workflow_rows(view)[0][1],
227
+ "Dependabot Alerts": _dependabot_text(view),
228
+ }
229
+ if label in cells:
230
+ return cells[label]
231
+ if label == "Pull requests":
232
+ return _listed_count(
233
+ label,
234
+ view.pull_requests_enabled,
235
+ view.open_pulls,
236
+ "Pull request checks are disabled.",
237
+ detail=detail,
238
+ )
239
+ if label == "Issues":
240
+ return _listed_count(
241
+ label,
242
+ view.issues_enabled,
243
+ view.open_issues,
244
+ "Issue checks are disabled.",
245
+ detail=detail,
246
+ )
247
+ if label == "Quality":
248
+ if detail:
249
+ return _count_row(
250
+ label,
251
+ view.quality_enabled,
252
+ view.codeql,
253
+ "Quality checks are disabled.",
254
+ unknown=view.codeql_reason or "Unknown",
255
+ )[1]
256
+ return quality_text(view.quality_enabled, view.codeql, view.codeql_reason)
257
+ message = f"unknown display field {label!r}."
258
+ raise ValueError(message)
259
+
260
+
261
+ def _listed_count(
262
+ label: str,
263
+ enabled: bool,
264
+ value: int | None,
265
+ reason: str,
266
+ *,
267
+ detail: bool,
268
+ ) -> str:
269
+ if detail:
270
+ return _count_row(label, enabled, value, reason)[1]
271
+ if not enabled:
272
+ return ""
273
+ return format_count(value)
274
+
275
+
276
+ def detail_rows(
277
+ view: RepositoryView,
278
+ *,
279
+ date_format: str = DEFAULT_DATE_FORMAT,
280
+ fields: tuple[str, ...] | None = None,
281
+ ) -> tuple[tuple[str, str], ...]:
282
+ """
283
+ Return the field and value rows for one repository.
284
+
285
+ Args:
286
+ view: Repository to describe.
287
+ date_format: Pattern such as ``DD/MM/YYYY HH:mm UTC``.
288
+ fields: Catalogue labels to show. ``None`` keeps every current row.
289
+
290
+ Returns:
291
+ Ordered pairs for the detail table.
292
+
293
+ """
294
+
295
+ if fields is not None:
296
+ selected: list[tuple[str, str]] = []
297
+ if view.metadata_error:
298
+ selected.append(("Metadata error", view.metadata_error))
299
+ selected.extend(
300
+ (label, field_text(view, label, date_format, detail=True))
301
+ for label in fields
302
+ )
303
+ return tuple(selected)
304
+
305
+ org, name = split_identifier(view.repo)
306
+ status, checks = _status_and_checks(view)
307
+ rows = []
308
+ if view.metadata_error:
309
+ rows.append(("Metadata error", view.metadata_error))
310
+ rows.extend(
311
+ [
312
+ ("Organisation", org),
313
+ ("Repository", name),
314
+ ("Visibility", view.visibility),
315
+ ("Description", view.description or "No description"),
316
+ ("Homepage", view.homepage),
317
+ ("Language", view.language),
318
+ ("Topics", ", ".join(view.topics)),
319
+ ("Group", view.group),
320
+ ("Profile", view.profile),
321
+ ("Tags", ", ".join(view.tags)),
322
+ ("Enabled", _yes_no(view.enabled)),
323
+ ("Status", status),
324
+ ("Cache", ", ".join(view.statuses)),
325
+ ("Archived", _yes_no(view.archived)),
326
+ ("Fork", _yes_no(view.fork)),
327
+ ("Forks", format_count(view.forks)),
328
+ ("Last push", format_date(view.last_push, date_format)),
329
+ ("Created", format_date(view.created, date_format)),
330
+ ("Updated", format_date(view.github_updated, date_format)),
331
+ ("Synchronised", format_timestamp(view.updated_at, date_format)),
332
+ ("Default branch", view.default_branch),
333
+ ("Size", "" if view.size is None else f"{view.size} KB"),
334
+ ("Stars", format_count(view.stars)),
335
+ ("Watchers", format_count(view.watchers)),
336
+ ("Licence", view.licence),
337
+ ("Version", view.version),
338
+ ("Release date", format_date(view.release_date, date_format)),
339
+ ("Ahead", format_count(view.ahead)),
340
+ _count_row(
341
+ "Pull requests",
342
+ view.pull_requests_enabled,
343
+ view.open_pulls,
344
+ "Pull request checks are disabled.",
345
+ ),
346
+ _count_row(
347
+ "Issues",
348
+ view.issues_enabled,
349
+ view.open_issues,
350
+ "Issue checks are disabled.",
351
+ ),
352
+ _count_row(
353
+ "Quality",
354
+ view.quality_enabled,
355
+ view.codeql,
356
+ "Quality checks are disabled.",
357
+ unknown=view.codeql_reason or "Unknown",
358
+ ),
359
+ *_workflow_rows(view),
360
+ ("Dependabot Alerts", _dependabot_text(view)),
361
+ *checks,
362
+ ]
363
+ )
364
+ return tuple(rows)
365
+
366
+
367
+ def visibility_style(value: str) -> str:
368
+ """
369
+ Choose a colour for a visibility word.
370
+
371
+ Args:
372
+ value: ``public``, ``private``, ``internal``, or empty.
373
+
374
+ Returns:
375
+ ``green``, ``magenta``, ``cyan``, or an empty string.
376
+
377
+ """
378
+
379
+ return {"public": "green", "private": "magenta", "internal": "cyan"}.get(
380
+ value.strip().casefold(),
381
+ "",
382
+ )
383
+
384
+
385
+ def visibility_text(value: str) -> str | Text:
386
+ """
387
+ Colour a visibility word.
388
+
389
+ Args:
390
+ value: ``public``, ``private``, ``internal``, or empty.
391
+
392
+ Returns:
393
+ Styled text, or the plain word when it has no colour.
394
+
395
+ """
396
+
397
+ style = visibility_style(value)
398
+ if not style:
399
+ return value
400
+ return Text(value, style=style)
401
+
402
+
403
+ def status_style(value: str) -> str:
404
+ """
405
+ Choose a terminal colour for a status.
406
+
407
+ Args:
408
+ value: Status text, or a detail value that starts with one.
409
+
410
+ Returns:
411
+ ``green``, ``cyan``, ``yellow``, ``red``, ``dim``, or an empty string.
412
+
413
+ """
414
+
415
+ label = value.split(" — ", 1)[0].strip().casefold()
416
+ if label in {"active", "current", "healthy", "success"}:
417
+ return "green"
418
+ if label == "no releases":
419
+ return "cyan"
420
+ if label == "no scanning":
421
+ return "dim"
422
+ if label.endswith(" ahead") or label in {"unknown", "forbidden", "in progress"}:
423
+ return "yellow"
424
+ if label in {"fail", "error", "failure", "unavailable"}:
425
+ return "red"
426
+ if (
427
+ "disabled" in label
428
+ or label in {"not synchronised", "never run"}
429
+ or label.startswith("skipped")
430
+ ):
431
+ return "dim"
432
+ return ""
433
+
434
+
435
+ def split_identifier(identifier: str) -> tuple[str, str]:
436
+ """
437
+ Split ``owner/name`` into the organisation and the repository name.
438
+
439
+ Args:
440
+ identifier: GitHub repository identifier.
441
+
442
+ Returns:
443
+ Organisation and repository name.
444
+
445
+ """
446
+
447
+ org, separator, name = identifier.partition("/")
448
+ if separator != "/":
449
+ return "", identifier
450
+ return org, name
451
+
452
+
453
+ def select_repositories(
454
+ loaded: LoadedConfig,
455
+ *,
456
+ group: str | None = None,
457
+ profile: str | None = None,
458
+ tag: str | None = None,
459
+ repo: str | None = None,
460
+ enabled_only: bool = False,
461
+ ) -> tuple[RepositorySpec, ...]:
462
+ """
463
+ Filter the configured collection.
464
+
465
+ Args:
466
+ loaded: Configuration.
467
+ group: Required group, if any.
468
+ profile: Required profile, if any.
469
+ tag: Required tag, compared without case.
470
+ repo: One repository identifier.
471
+ enabled_only: Drop disabled repositories.
472
+
473
+ Returns:
474
+ Matching repositories.
475
+
476
+ """
477
+
478
+ chosen: list[RepositorySpec] = []
479
+ for repository in loaded.repositories:
480
+ if enabled_only and not repository.enabled:
481
+ continue
482
+ if group is not None and repository.group != group:
483
+ continue
484
+ name = repository.profile or loaded.config.defaults.profile
485
+ if profile is not None and name != profile:
486
+ continue
487
+ if tag is not None and tag.casefold() not in {
488
+ item.casefold() for item in repository.tags
489
+ }:
490
+ continue
491
+ if repo is not None and repository.repo.casefold() != repo.casefold():
492
+ continue
493
+ chosen.append(repository)
494
+ return tuple(chosen)
495
+
496
+
497
+ def _shown_checks(checks: list[StoredCheck]) -> list[StoredCheck]:
498
+ return [
499
+ item
500
+ for item in checks
501
+ if item.name not in {"issues", "security"}
502
+ and not item.name.startswith("standards:")
503
+ ]
504
+
505
+
506
+ def _statuses(
507
+ repository: RepositorySpec,
508
+ metadata: Document | None,
509
+ ttl: int,
510
+ ) -> set[str]:
511
+ statuses = {"enabled" if repository.enabled else "disabled"}
512
+ if metadata is None or metadata.fetched_at is None:
513
+ statuses.add("not-synchronised")
514
+ return statuses
515
+ statuses.add("cached")
516
+ if metadata.error and "404" in metadata.error:
517
+ statuses.add("unavailable")
518
+ elif metadata.error:
519
+ statuses.add("error")
520
+ if time.time() - metadata.fetched_at >= ttl:
521
+ statuses.add("stale")
522
+ return statuses
523
+
524
+
525
+ def _headline(
526
+ repository: RepositorySpec,
527
+ metadata: Document | None,
528
+ checks: list[StoredCheck],
529
+ ) -> str:
530
+ if not repository.enabled:
531
+ return "Disabled"
532
+ if metadata is None or metadata.fetched_at is None:
533
+ return "Not synchronised"
534
+ if metadata.error and "404" in metadata.error:
535
+ return "Unavailable"
536
+ if metadata.error:
537
+ return "Error"
538
+ for check in checks:
539
+ if check.name == "releases" and check.status == "No releases":
540
+ return check.status
541
+ for check in checks:
542
+ if check.failure:
543
+ return check.status
544
+ for check in checks:
545
+ if check.name == "releases":
546
+ return check.status
547
+ return "Healthy"
548
+
549
+
550
+ def _release_label(status: str, ahead: int | None) -> str:
551
+ if status == "Unreleased changes" and ahead is not None and ahead > 0:
552
+ return f"{ahead} Ahead"
553
+ return status
554
+
555
+
556
+ def _summary(documents: list[Document]) -> dict[str, object]:
557
+ document = _document(documents, "summary")
558
+ if document is None or document.error:
559
+ return {}
560
+ body = document.json_body()
561
+ return body if isinstance(body, dict) else {}
562
+
563
+
564
+ def _text(summary: dict[str, object], key: str) -> str:
565
+ value = summary.get(key)
566
+ return value if isinstance(value, str) else ""
567
+
568
+
569
+ def _yes_no(value: bool) -> str:
570
+ return "yes" if value else "no"
571
+
572
+
573
+ def _flag(body: object, key: str) -> bool:
574
+ return isinstance(body, dict) and body.get(key) is True
575
+
576
+
577
+ def _body_day(body: object, key: str) -> str:
578
+ return normalize_instant(_body_text(body, key))
579
+
580
+
581
+ def _body_text(body: object, key: str) -> str:
582
+ if not isinstance(body, dict):
583
+ return ""
584
+ value = body.get(key)
585
+ return value if isinstance(value, str) else ""
586
+
587
+
588
+ def _body_whole(body: object, key: str) -> int | None:
589
+ if not isinstance(body, dict):
590
+ return None
591
+ return _whole({key: body.get(key)}, key)
592
+
593
+
594
+ def _topics(body: object) -> tuple[str, ...]:
595
+ if not isinstance(body, dict):
596
+ return ()
597
+ value = body.get("topics")
598
+ if not isinstance(value, list):
599
+ return ()
600
+ return tuple(item for item in value if isinstance(item, str) and item)
601
+
602
+
603
+ def _archived(body: object) -> bool:
604
+ return _flag(body, "archived")
605
+
606
+
607
+ def _default_branch(body: object) -> str:
608
+ if not isinstance(body, dict):
609
+ return ""
610
+ branch = body.get("default_branch")
611
+ return branch if isinstance(branch, str) else ""
612
+
613
+
614
+ def _last_push(body: object) -> str:
615
+ if not isinstance(body, dict):
616
+ return ""
617
+ # GitHub sets pushed_at when it creates an empty repository.
618
+ if body.get("size") == 0:
619
+ return ""
620
+ return normalize_instant(body.get("pushed_at"))
621
+
622
+
623
+ def _visibility(body: object) -> str:
624
+ if not isinstance(body, dict):
625
+ return ""
626
+ visibility = body.get("visibility")
627
+ if visibility in {"public", "private", "internal"}:
628
+ return str(visibility)
629
+ if body.get("private") is True:
630
+ return "private"
631
+ if body.get("private") is False:
632
+ return "public"
633
+ return ""
634
+
635
+
636
+ def _stars(body: object) -> int | None:
637
+ if not isinstance(body, dict):
638
+ return None
639
+ value = body.get("stargazers_count")
640
+ if isinstance(value, bool) or not isinstance(value, int):
641
+ return None
642
+ return value
643
+
644
+
645
+ def _licence(body: object) -> str:
646
+ if not isinstance(body, dict):
647
+ return ""
648
+ license_info = body.get("license")
649
+ if not isinstance(license_info, dict):
650
+ return ""
651
+ spdx = license_info.get("spdx_id")
652
+ if not isinstance(spdx, str) or spdx in {"", "NOASSERTION"}:
653
+ return ""
654
+ return spdx
655
+
656
+
657
+ def _release_date(summary: dict[str, object], documents: list[Document]) -> str:
658
+ releases = _document(documents, "releases")
659
+ if releases is not None and not releases.error:
660
+ published = latest_published(releases.json_body())
661
+ if published:
662
+ return published
663
+ if "release_date" in summary:
664
+ return _text(summary, "release_date")
665
+ return ""
666
+
667
+
668
+ def _ahead(summary: dict[str, object], documents: list[Document]) -> int | None:
669
+ if "ahead" in summary:
670
+ return _whole(summary, "ahead")
671
+ for document in documents:
672
+ if document.category.startswith("compare:") and document.error is None:
673
+ return ahead_count(document.json_body())
674
+ return None
675
+
676
+
677
+ def _workflows(summary: dict[str, object]) -> tuple[tuple[str, str], ...] | None:
678
+ if "workflows" not in summary:
679
+ return None
680
+ value = summary["workflows"]
681
+ if not isinstance(value, list):
682
+ return None
683
+ rows: list[tuple[str, str]] = []
684
+ for item in value:
685
+ if not isinstance(item, dict):
686
+ continue
687
+ name = item.get("name")
688
+ status = item.get("status")
689
+ if isinstance(name, str) and name and isinstance(status, str):
690
+ rows.append((name, status))
691
+ return tuple(rows)
692
+
693
+
694
+ def metric_columns(views: list[RepositoryView]) -> tuple[str, ...]:
695
+ """
696
+ Return the numeric list columns that at least one repository shows.
697
+
698
+ Args:
699
+ views: Rows about to be printed. An empty list keeps the default columns.
700
+
701
+ Returns:
702
+ Column headings, starting with Stars.
703
+
704
+ """
705
+
706
+ columns = ["Stars"]
707
+ for label, enabled, default in (
708
+ ("Pull requests", "pull_requests_enabled", True),
709
+ ("Issues", "issues_enabled", False),
710
+ ("Quality", "quality_enabled", True),
711
+ ):
712
+ shown = default if not views else any(getattr(view, enabled) for view in views)
713
+ if shown:
714
+ columns.append(label)
715
+ return tuple(columns)
716
+
717
+
718
+ def metric_cells(view: RepositoryView, columns: tuple[str, ...]) -> tuple[str, ...]:
719
+ """
720
+ Return the numeric cells for one row.
721
+
722
+ Args:
723
+ view: Repository row.
724
+ columns: Headings from :func:`metric_columns`.
725
+
726
+ Returns:
727
+ One cell per heading. A disabled check is blank.
728
+
729
+ """
730
+
731
+ values = {
732
+ "Stars": format_count(view.stars),
733
+ "Pull requests": (
734
+ format_count(view.open_pulls) if view.pull_requests_enabled else ""
735
+ ),
736
+ "Issues": format_count(view.open_issues) if view.issues_enabled else "",
737
+ "Quality": quality_text(
738
+ view.quality_enabled,
739
+ view.codeql,
740
+ view.codeql_reason,
741
+ ),
742
+ }
743
+ return tuple(values[column] for column in columns)
744
+
745
+
746
+ def quality_text(enabled: bool, count: int | None, reason: str = "") -> str:
747
+ """
748
+ Return the Quality cell.
749
+
750
+ Args:
751
+ enabled: The profile includes the quality check.
752
+ count: Open CodeQL alerts, or None when GitHub did not share them.
753
+ reason: Why the count is missing. Empty means ``Unknown``.
754
+
755
+ Returns:
756
+ A blank cell when the check is off, the reason when the count is
757
+ missing, or the decimal count.
758
+
759
+ """
760
+
761
+ if not enabled:
762
+ return ""
763
+ if count is None:
764
+ return reason or "Unknown"
765
+ return str(count)
766
+
767
+
768
+ def _count_row(
769
+ label: str,
770
+ enabled: bool,
771
+ value: int | None,
772
+ reason: str,
773
+ *,
774
+ unknown: str = "",
775
+ ) -> tuple[str, str]:
776
+ if not enabled:
777
+ return label, f"SKIPPED — {reason}"
778
+ if value is None and unknown:
779
+ return label, unknown
780
+ return label, format_count(value)
781
+
782
+
783
+ def _status_and_checks(view: RepositoryView) -> tuple[str, tuple[tuple[str, str], ...]]:
784
+ status = view.status
785
+ rows: list[tuple[str, str]] = []
786
+ if not view.checks:
787
+ return status, (("Checks", "Not synchronised"),)
788
+ for item in view.checks:
789
+ shown = item.status
790
+ if item.name == "releases":
791
+ shown = _release_label(item.status, view.ahead)
792
+ text = f"{shown} — {item.summary}"
793
+ if item.name == "releases" and shown == view.status:
794
+ status = item.summary or shown
795
+ continue
796
+ if item.name == "workflows":
797
+ continue
798
+ rows.append((_check_label(item.name), text))
799
+ return status, tuple(rows)
800
+
801
+
802
+ def _check_label(name: str) -> str:
803
+ return f"Check {name}"
804
+
805
+
806
+ def _workflow_rows(view: RepositoryView) -> tuple[tuple[str, str], ...]:
807
+ if not view.workflows_enabled:
808
+ return (("Workflows", "SKIPPED — Workflow checks are disabled."),)
809
+ failed = next(
810
+ (item for item in view.checks if item.name == "workflows" and item.failure),
811
+ None,
812
+ )
813
+ if failed is not None:
814
+ return (("Workflows", f"{failed.status} — {failed.summary}"),)
815
+ if not view.workflows:
816
+ return (("Workflows", "No workflows found"),)
817
+ return (("Workflows", _workflow_table(view.workflows)),)
818
+
819
+
820
+ def workflow_text(workflows: tuple[tuple[str, str], ...]) -> Text:
821
+ """
822
+ Return the workflow list with each state coloured.
823
+
824
+ Args:
825
+ workflows: Name and state pairs, already sorted.
826
+
827
+ Returns:
828
+ A monospaced block. Names share a width and states share a column.
829
+
830
+ """
831
+
832
+ if not workflows:
833
+ return Text("No workflows found")
834
+ width = max(len(name) for name, _status in workflows)
835
+ text = Text()
836
+ for index, (name, status) in enumerate(workflows):
837
+ if index:
838
+ text.append("\n")
839
+ text.append(f"{name:<{width}} ")
840
+ text.append(status, style=status_style(status) or None)
841
+ return text
842
+
843
+
844
+ def _workflow_table(workflows: tuple[tuple[str, str], ...]) -> str:
845
+ return workflow_text(workflows).plain
846
+
847
+
848
+ def _workflow(summary: dict[str, object], documents: list[Document]) -> str:
849
+ if "workflow" in summary:
850
+ return _text(summary, "workflow") or "Never run"
851
+ document = _document(documents, "workflows")
852
+ if document is None:
853
+ return ""
854
+ return workflow_label(document.json_body(), failed=document.error is not None)
855
+
856
+
857
+ def _dependabot_text(view: RepositoryView) -> str:
858
+ if view.dependencies is None:
859
+ return view.dependencies_reason or "Unknown"
860
+ return str(view.dependencies)
861
+
862
+
863
+ def _dependencies(summary: dict[str, object]) -> int | None:
864
+ if "dependencies" not in summary:
865
+ return None
866
+ return _whole(summary, "dependencies")
867
+
868
+
869
+ def _dependencies_reason(summary: dict[str, object]) -> str:
870
+ if _dependencies(summary) is not None:
871
+ return ""
872
+ return _text(summary, "dependencies_reason")
873
+
874
+
875
+ def _quality(summary: dict[str, object]) -> int | None:
876
+ return _whole(summary, "codeql")
877
+
878
+
879
+ def _codeql_reason(summary: dict[str, object]) -> str:
880
+ if _quality(summary) is not None:
881
+ return ""
882
+ return _text(summary, "codeql_reason")
883
+
884
+
885
+ def _whole(summary: dict[str, object], key: str) -> int | None:
886
+ value = summary.get(key)
887
+ if isinstance(value, bool) or not isinstance(value, int):
888
+ return None
889
+ return value
890
+
891
+
892
+ def _document(documents: list[Document], category: str) -> Document | None:
893
+ for document in documents:
894
+ if document.category == category:
895
+ return document
896
+ return None