| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/cookiejar" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 18 | "gitlab.com/Timfa/bluegit/internal/config" |
| 19 | "gitlab.com/Timfa/bluegit/internal/store" |
| 20 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 21 | ) |
| 22 | |
| 23 | func TestWebhookPayloads(t *testing.T) { |
| 24 | if _, err := exec.LookPath("git"); err != nil { |
| 25 | t.Skip("git ontbreekt") |
| 26 | } |
| 27 | storage := t.TempDir() |
| 28 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | t.Cleanup(func() { _ = st.Close() }) |
| 33 | cfg := config.Config{ |
| 34 | BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage, |
| 35 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 36 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 37 | } |
| 38 | var bodies [][]byte |
| 39 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 40 | srv.hookSink = func(b []byte) { |
| 41 | bodies = append(bodies, append([]byte(nil), b...)) |
| 42 | } |
| 43 | ts := httptest.NewServer(srv.Handler()) |
| 44 | t.Cleanup(ts.Close) |
| 45 | jar, _ := cookiejar.New(nil) |
| 46 | tim := &http.Client{Jar: jar} |
| 47 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 48 | page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 49 | "csrf": {csrfOf(t, page)}, "name": {"hooks"}, "visibility": {"public"}, |
| 50 | })) |
| 51 | ctx := context.Background() |
| 52 | repo, err := st.RepoByPath(ctx, "tim", "hooks") |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if _, err := st.CreateWebhook(ctx, store.Webhook{ |
| 57 | ScopeKind: "repo", ScopeID: repo.ID, Name: "alles", Preset: "generic", |
| 58 | URLTemplate: "https://example.test/hook", |
| 59 | Events: []string{"push", "merge_request", "pipeline_failure", "release_published"}, |
| 60 | }); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if err := st.PutSecret(ctx, "repo", repo.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{ |
| 68 | "csrf": {csrfOf(t, page)}, "name": {"laptop"}, |
| 69 | })) |
| 70 | token := between(page, `<p class="code">`, "</p>") |
| 71 | remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/hooks.git" |
| 72 | work := t.TempDir() |
| 73 | git(t, work, "init", "-b", "master") |
| 74 | git(t, work, "config", "user.email", "tim@users.git.timfalken.com") |
| 75 | git(t, work, "config", "user.name", "tim") |
| 76 | writeFile(t, filepath.Join(work, "README.md"), "a\n") |
| 77 | git(t, work, "add", "README.md") |
| 78 | git(t, work, "commit", "-m", "eerste regel", "-m", "meer tekst") |
| 79 | writeFile(t, filepath.Join(work, "README.md"), "b\n") |
| 80 | git(t, work, "add", "README.md") |
| 81 | git(t, work, "commit", "-m", "tweede") |
| 82 | git(t, work, "remote", "add", "origin", remote) |
| 83 | git(t, work, "push", "-u", "origin", "master") |
| 84 | |
| 85 | push := decodeKind(t, bodies, "push") |
| 86 | if push["ref"] != "refs/heads/master" || push["before"] != strings.Repeat("0", 40) { |
| 87 | t.Fatalf("push ref/before %v", push) |
| 88 | } |
| 89 | commits := push["commits"].([]any) |
| 90 | if len(commits) != 2 { |
| 91 | t.Fatalf("commits %d %s", len(commits), mustJSON(t, push)) |
| 92 | } |
| 93 | first := commits[0].(map[string]any) |
| 94 | second := commits[1].(map[string]any) |
| 95 | if first["message"] != "eerste regel\n\nmeer tekst" || second["message"] != "tweede" { |
| 96 | t.Fatalf("berichten %v %v", first["message"], second["message"]) |
| 97 | } |
| 98 | if first["author"] != "tim <tim@users.git.timfalken.com>" || !strings.Contains(first["url"].(string), "/tim/hooks/-/commit/") { |
| 99 | t.Fatalf("auteur of url %v", first) |
| 100 | } |
| 101 | if push["after"] != second["sha"] { |
| 102 | t.Fatalf("after %v tip %v", push["after"], second["sha"]) |
| 103 | } |
| 104 | |
| 105 | git(t, work, "checkout", "-b", "topic") |
| 106 | writeFile(t, filepath.Join(work, "extra.txt"), "c\n") |
| 107 | git(t, work, "add", "extra.txt") |
| 108 | git(t, work, "commit", "-m", "derde") |
| 109 | git(t, work, "push", "-u", "origin", "topic") |
| 110 | |
| 111 | page = read(t, mustGet(t, tim, ts.URL+"/tim/hooks/-/merge-requests/new?source=topic&target=master")) |
| 112 | page = read(t, mustPost(t, tim, ts.URL+"/tim/hooks/-/merge-requests", url.Values{ |
| 113 | "csrf": {csrfOf(t, page)}, "source": {"topic"}, "target": {"master"}, |
| 114 | "title": {"Toon de kloon-URL"}, "description": {"SSH en HTTPS."}, |
| 115 | })) |
| 116 | opened := decodeAction(t, bodies, "opened") |
| 117 | mr := opened["merge_request"].(map[string]any) |
| 118 | if mr["title"] != "Toon de kloon-URL" || mr["description"] != "SSH en HTTPS." || mr["source_branch"] != "topic" || mr["target_branch"] != "master" || mr["status"] != "open" || mr["author"] != "tim" || mr["id"] != float64(1) { |
| 119 | t.Fatalf("opened %s", mustJSON(t, mr)) |
| 120 | } |
| 121 | if _, ok := opened["comment"]; ok { |
| 122 | t.Fatalf("opened heeft een comment: %s", mustJSON(t, opened)) |
| 123 | } |
| 124 | |
| 125 | page = read(t, mustPost(t, tim, ts.URL+"/tim/hooks/-/merge-requests/1/notes", url.Values{ |
| 126 | "csrf": {csrfOf(t, page)}, "body": {"dit is het commentaar"}, |
| 127 | })) |
| 128 | commented := decodeAction(t, bodies, "commented") |
| 129 | if commented["comment"] != "dit is het commentaar" { |
| 130 | t.Fatalf("comment %s", mustJSON(t, commented)) |
| 131 | } |
| 132 | |
| 133 | page = read(t, mustPost(t, tim, ts.URL+"/tim/hooks/-/merge-requests/1/merge", url.Values{ |
| 134 | "csrf": {csrfOf(t, page)}, |
| 135 | })) |
| 136 | if strings.Contains(page, "flash") && strings.Contains(strings.ToLower(page), "error") { |
| 137 | t.Fatalf("merge pagina:\n%s", page) |
| 138 | } |
| 139 | merged := decodeAction(t, bodies, "merged") |
| 140 | if merged["merge_request"].(map[string]any)["status"] != "merged" { |
| 141 | t.Fatalf("merged %s", mustJSON(t, merged)) |
| 142 | } |
| 143 | |
| 144 | sha := second["sha"].(string) |
| 145 | id, err := st.CreatePipeline(ctx, repo.ID, sha, "refs/heads/master", "push", "tim", "tweede", []store.NewJob{ |
| 146 | {Name: "test", Stage: "test", Script: "exit 1"}, |
| 147 | {Name: "build", Stage: "build", Script: "echo ok"}, |
| 148 | {Name: "soft", Stage: "test", Script: "exit 1", AllowFailure: true}, |
| 149 | }) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | jobs, err := st.JobsByPipeline(ctx, id) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | var lines []string |
| 158 | for i := 0; i < webhook.LogMaxLines+40; i++ { |
| 159 | lines = append(lines, "regel") |
| 160 | } |
| 161 | lines = append(lines, "token super-secret-token") |
| 162 | log := strings.Join(lines, "\n") |
| 163 | for _, j := range jobs { |
| 164 | if _, err := st.MarkJobRunning(ctx, j.ID); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | switch j.Name { |
| 168 | case "test": |
| 169 | if err := st.AppendJobLog(ctx, j.ID, log); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | case "soft": |
| 176 | if err := st.AppendJobLog(ctx, j.ID, "zacht super-secret-token\n"); err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | default: |
| 183 | if err := st.FinishJob(ctx, j.ID, "success", ""); err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | if _, err := st.RefreshPipelineStatus(ctx, id); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | before := len(bodies) |
| 192 | srv.Dispatch(repo.ID, webhook.Event{ |
| 193 | Type: webhook.EventPipelineFailure, PipelineID: strconv.FormatInt(id, 10), |
| 194 | }) |
| 195 | if len(bodies) == before { |
| 196 | t.Fatal("geen pipeline-payload") |
| 197 | } |
| 198 | var pipe map[string]any |
| 199 | if err := json.Unmarshal(bodies[len(bodies)-1], &pipe); err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | if pipe["object_kind"] != "pipeline" || pipe["type"] != "pipeline_failure" { |
| 203 | t.Fatalf("pipeline %s", bodies[len(bodies)-1]) |
| 204 | } |
| 205 | detail := pipe["pipeline"].(map[string]any) |
| 206 | if detail["id"] != float64(id) || detail["status"] != "failed" || detail["ref"] != "refs/heads/master" || detail["sha"] != sha || detail["author"] != "tim" { |
| 207 | t.Fatalf("detail %s", mustJSON(t, detail)) |
| 208 | } |
| 209 | if !strings.Contains(detail["commit_message"].(string), "tweede") { |
| 210 | t.Fatalf("commit_message %v", detail["commit_message"]) |
| 211 | } |
| 212 | if _, ok := detail["duration"]; !ok { |
| 213 | t.Fatal("duur ontbreekt") |
| 214 | } |
| 215 | for _, item := range pipe["jobs"].([]any) { |
| 216 | job := item.(map[string]any) |
| 217 | raw, _ := json.Marshal(job) |
| 218 | if strings.Contains(string(raw), "super-secret-token") { |
| 219 | t.Fatalf("geheim in job %s", raw) |
| 220 | } |
| 221 | switch job["name"] { |
| 222 | case "test": |
| 223 | if job["log_truncated"] != true || job["allow_failure"] != false { |
| 224 | t.Fatalf("test %s", raw) |
| 225 | } |
| 226 | text := job["log"].(string) |
| 227 | if !strings.Contains(text, "[MASKED]") || len(strings.Split(text, "\n")) != webhook.LogMaxLines { |
| 228 | t.Fatalf("log %d regels, staart %q", len(strings.Split(text, "\n")), text[max(0, len(text)-40):]) |
| 229 | } |
| 230 | if !strings.Contains(job["log_url"].(string), "/log?raw=1") || !strings.Contains(job["web_url"].(string), "/jobs/") { |
| 231 | t.Fatalf("urls %s", raw) |
| 232 | } |
| 233 | case "build": |
| 234 | if _, ok := job["log"]; ok || job["status"] != "success" { |
| 235 | t.Fatalf("build %s", raw) |
| 236 | } |
| 237 | case "soft": |
| 238 | if job["allow_failure"] != true || !strings.Contains(job["log"].(string), "[MASKED]") { |
| 239 | t.Fatalf("soft %s", raw) |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | rel := srv.releaseEvent(repo, "v1.2.0", "BlueGit 1.2.0", sha, []store.ReleaseAsset{{Name: "blue git", Size: 9}}) |
| 245 | raw, err := webhook.RenderBody("", rel, nil) |
| 246 | if err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | var release map[string]any |
| 250 | if err := json.Unmarshal(raw, &release); err != nil { |
| 251 | t.Fatal(err) |
| 252 | } |
| 253 | info := release["release"].(map[string]any) |
| 254 | asset := info["assets"].([]any)[0].(map[string]any) |
| 255 | if info["tag"] != "v1.2.0" || info["name"] != "BlueGit 1.2.0" || info["commit"] != sha || !strings.Contains(asset["url"].(string), "/-/releases/v1.2.0/assets/blue%20git") { |
| 256 | t.Fatalf("release %s", raw) |
| 257 | } |
| 258 | |
| 259 | if elapsedSeconds(time.Unix(10, 0), time.Unix(12, 0)) != 2 { |
| 260 | t.Fatal("duur") |
| 261 | } |
| 262 | |
| 263 | infoPage := read(t, mustGet(t, tim, ts.URL+"/-/info")) |
| 264 | for _, want := range []string{"Webhook payloads", "log_truncated", "log_url", "/api/v1/webhooks/openapi.json", "commit_message", "source_branch"} { |
| 265 | if !strings.Contains(infoPage, want) { |
| 266 | t.Errorf("infopagina mist %q", want) |
| 267 | } |
| 268 | } |
| 269 | specRes, err := http.Get(ts.URL + "/api/v1/webhooks/openapi.json") |
| 270 | if err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | spec := read(t, specRes) |
| 274 | if specRes.StatusCode != 200 || !strings.Contains(spec, `"openapi": "3.1.0"`) || !strings.Contains(spec, "log_truncated") { |
| 275 | t.Fatalf("spec %d %s", specRes.StatusCode, spec[:min(200, len(spec))]) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | func decodeKind(t *testing.T, bodies [][]byte, kind string) map[string]any { |
| 280 | t.Helper() |
| 281 | for i := len(bodies) - 1; i >= 0; i-- { |
| 282 | var doc map[string]any |
| 283 | if err := json.Unmarshal(bodies[i], &doc); err != nil { |
| 284 | continue |
| 285 | } |
| 286 | if doc["object_kind"] == kind { |
| 287 | return doc |
| 288 | } |
| 289 | } |
| 290 | t.Fatalf("geen %s in %d payloads", kind, len(bodies)) |
| 291 | return nil |
| 292 | } |
| 293 | |
| 294 | func decodeAction(t *testing.T, bodies [][]byte, action string) map[string]any { |
| 295 | t.Helper() |
| 296 | for i := len(bodies) - 1; i >= 0; i-- { |
| 297 | var doc map[string]any |
| 298 | if err := json.Unmarshal(bodies[i], &doc); err != nil { |
| 299 | continue |
| 300 | } |
| 301 | mr, _ := doc["merge_request"].(map[string]any) |
| 302 | if mr != nil && mr["action"] == action { |
| 303 | return doc |
| 304 | } |
| 305 | } |
| 306 | t.Fatalf("geen actie %s", action) |
| 307 | return nil |
| 308 | } |
| 309 | |
| 310 | func mustJSON(t *testing.T, v any) string { |
| 311 | t.Helper() |
| 312 | b, err := json.Marshal(v) |
| 313 | if err != nil { |
| 314 | t.Fatal(err) |
| 315 | } |
| 316 | return string(b) |
| 317 | } |