BlueGit

timfa bluegit

Files

bluegit / internal / server / api_openapi.go

api_openapi.go

feat: deel een profiel via een korte url en een openbare api
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed b9293125 History
api_openapi.go 15.8 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, "/repos/{owner}/{repo}/issues/{number}/notes", "POST"); op != nil {
41 responses, _ := op["responses"].(map[string]any)
42 if responses == nil {
43 return
44 }
45 responses["200"] = map[string]any{
46 "description": "Issue, when the note contains only quick actions.",
47 "content": jsonSchema(schemaRef("Issue")),
48 }
49 }
50}
51
52func apiOp(paths map[string]map[string]any, path, method string) map[string]any {
53 item := paths[path]
54 if item == nil {
55 return nil
56 }
57 op, _ := item[strings.ToLower(method)].(map[string]any)
58 return op
59}
60
61func schemaRef(name string) map[string]any {
62 return map[string]any{"$ref": "#/components/schemas/" + name}
63}
64
65func jsonSchema(schema map[string]any) map[string]any {
66 return map[string]any{"application/json": map[string]any{"schema": schema}}
67}
68
69type apiBind struct {
70 path, method, body, resp, code string
71 array, empty bool
72}
73
74func apiBinds() []apiBind {
75 const (
76 mrs = "/projects/{owner}/{repo}/merge_requests"
77 mr = mrs + "/{number}"
78 pipes = "/projects/{owner}/{repo}/pipelines"
79 pipe = pipes + "/{id}"
80 job = pipe + "/jobs/{job}"
81 issues = "/repos/{owner}/{repo}/issues"
82 issue = issues + "/{number}"
83 branches = "/projects/{owner}/{repo}/repository/branches"
84 branch = branches + "/{branch}"
85 )
86 return []apiBind{
87 {path: "/user", method: "GET", code: "200", resp: "User"},
88 {path: "/user", method: "PUT", code: "200", body: "UserUpdate", resp: "User"},
89 {path: "/users/{username}", method: "GET", code: "200", resp: "User"},
90
91 {path: mrs, method: "GET", code: "200", resp: "MergeRequest", array: true},
92 {path: mrs, method: "POST", code: "201", body: "MergeRequestCreate", resp: "MergeRequest"},
93 {path: mr, method: "GET", code: "200", resp: "MergeRequest"},
94 {path: mr, method: "PUT", code: "200", body: "MergeRequestUpdate", resp: "MergeRequest"},
95 {path: mr + "/merge", method: "POST", code: "200", body: "MergeRequestMerge", resp: "MergeResult"},
96 {path: mr + "/close", method: "POST", code: "200", resp: "MergeState"},
97 {path: mr + "/reopen", method: "POST", code: "200", resp: "MergeState"},
98
99 {path: issues, method: "GET", code: "200", resp: "IssueList"},
100 {path: issues, method: "POST", code: "201", body: "IssueWrite", resp: "Issue"},
101 {path: issue, method: "GET", code: "200", resp: "Issue"},
102 {path: issue, method: "PUT", code: "200", body: "IssueWrite", resp: "Issue"},
103 {path: issue, method: "DELETE", code: "204", empty: true},
104 {path: issue + "/notes", method: "POST", code: "201", body: "IssueNoteCreate", resp: "IssueNote"},
105
106 {path: pipes, method: "GET", code: "200", resp: "Pipeline", array: true},
107 {path: pipes, method: "POST", code: "201", body: "PipelineCreate", resp: "Pipeline"},
108 {path: pipe, method: "GET", code: "200", resp: "Pipeline"},
109 {path: pipe + "/cancel", method: "POST", code: "200", resp: "Pipeline"},
110 {path: pipe + "/jobs", method: "GET", code: "200", resp: "Job", array: true},
111 {path: job, method: "GET", code: "200", resp: "Job"},
112 {path: job + "/play", method: "POST", code: "200", resp: "Job"},
113 {path: job + "/retry", method: "POST", code: "200", resp: "Job"},
114 {path: job + "/cancel", method: "POST", code: "200", resp: "Job"},
115
116 {path: branches, method: "GET", code: "200", resp: "Branch", array: true},
117 {path: branches, method: "POST", code: "201", body: "BranchCreate", resp: "Branch"},
118 {path: branch, method: "PUT", code: "200", body: "BranchUpdate", resp: "Branch"},
119 {path: branch, method: "DELETE", code: "204", body: "BranchDelete", empty: true},
120
121 {path: "/groups/{path}/webhooks", method: "GET", code: "200", resp: "Webhook", array: true},
122 {path: "/groups/{path}/webhooks", method: "POST", code: "201", body: "WebhookCreate", resp: "Webhook"},
123 {path: "/projects/{owner}/{repo}/webhooks", method: "GET", code: "200", resp: "Webhook", array: true},
124 {path: "/projects/{owner}/{repo}/webhooks", method: "POST", code: "201", body: "WebhookCreate", resp: "Webhook"},
125 }
126}
127
128func apiSchemas() map[string]any {
129 when := str("RFC3339 timestamp. Null when the moment has not happened yet.")
130 return map[string]any{
131 "MergeRequest": obj("Merge request.", map[string]any{
132 "id": integer("Internal id."),
133 "iid": integer("Merge request number in the project."),
134 "title": str("Title."),
135 "description": str("Description."),
136 "source_branch": str("Source branch."),
137 "target_branch": str("Target branch."),
138 "author": str("Author username."),
139 "state": str("open, closed or merged."),
140 "draft": boolean("Draft merge request."),
141 "labels": str("Comma-separated label names."),
142 "remove_source_branch": boolean("Delete the source branch after a successful merge."),
143 "squash": boolean("Squash commits when merging."),
144 "squash_commit_message": str("Commit message used when squash is enabled. Empty uses the title."),
145 "web_url": str("Full page URL. An empty base URL yields a path."),
146 "created_at": when,
147 "updated_at": when,
148 }),
149 "MergeRequestCreate": obj("Fields for a new merge request. Omitted remove_source_branch means true. Omitted squash means false.", map[string]any{
150 "title": str("Title."),
151 "description": str("Description."),
152 "source_branch": str("Source branch."),
153 "target_branch": str("Target branch."),
154 "draft": boolean("Create as draft."),
155 "remove_source_branch": boolean("Delete the source branch after merge. Omitted means true."),
156 "squash": boolean("Squash commits. Omitted means false."),
157 "squash_commit_message": str("Squash commit message. Empty uses the merge request title."),
158 }),
159 "MergeRequestUpdate": obj("Fields to change on a merge request. Omitted merge options keep the stored value.", map[string]any{
160 "title": str("Title."),
161 "description": str("Description."),
162 "labels": str("Comma-separated label names."),
163 "remove_source_branch": boolean("Delete the source branch after merge."),
164 "squash": boolean("Squash commits."),
165 "squash_commit_message": str("Squash commit message."),
166 }),
167 "MergeRequestMerge": obj("Merge options for this attempt. Omitted fields keep the stored value.", map[string]any{
168 "remove_source_branch": boolean("Delete the source branch after merge."),
169 "squash": boolean("Squash commits."),
170 "squash_commit_message": str("Squash commit message."),
171 }),
172 "MergeResult": obj("Result of a merge.", map[string]any{
173 "state": str("merged."),
174 "source_branch_removed": boolean("True when the source branch was deleted."),
175 "source_branch_skip": str("Empty when the branch was removed. Otherwise disabled, protected, default, forbidden, missing or error."),
176 "web_url": str("Full page URL of the merge request."),
177 }),
178 "MergeState": obj("State after close or reopen.", map[string]any{
179 "state": str("open or closed."),
180 "web_url": str("Full page URL of the merge request."),
181 }),
182 "Issue": obj("Issue. The list omits notes.", map[string]any{
183 "id": integer("Issue number in the project."),
184 "title": str("Title."),
185 "description": str("Description."),
186 "status": str("open or closed."),
187 "confidential": boolean("Hidden from users who cannot triage."),
188 "author": str("Author username."),
189 "assignees": strArr("Assignee usernames."),
190 "labels": strArr("Label names."),
191 "milestone": str("Milestone title."),
192 "due_date": str("Due date, YYYY-MM-DD, or empty."),
193 "created_at": when,
194 "updated_at": when,
195 "closed_at": when,
196 "web_url": str("Full page URL. An empty base URL yields a path."),
197 "notes": map[string]any{"type": "array", "items": schemaRef("IssueNote"), "description": "Notes. Omitted on the list."},
198 }),
199 "IssueNote": obj("Issue note.", map[string]any{
200 "id": integer("Note id."),
201 "author": str("Author username."),
202 "body": str("Note text."),
203 "created_at": when,
204 }),
205 "IssueList": obj("Issue list.", map[string]any{
206 "issues": map[string]any{"type": "array", "items": schemaRef("Issue")},
207 }),
208 "IssueWrite": obj("Fields to create or update an issue. Omitted fields stay unchanged on update.", map[string]any{
209 "title": str("Title. Required when creating."),
210 "description": str("Description."),
211 "state": str("open or closed."),
212 "confidential": boolean("Confidential issue."),
213 "labels": strArr("Label names."),
214 "assignees": strArr("Assignee usernames."),
215 "milestone": str("Milestone title."),
216 "due_date": str("Due date, YYYY-MM-DD, or empty."),
217 }),
218 "IssueNoteCreate": obj("New issue note. A body that is only quick actions returns the issue instead of a note.", map[string]any{
219 "body": str("Note text, including optional quick actions."),
220 }),
221 "Pipeline": obj("Pipeline.", map[string]any{
222 "id": integer("Pipeline id."),
223 "sha": str("Commit SHA."),
224 "ref": str("Git ref, for example refs/heads/master."),
225 "status": str("Pipeline status."),
226 "source": str("Trigger source."),
227 "actor": str("Username that started the pipeline."),
228 "created_at": when,
229 "started_at": when,
230 "finished_at": when,
231 "web_url": str("Full page URL. An empty base URL yields a path."),
232 }),
233 "PipelineCreate": obj("Start a pipeline.", map[string]any{
234 "ref": str("Branch, tag or full ref to run."),
235 }),
236 "Job": obj("CI job.", map[string]any{
237 "id": integer("Job id."),
238 "name": str("Job name."),
239 "stage": str("Stage name."),
240 "status": str("Job status."),
241 "when": str("When the job runs."),
242 "allow_failure": boolean("A failed job does not fail the pipeline."),
243 "started_at": when,
244 "finished_at": when,
245 }),
246 "Branch": obj("Branch tip. Create and rename responses only include name.", map[string]any{
247 "name": str("Branch name."),
248 "commit": str("Tip commit SHA."),
249 "subject": str("Tip commit subject."),
250 "date": str("Tip commit date."),
251 "default": boolean("True for the default branch."),
252 }),
253 "BranchCreate": obj("Create a branch.", map[string]any{
254 "name": str("New branch name."),
255 "from": str("Existing branch or commit to start from."),
256 }),
257 "BranchUpdate": obj("Rename a branch.", map[string]any{
258 "name": str("New branch name."),
259 "confirm": str("Current branch name, repeated as confirmation."),
260 }),
261 "BranchDelete": obj("Delete a branch.", map[string]any{
262 "confirm": str("Branch name, repeated as confirmation."),
263 }),
264 "Webhook": obj("Webhook.", map[string]any{
265 "id": integer("Webhook id."),
266 "name": str("Display name."),
267 "preset": str("Preset name, or empty for a custom hook."),
268 "url": str("URL template."),
269 "method": str("HTTP method."),
270 "events": strArr("Events that trigger the hook."),
271 "branch_filter": str("Branch filter. Empty matches every branch."),
272 "active": boolean("Hook is enabled."),
273 }),
274 "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{
275 "id": integer("User id."),
276 "username": str("Username."),
277 "name": str("Display name. Empty when unset."),
278 "bio": str("Profile text. Empty when unset."),
279 "avatar_url": str("Avatar URL."),
280 "created_at": when,
281 "web_url": str("Profile page at /users/{username}."),
282 "instance_admin": boolean("Instance administrator. Only on the signed-in user."),
283 "language": str("Interface language. Only on the signed-in user."),
284 "counts": obj("Totals the viewer is allowed to see.", map[string]any{
285 "repos": integer("Owned repositories visible to the viewer."),
286 "groups": integer("Groups the user belongs to that the viewer may see."),
287 "contributions": integer("Contributions in the last 12 months in repositories the viewer may see."),
288 }),
289 "repos": map[string]any{"type": "array", "description": "Owned repositories visible to the viewer.", "items": schemaRef("UserRepo")},
290 "groups": map[string]any{"type": "array", "description": "Groups visible to the viewer.", "items": schemaRef("UserGroup")},
291 }),
292 "UserUpdate": obj("Fields to change on the signed-in user. Omitted fields stay unchanged.", map[string]any{
293 "name": str("Display name. Empty clears it."),
294 "bio": str("Profile text. Empty clears it. Newlines are kept."),
295 "language": str("Interface language."),
296 }),
297 "UserRepo": obj("Repository on a user profile.", map[string]any{
298 "id": integer("Repository id."),
299 "name": str("Repository name."),
300 "path": str("Full path, owner/name."),
301 "description": str("Description."),
302 "visibility": str("public or private."),
303 "web_url": str("Full page URL."),
304 }),
305 "UserGroup": obj("Group on a user profile.", map[string]any{
306 "id": integer("Group id."),
307 "name": str("Group name."),
308 "path": str("Group path."),
309 "description": str("Description."),
310 "visibility": str("public or private."),
311 "web_url": str("Full page URL."),
312 }),
313 "WebhookCreate": obj("Create a webhook. Preset fields depend on the chosen preset.", map[string]any{
314 "name": str("Display name."),
315 "preset": str("Preset name."),
316 "branch_filter": str("Branch filter."),
317 "events": strArr("Events."),
318 "url": str("URL template."),
319 "username": str("Username for the preset."),
320 "avatar": str("Avatar URL."),
321 "icon": str("Icon URL or emoji."),
322 "alias": str("Alias."),
323 "token": str("Token. Stored as a secret and not returned."),
324 "chat": str("Chat id."),
325 "server": str("Server URL."),
326 "topic": str("Topic."),
327 "homeserver": str("Matrix homeserver."),
328 "room": str("Matrix room."),
329 "method": str("HTTP method."),
330 "content_type": str("Content type."),
331 "headers": str("Extra headers."),
332 "body": str("Body template."),
333 }),
334 }
335}
336
337func obj(desc string, props map[string]any) map[string]any {
338 return map[string]any{"type": "object", "description": desc, "properties": props}
339}
340
341func str(desc string) map[string]any {
342 return map[string]any{"type": "string", "description": desc}
343}
344
345func strArr(desc string) map[string]any {
346 return map[string]any{"type": "array", "description": desc, "items": map[string]any{"type": "string"}}
347}
348
349func boolean(desc string) map[string]any {
350 return map[string]any{"type": "boolean", "description": desc}
351}
352
353func integer(desc string) map[string]any {
354 return map[string]any{"type": "integer", "format": "int64", "description": desc}
355}