quality-graph-github 0.1.2__tar.gz → 0.1.6__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.
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/.gitignore +1 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/PKG-INFO +4 -2
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/pyproject.toml +6 -2
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/comments.py +2 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/compiler.py +39 -10
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/controls.py +11 -3
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/dashboard.py +41 -1
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/publication.py +123 -27
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/reporting.py +40 -6
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/runtime.py +41 -8
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/README.md +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/__init__.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/annotations.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/approvals.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/artifacts.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/commands.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/github.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/labels.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/provider.py +0 -0
- {quality_graph_github-0.1.2 → quality_graph_github-0.1.6}/src/qg_github/py.typed +0 -0
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: quality-graph-github
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.6
|
|
4
4
|
Summary: GitHub provider and Action runtime for Quality Graph
|
|
5
|
+
Project-URL: Homepage, https://alchemmist.github.io/quality-graph/
|
|
6
|
+
Project-URL: Documentation, https://alchemmist.github.io/quality-graph/
|
|
5
7
|
License-Expression: MIT
|
|
6
8
|
Requires-Python: >=3.12
|
|
7
9
|
Requires-Dist: httpx>=0.28.1
|
|
8
10
|
Requires-Dist: jinja2>=3.1.6
|
|
9
11
|
Requires-Dist: pyyaml>=6.0.2
|
|
10
|
-
Requires-Dist: quality-graph-core==0.1.
|
|
12
|
+
Requires-Dist: quality-graph-core==0.1.6
|
|
11
13
|
Description-Content-Type: text/markdown
|
|
12
14
|
|
|
13
15
|
# Quality Graph GitHub Provider
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "quality-graph-github"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.6"
|
|
8
8
|
description = "GitHub provider and Action runtime for Quality Graph"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.12"
|
|
@@ -13,9 +13,13 @@ dependencies = [
|
|
|
13
13
|
"httpx>=0.28.1",
|
|
14
14
|
"jinja2>=3.1.6",
|
|
15
15
|
"pyyaml>=6.0.2",
|
|
16
|
-
"quality-graph-core==0.1.
|
|
16
|
+
"quality-graph-core==0.1.6",
|
|
17
17
|
]
|
|
18
18
|
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://alchemmist.github.io/quality-graph/"
|
|
21
|
+
Documentation = "https://alchemmist.github.io/quality-graph/"
|
|
22
|
+
|
|
19
23
|
[project.entry-points."qg.providers"]
|
|
20
24
|
github = "qg_github:provider"
|
|
21
25
|
|
|
@@ -83,6 +83,8 @@ def upsert_managed_comment(
|
|
|
83
83
|
existing = find_managed_comment(port, number, name, bot_logins=bot_logins)
|
|
84
84
|
if existing is None:
|
|
85
85
|
response = port.request("POST", f"/issues/{number}/comments", {"body": rendered})
|
|
86
|
+
elif existing.body == rendered:
|
|
87
|
+
return existing
|
|
86
88
|
else:
|
|
87
89
|
response = port.request("PATCH", f"/issues/comments/{existing.id}", {"body": rendered})
|
|
88
90
|
data = _object(response, "managed comment")
|
|
@@ -18,7 +18,11 @@ if TYPE_CHECKING:
|
|
|
18
18
|
|
|
19
19
|
from quality_graph_core.result import JsonValue
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
GENERATED_NOTICE = (
|
|
22
|
+
"Generated by Quality Graph. DO NOT EDIT MANUALLY. "
|
|
23
|
+
"Update quality-graph.yml and run qg generate."
|
|
24
|
+
)
|
|
25
|
+
GENERATED_HEADER = f"# {GENERATED_NOTICE}\n---\n"
|
|
22
26
|
EXECUTION_WORKFLOW = PurePosixPath(".github/workflows/quality-graph.yml")
|
|
23
27
|
PUBLICATION_WORKFLOW = PurePosixPath(".github/workflows/quality-graph-publish.yml")
|
|
24
28
|
GRAPH_MANIFEST = PurePosixPath(".quality-graph/manifest.json")
|
|
@@ -50,8 +54,13 @@ class WorkflowDumper(yaml.SafeDumper):
|
|
|
50
54
|
"""Keep generated workflows locally readable."""
|
|
51
55
|
return True
|
|
52
56
|
|
|
57
|
+
@override
|
|
58
|
+
def increase_indent(self, flow: bool = False, indentless: bool = False) -> None:
|
|
59
|
+
del indentless
|
|
60
|
+
super().increase_indent(flow, indentless=False)
|
|
61
|
+
|
|
53
62
|
|
|
54
|
-
def _validate_github_graph(graph: Graph) -> str:
|
|
63
|
+
def _validate_github_graph(graph: Graph) -> tuple[str, str]:
|
|
55
64
|
if graph.provider.name != "github":
|
|
56
65
|
message = f"GitHub provider cannot compile provider '{graph.provider.name}'"
|
|
57
66
|
raise ValueError(message)
|
|
@@ -63,12 +72,13 @@ def _validate_github_graph(graph: Graph) -> str:
|
|
|
63
72
|
if not isinstance(runtime, dict):
|
|
64
73
|
message = "GitHub provider requires a runtime object"
|
|
65
74
|
raise TypeError(message)
|
|
66
|
-
if runtime.keys() - {"action"}:
|
|
75
|
+
if runtime.keys() - {"action", "publisher-action"}:
|
|
67
76
|
message = "GitHub runtime contains unknown fields"
|
|
68
77
|
raise ValueError(message)
|
|
69
|
-
action = runtime.get("action")
|
|
70
|
-
|
|
71
|
-
|
|
78
|
+
action = _runtime_action(runtime.get("action"), "runtime")
|
|
79
|
+
publisher_action = _runtime_action(runtime.get("publisher-action", action), "publisher")
|
|
80
|
+
if publisher_action.partition("@")[0] != action.partition("@")[0]:
|
|
81
|
+
message = "GitHub publisher action must use the runtime action repository"
|
|
72
82
|
raise ValueError(message)
|
|
73
83
|
for profile in graph.profiles:
|
|
74
84
|
_validate_permissions(profile)
|
|
@@ -76,7 +86,18 @@ def _validate_github_graph(graph: Graph) -> str:
|
|
|
76
86
|
_validate_step(step)
|
|
77
87
|
for node in graph.nodes:
|
|
78
88
|
_validate_step(node.step)
|
|
79
|
-
|
|
89
|
+
titles = [node.title for node in graph.nodes]
|
|
90
|
+
if len(set(titles)) != len(titles):
|
|
91
|
+
message = "GitHub dashboard requires unique node titles"
|
|
92
|
+
raise ValueError(message)
|
|
93
|
+
return action, publisher_action
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _runtime_action(value: JsonValue, context: str) -> str:
|
|
97
|
+
if not isinstance(value, str) or RUNTIME_ACTION_RE.fullmatch(value) is None:
|
|
98
|
+
message = f"GitHub {context} action must use owner/repository@40-character-commit"
|
|
99
|
+
raise ValueError(message)
|
|
100
|
+
return value
|
|
80
101
|
|
|
81
102
|
|
|
82
103
|
def _validate_permissions(profile: Profile) -> None:
|
|
@@ -100,10 +121,11 @@ def _validate_step(step: Step) -> None:
|
|
|
100
121
|
|
|
101
122
|
def compile_graph(graph: Graph) -> GeneratedProject:
|
|
102
123
|
"""Compile one graph through the public declaration seam."""
|
|
103
|
-
runtime_action = _validate_github_graph(graph)
|
|
124
|
+
runtime_action, publisher_action = _validate_github_graph(graph)
|
|
104
125
|
manifest = _manifest_value(graph, runtime_action)
|
|
105
126
|
digest = hashlib.sha256(_canonical_json(manifest).encode()).hexdigest()
|
|
106
127
|
manifest["graphDigest"] = digest
|
|
128
|
+
manifest["_generated"] = GENERATED_NOTICE
|
|
107
129
|
files = (
|
|
108
130
|
GeneratedFile(
|
|
109
131
|
EXECUTION_WORKFLOW,
|
|
@@ -111,7 +133,7 @@ def compile_graph(graph: Graph) -> GeneratedProject:
|
|
|
111
133
|
),
|
|
112
134
|
GeneratedFile(
|
|
113
135
|
PUBLICATION_WORKFLOW,
|
|
114
|
-
_yaml_file(_publication_workflow(
|
|
136
|
+
_yaml_file(_publication_workflow(publisher_action)),
|
|
115
137
|
),
|
|
116
138
|
GeneratedFile(GRAPH_MANIFEST, _canonical_json(manifest)),
|
|
117
139
|
)
|
|
@@ -261,6 +283,9 @@ def _execution_job(
|
|
|
261
283
|
"report-path": report_path,
|
|
262
284
|
"command-outcome": "${{ steps.quality-command.outcome }}",
|
|
263
285
|
"graph-digest": graph_digest,
|
|
286
|
+
"approval-findings": str(node.policy.approvals.findings).lower(),
|
|
287
|
+
"approval-files": str(node.policy.approvals.files).lower(),
|
|
288
|
+
"approval-node": str(node.policy.approvals.node).lower(),
|
|
264
289
|
},
|
|
265
290
|
}
|
|
266
291
|
upload: dict[str, JsonValue] = {
|
|
@@ -331,7 +356,11 @@ def _publication_workflow(runtime_action: str) -> dict[str, JsonValue]:
|
|
|
331
356
|
{
|
|
332
357
|
"name": "Publish trusted Quality Graph state",
|
|
333
358
|
"uses": runtime_action,
|
|
334
|
-
"with": {
|
|
359
|
+
"with": {
|
|
360
|
+
"operation": (
|
|
361
|
+
"${{ github.event.action == 'completed' && 'publish' || 'watch' }}"
|
|
362
|
+
)
|
|
363
|
+
},
|
|
335
364
|
}
|
|
336
365
|
],
|
|
337
366
|
}
|
|
@@ -59,9 +59,17 @@ def control_states(body: str) -> dict[str, bool]:
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
|
|
62
|
-
def render_control(control: Control) -> str:
|
|
62
|
+
def render_control(control: Control, *, show_commands: bool = False) -> str:
|
|
63
63
|
"""Render one reversible checkbox control."""
|
|
64
64
|
state = "x" if control.checked else " "
|
|
65
65
|
marker = control_marker(control)
|
|
66
|
-
target =
|
|
67
|
-
|
|
66
|
+
target = _code(control.target)
|
|
67
|
+
rendered = f"- [{state}] {control.kind.value}: `{target}` <!-- {marker} -->"
|
|
68
|
+
if not show_commands:
|
|
69
|
+
return rendered
|
|
70
|
+
apply, reverse = control_commands(control)
|
|
71
|
+
return f"{rendered}\n - apply: `{_code(apply)}`\n - reverse: `{_code(reverse)}`"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _code(value: str) -> str:
|
|
75
|
+
return html.escape(value).replace("`", "`").replace("\n", " ")
|
|
@@ -68,6 +68,14 @@ class DashboardRun:
|
|
|
68
68
|
url: str
|
|
69
69
|
|
|
70
70
|
|
|
71
|
+
@dataclass(frozen=True)
|
|
72
|
+
class DashboardNode:
|
|
73
|
+
"""Identify one graph node in live GitHub job state."""
|
|
74
|
+
|
|
75
|
+
node_id: str
|
|
76
|
+
title: str
|
|
77
|
+
|
|
78
|
+
|
|
71
79
|
def load_results(directory: Path) -> dict[str, Result]:
|
|
72
80
|
"""Load the newest artifact attempt for every node."""
|
|
73
81
|
results: dict[str, Result] = {}
|
|
@@ -143,13 +151,15 @@ def final_dashboard(
|
|
|
143
151
|
def pending_dashboard(
|
|
144
152
|
graph: Graph,
|
|
145
153
|
run: DashboardRun,
|
|
154
|
+
*,
|
|
155
|
+
started: bool = False,
|
|
146
156
|
) -> DashboardModel:
|
|
147
157
|
"""Build an early dashboard before portable results are available."""
|
|
148
158
|
rows = tuple(
|
|
149
159
|
DashboardRow(
|
|
150
160
|
node.id,
|
|
151
161
|
node.title,
|
|
152
|
-
ResultStatus.WAITING,
|
|
162
|
+
ResultStatus.IN_PROGRESS if started and not node.needs else ResultStatus.WAITING,
|
|
153
163
|
"—",
|
|
154
164
|
f"{run.url}#quality-graph-{node.id}",
|
|
155
165
|
run.url,
|
|
@@ -167,6 +177,36 @@ def pending_dashboard(
|
|
|
167
177
|
)
|
|
168
178
|
|
|
169
179
|
|
|
180
|
+
def live_dashboard(
|
|
181
|
+
nodes: tuple[DashboardNode, ...],
|
|
182
|
+
statuses: Mapping[str, ResultStatus],
|
|
183
|
+
run: DashboardRun,
|
|
184
|
+
*,
|
|
185
|
+
managed_labels: tuple[str, ...] = (),
|
|
186
|
+
) -> DashboardModel:
|
|
187
|
+
"""Build a live dashboard from authoritative GitHub job statuses."""
|
|
188
|
+
rows = tuple(
|
|
189
|
+
DashboardRow(
|
|
190
|
+
node.node_id,
|
|
191
|
+
node.title,
|
|
192
|
+
statuses.get(node.node_id, ResultStatus.WAITING),
|
|
193
|
+
"—",
|
|
194
|
+
f"{run.url}#quality-graph-{node.node_id}",
|
|
195
|
+
run.url,
|
|
196
|
+
)
|
|
197
|
+
for node in nodes
|
|
198
|
+
)
|
|
199
|
+
return DashboardModel(
|
|
200
|
+
ResultStatus.IN_PROGRESS,
|
|
201
|
+
"The current Quality Graph run is in progress.",
|
|
202
|
+
run.id,
|
|
203
|
+
run.attempt,
|
|
204
|
+
run.head_sha,
|
|
205
|
+
rows,
|
|
206
|
+
managed_labels=managed_labels,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
170
210
|
def render_dashboard(model: DashboardModel) -> str:
|
|
171
211
|
"""Render a complete dashboard while retaining high-value controls."""
|
|
172
212
|
rendered = _render(model)
|
|
@@ -4,6 +4,7 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import base64
|
|
6
6
|
import json
|
|
7
|
+
import time
|
|
7
8
|
import urllib.parse
|
|
8
9
|
from dataclasses import dataclass, replace
|
|
9
10
|
from typing import TYPE_CHECKING, cast
|
|
@@ -15,8 +16,10 @@ from qg_github.compiler import compile_graph
|
|
|
15
16
|
from qg_github.dashboard import (
|
|
16
17
|
DASHBOARD_MARKER,
|
|
17
18
|
DashboardModel,
|
|
19
|
+
DashboardNode,
|
|
18
20
|
DashboardRun,
|
|
19
21
|
final_dashboard,
|
|
22
|
+
live_dashboard,
|
|
20
23
|
pending_dashboard,
|
|
21
24
|
render_dashboard,
|
|
22
25
|
)
|
|
@@ -27,7 +30,7 @@ from quality_graph_core.policy import effective_graph
|
|
|
27
30
|
from quality_graph_core.result import JsonValue, ResultStatus
|
|
28
31
|
|
|
29
32
|
if TYPE_CHECKING:
|
|
30
|
-
from collections.abc import Mapping
|
|
33
|
+
from collections.abc import Callable, Mapping
|
|
31
34
|
|
|
32
35
|
from quality_graph_core.result import Result
|
|
33
36
|
|
|
@@ -95,7 +98,7 @@ def publish_workflow_run(
|
|
|
95
98
|
) -> PublicationOutcome:
|
|
96
99
|
"""Publish one trusted workflow-run event if it is current."""
|
|
97
100
|
event = WorkflowRunEvent.from_value(event_value)
|
|
98
|
-
if event.event != "pull_request":
|
|
101
|
+
if event.event != "pull_request" or event.action != "completed":
|
|
99
102
|
return PublicationOutcome(published=False)
|
|
100
103
|
number = event.pull_request or _resolve_pull_request(port, event.workflow_head_sha)
|
|
101
104
|
if number is None:
|
|
@@ -108,20 +111,13 @@ def publish_workflow_run(
|
|
|
108
111
|
graph = Graph.from_yaml(_repository_file(port, "quality-graph.yml", pull.base_sha))
|
|
109
112
|
compiled = compile_graph(graph)
|
|
110
113
|
run = DashboardRun(event.id, event.attempt, pull.head_sha, event.url)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
compiled.graph_digest,
|
|
119
|
-
pull,
|
|
120
|
-
run,
|
|
121
|
-
)
|
|
122
|
-
else:
|
|
123
|
-
message = f"unsupported workflow_run action: {event.action}"
|
|
124
|
-
raise ValueError(message)
|
|
114
|
+
model, effective_results = _completed_dashboard(
|
|
115
|
+
port,
|
|
116
|
+
graph,
|
|
117
|
+
compiled.graph_digest,
|
|
118
|
+
pull,
|
|
119
|
+
run,
|
|
120
|
+
)
|
|
125
121
|
existing = find_managed_comment(port, number, DASHBOARD_MARKER)
|
|
126
122
|
previous_labels = parse_label_state(existing.body) if existing is not None else frozenset()
|
|
127
123
|
if effective_results is None and previous_labels:
|
|
@@ -139,6 +135,107 @@ def publish_workflow_run(
|
|
|
139
135
|
return PublicationOutcome(published=True, status=model.status, comment_id=comment.id)
|
|
140
136
|
|
|
141
137
|
|
|
138
|
+
def watch_workflow_run(
|
|
139
|
+
port: GitHubPort,
|
|
140
|
+
event_value: JsonValue,
|
|
141
|
+
*,
|
|
142
|
+
poll_interval: float = 5.0,
|
|
143
|
+
sleep: Callable[[float], None] = time.sleep,
|
|
144
|
+
) -> PublicationOutcome:
|
|
145
|
+
"""Serialize live dashboard updates from authoritative GitHub job state."""
|
|
146
|
+
event = WorkflowRunEvent.from_value(event_value)
|
|
147
|
+
if event.event != "pull_request" or event.action not in {"requested", "in_progress"}:
|
|
148
|
+
return PublicationOutcome(published=False)
|
|
149
|
+
number = event.pull_request or _resolve_pull_request(port, event.workflow_head_sha)
|
|
150
|
+
if number is None:
|
|
151
|
+
return PublicationOutcome(published=False)
|
|
152
|
+
pull = _pull_request(port, number)
|
|
153
|
+
if event.pull_head_sha is not None and event.pull_head_sha != pull.head_sha:
|
|
154
|
+
return PublicationOutcome(published=False)
|
|
155
|
+
if not _is_latest_run(port, event, number):
|
|
156
|
+
return PublicationOutcome(published=False)
|
|
157
|
+
graph = Graph.from_yaml(_repository_file(port, "quality-graph.yml", pull.base_sha))
|
|
158
|
+
nodes = tuple(DashboardNode(node.id, node.title) for node in graph.nodes)
|
|
159
|
+
run = DashboardRun(event.id, event.attempt, pull.head_sha, event.url)
|
|
160
|
+
_publish_check(port, pending_dashboard(graph, run))
|
|
161
|
+
|
|
162
|
+
def is_current() -> bool:
|
|
163
|
+
return _is_latest_run(port, event, number)
|
|
164
|
+
|
|
165
|
+
while not publish_workflow_jobs(port, number, nodes, run, is_current=is_current):
|
|
166
|
+
sleep(poll_interval)
|
|
167
|
+
return PublicationOutcome(published=True, status=ResultStatus.IN_PROGRESS)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def publish_workflow_jobs(
|
|
171
|
+
port: GitHubPort,
|
|
172
|
+
number: int,
|
|
173
|
+
nodes: tuple[DashboardNode, ...],
|
|
174
|
+
run: DashboardRun,
|
|
175
|
+
*,
|
|
176
|
+
is_current: Callable[[], bool] = lambda: True,
|
|
177
|
+
) -> bool:
|
|
178
|
+
"""Merge authoritative job lifecycle into the single live dashboard."""
|
|
179
|
+
by_title = {node.title: node.node_id for node in nodes}
|
|
180
|
+
statuses: dict[str, ResultStatus] = {}
|
|
181
|
+
terminal = True
|
|
182
|
+
for value in _workflow_jobs(port, run.id):
|
|
183
|
+
job = _object(value, "workflow job")
|
|
184
|
+
name = _string(job.get("name"), "workflow job name")
|
|
185
|
+
node_id = by_title.get(name)
|
|
186
|
+
if node_id is None:
|
|
187
|
+
continue
|
|
188
|
+
status = _workflow_job_status(job)
|
|
189
|
+
statuses[node_id] = status
|
|
190
|
+
terminal = terminal and status not in {ResultStatus.WAITING, ResultStatus.IN_PROGRESS}
|
|
191
|
+
terminal = terminal and len(statuses) == len(nodes)
|
|
192
|
+
if terminal or not is_current():
|
|
193
|
+
return True
|
|
194
|
+
existing = find_managed_comment(port, number, DASHBOARD_MARKER)
|
|
195
|
+
previous_labels = parse_label_state(existing.body) if existing is not None else frozenset()
|
|
196
|
+
model = live_dashboard(nodes, statuses, run, managed_labels=tuple(sorted(previous_labels)))
|
|
197
|
+
upsert_managed_comment(port, number, DASHBOARD_MARKER, render_dashboard(model))
|
|
198
|
+
return terminal
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _workflow_jobs(port: GitHubPort, run_id: int) -> tuple[JsonValue, ...]:
|
|
202
|
+
jobs: list[JsonValue] = []
|
|
203
|
+
page = 1
|
|
204
|
+
while True:
|
|
205
|
+
response = _object(
|
|
206
|
+
port.request(
|
|
207
|
+
"GET",
|
|
208
|
+
f"/actions/runs/{run_id}/jobs?filter=latest"
|
|
209
|
+
f"&per_page={GITHUB_PAGE_SIZE}&page={page}",
|
|
210
|
+
),
|
|
211
|
+
"workflow jobs",
|
|
212
|
+
)
|
|
213
|
+
values = _array(response.get("jobs", []), "workflow jobs")
|
|
214
|
+
jobs.extend(values)
|
|
215
|
+
if len(values) < GITHUB_PAGE_SIZE:
|
|
216
|
+
return tuple(jobs)
|
|
217
|
+
page += 1
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _workflow_job_status(job: Mapping[str, JsonValue]) -> ResultStatus:
|
|
221
|
+
status = _string(job.get("status"), "workflow job status")
|
|
222
|
+
if status == "queued":
|
|
223
|
+
return ResultStatus.WAITING
|
|
224
|
+
if status in {"in_progress", "pending", "requested", "waiting"}:
|
|
225
|
+
return ResultStatus.IN_PROGRESS
|
|
226
|
+
if status != "completed":
|
|
227
|
+
message = f"unsupported workflow job status: {status}"
|
|
228
|
+
raise ValueError(message)
|
|
229
|
+
conclusion = _optional_string(job.get("conclusion"), "workflow job conclusion")
|
|
230
|
+
if conclusion == "success":
|
|
231
|
+
return ResultStatus.PASSED
|
|
232
|
+
if conclusion == "skipped":
|
|
233
|
+
return ResultStatus.SKIPPED
|
|
234
|
+
if conclusion == "cancelled":
|
|
235
|
+
return ResultStatus.CANCELLED
|
|
236
|
+
return ResultStatus.FAILED
|
|
237
|
+
|
|
238
|
+
|
|
142
239
|
def _completed_dashboard(
|
|
143
240
|
port: GitHubPort,
|
|
144
241
|
graph: Graph,
|
|
@@ -156,7 +253,6 @@ def _completed_dashboard(
|
|
|
156
253
|
)
|
|
157
254
|
try:
|
|
158
255
|
results = download_results(port, expectation)
|
|
159
|
-
_require_complete_results(results, expectation.node_ids)
|
|
160
256
|
except ArtifactError as error:
|
|
161
257
|
pending = pending_dashboard(graph, run)
|
|
162
258
|
return (
|
|
@@ -164,22 +260,22 @@ def _completed_dashboard(
|
|
|
164
260
|
pending,
|
|
165
261
|
status=ResultStatus.FAILED,
|
|
166
262
|
message=f"The final dashboard could not be assembled: {error}",
|
|
263
|
+
rows=tuple(replace(row, status=ResultStatus.FAILED) for row in pending.rows),
|
|
167
264
|
),
|
|
168
265
|
None,
|
|
169
266
|
)
|
|
170
267
|
approvals = approval_ledger(port, pull.number)
|
|
171
268
|
effective = effective_graph(graph, results, approvals)
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
def _require_complete_results(
|
|
176
|
-
results: Mapping[str, object],
|
|
177
|
-
node_ids: frozenset[str],
|
|
178
|
-
) -> None:
|
|
179
|
-
missing = node_ids - results.keys()
|
|
269
|
+
model = final_dashboard(graph, effective.results, run)
|
|
270
|
+
missing = expectation.node_ids - results.keys()
|
|
180
271
|
if missing:
|
|
181
|
-
|
|
182
|
-
|
|
272
|
+
model = replace(
|
|
273
|
+
model,
|
|
274
|
+
status=ResultStatus.FAILED,
|
|
275
|
+
message=f"Missing result artifacts for nodes: {', '.join(sorted(missing))}.",
|
|
276
|
+
)
|
|
277
|
+
return model, None
|
|
278
|
+
return model, effective.results
|
|
183
279
|
|
|
184
280
|
|
|
185
281
|
def _pull_request(port: GitHubPort, number: int) -> PullRequestState:
|
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import html
|
|
6
6
|
from typing import TYPE_CHECKING
|
|
7
7
|
|
|
8
|
+
from qg_github.controls import render_control
|
|
8
9
|
from quality_graph_core.result import Finding, Result, ResultStatus
|
|
9
10
|
|
|
10
11
|
if TYPE_CHECKING:
|
|
@@ -45,7 +46,8 @@ def render_job_summary(result: Result) -> str:
|
|
|
45
46
|
if result.notes:
|
|
46
47
|
lines.extend(("", "### Notes", ""))
|
|
47
48
|
lines.extend(f"- {html.escape(note)}" for note in result.notes)
|
|
48
|
-
|
|
49
|
+
body = "\n".join(lines).strip() + "\n"
|
|
50
|
+
return _bounded_with_controls(body, result)
|
|
49
51
|
|
|
50
52
|
|
|
51
53
|
def append_job_summary(path: Path, result: Result) -> None:
|
|
@@ -61,8 +63,9 @@ def _finding_line(finding: Finding) -> str:
|
|
|
61
63
|
location = f" — `{html.escape(finding.location.path)}:{finding.location.start_line}`"
|
|
62
64
|
rule = f" `{html.escape(finding.rule_id)}`" if finding.rule_id else ""
|
|
63
65
|
severity = html.escape(finding.severity.value)
|
|
66
|
+
finding_id = html.escape(finding.id)
|
|
64
67
|
message = html.escape(finding.message)
|
|
65
|
-
return f"- **{severity}**{rule}: {message}{location}"
|
|
68
|
+
return f"- `{finding_id}` — **{severity}**{rule}: {message}{location}"
|
|
66
69
|
|
|
67
70
|
|
|
68
71
|
def _status_icon(status: ResultStatus) -> str:
|
|
@@ -84,13 +87,44 @@ def _code(value: str) -> str:
|
|
|
84
87
|
return value.replace("```", "` ` `")
|
|
85
88
|
|
|
86
89
|
|
|
87
|
-
def
|
|
88
|
-
if
|
|
90
|
+
def _bounded_with_controls(body: str, result: Result) -> str:
|
|
91
|
+
if not result.controls:
|
|
92
|
+
return _bounded(body, MAX_JOB_SUMMARY_CHARACTERS)
|
|
93
|
+
minimum = _control_section((), len(result.controls))
|
|
94
|
+
bounded_body = _bounded(body, MAX_JOB_SUMMARY_CHARACTERS - len(minimum) - 2)
|
|
95
|
+
rendered: list[str] = []
|
|
96
|
+
for control in result.controls:
|
|
97
|
+
candidate = [*rendered, render_control(control, show_commands=True)]
|
|
98
|
+
omitted = len(result.controls) - len(candidate)
|
|
99
|
+
section = _control_section(candidate, omitted)
|
|
100
|
+
if len(f"{bounded_body.rstrip()}\n\n{section}") > MAX_JOB_SUMMARY_CHARACTERS:
|
|
101
|
+
break
|
|
102
|
+
rendered = candidate
|
|
103
|
+
omitted = len(result.controls) - len(rendered)
|
|
104
|
+
return f"{bounded_body.rstrip()}\n\n{_control_section(rendered, omitted)}"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _control_section(controls: list[str] | tuple[str, ...], omitted: int) -> str:
|
|
108
|
+
lines = ["<details><summary>For repository administrators</summary>", ""]
|
|
109
|
+
lines.extend(controls)
|
|
110
|
+
if omitted:
|
|
111
|
+
lines.extend(
|
|
112
|
+
(
|
|
113
|
+
"",
|
|
114
|
+
f"_{omitted} additional actions are available in the result artifact._",
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
lines.extend(("", "</details>"))
|
|
118
|
+
return "\n".join(lines).strip() + "\n"
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _bounded(value: str, maximum: int) -> str:
|
|
122
|
+
if len(value) <= maximum:
|
|
89
123
|
return value
|
|
90
|
-
omitted = len(value) -
|
|
124
|
+
omitted = len(value) - maximum
|
|
91
125
|
while True:
|
|
92
126
|
notice = f"\n\n_Job Summary truncated; {omitted} characters omitted._\n"
|
|
93
|
-
prefix =
|
|
127
|
+
prefix = maximum - len(notice)
|
|
94
128
|
updated = len(value) - prefix
|
|
95
129
|
if updated == omitted:
|
|
96
130
|
return value[:prefix] + notice
|
|
@@ -5,14 +5,18 @@ from __future__ import annotations
|
|
|
5
5
|
import json
|
|
6
6
|
import os
|
|
7
7
|
import sys
|
|
8
|
-
from dataclasses import dataclass
|
|
8
|
+
from dataclasses import dataclass, replace
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
from typing import TYPE_CHECKING, cast
|
|
11
11
|
|
|
12
12
|
from qg_github.annotations import escape_data, publish_annotations
|
|
13
13
|
from qg_github.commands import handle_command
|
|
14
14
|
from qg_github.github import HttpGitHubPort
|
|
15
|
-
from qg_github.publication import
|
|
15
|
+
from qg_github.publication import (
|
|
16
|
+
publish_workflow_run,
|
|
17
|
+
read_event_json,
|
|
18
|
+
watch_workflow_run,
|
|
19
|
+
)
|
|
16
20
|
from qg_github.reporting import append_job_summary
|
|
17
21
|
from quality_graph_core.adapters import (
|
|
18
22
|
AdapterContext,
|
|
@@ -24,7 +28,8 @@ from quality_graph_core.adapters import (
|
|
|
24
28
|
adapter_failure,
|
|
25
29
|
read_report,
|
|
26
30
|
)
|
|
27
|
-
from quality_graph_core.graph import AdapterKind
|
|
31
|
+
from quality_graph_core.graph import AdapterKind, ApprovalPolicy
|
|
32
|
+
from quality_graph_core.policy import policy_controls
|
|
28
33
|
from quality_graph_core.result import FailureKind, JsonValue, Provenance, Result, ResultStatus
|
|
29
34
|
|
|
30
35
|
if TYPE_CHECKING:
|
|
@@ -42,6 +47,7 @@ class CollectionRequest:
|
|
|
42
47
|
result_path: Path
|
|
43
48
|
summary_path: Path | None
|
|
44
49
|
output_path: Path
|
|
50
|
+
approval_policy: ApprovalPolicy
|
|
45
51
|
|
|
46
52
|
@classmethod
|
|
47
53
|
def from_environment(
|
|
@@ -73,6 +79,11 @@ class CollectionRequest:
|
|
|
73
79
|
result_path,
|
|
74
80
|
Path(summary) if summary else None,
|
|
75
81
|
Path(_required(environment, "GITHUB_OUTPUT")),
|
|
82
|
+
ApprovalPolicy(
|
|
83
|
+
_boolean(environment, "QG_APPROVAL_FINDINGS"),
|
|
84
|
+
_boolean(environment, "QG_APPROVAL_FILES"),
|
|
85
|
+
_boolean(environment, "QG_APPROVAL_NODE"),
|
|
86
|
+
),
|
|
76
87
|
)
|
|
77
88
|
|
|
78
89
|
|
|
@@ -81,15 +92,25 @@ def collect(request: CollectionRequest) -> Result:
|
|
|
81
92
|
try:
|
|
82
93
|
if request.adapter is AdapterKind.EXIT_CODE:
|
|
83
94
|
state = "passed" if request.context.command_succeeded else "failed"
|
|
84
|
-
return
|
|
95
|
+
return _with_policy_controls(
|
|
96
|
+
request,
|
|
97
|
+
adapt_exit(request.context, f"The declared command {state}."),
|
|
98
|
+
)
|
|
85
99
|
report = _structured_report(request)
|
|
86
100
|
if request.adapter is AdapterKind.NATIVE:
|
|
87
|
-
return adapt_native(request.context, report)
|
|
101
|
+
return _with_policy_controls(request, adapt_native(request.context, report))
|
|
88
102
|
if request.adapter is AdapterKind.SARIF:
|
|
89
|
-
|
|
90
|
-
|
|
103
|
+
result = adapt_sarif(request.context, report)
|
|
104
|
+
else:
|
|
105
|
+
result = adapt_junit(request.context, report)
|
|
106
|
+
return _with_policy_controls(request, result)
|
|
91
107
|
except AdapterError as error:
|
|
92
|
-
return adapter_failure(request.context, error)
|
|
108
|
+
return _with_policy_controls(request, adapter_failure(request.context, error))
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _with_policy_controls(request: CollectionRequest, result: Result) -> Result:
|
|
112
|
+
controls = policy_controls(result.node_id, result.findings, request.approval_policy)
|
|
113
|
+
return replace(result, controls=controls)
|
|
93
114
|
|
|
94
115
|
|
|
95
116
|
def _structured_report(request: CollectionRequest) -> bytes:
|
|
@@ -126,6 +147,10 @@ def main(arguments: Sequence[str] | None = None) -> int:
|
|
|
126
147
|
event = read_event_json(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())
|
|
127
148
|
publish_workflow_run(HttpGitHubPort.from_environment(), event)
|
|
128
149
|
return 0
|
|
150
|
+
if operation == ["watch"]:
|
|
151
|
+
event = read_event_json(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())
|
|
152
|
+
watch_workflow_run(HttpGitHubPort.from_environment(), event)
|
|
153
|
+
return 0
|
|
129
154
|
if operation == ["command"]:
|
|
130
155
|
event = read_event_json(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())
|
|
131
156
|
handle_command(HttpGitHubPort.from_environment(), event)
|
|
@@ -185,6 +210,14 @@ def _required(environment: Mapping[str, str], name: str) -> str:
|
|
|
185
210
|
return value
|
|
186
211
|
|
|
187
212
|
|
|
213
|
+
def _boolean(environment: Mapping[str, str], name: str) -> bool:
|
|
214
|
+
value = _required(environment, name)
|
|
215
|
+
if value not in {"true", "false"}:
|
|
216
|
+
message = f"Environment variable must be true or false: {name}"
|
|
217
|
+
raise ValueError(message)
|
|
218
|
+
return value == "true"
|
|
219
|
+
|
|
220
|
+
|
|
188
221
|
def _result_exit_code(result: Result) -> int:
|
|
189
222
|
if result.status not in {ResultStatus.FAILED, ResultStatus.CANCELLED}:
|
|
190
223
|
return 0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|