package webhook import ( "encoding/json" "strings" "gitlab.com/Timfa/bluegit/internal/ci" ) // Grenzen voor wat een webhook van een gefaalde job meestuurt. const ( LogMaxLines = 200 LogMaxBytes = 64 << 10 ) // MaxPushCommits is het aantal commits in een push-payload. // Dat zijn de nieuwste in het bereik, oudste eerst. const MaxPushCommits = 20 // Document is de JSON-body van een webhook zonder eigen sjabloon. type Document struct { ObjectKind string `json:"object_kind"` Type string `json:"type"` Project string `json:"project"` Group string `json:"group,omitempty"` Actor string `json:"actor,omitempty"` Summary string `json:"summary,omitempty"` URL string `json:"url,omitempty"` Branch string `json:"branch,omitempty"` Commit string `json:"commit,omitempty"` PipelineID string `json:"pipeline_id,omitempty"` ReleaseTag string `json:"release_tag,omitempty"` Pipeline *PipelineInfo `json:"pipeline,omitempty"` Jobs *[]JobInfo `json:"jobs,omitempty"` Ref string `json:"ref,omitempty"` Before string `json:"before,omitempty"` After string `json:"after,omitempty"` Commits *[]CommitInfo `json:"commits,omitempty"` MergeRequest *MRInfo `json:"merge_request,omitempty"` Comment string `json:"comment,omitempty"` Release *ReleaseInfo `json:"release,omitempty"` } // PipelineInfo is de pipeline waar het event over gaat. type PipelineInfo struct { ID int64 `json:"id"` Status string `json:"status"` Ref string `json:"ref"` SHA string `json:"sha"` CommitMessage string `json:"commit_message"` Author string `json:"author"` Duration float64 `json:"duration"` URL string `json:"url,omitempty"` } // JobInfo is één job in een pipeline-event. // Log, log_truncated en log_url staan er alleen bij een gefaalde job. type JobInfo struct { Name string `json:"name"` Stage string `json:"stage"` Status string `json:"status"` Duration float64 `json:"duration"` AllowFailure bool `json:"allow_failure"` WebURL string `json:"web_url"` Log string `json:"log,omitempty"` LogTruncated *bool `json:"log_truncated,omitempty"` LogURL string `json:"log_url,omitempty"` } // CommitInfo is één commit in een push. type CommitInfo struct { SHA string `json:"sha"` Message string `json:"message"` Author string `json:"author"` URL string `json:"url"` } // MRInfo is de merge request van het event. type MRInfo struct { ID int64 `json:"id"` Title string `json:"title"` Description string `json:"description"` SourceBranch string `json:"source_branch"` TargetBranch string `json:"target_branch"` Status string `json:"status"` Author string `json:"author"` Action string `json:"action"` URL string `json:"url,omitempty"` } // ReleaseInfo is een gepubliceerde release. type ReleaseInfo struct { Tag string `json:"tag"` Name string `json:"name"` Commit string `json:"commit"` Assets []AssetInfo `json:"assets"` } // AssetInfo is een bestand bij een release. type AssetInfo struct { Name string `json:"name"` URL string `json:"url"` Size int64 `json:"size"` } // Document bouwt de JSON-structuur. Lege deelobjecten blijven weg. func (ev Event) Document() Document { d := Document{ ObjectKind: objectKind(ev.Type), Type: ev.Type, Project: ev.Project, Group: ev.Group, Actor: ev.Actor, Summary: ev.Summary, URL: ev.URL, Branch: ev.Branch, Commit: ev.Commit, PipelineID: ev.PipelineID, ReleaseTag: ev.ReleaseTag, Pipeline: ev.Pipeline, Ref: ev.Ref, Before: ev.Before, After: ev.After, MergeRequest: ev.MergeRequest, Release: ev.Release, } switch ev.Type { case EventPush: commits := ev.Commits if commits == nil { commits = []CommitInfo{} } d.Commits = &commits case EventPipelineSuccess, EventPipelineFailure: jobs := ev.Jobs if jobs == nil { jobs = []JobInfo{} } d.Jobs = &jobs case EventMergeRequest: if ev.MergeRequest != nil && ev.MergeRequest.Action == "commented" { d.Comment = ev.Comment } } if d.Release != nil && d.Release.Assets == nil { rel := *d.Release rel.Assets = []AssetInfo{} d.Release = &rel } return d } func objectKind(eventType string) string { switch eventType { case EventPipelineSuccess, EventPipelineFailure: return "pipeline" case EventRelease: return "release" case EventMergeRequest: return "merge_request" default: return eventType } } // TailLog houdt de laatste LogMaxLines regels en hoogstens LogMaxBytes. // Geheimwaarden van minstens 8 tekens worden [MASKED], net als in de joblog. // truncated is true zodra er regels of bytes af zijn. func TailLog(text string, secrets []string) (string, bool) { text = ci.Mask(text, secrets) if text == "" { return "", false } truncated := false lines := strings.Split(text, "\n") if len(lines) > LogMaxLines { lines = lines[len(lines)-LogMaxLines:] truncated = true } out := strings.Join(lines, "\n") if len(out) > LogMaxBytes { out = cutUTF8Tail(out, LogMaxBytes) truncated = true } return out, truncated } func cutUTF8Tail(s string, max int) string { if len(s) <= max { return s } s = s[len(s)-max:] for len(s) > 0 && s[0]&0xC0 == 0x80 { s = s[1:] } return s } // Example is het voorbeeld op de infopagina. func Example() string { trunc := true samples := []Event{ { Type: EventPipelineFailure, Project: "tim/app", Actor: "tim", Summary: "pipeline #26 voor tim/app is mislukt", URL: "https://git.example/tim/app/-/pipelines/26", Branch: "master", Commit: "725e6947", PipelineID: "26", Pipeline: &PipelineInfo{ ID: 26, Status: "failed", Ref: "refs/heads/master", SHA: "725e6947", CommitMessage: "fix de build", Author: "tim", Duration: 12.4, URL: "https://git.example/tim/app/-/pipelines/26", }, Jobs: []JobInfo{ { Name: "test", Stage: "test", Status: "failed", Duration: 4.1, AllowFailure: false, WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/7", Log: "go: token [MASKED]\nFAIL", LogTruncated: &trunc, LogURL: "https://git.example/tim/app/-/pipelines/26/jobs/7/log?raw=1", }, { Name: "build", Stage: "build", Status: "skipped", Duration: 0, WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/8", }, }, }, { Type: EventPush, Project: "tim/app", Actor: "tim", Branch: "master", Ref: "refs/heads/master", Before: strings.Repeat("0", 40), After: "abc123", Commits: []CommitInfo{{ SHA: "abc123", Message: "fix de build", Author: "Tim ", URL: "https://git.example/tim/app/-/commit/abc123", }}, }, { Type: EventMergeRequest, Project: "tim/app", Actor: "ada", MergeRequest: &MRInfo{ ID: 4, Title: "Toon de kloon-URL", Description: "SSH en HTTPS.", SourceBranch: "feature", TargetBranch: "master", Status: "open", Author: "ada", Action: "opened", URL: "https://git.example/tim/app/-/merge-requests/4", }, }, { Type: EventRelease, Project: "tim/app", ReleaseTag: "v1.2.0", Commit: "abc123", Release: &ReleaseInfo{ Tag: "v1.2.0", Name: "BlueGit 1.2.0", Commit: "abc123", Assets: []AssetInfo{{ Name: "bluegit", Size: 8123456, URL: "https://git.example/tim/app/-/releases/v1.2.0/assets/bluegit", }}, }, }, } var b strings.Builder for i, ev := range samples { if i > 0 { b.WriteString("\n\n") } raw, err := json.MarshalIndent(ev.Document(), "", " ") if err != nil { continue } b.Write(raw) } return b.String() }