open-quality-cli 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.
- cli/__init__.py +1 -0
- cli/cli.py +248 -0
- cli/core.py +553 -0
- cli/model.py +63 -0
- cli/provider.py +40 -0
- cli/providers/__init__.py +5 -0
- cli/providers/github.py +106 -0
- cli/providers/gitlab.py +73 -0
- cli/providers/interfaces.py +24 -0
- cli/providers/jira_cloud.py +122 -0
- cli/providers/openproject.py +673 -0
- cli/renderer.py +81 -0
- open_quality_cli-0.1.0.dist-info/METADATA +168 -0
- open_quality_cli-0.1.0.dist-info/RECORD +19 -0
- open_quality_cli-0.1.0.dist-info/WHEEL +5 -0
- open_quality_cli-0.1.0.dist-info/entry_points.txt +2 -0
- open_quality_cli-0.1.0.dist-info/licenses/LICENSE +674 -0
- open_quality_cli-0.1.0.dist-info/licenses/NOTICE +6 -0
- open_quality_cli-0.1.0.dist-info/top_level.txt +1 -0
cli/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Open Quality reference implementation."""
|
cli/cli.py
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from . import renderer
|
|
8
|
+
from .core import evaluate, load_contract, load_state, validate
|
|
9
|
+
from .provider import (
|
|
10
|
+
ExternalResource,
|
|
11
|
+
OpenProjectClient,
|
|
12
|
+
OpenProjectProvider,
|
|
13
|
+
apply,
|
|
14
|
+
load_config,
|
|
15
|
+
load_members,
|
|
16
|
+
load_state as load_provider_state,
|
|
17
|
+
plan,
|
|
18
|
+
save_state,
|
|
19
|
+
)
|
|
20
|
+
from .providers import github, gitlab, jira_cloud
|
|
21
|
+
import yaml
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _valid(path: str):
|
|
25
|
+
bundle = load_contract(path)
|
|
26
|
+
errors = validate(bundle)
|
|
27
|
+
if errors:
|
|
28
|
+
for error in errors:
|
|
29
|
+
print("-", error, file=sys.stderr)
|
|
30
|
+
raise ValueError(f"contract has {len(errors)} validation error(s)")
|
|
31
|
+
return bundle
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _provider(args: list[str], is_apply: bool) -> int:
|
|
35
|
+
parser = argparse.ArgumentParser(
|
|
36
|
+
prog=f"oq {'apply' if is_apply else 'plan'}", add_help=False
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument("--target")
|
|
39
|
+
parser.add_argument("--provider-role")
|
|
40
|
+
parser.add_argument("--state")
|
|
41
|
+
parser.add_argument("--members")
|
|
42
|
+
parser.add_argument("directory", nargs="?")
|
|
43
|
+
values = parser.parse_args(args)
|
|
44
|
+
if not values.target or not values.directory:
|
|
45
|
+
raise ValueError(
|
|
46
|
+
f"usage: oq {'apply' if is_apply else 'plan'} --target <target.yaml> [--state <state.json>] <quality-directory>"
|
|
47
|
+
)
|
|
48
|
+
target_document = yaml.safe_load(Path(values.target).read_text()) or {}
|
|
49
|
+
provider_role = values.provider_role
|
|
50
|
+
if "providers" in target_document:
|
|
51
|
+
providers = target_document["providers"]
|
|
52
|
+
if not isinstance(providers, dict):
|
|
53
|
+
raise ValueError("providers must be a mapping")
|
|
54
|
+
if not provider_role:
|
|
55
|
+
raise ValueError("--provider-role is required when project file contains providers")
|
|
56
|
+
target_document = providers.get(provider_role)
|
|
57
|
+
if not isinstance(target_document, dict):
|
|
58
|
+
raise ValueError(f"provider role {provider_role!r} was not found")
|
|
59
|
+
elif provider_role:
|
|
60
|
+
raise ValueError("--provider-role is only valid for a project file with providers")
|
|
61
|
+
target_provider = target_document.get("provider")
|
|
62
|
+
if target_provider == "jira-cloud":
|
|
63
|
+
return _jira_provider(values, is_apply, target_document, provider_role)
|
|
64
|
+
if target_provider == "github":
|
|
65
|
+
config = github.load_config(target_document, provider_role)
|
|
66
|
+
bundle = _valid(values.directory)
|
|
67
|
+
provider_config = target_document.get("config") or {}
|
|
68
|
+
members_path = values.members or config.members_file
|
|
69
|
+
if "members" in provider_config and not values.members:
|
|
70
|
+
members = github.load_members({"provider": "github", "members": provider_config["members"]})
|
|
71
|
+
elif members_path and not Path(members_path).is_absolute():
|
|
72
|
+
members_path = str(Path(values.target).parent / members_path)
|
|
73
|
+
members = github.load_members(members_path)
|
|
74
|
+
else:
|
|
75
|
+
members = github.load_members(members_path) if members_path else []
|
|
76
|
+
print(f"Open Quality provider plan\n\nProvider: github\nRole: {config.name}\n")
|
|
77
|
+
print(f" ENSURE GitHubRepository {config.owner}/{config.repository}")
|
|
78
|
+
for member in members:
|
|
79
|
+
print(f" INVITE GitHubCollaborator {member.username} ({member.permission})")
|
|
80
|
+
if is_apply:
|
|
81
|
+
print(f"\nApplied GitHub provisioning for {github.apply(config, members)} repository/repositories.")
|
|
82
|
+
return 0
|
|
83
|
+
if target_provider == "gitlab":
|
|
84
|
+
config = gitlab.load_config(target_document, provider_role)
|
|
85
|
+
bundle = _valid(values.directory)
|
|
86
|
+
members = gitlab.load_members((target_document.get("config") or {}))
|
|
87
|
+
print(f"Open Quality provider plan\n\nProvider: gitlab\nRole: {config.name}\n")
|
|
88
|
+
print(f" ENSURE GitLabProject {config.project}")
|
|
89
|
+
for member in members: print(f" INVITE GitLabMember {member.username} ({member.access_level})")
|
|
90
|
+
if is_apply: print(f"\nApplied GitLab provisioning for {gitlab.apply(config, members)} project(s).")
|
|
91
|
+
return 0
|
|
92
|
+
config = load_config(target_document, provider_role)
|
|
93
|
+
provider_config = target_document.get("config") or {}
|
|
94
|
+
members_path = values.members or config.members_file
|
|
95
|
+
if "members" in provider_config and not values.members:
|
|
96
|
+
members = load_members({"provider": "openproject", "members": provider_config["members"]})
|
|
97
|
+
elif members_path and not Path(members_path).is_absolute():
|
|
98
|
+
members_path = str(Path(values.target).parent / members_path)
|
|
99
|
+
members = load_members(members_path)
|
|
100
|
+
else:
|
|
101
|
+
members = load_members(members_path) if members_path else []
|
|
102
|
+
state_path = values.state or values.target + (f".{provider_role}" if provider_role else "") + ".state.json"
|
|
103
|
+
state = load_provider_state(state_path, config.name)
|
|
104
|
+
bundle = _valid(values.directory)
|
|
105
|
+
existing_project = None
|
|
106
|
+
provider = None
|
|
107
|
+
client = None
|
|
108
|
+
if is_apply:
|
|
109
|
+
provider = OpenProjectProvider(config, config.token())
|
|
110
|
+
client = provider.client
|
|
111
|
+
if bundle.project and bundle.project.id not in state.resources:
|
|
112
|
+
existing_project = client.find_project(bundle.project.id)
|
|
113
|
+
operations = provider.plan(bundle, state, members) if provider else plan(bundle, state, config, members)
|
|
114
|
+
if existing_project and bundle.project:
|
|
115
|
+
project = next(item for item in operations if item.resource_id == bundle.project.id)
|
|
116
|
+
project.action = "no-op"
|
|
117
|
+
state.resources[bundle.project.id] = ExternalResource(
|
|
118
|
+
bundle.project.id, "Project", existing_project[0], existing_project[1], project.hash, ""
|
|
119
|
+
)
|
|
120
|
+
print(
|
|
121
|
+
"Open Quality provider plan\n\nProvider: "
|
|
122
|
+
+ config.provider
|
|
123
|
+
+ "\nRole: "
|
|
124
|
+
+ config.name
|
|
125
|
+
+ "\n"
|
|
126
|
+
)
|
|
127
|
+
groups = (
|
|
128
|
+
("Provisionamento", {"Project", "KanbanBoard", "ProjectMember"}),
|
|
129
|
+
("Workflow", {"QualityRequirement", "Stage"}),
|
|
130
|
+
("Revisão", {"CodeReviewer"}),
|
|
131
|
+
)
|
|
132
|
+
displayed: set[str] = set()
|
|
133
|
+
for title, kinds in groups:
|
|
134
|
+
selected = [item for item in operations if item.kind in kinds]
|
|
135
|
+
if not selected:
|
|
136
|
+
continue
|
|
137
|
+
print(f"\n{title}")
|
|
138
|
+
for item in selected:
|
|
139
|
+
displayed.add(item.resource_id)
|
|
140
|
+
print(f" {item.action.upper():<7} {item.kind:<18} {item.subject}")
|
|
141
|
+
for item in operations:
|
|
142
|
+
if item.resource_id not in displayed:
|
|
143
|
+
print(f" {item.action.upper():<7} {item.kind:<18} {item.subject}")
|
|
144
|
+
print(
|
|
145
|
+
f"\nPlan: {sum(item.action == 'create' for item in operations)} to create, {sum(item.action == 'update' for item in operations)} to update, {sum(item.action == 'no-op' for item in operations)} unchanged"
|
|
146
|
+
)
|
|
147
|
+
if is_apply:
|
|
148
|
+
assert client is not None
|
|
149
|
+
provider.apply(operations, state, lambda current: save_state(state_path, current))
|
|
150
|
+
print(
|
|
151
|
+
f"\nApplied {sum(item.action != 'no-op' for item in operations)} resource(s); state saved to {state_path}"
|
|
152
|
+
)
|
|
153
|
+
return 0
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _jira_provider(values, is_apply: bool, target_document=None, provider_role="") -> int:
|
|
157
|
+
config = jira_cloud.load_config(target_document or values.target, provider_role)
|
|
158
|
+
provider_config = (target_document or {}).get("config") or {}
|
|
159
|
+
members_path = values.members or config.members_file
|
|
160
|
+
if "members" in provider_config and not values.members:
|
|
161
|
+
members = jira_cloud.load_members({"provider": "jira-cloud", "members": provider_config["members"]})
|
|
162
|
+
else:
|
|
163
|
+
if members_path and not Path(members_path).is_absolute(): members_path = str(Path(values.target).parent / members_path)
|
|
164
|
+
members = jira_cloud.load_members(members_path) if members_path else []
|
|
165
|
+
state_path = values.state or values.target + (f".{provider_role}" if provider_role else "") + ".state.json"
|
|
166
|
+
state = jira_cloud.load_state(state_path, config.name)
|
|
167
|
+
bundle = _valid(values.directory)
|
|
168
|
+
ops = jira_cloud.plan(bundle, state, config, members)
|
|
169
|
+
print(f"Open Quality provider plan\n\nProvider: jira-cloud\nRole: {config.name}\n")
|
|
170
|
+
for op in ops: print(f" {op.action.upper():<7} {op.kind:<18} {op.subject}")
|
|
171
|
+
print(f"\nPlan: {sum(x.action=='create' for x in ops)} to create, {sum(x.action=='update' for x in ops)} to update, {sum(x.action=='no-op' for x in ops)} unchanged")
|
|
172
|
+
if is_apply:
|
|
173
|
+
email, token = config.credentials(); jira_cloud.apply(ops, state, jira_cloud.JiraClient(config,email,token), config, lambda current: jira_cloud.save_state(state_path,current))
|
|
174
|
+
print(f"\nApplied {sum(x.action != 'no-op' for x in ops)} resource(s); state saved to {state_path}")
|
|
175
|
+
return 0
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def run(args: list[str]) -> int:
|
|
179
|
+
if not args:
|
|
180
|
+
raise ValueError("usage: oq <validate|graph|evaluate|status|plan|apply> ...")
|
|
181
|
+
command, rest = args[0], args[1:]
|
|
182
|
+
if command in {"plan", "apply"}:
|
|
183
|
+
return _provider(rest, command == "apply")
|
|
184
|
+
if command == "validate":
|
|
185
|
+
if len(rest) != 1:
|
|
186
|
+
raise ValueError("usage: oq validate <quality-directory>")
|
|
187
|
+
bundle = _valid(rest[0])
|
|
188
|
+
documentation_count = sum(
|
|
189
|
+
item.spec.get("category") == "documentation"
|
|
190
|
+
for item in bundle.artifacts.values()
|
|
191
|
+
)
|
|
192
|
+
print(
|
|
193
|
+
f"PASS {bundle.project.name}\n\n1 Project\n{len(bundle.workflows)} Workflow(s)\n{len(bundle.requirements)} Requirement(s)\n{len(bundle.stages)} Stage(s)\n{len(bundle.metrics)} QualityMeasure(s)\n{documentation_count} Documentation definition(s)\n{len(bundle.roles)} Role(s)\n{len(bundle.approval_policies)} Approval policy(s)"
|
|
194
|
+
)
|
|
195
|
+
return 0
|
|
196
|
+
if command == "graph":
|
|
197
|
+
parser = argparse.ArgumentParser(prog="oq graph", add_help=False)
|
|
198
|
+
parser.add_argument("--format", default="both")
|
|
199
|
+
parser.add_argument("directory", nargs="?")
|
|
200
|
+
values = parser.parse_args(rest)
|
|
201
|
+
if not values.directory:
|
|
202
|
+
raise ValueError(
|
|
203
|
+
"usage: oq graph [--format ascii|mermaid|both] <quality-directory>"
|
|
204
|
+
)
|
|
205
|
+
bundle = _valid(values.directory)
|
|
206
|
+
if values.format == "ascii":
|
|
207
|
+
print(renderer.ascii(bundle), end="")
|
|
208
|
+
elif values.format == "mermaid":
|
|
209
|
+
print(renderer.mermaid(bundle), end="")
|
|
210
|
+
elif values.format == "both":
|
|
211
|
+
print(
|
|
212
|
+
renderer.ascii(bundle)
|
|
213
|
+
+ "\n```mermaid\n"
|
|
214
|
+
+ renderer.mermaid(bundle)
|
|
215
|
+
+ "```"
|
|
216
|
+
)
|
|
217
|
+
else:
|
|
218
|
+
raise ValueError(f"unknown graph format {values.format!r}")
|
|
219
|
+
return 0
|
|
220
|
+
if command in {"evaluate", "status"}:
|
|
221
|
+
if not 1 <= len(rest) <= 2:
|
|
222
|
+
raise ValueError(f"usage: oq {command} <quality-directory> [state.yaml]")
|
|
223
|
+
state_path = (
|
|
224
|
+
rest[1] if len(rest) == 2 else str(Path(rest[0]).parent / "state.yaml")
|
|
225
|
+
)
|
|
226
|
+
report = evaluate(_valid(rest[0]), load_state(state_path))
|
|
227
|
+
print(
|
|
228
|
+
(
|
|
229
|
+
renderer.evaluation(report)
|
|
230
|
+
if command == "evaluate"
|
|
231
|
+
else renderer.status(report)
|
|
232
|
+
),
|
|
233
|
+
end="",
|
|
234
|
+
)
|
|
235
|
+
return 0 if command == "status" or report.ready else 2
|
|
236
|
+
raise ValueError("usage: oq <validate|graph|evaluate|status|plan|apply> ...")
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def main() -> None:
|
|
240
|
+
try:
|
|
241
|
+
sys.exit(run(sys.argv[1:]))
|
|
242
|
+
except Exception as error:
|
|
243
|
+
print("error:", error, file=sys.stderr)
|
|
244
|
+
sys.exit(1)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
if __name__ == "__main__":
|
|
248
|
+
main()
|