quality-graph-gitlab 0.2.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.
- qg_gitlab/__init__.py +25 -0
- qg_gitlab/admission.py +231 -0
- qg_gitlab/api.py +223 -0
- qg_gitlab/compiler.py +349 -0
- qg_gitlab/markers.py +11 -0
- qg_gitlab/publication.py +588 -0
- qg_gitlab/py.typed +0 -0
- qg_gitlab/runtime.py +238 -0
- quality_graph_gitlab-0.2.0.dist-info/METADATA +31 -0
- quality_graph_gitlab-0.2.0.dist-info/RECORD +12 -0
- quality_graph_gitlab-0.2.0.dist-info/WHEEL +4 -0
- quality_graph_gitlab-0.2.0.dist-info/entry_points.txt +5 -0
qg_gitlab/runtime.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"""Execute repository commands and collect GitLab-native result artifacts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import os
|
|
7
|
+
import shutil
|
|
8
|
+
import sys
|
|
9
|
+
from functools import partial
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from time import monotonic, sleep
|
|
12
|
+
from typing import TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
import anyio
|
|
15
|
+
|
|
16
|
+
from qg_gitlab.api import HttpGitLab, integer, object_value, string
|
|
17
|
+
from qg_gitlab.compiler import (
|
|
18
|
+
QUALITY_EXIT_CODE,
|
|
19
|
+
configuration,
|
|
20
|
+
execution_graphs,
|
|
21
|
+
graph_digest,
|
|
22
|
+
publishes_mr,
|
|
23
|
+
)
|
|
24
|
+
from qg_gitlab.markers import execution_marker, gate_marker
|
|
25
|
+
from qg_gitlab.publication import publish_from_environment
|
|
26
|
+
from quality_graph_core.adapters import (
|
|
27
|
+
AdapterContext,
|
|
28
|
+
AdapterError,
|
|
29
|
+
adapt_exit,
|
|
30
|
+
adapt_junit,
|
|
31
|
+
adapt_native,
|
|
32
|
+
adapt_sarif,
|
|
33
|
+
adapter_failure,
|
|
34
|
+
read_report,
|
|
35
|
+
)
|
|
36
|
+
from quality_graph_core.graph import AdapterKind, Graph
|
|
37
|
+
from quality_graph_core.result import FailureKind, GitLabProvenance, Result
|
|
38
|
+
|
|
39
|
+
if TYPE_CHECKING:
|
|
40
|
+
from collections.abc import Mapping, Sequence
|
|
41
|
+
|
|
42
|
+
from quality_graph_core.graph import Node, Profile, Step
|
|
43
|
+
|
|
44
|
+
GATE_TIMEOUT_SECONDS = 240
|
|
45
|
+
GATE_INTERVAL_SECONDS = 3
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def execution(root: Path, flow_id: str, node_id: str) -> tuple[Graph, Graph, Node, Profile]:
|
|
49
|
+
"""Resolve the exact declared execution without importing the CLI."""
|
|
50
|
+
graph = Graph.from_yaml((root / "qg.yaml").read_text())
|
|
51
|
+
configuration(graph)
|
|
52
|
+
for selected_id, _event, projected in execution_graphs(graph):
|
|
53
|
+
if selected_id == flow_id:
|
|
54
|
+
for node in projected.nodes:
|
|
55
|
+
if node.id == node_id:
|
|
56
|
+
return graph, projected, node, graph.expanded_profiles()[node.profile]
|
|
57
|
+
message = f"unknown GitLab execution {flow_id}/{node_id}"
|
|
58
|
+
raise ValueError(message)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def provenance(
|
|
62
|
+
graph: Graph,
|
|
63
|
+
flow_id: str,
|
|
64
|
+
node: Node,
|
|
65
|
+
environment: Mapping[str, str],
|
|
66
|
+
) -> GitLabProvenance:
|
|
67
|
+
"""Collect native execution identities from the GitLab runner environment."""
|
|
68
|
+
mr = environment.get("CI_MERGE_REQUEST_IID")
|
|
69
|
+
if environment["CI_SERVER_URL"].rstrip("/") != configuration(graph)["server-url"]:
|
|
70
|
+
message = "GitLab execution server does not match the declaration"
|
|
71
|
+
raise ValueError(message)
|
|
72
|
+
if mr and environment.get("CI_MERGE_REQUEST_EVENT_TYPE", "detached") != "detached":
|
|
73
|
+
message = "GitLab merged-results and merge-train pipelines are not supported"
|
|
74
|
+
raise ValueError(message)
|
|
75
|
+
return GitLabProvenance(
|
|
76
|
+
environment["CI_SERVER_URL"].rstrip("/"),
|
|
77
|
+
int(environment["CI_PROJECT_ID"]),
|
|
78
|
+
environment["CI_COMMIT_SHA"],
|
|
79
|
+
int(environment["CI_PIPELINE_ID"]),
|
|
80
|
+
int(environment["CI_JOB_ID"]),
|
|
81
|
+
graph_digest(graph),
|
|
82
|
+
int(environment["CI_MERGE_REQUEST_PROJECT_ID"]) if mr else None,
|
|
83
|
+
int(mr) if mr else None,
|
|
84
|
+
flow_id,
|
|
85
|
+
node.operation_id or node.id,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def run_step(step: Step, root: Path, environment: Mapping[str, str]) -> bool:
|
|
90
|
+
"""Run one declared shell command in its explicit working directory."""
|
|
91
|
+
shell = shutil.which(step.shell or "sh")
|
|
92
|
+
if shell is None or step.run is None:
|
|
93
|
+
message = "GitLab execution requires an available shell and a run command"
|
|
94
|
+
raise ValueError(message)
|
|
95
|
+
command = (shell, "-e", "-c", step.run)
|
|
96
|
+
directory = root / (step.working_directory or ".")
|
|
97
|
+
completed = anyio.run(
|
|
98
|
+
partial(
|
|
99
|
+
anyio.run_process,
|
|
100
|
+
command,
|
|
101
|
+
cwd=directory,
|
|
102
|
+
env={**environment, **step.environment},
|
|
103
|
+
stdout=None,
|
|
104
|
+
stderr=None,
|
|
105
|
+
check=False,
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
return completed.returncode == 0
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def collect(context: AdapterContext, node: Node, root: Path) -> Result:
|
|
112
|
+
"""Adapt the existing command execution without rerunning any checks."""
|
|
113
|
+
if node.result.kind is AdapterKind.EXIT_CODE:
|
|
114
|
+
return adapt_exit(context)
|
|
115
|
+
try:
|
|
116
|
+
report = read_report(root, node.result.path or "")
|
|
117
|
+
adapters = {
|
|
118
|
+
AdapterKind.NATIVE: adapt_native,
|
|
119
|
+
AdapterKind.JUNIT: adapt_junit,
|
|
120
|
+
AdapterKind.SARIF: adapt_sarif,
|
|
121
|
+
}
|
|
122
|
+
return adapters[node.result.kind](context, report)
|
|
123
|
+
except (AdapterError, OSError, ValueError) as error:
|
|
124
|
+
return adapter_failure(context, AdapterError(str(error)))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def execute(root: Path, flow_id: str, node_id: str) -> int:
|
|
128
|
+
"""Execute each step once and persist one versioned result for this job."""
|
|
129
|
+
graph, _projected, node, profile = execution(root, flow_id, node_id)
|
|
130
|
+
context = provenance(graph, flow_id, node, os.environ)
|
|
131
|
+
if context.merge_request is not None and publishes_mr(graph, flow_id):
|
|
132
|
+
marker = execution_marker(
|
|
133
|
+
context.project_id, context.pipeline_id, context.job_id, context.graph_digest
|
|
134
|
+
)
|
|
135
|
+
if not _acknowledged(graph, marker):
|
|
136
|
+
sys.stderr.write("Publisher did not acknowledge the current job execution.\n")
|
|
137
|
+
return 1
|
|
138
|
+
output = root / ".qg/results" / flow_id / f"{node_id}.json"
|
|
139
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
140
|
+
output.unlink(missing_ok=True)
|
|
141
|
+
environment = {
|
|
142
|
+
**os.environ,
|
|
143
|
+
**profile.environment,
|
|
144
|
+
**node.environment,
|
|
145
|
+
"QG_NODE_ID": node_id,
|
|
146
|
+
"QG_NODE_TITLE": node.title,
|
|
147
|
+
"QG_FLOW_ID": flow_id,
|
|
148
|
+
"QG_OPERATION_ID": node.operation_id or node_id,
|
|
149
|
+
"QG_GRAPH_DIGEST": context.graph_digest,
|
|
150
|
+
}
|
|
151
|
+
succeeded = all(
|
|
152
|
+
run_step(step, root, environment)
|
|
153
|
+
for step in (*profile.setup, *(node.steps or (node.step,)))
|
|
154
|
+
)
|
|
155
|
+
result = collect(AdapterContext(node.id, node.title, succeeded, context), node, root)
|
|
156
|
+
output.write_text(result.to_json())
|
|
157
|
+
if result.failure_kind is FailureKind.QUALITY:
|
|
158
|
+
return QUALITY_EXIT_CODE
|
|
159
|
+
return int(result.failure_kind is not None)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def gate(root: Path, flow_id: str) -> int:
|
|
163
|
+
"""Hold the native pipeline until its trusted external status is installed."""
|
|
164
|
+
graph = Graph.from_yaml((root / "qg.yaml").read_text())
|
|
165
|
+
pipeline = int(os.environ["CI_PIPELINE_ID"])
|
|
166
|
+
digest = graph_digest(graph)
|
|
167
|
+
marker = gate_marker(int(os.environ["CI_PROJECT_ID"]), pipeline, flow_id, digest)
|
|
168
|
+
if _acknowledged(graph, marker):
|
|
169
|
+
return 0
|
|
170
|
+
sys.stderr.write("Trusted Quality Graph status was not installed before the gate deadline.\n")
|
|
171
|
+
return 1
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _acknowledged(graph: Graph, marker: str) -> bool:
|
|
175
|
+
settings = configuration(graph)
|
|
176
|
+
actor = integer(settings.get("publisher-user-id"), "publisher-user-id")
|
|
177
|
+
project = int(os.environ["CI_MERGE_REQUEST_PROJECT_ID"])
|
|
178
|
+
mr = int(os.environ["CI_MERGE_REQUEST_IID"])
|
|
179
|
+
deadline = monotonic() + GATE_TIMEOUT_SECONDS
|
|
180
|
+
endpoint = (
|
|
181
|
+
string(settings["api-url"], "API URL")
|
|
182
|
+
if "api-url" in settings
|
|
183
|
+
else os.environ.get("CI_API_V4_URL")
|
|
184
|
+
)
|
|
185
|
+
with HttpGitLab(
|
|
186
|
+
os.environ["CI_SERVER_URL"], os.environ["CI_JOB_TOKEN"], api_url=endpoint, job_token=True
|
|
187
|
+
) as api:
|
|
188
|
+
while monotonic() < deadline:
|
|
189
|
+
current = object_value(
|
|
190
|
+
api.request("GET", f"/projects/{project}/merge_requests/{mr}"),
|
|
191
|
+
"current merge request",
|
|
192
|
+
)
|
|
193
|
+
head = current.get("head_pipeline")
|
|
194
|
+
if current.get("state") != "opened":
|
|
195
|
+
return False
|
|
196
|
+
expected_pipeline = int(os.environ["CI_PIPELINE_ID"])
|
|
197
|
+
if (
|
|
198
|
+
not isinstance(head, dict)
|
|
199
|
+
or integer(head.get("id"), "MR pipeline ID") < expected_pipeline
|
|
200
|
+
):
|
|
201
|
+
sleep(GATE_INTERVAL_SECONDS)
|
|
202
|
+
continue
|
|
203
|
+
if (
|
|
204
|
+
head.get("id") != expected_pipeline
|
|
205
|
+
or current.get("sha") != os.environ["CI_COMMIT_SHA"]
|
|
206
|
+
):
|
|
207
|
+
return False
|
|
208
|
+
for note in api.pages(f"/projects/{project}/merge_requests/{mr}/notes"):
|
|
209
|
+
author = note.get("author")
|
|
210
|
+
body = note.get("body")
|
|
211
|
+
if (
|
|
212
|
+
isinstance(author, dict)
|
|
213
|
+
and author.get("id") == actor
|
|
214
|
+
and isinstance(body, str)
|
|
215
|
+
and marker in body
|
|
216
|
+
):
|
|
217
|
+
return True
|
|
218
|
+
sleep(GATE_INTERVAL_SECONDS)
|
|
219
|
+
return False
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def main(arguments: Sequence[str] | None = None) -> int:
|
|
223
|
+
"""Dispatch the independently installed GitLab runtime."""
|
|
224
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
225
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
226
|
+
commands.add_parser("publish")
|
|
227
|
+
for name in ("execute", "gate"):
|
|
228
|
+
command = commands.add_parser(name)
|
|
229
|
+
command.add_argument("--root", type=Path, default=Path.cwd())
|
|
230
|
+
command.add_argument("--flow", required=True)
|
|
231
|
+
if name == "execute":
|
|
232
|
+
command.add_argument("--node", required=True)
|
|
233
|
+
args = parser.parse_args(arguments)
|
|
234
|
+
if args.command == "publish":
|
|
235
|
+
return publish_from_environment()
|
|
236
|
+
if args.command == "execute":
|
|
237
|
+
return execute(args.root, args.flow, args.node)
|
|
238
|
+
return gate(args.root, args.flow)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: quality-graph-gitlab
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: GitLab provider and CI runtime for Quality Graph
|
|
5
|
+
Project-URL: Homepage, https://quality-graph.xyz/
|
|
6
|
+
Project-URL: Documentation, https://quality-graph.xyz/
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: anyio>=4.0
|
|
10
|
+
Requires-Dist: httpx>=0.28.1
|
|
11
|
+
Requires-Dist: pyyaml>=6.0.2
|
|
12
|
+
Requires-Dist: quality-graph-core==0.2.0
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Quality Graph GitLab
|
|
16
|
+
|
|
17
|
+
The independently installed GitLab provider and CI runtime for Quality Graph.
|
|
18
|
+
Install the CLI and this provider at the same exact release version:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
uv tool install quality-graph-cli==X.Y.Z --with quality-graph-gitlab==X.Y.Z
|
|
22
|
+
qg init --provider gitlab
|
|
23
|
+
qg generate
|
|
24
|
+
qg validate
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Consumer checks execute as native GitLab jobs. A separate trusted GitLab project
|
|
28
|
+
publishes admitted results and handles authorized merge-request commands.
|
|
29
|
+
|
|
30
|
+
GitLab provenance identifies the instance, project, pipeline and exact job;
|
|
31
|
+
GitHub workflow attempts are not used. GitHub is not a runtime dependency.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
qg_gitlab/__init__.py,sha256=zrw_p05qRcecB6MSj1MiAoitYNlnsV7WT8I8lDXXoTk,857
|
|
2
|
+
qg_gitlab/admission.py,sha256=T-sT3PqqO7iFbMQ2CZRHA67RLmTC8IQyToDJ3BQ5eNw,9117
|
|
3
|
+
qg_gitlab/api.py,sha256=SpL-jVTIdF6B4DBJiPCjSxQPVi_9hwq2DZcRF4jf-Tw,8334
|
|
4
|
+
qg_gitlab/compiler.py,sha256=bLfhBarV5lIkxtzsMsyteeCy81Hyb_Uh8LWUWE8YIz0,14203
|
|
5
|
+
qg_gitlab/markers.py,sha256=cmrZSWG2LAGJxBMH_5SKIIdZyUgegH7LTx5Q-ra_k-c,524
|
|
6
|
+
qg_gitlab/publication.py,sha256=KG2daTkgKULv4TaPCGGP0h-Bxp9o8EvCK7mMLWBkR6s,22336
|
|
7
|
+
qg_gitlab/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
qg_gitlab/runtime.py,sha256=8_fYqsbvfL3WKKf_pRIN_YzOckwHIiGUBxa918JgoEQ,9009
|
|
9
|
+
quality_graph_gitlab-0.2.0.dist-info/METADATA,sha256=TBzOI36ZZAAdGg9SJNVddF0IjTdLIf7V4UCsJ1n5lUI,1057
|
|
10
|
+
quality_graph_gitlab-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
11
|
+
quality_graph_gitlab-0.2.0.dist-info/entry_points.txt,sha256=Ga7LJkB72P31BHqf-8TmlxE_McJbmikFdrN-lxcgD2Y,97
|
|
12
|
+
quality_graph_gitlab-0.2.0.dist-info/RECORD,,
|