BlueGit

timfa bluegit

Files

bluegit / internal / webhook / openapi.go

openapi.go

fix: stuur de console-output van elke job mee in pipeline-webhooks
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
6080426e History
openapi.go 9.3 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. " +
48 "A merge request uses action opened, merged or commented, and commented includes the comment text. " +
49 "A release lists the tag, name, commit and asset URLs.",
50 },
51 "servers": []any{map[string]any{"url": "/api/v1"}},
52 "paths": map[string]any{
53 "/repos/{owner}/{repo}/jobs/{id}/log": map[string]any{
54 "get": map[string]any{
55 "summary": "Full job log",
56 "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.",
57 "security": []any{map[string]any{"bearer": []any{}}},
58 "parameters": []any{
59 map[string]any{"name": "owner", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
60 map[string]any{"name": "repo", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
61 map[string]any{"name": "id", "in": "path", "required": true, "schema": map[string]any{"type": "integer"}},
62 },
63 "responses": map[string]any{
64 "200": map[string]any{
65 "description": "Full log.",
66 "content": map[string]any{"text/plain": map[string]any{"schema": map[string]any{"type": "string"}}},
67 },
68 "401": map[string]any{"description": "Missing or invalid token."},
69 "403": map[string]any{"description": "The token cannot read this private repo."},
70 "404": map[string]any{"description": "Unknown repo or job."},
71 },
72 },
73 },
74 },
75 "webhooks": map[string]any{
76 "pipeline": post("Pipeline finished", "#/components/schemas/PipelineEvent"),
77 "push": post("Push", "#/components/schemas/PushEvent"),
78 "merge_request": post("Merge request", "#/components/schemas/MergeRequestEvent"),
79 "release": post("Release published", "#/components/schemas/ReleaseEvent"),
80 },
81 "components": map[string]any{
82 "securitySchemes": map[string]any{
83 "bearer": map[string]any{"type": "http", "scheme": "bearer", "description": "Personal access token (bg_)."},
84 },
85 "schemas": map[string]any{
86 "PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{
87 "object_kind": str("Always pipeline."),
88 "type": str("pipeline_success or pipeline_failure."),
89 "project": str("owner/name."),
90 "actor": str("User who started the pipeline."),
91 "summary": str("One-line summary."),
92 "url": str("Pipeline page."),
93 "pipeline_id": str("Same id as pipeline.id, as text."),
94 "pipeline": map[string]any{"$ref": "#/components/schemas/Pipeline"},
95 "jobs": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Job"}},
96 }, []string{"object_kind", "type", "project", "pipeline", "jobs"}),
97 "Pipeline": obj("The pipeline this event is about.", map[string]any{
98 "id": map[string]any{"type": "integer", "description": "Pipeline id."},
99 "status": str("success or failed."),
100 "ref": str("Full ref, such as refs/heads/master."),
101 "sha": str("Commit SHA."),
102 "commit_message": str("Subject and body of the commit."),
103 "author": str("User who started the pipeline."),
104 "duration": num("Seconds from start to finish."),
105 "url": str("Pipeline page."),
106 }, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}),
107 "Job": obj("One job. log is always the console tail. log_url is present only when log_truncated is true.", map[string]any{
108 "name": str("Job name."),
109 "stage": str("Stage name."),
110 "status": str("Job status."),
111 "duration": num("Seconds the job ran."),
112 "allow_failure": boolean("True when a failure does not fail the pipeline."),
113 "web_url": str("Job page."),
114 "log": str("Last 200 lines of the console output, at most 64 KiB. Secrets of at least 8 characters are [MASKED]."),
115 "log_truncated": boolean("True when log is shorter than the stored console output."),
116 "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."),
117 }, []string{"name", "stage", "status", "duration", "allow_failure", "web_url", "log", "log_truncated"}),
118 "PushEvent": obj("A branch or tag update.", map[string]any{
119 "object_kind": str("Always push."),
120 "type": str("Always push."),
121 "project": str("owner/name."),
122 "actor": str("User who pushed."),
123 "ref": str("Full ref."),
124 "before": str("Previous SHA, or 40 zeros when the ref is new."),
125 "after": str("New SHA."),
126 "commits": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Commit"}},
127 }, []string{"object_kind", "type", "project", "ref", "before", "after", "commits"}),
128 "Commit": obj("One commit made reachable by the push.", map[string]any{
129 "sha": str("Commit SHA."),
130 "message": str("Subject and body."),
131 "author": str("Name and email."),
132 "url": str("Commit page."),
133 }, []string{"sha", "message", "author", "url"}),
134 "MergeRequestEvent": obj("Opened, merged or commented.", map[string]any{
135 "object_kind": str("Always merge_request."),
136 "type": str("Always merge_request."),
137 "project": str("owner/name."),
138 "actor": str("User who did this action."),
139 "merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"},
140 "comment": str("Comment text. Present only when action is commented."),
141 }, []string{"object_kind", "type", "project", "merge_request"}),
142 "MergeRequest": obj("The merge request.", map[string]any{
143 "id": map[string]any{"type": "integer", "description": "Number in the project."},
144 "title": str("Title."),
145 "description": str("Description. May be empty."),
146 "source_branch": str("Source branch."),
147 "target_branch": str("Target branch."),
148 "status": str("open, merged or closed."),
149 "author": str("User who opened the merge request."),
150 "action": str("opened, merged or commented."),
151 "url": str("Merge request page."),
152 }, []string{"id", "title", "description", "source_branch", "target_branch", "status", "author", "action"}),
153 "ReleaseEvent": obj("A published release.", map[string]any{
154 "object_kind": str("Always release."),
155 "type": str("Always release_published."),
156 "project": str("owner/name."),
157 "release_tag": str("Same value as release.tag."),
158 "release": map[string]any{"$ref": "#/components/schemas/Release"},
159 }, []string{"object_kind", "type", "project", "release"}),
160 "Release": obj("The release.", map[string]any{
161 "tag": str("Tag name."),
162 "name": str("Release title."),
163 "commit": str("Commit SHA."),
164 "assets": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Asset"}},
165 }, []string{"tag", "name", "commit", "assets"}),
166 "Asset": obj("A file attached to the release.", map[string]any{
167 "name": str("File name."),
168 "url": str("Download URL."),
169 "size": map[string]any{"type": "integer", "description": "Size in bytes."},
170 }, []string{"name", "url", "size"}),
171 },
172 },
173 }
174 raw, err := json.MarshalIndent(doc, "", " ")
175 if err != nil {
176 return []byte("{}\n")
177 }
178 return append(raw, '\n')
179}