BlueGit

timfa bluegit

Files

bluegit / internal / server / api_openapi.go

api_openapi.go

api_openapi.go 21.4 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: "/groups/{path}/webhooks/{id}", method: "PATCH", code: "200", body: "WebhookUpdate", resp: "Webhook"},
135 {path: "/groups/{path}/webhooks/{id}", method: "DELETE", code: "204", empty: true},
136 {path: "/groups/{path}/webhooks/{id}/copy", method: "POST", code: "201", body: "WebhookCopy", resp: "WebhookCopyResult"},
137 {path: "/projects/{owner}/{repo}/webhooks/{id}", method: "PATCH", code: "200", body: "WebhookUpdate", resp: "Webhook"},
138 {path: "/projects/{owner}/{repo}/webhooks/{id}", method: "DELETE", code: "204", empty: true},
139 {path: "/projects/{owner}/{repo}/webhooks/{id}/copy", method: "POST", code: "201", body: "WebhookCopy", resp: "WebhookCopyResult"},
140 }
141}
142
143func apiSchemas() map[string]any {
144 when := str("RFC3339 timestamp. Null when the moment has not happened yet.")
145 return map[string]any{
146 "MergeRequest": obj("Merge request.", map[string]any{
147 "id": integer("Internal id."),
148 "iid": integer("Merge request number in the project."),
149 "title": str("Title."),
150 "description": str("Description."),
151 "source_branch": str("Source branch."),
152 "target_branch": str("Target branch."),
153 "author": str("Author username."),
154 "state": str("open, closed or merged."),
155 "draft": boolean("Draft merge request."),
156 "labels": str("Comma-separated label names."),
157 "remove_source_branch": boolean("Delete the source branch after a successful merge."),
158 "squash": boolean("Squash commits when merging."),
159 "squash_commit_message": str("Commit message used when squash is enabled. Empty uses the title."),
160 "web_url": str("Full page URL. An empty base URL yields a path."),
161 "created_at": when,
162 "updated_at": when,
163 }),
164 "MergeRequestCreate": obj("Fields for a new merge request. Omitted remove_source_branch means true. Omitted squash means false.", map[string]any{
165 "title": str("Title."),
166 "description": str("Description."),
167 "source_branch": str("Source branch."),
168 "target_branch": str("Target branch."),
169 "draft": boolean("Create as draft."),
170 "remove_source_branch": boolean("Delete the source branch after merge. Omitted means true."),
171 "squash": boolean("Squash commits. Omitted means false."),
172 "squash_commit_message": str("Squash commit message. Empty uses the merge request title."),
173 }),
174 "MergeRequestUpdate": obj("Fields to change on a merge request. Omitted merge options keep the stored value.", map[string]any{
175 "title": str("Title."),
176 "description": str("Description."),
177 "labels": str("Comma-separated label names."),
178 "remove_source_branch": boolean("Delete the source branch after merge."),
179 "squash": boolean("Squash commits."),
180 "squash_commit_message": str("Squash commit message."),
181 }),
182 "MergeRequestMerge": obj("Merge options for this attempt. Omitted fields keep the stored value.", map[string]any{
183 "remove_source_branch": boolean("Delete the source branch after merge."),
184 "squash": boolean("Squash commits."),
185 "squash_commit_message": str("Squash commit message."),
186 }),
187 "MergeResult": obj("Result of a merge.", map[string]any{
188 "state": str("merged."),
189 "source_branch_removed": boolean("True when the source branch was deleted."),
190 "source_branch_skip": str("Empty when the branch was removed. Otherwise disabled, protected, default, forbidden, missing or error."),
191 "web_url": str("Full page URL of the merge request."),
192 }),
193 "MergeState": obj("State after close or reopen.", map[string]any{
194 "state": str("open or closed."),
195 "web_url": str("Full page URL of the merge request."),
196 }),
197 "Issue": obj("Issue. The list omits notes.", map[string]any{
198 "id": integer("Issue number in the project."),
199 "title": str("Title."),
200 "description": str("Description."),
201 "status": str("open or closed."),
202 "confidential": boolean("Hidden from users who cannot triage."),
203 "author": str("Author username."),
204 "assignees": strArr("Assignee usernames."),
205 "labels": strArr("Label names."),
206 "milestone": str("Milestone title."),
207 "due_date": str("Due date, YYYY-MM-DD, or empty."),
208 "created_at": when,
209 "updated_at": when,
210 "closed_at": when,
211 "web_url": str("Full page URL. An empty base URL yields a path."),
212 "notes": map[string]any{"type": "array", "items": schemaRef("IssueNote"), "description": "Notes. Omitted on the list."},
213 }),
214 "IssueNote": obj("Issue note.", map[string]any{
215 "id": integer("Note id."),
216 "author": str("Author username."),
217 "body": str("Note text."),
218 "created_at": when,
219 }),
220 "IssueList": obj("Issue list.", map[string]any{
221 "issues": map[string]any{"type": "array", "items": schemaRef("Issue")},
222 }),
223 "IssueWrite": obj("Fields to create or update an issue. Omitted fields stay unchanged on update.", map[string]any{
224 "title": str("Title. Required when creating."),
225 "description": str("Description."),
226 "state": str("open or closed."),
227 "confidential": boolean("Confidential issue."),
228 "labels": strArr("Label names."),
229 "assignees": strArr("Assignee usernames."),
230 "milestone": str("Milestone title."),
231 "due_date": str("Due date, YYYY-MM-DD, or empty."),
232 }),
233 "IssueNoteCreate": obj("New issue note. A body that is only quick actions returns the issue instead of a note.", map[string]any{
234 "body": str("Note text, including optional quick actions."),
235 }),
236 "Pipeline": obj("Pipeline.", map[string]any{
237 "id": integer("Pipeline id."),
238 "sha": str("Commit SHA."),
239 "ref": str("Git ref, for example refs/heads/master."),
240 "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."),
241 "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."),
242 "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."),
243 "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."},
244 "source": str("Trigger source."),
245 "actor": str("Username that started the pipeline."),
246 "created_at": when,
247 "started_at": when,
248 "finished_at": when,
249 "web_url": str("Full page URL. An empty base URL yields a path."),
250 }),
251 "PipelineCreate": obj("Start a pipeline.", map[string]any{
252 "ref": str("Branch, tag or full ref to run."),
253 }),
254 "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{
255 "mode": map[string]any{
256 "type": "string",
257 "description": "all or failed.",
258 "enum": []any{"all", "failed"},
259 },
260 }),
261 "Job": obj("CI job.", map[string]any{
262 "id": integer("Job id."),
263 "name": str("Job name."),
264 "stage": str("Stage name."),
265 "status": str("Job status."),
266 "when": str("When the job runs."),
267 "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."),
268 "cancel_reason": str("For status canceled: superseded or manual. Null otherwise."),
269 "do_not_interrupt": boolean("doNotInterrupt is set: the job is never stopped automatically, and cancelling it while it runs needs force=true."),
270 "failure_reason": str("For status failed: job_execution_timeout, script_failure or runner_system_failure when known. Null otherwise."),
271 "allow_failure": boolean("A failed job does not fail the pipeline."),
272 "started_at": when,
273 "finished_at": when,
274 }),
275 "Branch": obj("Branch tip. Create and rename responses only include name.", map[string]any{
276 "name": str("Branch name."),
277 "commit": str("Tip commit SHA."),
278 "subject": str("Tip commit subject."),
279 "date": str("Tip commit date."),
280 "default": boolean("True for the default branch."),
281 }),
282 "BranchCreate": obj("Create a branch.", map[string]any{
283 "name": str("New branch name."),
284 "from": str("Existing branch or commit to start from."),
285 }),
286 "BranchUpdate": obj("Rename a branch.", map[string]any{
287 "name": str("New branch name."),
288 "confirm": str("Current branch name, repeated as confirmation."),
289 }),
290 "BranchDelete": obj("Delete a branch.", map[string]any{
291 "confirm": str("Branch name, repeated as confirmation."),
292 }),
293 "Webhook": obj("Webhook.", map[string]any{
294 "id": integer("Webhook id."),
295 "name": str("Display name."),
296 "preset": str("Preset name, or empty for a custom hook."),
297 "url": str("URL template."),
298 "method": str("HTTP method."),
299 "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."),
300 "branch_filter": str("Branch filter. Empty matches every branch."),
301 "active": boolean("Hook is enabled."),
302 }),
303 "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{
304 "id": integer("User id."),
305 "username": str("Username."),
306 "name": str("Display name. Empty when unset."),
307 "bio": str("Profile text. Empty when unset."),
308 "avatar_url": str("Avatar URL."),
309 "created_at": when,
310 "web_url": str("Profile page at /users/{username}."),
311 "instance_admin": boolean("Instance administrator. Only on the signed-in user."),
312 "language": str("Interface language. Only on the signed-in user."),
313 "counts": obj("Totals the viewer is allowed to see.", map[string]any{
314 "repos": integer("Owned repositories visible to the viewer."),
315 "groups": integer("Groups the user belongs to that the viewer may see."),
316 "contributions": integer("Contributions in the last 12 months in repositories the viewer may see."),
317 }),
318 "repos": map[string]any{"type": "array", "description": "Owned repositories visible to the viewer.", "items": schemaRef("UserRepo")},
319 "groups": map[string]any{"type": "array", "description": "Groups visible to the viewer.", "items": schemaRef("UserGroup")},
320 }),
321 "UserUpdate": obj("Fields to change on the signed-in user. Omitted fields stay unchanged.", map[string]any{
322 "name": str("Display name. Empty clears it."),
323 "bio": str("Profile text. Empty clears it. Newlines are kept."),
324 "language": str("Interface language."),
325 }),
326 "UserRepo": obj("Repository on a user profile.", map[string]any{
327 "id": integer("Repository id."),
328 "name": str("Repository name."),
329 "path": str("Full path, owner/name."),
330 "description": str("Description."),
331 "visibility": str("public or private."),
332 "web_url": str("Full page URL."),
333 }),
334 "UserGroup": obj("Group on a user profile.", map[string]any{
335 "id": integer("Group id."),
336 "name": str("Group name."),
337 "path": str("Group path."),
338 "description": str("Description."),
339 "visibility": str("public or private."),
340 "web_url": str("Full page URL."),
341 }),
342 "WebhookCreate": obj("Create a webhook. Preset fields depend on the chosen preset.", map[string]any{
343 "name": str("Display name."),
344 "preset": str("Preset name."),
345 "branch_filter": str("Branch filter."),
346 "events": strArr("Events, see the list on the webhook schema."),
347 "url": str("URL template."),
348 "username": str("Username for the preset."),
349 "avatar": str("Avatar URL."),
350 "icon": str("Icon URL or emoji."),
351 "alias": str("Alias."),
352 "token": str("Name of the secret that holds the token. The value is never returned."),
353 "chat": str("Chat id."),
354 "server": str("Server URL."),
355 "topic": str("Topic."),
356 "homeserver": str("Matrix homeserver."),
357 "room": str("Matrix room."),
358 "method": str("HTTP method."),
359 "content_type": str("Content type."),
360 "headers": str("Extra headers."),
361 "body": str("Body template."),
362 }),
363 "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{
364 "name": str("Display name."),
365 "preset": str("Preset name."),
366 "branch_filter": str("Branch filter."),
367 "events": strArr("Events, see the list on the webhook schema."),
368 "active": boolean("Enable or disable the hook."),
369 "url": str("URL template."),
370 "username": str("Username for the preset."),
371 "avatar": str("Avatar URL."),
372 "icon": str("Icon URL or emoji."),
373 "alias": str("Alias."),
374 "token": str("Name of the secret that holds the token. Empty keeps the current one."),
375 "chat": str("Chat id."),
376 "server": str("Server URL."),
377 "topic": str("Topic."),
378 "homeserver": str("Matrix homeserver."),
379 "room": str("Matrix room."),
380 "method": str("HTTP method."),
381 "content_type": str("Content type."),
382 "headers": str("Extra headers. Empty keeps the current headers."),
383 "body": str("Body template."),
384 }),
385 "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{
386 "target_type": str("project or group."),
387 "target": str("Target path: owner/project or group path."),
388 "name": str("Optional new name. Default keeps the name."),
389 "events": strArr("Optional event filter. Default keeps the events."),
390 }),
391 "WebhookCopyResult": obj("The new webhook plus the names (never values) of copied secrets.", map[string]any{
392 "id": integer("New webhook id."),
393 "name": str("Display name."),
394 "preset": str("Preset name."),
395 "url": str("URL template."),
396 "method": str("HTTP method."),
397 "events": strArr("Events."),
398 "branch_filter": str("Branch filter."),
399 "active": boolean("Hook is enabled."),
400 "scope": str("repo or group."),
401 "copied_secrets": strArr("Names of secrets copied to the target."),
402 "missing_secrets": strArr("Referenced secret names found nowhere; add them in the target."),
403 }),
404 }
405}
406
407func obj(desc string, props map[string]any) map[string]any {
408 return map[string]any{"type": "object", "description": desc, "properties": props}
409}
410
411func str(desc string) map[string]any {
412 return map[string]any{"type": "string", "description": desc}
413}
414
415func strArr(desc string) map[string]any {
416 return map[string]any{"type": "array", "description": desc, "items": map[string]any{"type": "string"}}
417}
418
419func boolean(desc string) map[string]any {
420 return map[string]any{"type": "boolean", "description": desc}
421}
422
423func integer(desc string) map[string]any {
424 return map[string]any{"type": "integer", "format": "int64", "description": desc}
425}