BlueGit

timfa bluegit

Files

bluegit / internal / webhook / openapi.go

openapi.go

feat: stuur bestandsdetails en een diff-URL mee in webhooks
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
c5cfb816 History
openapi.go 15.6 KiB Download Raw
1package webhook
2
3import "encoding/json"
4
5// OpenAPI beschrijft de JSON-body die een webhook ontvangt.
6func OpenAPI() []byte {
7 obj := func(desc string, props map[string]any, required []string) map[string]any {
8 return map[string]any{
9 "type": "object", "description": desc,
10 "properties": props, "required": required,
11 }
12 }
13 str := func(desc string) map[string]any {
14 return map[string]any{"type": "string", "description": desc}
15 }
16 num := func(desc string) map[string]any {
17 return map[string]any{"type": "number", "description": desc}
18 }
19 boolean := func(desc string) map[string]any {
20 return map[string]any{"type": "boolean", "description": desc}
21 }
22 post := func(summary, ref string) map[string]any {
23 return map[string]any{
24 "post": map[string]any{
25 "summary": summary,
26 "requestBody": map[string]any{
27 "required": true,
28 "content": map[string]any{
29 "application/json": map[string]any{
30 "schema": map[string]any{"$ref": ref},
31 },
32 },
33 },
34 "responses": map[string]any{"200": map[string]any{"description": "The receiver accepted the event."}},
35 },
36 }
37 }
38 doc := map[string]any{
39 "openapi": "3.1.0",
40 "info": map[string]any{
41 "title": "BlueGit webhook payloads",
42 "version": "1",
43 "description": "JSON body of a BlueGit webhook when the body template is empty. " +
44 "pipeline_success and pipeline_failure include the pipeline and every job. " +
45 "Every job includes its console output: the last 200 lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. " +
46 "log_truncated is true when the log was cut. log_url is then GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log and needs Authorization Bearer with a personal token that can read the repo. " +
47 "A push lists ref, before, after and up to 20 commits. Each commit has sha, message, author, timestamp, URL, changed files and diff_url. A file has status added, modified, removed or renamed, plus added and deleted line counts. The payload does not include the diff text. " +
48 "diff_url for a commit is GET /api/v1/repos/{owner}/{repo}/commits/{sha}/diff. " +
49 "A merge request includes reviewers, comments, up to 20 commits, diff statistics and diff_url, and no diff text. " +
50 "diff_url for a merge request is GET /api/v1/repos/{owner}/{repo}/merge-requests/{id}/diff. " +
51 "Both diff requests need Authorization Bearer and a personal token that can read the repo. " +
52 "A merge request uses action opened, merged or commented, and commented includes the comment text. " +
53 "A release lists the tag, name, commit and asset URLs.",
54 },
55 "servers": []any{map[string]any{"url": "/api/v1"}},
56 "paths": map[string]any{
57 "/repos/{owner}/{repo}/commits/{sha}/diff": map[string]any{
58 "get": map[string]any{
59 "summary": "Commit diff",
60 "description": "Returns the unified diff of the commit against its first parent as text/plain. A root commit is compared with an empty tree. Requires a personal access token that can read the repo. On a public repo any valid personal token is enough. Job tokens are rejected.",
61 "security": []any{map[string]any{"bearer": []any{}}},
62 "parameters": []any{
63 map[string]any{"name": "owner", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
64 map[string]any{"name": "repo", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
65 map[string]any{"name": "sha", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
66 },
67 "responses": map[string]any{
68 "200": map[string]any{
69 "description": "Unified diff.",
70 "content": map[string]any{"text/plain": map[string]any{"schema": map[string]any{"type": "string"}}},
71 },
72 "401": map[string]any{"description": "Missing or invalid token."},
73 "403": map[string]any{"description": "The token cannot read this private repo."},
74 "404": map[string]any{"description": "Unknown repo or commit."},
75 },
76 },
77 },
78 "/repos/{owner}/{repo}/merge-requests/{id}/diff": map[string]any{
79 "get": map[string]any{
80 "summary": "Merge request diff",
81 "description": "Returns the three-dot unified diff from the target branch to the source branch as text/plain. Requires a personal access token that can read the repo. On a public repo any valid personal token is enough. Job tokens are rejected.",
82 "security": []any{map[string]any{"bearer": []any{}}},
83 "parameters": []any{
84 map[string]any{"name": "owner", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
85 map[string]any{"name": "repo", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
86 map[string]any{"name": "id", "in": "path", "required": true, "schema": map[string]any{"type": "integer"}},
87 },
88 "responses": map[string]any{
89 "200": map[string]any{
90 "description": "Unified diff.",
91 "content": map[string]any{"text/plain": map[string]any{"schema": map[string]any{"type": "string"}}},
92 },
93 "401": map[string]any{"description": "Missing or invalid token."},
94 "403": map[string]any{"description": "The token cannot read this private repo."},
95 "404": map[string]any{"description": "Unknown repo or merge request."},
96 },
97 },
98 },
99 "/repos/{owner}/{repo}/jobs/{id}/log": map[string]any{
100 "get": map[string]any{
101 "summary": "Full job log",
102 "description": "Returns the full console output as text/plain. Secret values of at least 8 characters are replaced with [MASKED]. Requires a personal access token. A token that can read the repo is enough, and on a public repo any valid personal token is enough. Job tokens are rejected.",
103 "security": []any{map[string]any{"bearer": []any{}}},
104 "parameters": []any{
105 map[string]any{"name": "owner", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
106 map[string]any{"name": "repo", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
107 map[string]any{"name": "id", "in": "path", "required": true, "schema": map[string]any{"type": "integer"}},
108 },
109 "responses": map[string]any{
110 "200": map[string]any{
111 "description": "Full log.",
112 "content": map[string]any{"text/plain": map[string]any{"schema": map[string]any{"type": "string"}}},
113 },
114 "401": map[string]any{"description": "Missing or invalid token."},
115 "403": map[string]any{"description": "The token cannot read this private repo."},
116 "404": map[string]any{"description": "Unknown repo or job."},
117 },
118 },
119 },
120 },
121 "webhooks": map[string]any{
122 "pipeline": post("Pipeline finished", "#/components/schemas/PipelineEvent"),
123 "push": post("Push", "#/components/schemas/PushEvent"),
124 "merge_request": post("Merge request", "#/components/schemas/MergeRequestEvent"),
125 "release": post("Release published", "#/components/schemas/ReleaseEvent"),
126 },
127 "components": map[string]any{
128 "securitySchemes": map[string]any{
129 "bearer": map[string]any{"type": "http", "scheme": "bearer", "description": "Personal access token (bg_)."},
130 },
131 "schemas": map[string]any{
132 "PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{
133 "object_kind": str("Always pipeline."),
134 "type": str("pipeline_success or pipeline_failure."),
135 "project": str("owner/name."),
136 "actor": str("User who started the pipeline."),
137 "summary": str("One-line summary."),
138 "url": str("Pipeline page."),
139 "pipeline_id": str("Same id as pipeline.id, as text."),
140 "pipeline": map[string]any{"$ref": "#/components/schemas/Pipeline"},
141 "jobs": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Job"}},
142 }, []string{"object_kind", "type", "project", "pipeline", "jobs"}),
143 "Pipeline": obj("The pipeline this event is about.", map[string]any{
144 "id": map[string]any{"type": "integer", "description": "Pipeline id."},
145 "status": str("success or failed."),
146 "ref": str("Full ref, such as refs/heads/master."),
147 "sha": str("Commit SHA."),
148 "commit_message": str("Subject and body of the commit."),
149 "author": str("User who started the pipeline."),
150 "duration": num("Seconds from start to finish."),
151 "url": str("Pipeline page."),
152 }, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}),
153 "Job": obj("One job. log is always the console tail. log_url is present only when log_truncated is true.", map[string]any{
154 "name": str("Job name."),
155 "stage": str("Stage name."),
156 "status": str("Job status."),
157 "duration": num("Seconds the job ran."),
158 "allow_failure": boolean("True when a failure does not fail the pipeline."),
159 "web_url": str("Job page."),
160 "log": str("Last 200 lines of the console output, at most 64 KiB. Secrets of at least 8 characters are [MASKED]."),
161 "log_truncated": boolean("True when log is shorter than the stored console output."),
162 "log_url": str("GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Present only when log_truncated is true. Requires Authorization Bearer and a personal token that can read the repo. The response is the full log as text/plain, with secrets masked."),
163 }, []string{"name", "stage", "status", "duration", "allow_failure", "web_url", "log", "log_truncated"}),
164 "PushEvent": obj("A branch or tag update.", map[string]any{
165 "object_kind": str("Always push."),
166 "type": str("Always push."),
167 "project": str("owner/name."),
168 "actor": str("User who pushed."),
169 "ref": str("Full ref."),
170 "before": str("Previous SHA, or 40 zeros when the ref is new."),
171 "after": str("New SHA."),
172 "commits": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Commit"}},
173 }, []string{"object_kind", "type", "project", "ref", "before", "after", "commits"}),
174 "Commit": obj("One commit. The diff text is not included.", map[string]any{
175 "sha": str("Commit SHA."),
176 "message": str("Subject and body."),
177 "author": str("Name and email."),
178 "timestamp": str("Author time in RFC3339."),
179 "url": str("Commit page."),
180 "files": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/FileChange"}},
181 "diff_url": str("GET /api/v1/repos/{owner}/{repo}/commits/{sha}/diff. Requires Authorization Bearer and a personal token that can read the repo. The response is the unified diff as text/plain."),
182 }, []string{"sha", "message", "author", "timestamp", "url", "files", "diff_url"}),
183 "FileChange": obj("One changed file, without the diff text.", map[string]any{
184 "path": str("Path after the change."),
185 "status": str("added, modified, removed or renamed."),
186 "previous_path": str("Path before a rename."),
187 "additions": map[string]any{"type": "integer", "description": "Added lines. Zero for a binary file."},
188 "deletions": map[string]any{"type": "integer", "description": "Deleted lines. Zero for a binary file."},
189 "binary": boolean("True when the file is binary."),
190 }, []string{"path", "status", "additions", "deletions"}),
191 "MergeRequestEvent": obj("Opened, merged or commented.", map[string]any{
192 "object_kind": str("Always merge_request."),
193 "type": str("Always merge_request."),
194 "project": str("owner/name."),
195 "actor": str("User who did this action."),
196 "merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"},
197 "comment": str("Comment text. Present only when action is commented."),
198 }, []string{"object_kind", "type", "project", "merge_request"}),
199 "MergeRequest": obj("The merge request.", map[string]any{
200 "id": map[string]any{"type": "integer", "description": "Number in the project."},
201 "title": str("Title."),
202 "description": str("Description. May be empty."),
203 "source_branch": str("Source branch."),
204 "target_branch": str("Target branch."),
205 "status": str("open, merged or closed."),
206 "author": str("User who opened the merge request."),
207 "action": str("opened, merged or commented."),
208 "url": str("Merge request page."),
209 "reviewers": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Reviewer"}},
210 "comments": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Comment"}},
211 "commits": map[string]any{"type": "array", "description": "Up to 20 commits on the source branch that the target does not have, oldest first.", "items": map[string]any{"$ref": "#/components/schemas/Commit"}},
212 "diff_stats": map[string]any{"$ref": "#/components/schemas/DiffStats"},
213 "diff_url": str("GET /api/v1/repos/{owner}/{repo}/merge-requests/{id}/diff. Requires Authorization Bearer and a personal token that can read the repo. The response is the three-dot unified diff as text/plain."),
214 }, []string{"id", "title", "description", "source_branch", "target_branch", "status", "author", "action", "reviewers", "comments", "commits", "diff_stats", "diff_url"}),
215 "Reviewer": obj("The current review of one person.", map[string]any{
216 "username": str("Username."),
217 "status": str("approved, changes, rejected or dismissed."),
218 }, []string{"username", "status"}),
219 "Comment": obj("One comment on the merge request.", map[string]any{
220 "id": map[string]any{"type": "integer", "description": "Comment id."},
221 "author": str("Username."),
222 "body": str("Comment text."),
223 "created_at": str("Time in RFC3339."),
224 "file_path": str("File, when the comment is on a line."),
225 "line_start": map[string]any{"type": "integer", "description": "First line."},
226 "line_end": map[string]any{"type": "integer", "description": "Last line."},
227 "parent_id": map[string]any{"type": "integer", "description": "Parent comment, when this is a reply."},
228 }, []string{"id", "author", "body", "created_at"}),
229 "DiffStats": obj("Line counts of the merge request diff. The diff text is not included.", map[string]any{
230 "additions": map[string]any{"type": "integer", "description": "Added lines."},
231 "deletions": map[string]any{"type": "integer", "description": "Deleted lines."},
232 "files": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/FileChange"}},
233 }, []string{"additions", "deletions", "files"}),
234 "ReleaseEvent": obj("A published release.", map[string]any{
235 "object_kind": str("Always release."),
236 "type": str("Always release_published."),
237 "project": str("owner/name."),
238 "release_tag": str("Same value as release.tag."),
239 "release": map[string]any{"$ref": "#/components/schemas/Release"},
240 }, []string{"object_kind", "type", "project", "release"}),
241 "Release": obj("The release.", map[string]any{
242 "tag": str("Tag name."),
243 "name": str("Release title."),
244 "commit": str("Commit SHA."),
245 "assets": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Asset"}},
246 }, []string{"tag", "name", "commit", "assets"}),
247 "Asset": obj("A file attached to the release.", map[string]any{
248 "name": str("File name."),
249 "url": str("Download URL."),
250 "size": map[string]any{"type": "integer", "description": "Size in bytes."},
251 }, []string{"name", "url", "size"}),
252 },
253 },
254 }
255 raw, err := json.MarshalIndent(doc, "", " ")
256 if err != nil {
257 return []byte("{}\n")
258 }
259 return append(raw, '\n')
260}