BlueGit

timfa bluegit

Files

bluegit / internal / server / api_openapi.go

api_openapi.go

api_openapi.go 21.9 KiB Download Raw
1package server
2
3import "strings"
4
5// decorateAPI vult request- en response-schema's in voor de kernobjecten.
6func decorateAPI(paths map[string]map[string]any, schemas map[string]any) {
7 for name, schema := range apiSchemas() {
8 schemas[name] = schema
9 }
10 for _, b := range apiBinds() {
11 op := apiOp(paths, b.path, b.method)
12 if op == nil {
13 continue
14 }
15 if b.body != "" {
16 op["requestBody"] = map[string]any{
17 "required": true,
18 "content": jsonSchema(schemaRef(b.body)),
19 }
20 }
21 responses, _ := op["responses"].(map[string]any)
22 if responses == nil {
23 responses = map[string]any{}
24 op["responses"] = responses
25 }
26 if b.empty {
27 responses[b.code] = map[string]any{"description": "No content"}
28 delete(responses, "200")
29 continue
30 }
31 schema := schemaRef(b.resp)
32 if b.array {
33 schema = map[string]any{"type": "array", "items": schema}
34 }
35 responses[b.code] = map[string]any{
36 "description": "OK",
37 "content": jsonSchema(schema),
38 }
39 }
40 if op := apiOp(paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "POST"); op != nil {
41 responses, _ := op["responses"].(map[string]any)
42 if responses != nil {
43 if okResp, ok := responses["200"]; ok {
44 responses["201"] = okResp
45 }
46 }
47 }
48 if op := apiOp(paths, "/repos/{owner}/{repo}/issues/{number}/notes", "POST"); op != nil {
49 responses, _ := op["responses"].(map[string]any)
50 if responses == nil {
51 return
52 }
53 responses["200"] = map[string]any{
54 "description": "Issue, when the note contains only quick actions.",
55 "content": jsonSchema(schemaRef("Issue")),
56 }
57 }
58}
59
60func apiOp(paths map[string]map[string]any, path, method string) map[string]any {
61 item := paths[path]
62 if item == nil {
63 return nil
64 }
65 op, _ := item[strings.ToLower(method)].(map[string]any)
66 return op
67}
68
69func schemaRef(name string) map[string]any {
70 return map[string]any{"$ref": "#/components/schemas/" + name}
71}
72
73func jsonSchema(schema map[string]any) map[string]any {
74 return map[string]any{"application/json": map[string]any{"schema": schema}}
75}
76
77type apiBind struct {
78 path, method, body, resp, code string
79 array, empty bool
80}
81
82func apiBinds() []apiBind {
83 const (
84 mrs = "/projects/{owner}/{repo}/merge_requests"
85 mr = mrs + "/{number}"
86 pipes = "/projects/{owner}/{repo}/pipelines"
87 pipe = pipes + "/{id}"
88 job = pipe + "/jobs/{job}"
89 issues = "/repos/{owner}/{repo}/issues"
90 issue = issues + "/{number}"
91 branches = "/projects/{owner}/{repo}/repository/branches"
92 branch = branches + "/{branch}"
93 )
94 return []apiBind{
95 {path: "/user", method: "GET", code: "200", resp: "User"},
96 {path: "/user", method: "PUT", code: "200", body: "UserUpdate", resp: "User"},
97 {path: "/users/{username}", method: "GET", code: "200", resp: "User"},
98
99 {path: mrs, method: "GET", code: "200", resp: "MergeRequest", array: true},
100 {path: mrs, method: "POST", code: "201", body: "MergeRequestCreate", resp: "MergeRequest"},
101 {path: mr, method: "GET", code: "200", resp: "MergeRequest"},
102 {path: mr, method: "PUT", code: "200", body: "MergeRequestUpdate", resp: "MergeRequest"},
103 {path: mr + "/merge", method: "POST", code: "200", body: "MergeRequestMerge", resp: "MergeResult"},
104 {path: mr + "/close", method: "POST", code: "200", resp: "MergeState"},
105 {path: mr + "/reopen", method: "POST", code: "200", resp: "MergeState"},
106
107 {path: issues, method: "GET", code: "200", resp: "IssueList"},
108 {path: issues, method: "POST", code: "201", body: "IssueWrite", resp: "Issue"},
109 {path: issue, method: "GET", code: "200", resp: "Issue"},
110 {path: issue, method: "PUT", code: "200", body: "IssueWrite", resp: "Issue"},
111 {path: issue, method: "DELETE", code: "204", empty: true},
112 {path: issue + "/notes", method: "POST", code: "201", body: "IssueNoteCreate", resp: "IssueNote"},
113
114 {path: pipes, method: "GET", code: "200", resp: "Pipeline", array: true},
115 {path: pipes, method: "POST", code: "201", body: "PipelineCreate", resp: "Pipeline"},
116 {path: pipe, method: "GET", code: "200", resp: "Pipeline"},
117 {path: pipe + "/cancel", method: "POST", code: "200", resp: "Pipeline"},
118 {path: pipe + "/retry", method: "POST", code: "200", body: "PipelineRetry", resp: "Pipeline"},
119 {path: pipe + "/jobs", method: "GET", code: "200", resp: "Job", array: true},
120 {path: job, method: "GET", code: "200", resp: "Job"},
121 {path: job + "/play", method: "POST", code: "200", resp: "Job"},
122 {path: job + "/retry", method: "POST", code: "200", resp: "Job"},
123 {path: job + "/cancel", method: "POST", code: "200", resp: "Job"},
124
125 {path: branches, method: "GET", code: "200", resp: "Branch", array: true},
126 {path: branches, method: "POST", code: "201", body: "BranchCreate", resp: "Branch"},
127 {path: branch, method: "PUT", code: "200", body: "BranchUpdate", resp: "Branch"},
128 {path: branch, method: "DELETE", code: "204", body: "BranchDelete", empty: true},
129
130 {path: "/groups/{path}/webhooks", method: "GET", code: "200", resp: "Webhook", array: true},
131 {path: "/groups/{path}/webhooks", method: "POST", code: "201", body: "WebhookCreate", resp: "Webhook"},
132 {path: "/projects/{owner}/{repo}/webhooks", method: "GET", code: "200", resp: "Webhook", array: true},
133 {path: "/projects/{owner}/{repo}/webhooks", method: "POST", code: "201", body: "WebhookCreate", resp: "Webhook"},
134 {path: "/projects/{owner}/{repo}/secrets/inherited", method: "GET", code: "200", resp: "InheritedSecret", array: true},
135 {path: "/groups/{path}/webhooks/{id}", method: "PATCH", code: "200", body: "WebhookUpdate", resp: "Webhook"},
136 {path: "/groups/{path}/webhooks/{id}", method: "DELETE", code: "204", empty: true},
137 {path: "/groups/{path}/webhooks/{id}/copy", method: "POST", code: "201", body: "WebhookCopy", resp: "WebhookCopyResult"},
138 {path: "/projects/{owner}/{repo}/webhooks/{id}", method: "PATCH", code: "200", body: "WebhookUpdate", resp: "Webhook"},
139 {path: "/projects/{owner}/{repo}/webhooks/{id}", method: "DELETE", code: "204", empty: true},
140 {path: "/projects/{owner}/{repo}/webhooks/{id}/copy", method: "POST", code: "201", body: "WebhookCopy", resp: "WebhookCopyResult"},
141 }
142}
143
144func apiSchemas() map[string]any {
145 when := str("RFC3339 timestamp. Null when the moment has not happened yet.")
146 return map[string]any{
147 "MergeRequest": obj("Merge request.", map[string]any{
148 "id": integer("Internal id."),
149 "iid": integer("Merge request number in the project."),
150 "title": str("Title."),
151 "description": str("Description."),
152 "source_branch": str("Source branch."),
153 "target_branch": str("Target branch."),
154 "author": str("Author username."),
155 "state": str("open, closed or merged."),
156 "draft": boolean("Draft merge request."),
157 "labels": str("Comma-separated label names."),
158 "remove_source_branch": boolean("Delete the source branch after a successful merge."),
159 "squash": boolean("Squash commits when merging."),
160 "squash_commit_message": str("Commit message used when squash is enabled. Empty uses the title."),
161 "web_url": str("Full page URL. An empty base URL yields a path."),
162 "created_at": when,
163 "updated_at": when,
164 }),
165 "MergeRequestCreate": obj("Fields for a new merge request. Omitted remove_source_branch means true. Omitted squash means false.", map[string]any{
166 "title": str("Title."),
167 "description": str("Description."),
168 "source_branch": str("Source branch."),
169 "target_branch": str("Target branch."),
170 "draft": boolean("Create as draft."),
171 "remove_source_branch": boolean("Delete the source branch after merge. Omitted means true."),
172 "squash": boolean("Squash commits. Omitted means false."),
173 "squash_commit_message": str("Squash commit message. Empty uses the merge request title."),
174 }),
175 "MergeRequestUpdate": obj("Fields to change on a merge request. Omitted merge options keep the stored value.", map[string]any{
176 "title": str("Title."),
177 "description": str("Description."),
178 "labels": str("Comma-separated label names."),
179 "remove_source_branch": boolean("Delete the source branch after merge."),
180 "squash": boolean("Squash commits."),
181 "squash_commit_message": str("Squash commit message."),
182 }),
183 "MergeRequestMerge": obj("Merge options for this attempt. Omitted fields keep the stored value.", map[string]any{
184 "remove_source_branch": boolean("Delete the source branch after merge."),
185 "squash": boolean("Squash commits."),
186 "squash_commit_message": str("Squash commit message."),
187 }),
188 "MergeResult": obj("Result of a merge.", map[string]any{
189 "state": str("merged."),
190 "source_branch_removed": boolean("True when the source branch was deleted."),
191 "source_branch_skip": str("Empty when the branch was removed. Otherwise disabled, protected, default, forbidden, missing or error."),
192 "web_url": str("Full page URL of the merge request."),
193 }),
194 "MergeState": obj("State after close or reopen.", map[string]any{
195 "state": str("open or closed."),
196 "web_url": str("Full page URL of the merge request."),
197 }),
198 "Issue": obj("Issue. The list omits notes.", map[string]any{
199 "id": integer("Issue number in the project."),
200 "title": str("Title."),
201 "description": str("Description."),
202 "status": str("open or closed."),
203 "confidential": boolean("Hidden from users who cannot triage."),
204 "author": str("Author username."),
205 "assignees": strArr("Assignee usernames."),
206 "labels": strArr("Label names."),
207 "milestone": str("Milestone title."),
208 "due_date": str("Due date, YYYY-MM-DD, or empty."),
209 "created_at": when,
210 "updated_at": when,
211 "closed_at": when,
212 "web_url": str("Full page URL. An empty base URL yields a path."),
213 "notes": map[string]any{"type": "array", "items": schemaRef("IssueNote"), "description": "Notes. Omitted on the list."},
214 }),
215 "IssueNote": obj("Issue note.", map[string]any{
216 "id": integer("Note id."),
217 "author": str("Author username."),
218 "body": str("Note text."),
219 "created_at": when,
220 }),
221 "IssueList": obj("Issue list.", map[string]any{
222 "issues": map[string]any{"type": "array", "items": schemaRef("Issue")},
223 }),
224 "IssueWrite": obj("Fields to create or update an issue. Omitted fields stay unchanged on update.", map[string]any{
225 "title": str("Title. Required when creating."),
226 "description": str("Description."),
227 "state": str("open or closed."),
228 "confidential": boolean("Confidential issue."),
229 "labels": strArr("Label names."),
230 "assignees": strArr("Assignee usernames."),
231 "milestone": str("Milestone title."),
232 "due_date": str("Due date, YYYY-MM-DD, or empty."),
233 }),
234 "IssueNoteCreate": obj("New issue note. A body that is only quick actions returns the issue instead of a note.", map[string]any{
235 "body": str("Note text, including optional quick actions."),
236 }),
237 "Pipeline": obj("Pipeline.", map[string]any{
238 "id": integer("Pipeline id."),
239 "sha": str("Commit SHA."),
240 "ref": str("Git ref, for example refs/heads/master."),
241 "status": str("Pipeline status: queued, running, success, failed, canceled, waiting_for_update, waiting_for_job or manual. manual means only manual jobs are left, plus jobs that wait on them; such a pipeline is not running and has no finished_at until a manual job is played. A pipeline stopped by a newer pipeline stays canceled; see cancel_reason."),
242 "display_status": str("Status as the UI shows it: obsolete when every canceled job was superseded by a newer pipeline on the same ref, otherwise equal to status. Only in list and get responses."),
243 "cancel_reason": str("For status canceled: superseded (a newer pipeline started for the same ref) or manual. Null otherwise. Only in list and get responses."),
244 "waiting_for": map[string]any{"type": "object", "nullable": true, "description": "For status waiting_for_job: the running doNotInterrupt job (job_id, job_name, pipeline_id) in an older pipeline on the same ref that this pipeline waits for. Only in list and get responses."},
245 "source": str("Trigger source."),
246 "actor": str("Username that started the pipeline."),
247 "created_at": when,
248 "started_at": when,
249 "finished_at": when,
250 "web_url": str("Full page URL. An empty base URL yields a path."),
251 }),
252 "PipelineCreate": obj("Start a pipeline.", map[string]any{
253 "ref": str("Branch, tag or full ref to run."),
254 }),
255 "PipelineRetry": obj("Retry a pipeline. all starts a new pipeline on the same commit, ref, source and merge request. failed requeues failed jobs and the skipped or never-started jobs that depend on them. The response is 201 for all and 200 for failed.", map[string]any{
256 "mode": map[string]any{
257 "type": "string",
258 "description": "all or failed.",
259 "enum": []any{"all", "failed"},
260 },
261 }),
262 "Job": obj("CI job.", map[string]any{
263 "id": integer("Job id."),
264 "name": str("Job name."),
265 "stage": str("Stage name."),
266 "status": str("Job status."),
267 "when": str("When the job runs."),
268 "display_status": str("Status as the UI shows it: obsolete for a job canceled because a newer pipeline started for the same ref, otherwise equal to status."),
269 "cancel_reason": str("For status canceled: superseded or manual. Null otherwise."),
270 "do_not_interrupt": boolean("doNotInterrupt is set: the job is never stopped automatically, and cancelling it while it runs needs force=true."),
271 "failure_reason": str("For status failed: job_execution_timeout, script_failure or runner_system_failure when known. Null otherwise."),
272 "allow_failure": boolean("A failed job does not fail the pipeline."),
273 "started_at": when,
274 "finished_at": when,
275 }),
276 "Branch": obj("Branch tip. Create and rename responses only include name.", map[string]any{
277 "name": str("Branch name."),
278 "commit": str("Tip commit SHA."),
279 "subject": str("Tip commit subject."),
280 "date": str("Tip commit date."),
281 "default": boolean("True for the default branch."),
282 }),
283 "BranchCreate": obj("Create a branch.", map[string]any{
284 "name": str("New branch name."),
285 "from": str("Existing branch or commit to start from."),
286 }),
287 "BranchUpdate": obj("Rename a branch.", map[string]any{
288 "name": str("New branch name."),
289 "confirm": str("Current branch name, repeated as confirmation."),
290 }),
291 "BranchDelete": obj("Delete a branch.", map[string]any{
292 "confirm": str("Branch name, repeated as confirmation."),
293 }),
294 "Webhook": obj("Webhook.", map[string]any{
295 "id": integer("Webhook id."),
296 "name": str("Display name."),
297 "preset": str("Preset name, or empty for a custom hook."),
298 "url": str("URL template."),
299 "method": str("HTTP method."),
300 "events": strArr("Events that trigger the hook: push, merge_request, pipeline_success, pipeline_failure, release_published, issue, issue_opened, issue_closed, issue_reopened, issue_updated, issue_comment, merge_request_comment. issue and merge_request also cover the finer issue_* and merge_request_comment events."),
301 "branch_filter": str("Branch filter. Empty matches every branch."),
302 "active": boolean("Hook is enabled."),
303 }),
304 "User": obj("User. counts, repos and groups are present on GET /users/{username}, which is public. Without a token only public repositories and groups are included, and contributions count only those repositories. instance_admin and language are present for the signed-in user.", map[string]any{
305 "id": integer("User id."),
306 "username": str("Username."),
307 "name": str("Display name. Empty when unset."),
308 "bio": str("Profile text. Empty when unset."),
309 "avatar_url": str("Avatar URL."),
310 "created_at": when,
311 "web_url": str("Profile page at /users/{username}."),
312 "instance_admin": boolean("Instance administrator. Only on the signed-in user."),
313 "language": str("Interface language. Only on the signed-in user."),
314 "counts": obj("Totals the viewer is allowed to see.", map[string]any{
315 "repos": integer("Owned repositories visible to the viewer."),
316 "groups": integer("Groups the user belongs to that the viewer may see."),
317 "contributions": integer("Contributions in the last 12 months in repositories the viewer may see."),
318 }),
319 "repos": map[string]any{"type": "array", "description": "Owned repositories visible to the viewer.", "items": schemaRef("UserRepo")},
320 "groups": map[string]any{"type": "array", "description": "Groups visible to the viewer.", "items": schemaRef("UserGroup")},
321 }),
322 "UserUpdate": obj("Fields to change on the signed-in user. Omitted fields stay unchanged.", map[string]any{
323 "name": str("Display name. Empty clears it."),
324 "bio": str("Profile text. Empty clears it. Newlines are kept."),
325 "language": str("Interface language."),
326 }),
327 "UserRepo": obj("Repository on a user profile.", map[string]any{
328 "id": integer("Repository id."),
329 "name": str("Repository name."),
330 "path": str("Full path, owner/name."),
331 "description": str("Description."),
332 "visibility": str("public or private."),
333 "web_url": str("Full page URL."),
334 }),
335 "UserGroup": obj("Group on a user profile.", map[string]any{
336 "id": integer("Group id."),
337 "name": str("Group name."),
338 "path": str("Group path."),
339 "description": str("Description."),
340 "visibility": str("public or private."),
341 "web_url": str("Full page URL."),
342 }),
343 "WebhookCreate": obj("Create a webhook. Preset fields depend on the chosen preset.", map[string]any{
344 "name": str("Display name."),
345 "preset": str("Preset name."),
346 "branch_filter": str("Branch filter."),
347 "events": strArr("Events, see the list on the webhook schema."),
348 "url": str("URL template."),
349 "username": str("Username for the preset."),
350 "avatar": str("Avatar URL."),
351 "icon": str("Icon URL or emoji."),
352 "alias": str("Alias."),
353 "token": str("Name of the secret that holds the token. The value is never returned."),
354 "chat": str("Chat id."),
355 "server": str("Server URL."),
356 "topic": str("Topic."),
357 "homeserver": str("Matrix homeserver."),
358 "room": str("Matrix room."),
359 "method": str("HTTP method."),
360 "content_type": str("Content type."),
361 "headers": str("Extra headers."),
362 "body": str("Body template."),
363 }),
364 "WebhookUpdate": obj("Change a webhook. Omitted fields keep the stored value. An empty or omitted token keeps the stored secret reference; secret values are never returned.", map[string]any{
365 "name": str("Display name."),
366 "preset": str("Preset name."),
367 "branch_filter": str("Branch filter."),
368 "events": strArr("Events, see the list on the webhook schema."),
369 "active": boolean("Enable or disable the hook."),
370 "url": str("URL template."),
371 "username": str("Username for the preset."),
372 "avatar": str("Avatar URL."),
373 "icon": str("Icon URL or emoji."),
374 "alias": str("Alias."),
375 "token": str("Name of the secret that holds the token. Empty keeps the current one."),
376 "chat": str("Chat id."),
377 "server": str("Server URL."),
378 "topic": str("Topic."),
379 "homeserver": str("Matrix homeserver."),
380 "room": str("Matrix room."),
381 "method": str("HTTP method."),
382 "content_type": str("Content type."),
383 "headers": str("Extra headers. Empty keeps the current headers."),
384 "body": str("Body template."),
385 }),
386 "InheritedSecret": obj("A group secret the project inherits. Values are never returned. Jobs get group secrets first; a project secret with the same name wins.", map[string]any{
387 "name": str("Secret name."),
388 "group": str("Path of the group it comes from."),
389 "protected": boolean("Only injected on protected refs."),
390 "overridden": boolean("A project secret with the same name replaces it."),
391 }),
392 "WebhookCopy": obj("Copy a webhook to a project or group where you are maintainer. Secrets the hook references and the target cannot already see are copied encrypted on the server; their values never reach the client.", map[string]any{
393 "target_type": str("project or group."),
394 "target": str("Target path: owner/project or group path."),
395 "name": str("Optional new name. Default keeps the name."),
396 "events": strArr("Optional event filter. Default keeps the events."),
397 }),
398 "WebhookCopyResult": obj("The new webhook plus the names (never values) of copied secrets.", map[string]any{
399 "id": integer("New webhook id."),
400 "name": str("Display name."),
401 "preset": str("Preset name."),
402 "url": str("URL template."),
403 "method": str("HTTP method."),
404 "events": strArr("Events."),
405 "branch_filter": str("Branch filter."),
406 "active": boolean("Hook is enabled."),
407 "scope": str("repo or group."),
408 "copied_secrets": strArr("Names of secrets copied to the target."),
409 "missing_secrets": strArr("Referenced secret names found nowhere; add them in the target."),
410 }),
411 }
412}
413
414func obj(desc string, props map[string]any) map[string]any {
415 return map[string]any{"type": "object", "description": desc, "properties": props}
416}
417
418func str(desc string) map[string]any {
419 return map[string]any{"type": "string", "description": desc}
420}
421
422func strArr(desc string) map[string]any {
423 return map[string]any{"type": "array", "description": desc, "items": map[string]any{"type": "string"}}
424}
425
426func boolean(desc string) map[string]any {
427 return map[string]any{"type": "boolean", "description": desc}
428}
429
430func integer(desc string) map[string]any {
431 return map[string]any{"type": "integer", "format": "int64", "description": desc}
432}