| 1 | package webhook |
| 2 | |
| 3 | import "encoding/json" |
| 4 | |
| 5 | // OpenAPI beschrijft de JSON-body die een webhook ontvangt. |
| 6 | func 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 | "A failed job adds the last 200 log 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 the full log. " + |
| 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 | "webhooks": map[string]any{ |
| 52 | "pipeline": post("Pipeline finished", "#/components/schemas/PipelineEvent"), |
| 53 | "push": post("Push", "#/components/schemas/PushEvent"), |
| 54 | "merge_request": post("Merge request", "#/components/schemas/MergeRequestEvent"), |
| 55 | "release": post("Release published", "#/components/schemas/ReleaseEvent"), |
| 56 | }, |
| 57 | "components": map[string]any{ |
| 58 | "schemas": map[string]any{ |
| 59 | "PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{ |
| 60 | "object_kind": str("Always pipeline."), |
| 61 | "type": str("pipeline_success or pipeline_failure."), |
| 62 | "project": str("owner/name."), |
| 63 | "actor": str("User who started the pipeline."), |
| 64 | "summary": str("One-line summary."), |
| 65 | "url": str("Pipeline page."), |
| 66 | "pipeline_id": str("Same id as pipeline.id, as text."), |
| 67 | "pipeline": map[string]any{"$ref": "#/components/schemas/Pipeline"}, |
| 68 | "jobs": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Job"}}, |
| 69 | }, []string{"object_kind", "type", "project", "pipeline", "jobs"}), |
| 70 | "Pipeline": obj("The pipeline this event is about.", map[string]any{ |
| 71 | "id": map[string]any{"type": "integer", "description": "Pipeline id."}, |
| 72 | "status": str("success or failed."), |
| 73 | "ref": str("Full ref, such as refs/heads/master."), |
| 74 | "sha": str("Commit SHA."), |
| 75 | "commit_message": str("Subject and body of the commit."), |
| 76 | "author": str("User who started the pipeline."), |
| 77 | "duration": num("Seconds from start to finish."), |
| 78 | "url": str("Pipeline page."), |
| 79 | }, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}), |
| 80 | "Job": obj("One job. log, log_truncated and log_url are present only when status is failed.", map[string]any{ |
| 81 | "name": str("Job name."), |
| 82 | "stage": str("Stage name."), |
| 83 | "status": str("Job status."), |
| 84 | "duration": num("Seconds the job ran."), |
| 85 | "allow_failure": boolean("True when a failure does not fail the pipeline."), |
| 86 | "web_url": str("Job page."), |
| 87 | "log": str("Tail of the console output."), |
| 88 | "log_truncated": boolean("True when log is not the full console output."), |
| 89 | "log_url": str("Plain-text URL of the full log."), |
| 90 | }, []string{"name", "stage", "status", "duration", "allow_failure", "web_url"}), |
| 91 | "PushEvent": obj("A branch or tag update.", map[string]any{ |
| 92 | "object_kind": str("Always push."), |
| 93 | "type": str("Always push."), |
| 94 | "project": str("owner/name."), |
| 95 | "actor": str("User who pushed."), |
| 96 | "ref": str("Full ref."), |
| 97 | "before": str("Previous SHA, or 40 zeros when the ref is new."), |
| 98 | "after": str("New SHA."), |
| 99 | "commits": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Commit"}}, |
| 100 | }, []string{"object_kind", "type", "project", "ref", "before", "after", "commits"}), |
| 101 | "Commit": obj("One commit made reachable by the push.", map[string]any{ |
| 102 | "sha": str("Commit SHA."), |
| 103 | "message": str("Subject and body."), |
| 104 | "author": str("Name and email."), |
| 105 | "url": str("Commit page."), |
| 106 | }, []string{"sha", "message", "author", "url"}), |
| 107 | "MergeRequestEvent": obj("Opened, merged or commented.", map[string]any{ |
| 108 | "object_kind": str("Always merge_request."), |
| 109 | "type": str("Always merge_request."), |
| 110 | "project": str("owner/name."), |
| 111 | "actor": str("User who did this action."), |
| 112 | "merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"}, |
| 113 | "comment": str("Comment text. Present only when action is commented."), |
| 114 | }, []string{"object_kind", "type", "project", "merge_request"}), |
| 115 | "MergeRequest": obj("The merge request.", map[string]any{ |
| 116 | "id": map[string]any{"type": "integer", "description": "Number in the project."}, |
| 117 | "title": str("Title."), |
| 118 | "description": str("Description. May be empty."), |
| 119 | "source_branch": str("Source branch."), |
| 120 | "target_branch": str("Target branch."), |
| 121 | "status": str("open, merged or closed."), |
| 122 | "author": str("User who opened the merge request."), |
| 123 | "action": str("opened, merged or commented."), |
| 124 | "url": str("Merge request page."), |
| 125 | }, []string{"id", "title", "description", "source_branch", "target_branch", "status", "author", "action"}), |
| 126 | "ReleaseEvent": obj("A published release.", map[string]any{ |
| 127 | "object_kind": str("Always release."), |
| 128 | "type": str("Always release_published."), |
| 129 | "project": str("owner/name."), |
| 130 | "release_tag": str("Same value as release.tag."), |
| 131 | "release": map[string]any{"$ref": "#/components/schemas/Release"}, |
| 132 | }, []string{"object_kind", "type", "project", "release"}), |
| 133 | "Release": obj("The release.", map[string]any{ |
| 134 | "tag": str("Tag name."), |
| 135 | "name": str("Release title."), |
| 136 | "commit": str("Commit SHA."), |
| 137 | "assets": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Asset"}}, |
| 138 | }, []string{"tag", "name", "commit", "assets"}), |
| 139 | "Asset": obj("A file attached to the release.", map[string]any{ |
| 140 | "name": str("File name."), |
| 141 | "url": str("Download URL."), |
| 142 | "size": map[string]any{"type": "integer", "description": "Size in bytes."}, |
| 143 | }, []string{"name", "url", "size"}), |
| 144 | }, |
| 145 | }, |
| 146 | } |
| 147 | raw, err := json.MarshalIndent(doc, "", " ") |
| 148 | if err != nil { |
| 149 | return []byte("{}\n") |
| 150 | } |
| 151 | return append(raw, '\n') |
| 152 | } |