| 1 | package webhook |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestParseCheckRun(t *testing.T) { |
| 15 | payload := `{ |
| 16 | "action": "completed", |
| 17 | "check_run": { |
| 18 | "conclusion": "success", |
| 19 | "pull_requests": [{"number": 42}, {"number": 43}] |
| 20 | }, |
| 21 | "repository": {"full_name": "owner/repo"} |
| 22 | }` |
| 23 | |
| 24 | events := parseEvent("check_run", json.RawMessage(payload)) |
| 25 | if len(events) != 2 { |
| 26 | t.Fatalf("expected 2 events, got %d", len(events)) |
| 27 | } |
| 28 | if events[0].PRNumber != "42" || events[0].Repo != "owner/repo" { |
| 29 | t.Errorf("unexpected event[0]: %+v", events[0]) |
| 30 | } |
| 31 | if events[0].EventType != "check_run" { |
| 32 | t.Errorf("expected EventType=check_run, got %q", events[0].EventType) |
| 33 | } |
| 34 | if events[1].PRNumber != "43" { |
| 35 | t.Errorf("unexpected event[1] PRNumber: %s", events[1].PRNumber) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestParseCheckRunFailure(t *testing.T) { |
| 40 | payload := `{ |
| 41 | "action": "completed", |
| 42 | "check_run": { |
| 43 | "conclusion": "failure", |
| 44 | "pull_requests": [{"number": 10}] |
| 45 | }, |
| 46 | "repository": {"full_name": "owner/repo"} |
| 47 | }` |
| 48 | |
| 49 | events := parseEvent("check_run", json.RawMessage(payload)) |
| 50 | if len(events) != 1 { |
| 51 | t.Fatalf("expected 1 event, got %d", len(events)) |
| 52 | } |
| 53 | if events[0].EventType != "check_run" { |
| 54 | t.Errorf("expected EventType=check_run, got %q", events[0].EventType) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestParseCheckRunIgnoresNonCompleted(t *testing.T) { |
| 59 | payload := `{ |
| 60 | "action": "created", |
| 61 | "check_run": { |
| 62 | "conclusion": "success", |
| 63 | "pull_requests": [{"number": 1}] |
| 64 | }, |
| 65 | "repository": {"full_name": "owner/repo"} |
| 66 | }` |
| 67 | |
| 68 | events := parseEvent("check_run", json.RawMessage(payload)) |
| 69 | if len(events) != 0 { |
| 70 | t.Errorf("expected 0 events for non-completed action, got %d", len(events)) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestParseCheckSuite(t *testing.T) { |
| 75 | payload := `{ |
| 76 | "action": "completed", |
| 77 | "check_suite": { |
| 78 | "conclusion": "failure", |
| 79 | "pull_requests": [{"number": 99}] |
| 80 | }, |
| 81 | "repository": {"full_name": "org/project"} |
| 82 | }` |
| 83 | |
| 84 | events := parseEvent("check_suite", json.RawMessage(payload)) |
| 85 | if len(events) != 1 { |
| 86 | t.Fatalf("expected 1 event, got %d", len(events)) |
| 87 | } |
| 88 | if events[0].PRNumber != "99" || events[0].Repo != "org/project" { |
| 89 | t.Errorf("unexpected event: %+v", events[0]) |
| 90 | } |
| 91 | if events[0].EventType != "check_suite" { |
| 92 | t.Errorf("expected EventType=check_suite, got %q", events[0].EventType) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestParsePullRequest(t *testing.T) { |
| 97 | tests := []struct { |
| 98 | name string |
| 99 | payload string |
| 100 | wantLen int |
| 101 | }{ |
| 102 | { |
| 103 | name: "opened", |
| 104 | payload: `{ |
| 105 | "action": "opened", |
| 106 | "pull_request": {"number": 5, "state": "open", "merged": false, "mergeable": true}, |
| 107 | "repository": {"full_name": "owner/repo"} |
| 108 | }`, |
| 109 | wantLen: 1, |
| 110 | }, |
| 111 | { |
| 112 | name: "closed with merge", |
| 113 | payload: `{ |
| 114 | "action": "closed", |
| 115 | "pull_request": {"number": 5, "state": "closed", "merged": true}, |
| 116 | "repository": {"full_name": "owner/repo"} |
| 117 | }`, |
| 118 | wantLen: 1, |
| 119 | }, |
| 120 | { |
| 121 | name: "synchronize", |
| 122 | payload: `{ |
| 123 | "action": "synchronize", |
| 124 | "pull_request": {"number": 5, "state": "open", "merged": false, "mergeable": false, "mergeable_state": "dirty"}, |
| 125 | "repository": {"full_name": "owner/repo"} |
| 126 | }`, |
| 127 | wantLen: 1, |
| 128 | }, |
| 129 | { |
| 130 | name: "ignored action", |
| 131 | payload: `{ |
| 132 | "action": "labeled", |
| 133 | "pull_request": {"number": 5, "state": "open", "merged": false}, |
| 134 | "repository": {"full_name": "owner/repo"} |
| 135 | }`, |
| 136 | wantLen: 0, |
| 137 | }, |
| 138 | } |
| 139 | |
| 140 | for _, tt := range tests { |
| 141 | t.Run(tt.name, func(t *testing.T) { |
| 142 | events := parseEvent("pull_request", json.RawMessage(tt.payload)) |
| 143 | if len(events) != tt.wantLen { |
| 144 | t.Fatalf("expected %d events, got %d", tt.wantLen, len(events)) |
| 145 | } |
| 146 | if tt.wantLen > 0 { |
| 147 | if events[0].EventType != "pull_request" { |
| 148 | t.Errorf("expected EventType=pull_request, got %q", events[0].EventType) |
| 149 | } |
| 150 | if events[0].PRNumber != "5" { |
| 151 | t.Errorf("expected PRNumber=5, got %q", events[0].PRNumber) |
| 152 | } |
| 153 | if events[0].Repo != "owner/repo" { |
| 154 | t.Errorf("expected Repo=owner/repo, got %q", events[0].Repo) |
| 155 | } |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestParsePullRequestReview(t *testing.T) { |
| 162 | // Any submitted review must produce an invalidation event regardless of |
| 163 | // state โ including "commented" (gemini-code-assist submits its review |
| 164 | // bundles as state=commented). The trusted-reviewer/timing filtering |
| 165 | // lives in the detection layer, not the webhook parser (see issue #271). |
| 166 | // Non-"submitted" actions (edited, dismissed) produce no event. |
| 167 | tests := []struct { |
| 168 | name string |
| 169 | payload string |
| 170 | wantLen int |
| 171 | }{ |
| 172 | { |
| 173 | name: "submitted approved", |
| 174 | payload: `{ |
| 175 | "action": "submitted", |
| 176 | "review": {"state": "approved"}, |
| 177 | "pull_request": {"number": 7}, |
| 178 | "repository": {"full_name": "owner/repo"} |
| 179 | }`, |
| 180 | wantLen: 1, |
| 181 | }, |
| 182 | { |
| 183 | name: "submitted changes_requested", |
| 184 | payload: `{ |
| 185 | "action": "submitted", |
| 186 | "review": {"state": "changes_requested"}, |
| 187 | "pull_request": {"number": 7}, |
| 188 | "repository": {"full_name": "owner/repo"} |
| 189 | }`, |
| 190 | wantLen: 1, |
| 191 | }, |
| 192 | { |
| 193 | name: "submitted commented (now emits)", |
| 194 | payload: `{ |
| 195 | "action": "submitted", |
| 196 | "review": {"state": "commented"}, |
| 197 | "pull_request": {"number": 7}, |
| 198 | "repository": {"full_name": "owner/repo"} |
| 199 | }`, |
| 200 | wantLen: 1, |
| 201 | }, |
| 202 | { |
| 203 | name: "submitted commented uppercase COMMENTED", |
| 204 | payload: `{ |
| 205 | "action": "submitted", |
| 206 | "review": {"state": "COMMENTED"}, |
| 207 | "pull_request": {"number": 7}, |
| 208 | "repository": {"full_name": "owner/repo"} |
| 209 | }`, |
| 210 | wantLen: 1, |
| 211 | }, |
| 212 | { |
| 213 | name: "edited (ignored)", |
| 214 | payload: `{ |
| 215 | "action": "edited", |
| 216 | "review": {"state": "commented"}, |
| 217 | "pull_request": {"number": 7}, |
| 218 | "repository": {"full_name": "owner/repo"} |
| 219 | }`, |
| 220 | wantLen: 0, |
| 221 | }, |
| 222 | { |
| 223 | name: "dismissed (ignored)", |
| 224 | payload: `{ |
| 225 | "action": "dismissed", |
| 226 | "review": {"state": "changes_requested"}, |
| 227 | "pull_request": {"number": 7}, |
| 228 | "repository": {"full_name": "owner/repo"} |
| 229 | }`, |
| 230 | wantLen: 0, |
| 231 | }, |
| 232 | } |
| 233 | |
| 234 | for _, tt := range tests { |
| 235 | t.Run(tt.name, func(t *testing.T) { |
| 236 | events := parseEvent("pull_request_review", json.RawMessage(tt.payload)) |
| 237 | if len(events) != tt.wantLen { |
| 238 | t.Fatalf("expected %d events, got %d", tt.wantLen, len(events)) |
| 239 | } |
| 240 | if tt.wantLen > 0 { |
| 241 | if events[0].EventType != "pull_request_review" { |
| 242 | t.Errorf("expected EventType=pull_request_review, got %q", events[0].EventType) |
| 243 | } |
| 244 | if events[0].PRNumber != "7" { |
| 245 | t.Errorf("expected PRNumber=7, got %q", events[0].PRNumber) |
| 246 | } |
| 247 | if events[0].Repo != "owner/repo" { |
| 248 | t.Errorf("expected Repo=owner/repo, got %q", events[0].Repo) |
| 249 | } |
| 250 | } |
| 251 | }) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func TestParseIssueComment(t *testing.T) { |
| 256 | // A PR conversation comment arrives as issue_comment with an |
| 257 | // issue.pull_request object. It must re-evaluate that PR so trusted |
| 258 | // reviewer conversation comments dispatch fixes; comments on plain |
| 259 | // issues and actions other than "created"/"edited" produce no event. |
| 260 | tests := []struct { |
| 261 | name string |
| 262 | payload string |
| 263 | wantLen int |
| 264 | }{ |
| 265 | { |
| 266 | name: "created on PR", |
| 267 | payload: `{ |
| 268 | "action": "created", |
| 269 | "issue": {"number": 22, "pull_request": {"url": "https://api.github.com/repos/owner/repo/pulls/22"}}, |
| 270 | "comment": {"body": "please rename this", "user": {"login": "maintainer"}}, |
| 271 | "repository": {"full_name": "owner/repo"} |
| 272 | }`, |
| 273 | wantLen: 1, |
| 274 | }, |
| 275 | { |
| 276 | name: "created on plain issue (ignored)", |
| 277 | payload: `{ |
| 278 | "action": "created", |
| 279 | "issue": {"number": 22}, |
| 280 | "comment": {"body": "me too", "user": {"login": "maintainer"}}, |
| 281 | "repository": {"full_name": "owner/repo"} |
| 282 | }`, |
| 283 | wantLen: 0, |
| 284 | }, |
| 285 | { |
| 286 | // A reviewer may edit an older comment to add feedback after the |
| 287 | // latest push; detection watermarks on updated_at, so the edit |
| 288 | // must trigger a re-evaluation. |
| 289 | name: "edited on PR", |
| 290 | payload: `{ |
| 291 | "action": "edited", |
| 292 | "issue": {"number": 22, "pull_request": {"url": "https://api.github.com/repos/owner/repo/pulls/22"}}, |
| 293 | "comment": {"body": "please rename this, and the test too", "user": {"login": "maintainer"}}, |
| 294 | "repository": {"full_name": "owner/repo"} |
| 295 | }`, |
| 296 | wantLen: 1, |
| 297 | }, |
| 298 | { |
| 299 | name: "edited on plain issue (ignored)", |
| 300 | payload: `{ |
| 301 | "action": "edited", |
| 302 | "issue": {"number": 22}, |
| 303 | "repository": {"full_name": "owner/repo"} |
| 304 | }`, |
| 305 | wantLen: 0, |
| 306 | }, |
| 307 | { |
| 308 | name: "deleted on PR (ignored)", |
| 309 | payload: `{ |
| 310 | "action": "deleted", |
| 311 | "issue": {"number": 22, "pull_request": {"url": "https://api.github.com/repos/owner/repo/pulls/22"}}, |
| 312 | "repository": {"full_name": "owner/repo"} |
| 313 | }`, |
| 314 | wantLen: 0, |
| 315 | }, |
| 316 | } |
| 317 | |
| 318 | for _, tt := range tests { |
| 319 | t.Run(tt.name, func(t *testing.T) { |
| 320 | events := parseEvent("issue_comment", json.RawMessage(tt.payload)) |
| 321 | if len(events) != tt.wantLen { |
| 322 | t.Fatalf("expected %d events, got %d", tt.wantLen, len(events)) |
| 323 | } |
| 324 | if tt.wantLen > 0 { |
| 325 | want := Event{PRNumber: "22", Repo: "owner/repo", EventType: "issue_comment"} |
| 326 | if events[0] != want { |
| 327 | t.Errorf("got %+v, want %+v", events[0], want) |
| 328 | } |
| 329 | } |
| 330 | }) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // TestServerIssueComment posts issue_comment deliveries through the real |
| 335 | // HTTP handler: a PR conversation comment yields an event, a plain-issue |
| 336 | // comment yields none. |
| 337 | func TestServerIssueComment(t *testing.T) { |
| 338 | ch := make(chan Event, 10) |
| 339 | srv := NewServer(0, "/webhook/github", ch) |
| 340 | mux := http.NewServeMux() |
| 341 | mux.HandleFunc("/webhook/github", srv.handleWebhook) |
| 342 | ts := httptest.NewServer(mux) |
| 343 | defer ts.Close() |
| 344 | |
| 345 | post := func(payload string) { |
| 346 | t.Helper() |
| 347 | req, _ := http.NewRequest(http.MethodPost, ts.URL+"/webhook/github", bytes.NewBufferString(payload)) |
| 348 | req.Header.Set("Content-Type", "application/json") |
| 349 | req.Header.Set("X-GitHub-Event", "issue_comment") |
| 350 | resp, err := http.DefaultClient.Do(req) |
| 351 | if err != nil { |
| 352 | t.Fatalf("request failed: %v", err) |
| 353 | } |
| 354 | resp.Body.Close() |
| 355 | if resp.StatusCode != http.StatusOK { |
| 356 | t.Fatalf("expected 200, got %d", resp.StatusCode) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | post(`{"action": "created", "issue": {"number": 9}, "repository": {"full_name": "test/repo"}}`) |
| 361 | select { |
| 362 | case ev := <-ch: |
| 363 | t.Fatalf("plain issue comment must not emit an event, got %+v", ev) |
| 364 | default: |
| 365 | } |
| 366 | |
| 367 | post(`{"action": "created", "issue": {"number": 24, "pull_request": {}}, "repository": {"full_name": "test/repo"}}`) |
| 368 | select { |
| 369 | case ev := <-ch: |
| 370 | if ev.PRNumber != "24" || ev.Repo != "test/repo" || ev.EventType != "issue_comment" { |
| 371 | t.Errorf("unexpected event: %+v", ev) |
| 372 | } |
| 373 | case <-time.After(time.Second): |
| 374 | t.Fatal("timed out waiting for issue_comment event") |
| 375 | } |
| 376 | |
| 377 | post(`{"action": "edited", "issue": {"number": 9}, "repository": {"full_name": "test/repo"}}`) |
| 378 | select { |
| 379 | case ev := <-ch: |
| 380 | t.Fatalf("edited plain issue comment must not emit an event, got %+v", ev) |
| 381 | default: |
| 382 | } |
| 383 | |
| 384 | post(`{"action": "edited", "issue": {"number": 25, "pull_request": {}}, "repository": {"full_name": "test/repo"}}`) |
| 385 | select { |
| 386 | case ev := <-ch: |
| 387 | if ev.PRNumber != "25" || ev.Repo != "test/repo" || ev.EventType != "issue_comment" { |
| 388 | t.Errorf("unexpected event: %+v", ev) |
| 389 | } |
| 390 | case <-time.After(time.Second): |
| 391 | t.Fatal("timed out waiting for edited issue_comment event") |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | func TestParsePush(t *testing.T) { |
| 396 | t.Run("default branch push", func(t *testing.T) { |
| 397 | payload := `{ |
| 398 | "ref": "refs/heads/main", |
| 399 | "repository": {"full_name": "owner/repo", "default_branch": "main"} |
| 400 | }` |
| 401 | events := parseEvent("push", json.RawMessage(payload)) |
| 402 | if len(events) != 1 { |
| 403 | t.Fatalf("expected 1 event, got %d", len(events)) |
| 404 | } |
| 405 | if events[0].Repo != "owner/repo" || events[0].PRNumber != "" { |
| 406 | t.Errorf("unexpected event: %+v", events[0]) |
| 407 | } |
| 408 | if events[0].EventType != "push" { |
| 409 | t.Errorf("expected EventType=push, got %q", events[0].EventType) |
| 410 | } |
| 411 | }) |
| 412 | |
| 413 | t.Run("non-default branch push ignored", func(t *testing.T) { |
| 414 | payload := `{ |
| 415 | "ref": "refs/heads/feature", |
| 416 | "repository": {"full_name": "owner/repo", "default_branch": "main"} |
| 417 | }` |
| 418 | events := parseEvent("push", json.RawMessage(payload)) |
| 419 | if len(events) != 0 { |
| 420 | t.Errorf("expected 0 events, got %d", len(events)) |
| 421 | } |
| 422 | }) |
| 423 | } |
| 424 | |
| 425 | func TestServerHandleWebhook(t *testing.T) { |
| 426 | ch := make(chan Event, 10) |
| 427 | srv := NewServer(0, "/webhook/github", ch) |
| 428 | |
| 429 | // Build the handler directly for testing |
| 430 | mux := http.NewServeMux() |
| 431 | mux.HandleFunc("/webhook/github", srv.handleWebhook) |
| 432 | ts := httptest.NewServer(mux) |
| 433 | defer ts.Close() |
| 434 | |
| 435 | payload := `{ |
| 436 | "action": "completed", |
| 437 | "check_run": { |
| 438 | "conclusion": "success", |
| 439 | "pull_requests": [{"number": 55}] |
| 440 | }, |
| 441 | "repository": {"full_name": "test/repo"} |
| 442 | }` |
| 443 | |
| 444 | req, _ := http.NewRequest(http.MethodPost, ts.URL+"/webhook/github", bytes.NewBufferString(payload)) |
| 445 | req.Header.Set("Content-Type", "application/json") |
| 446 | req.Header.Set("X-GitHub-Event", "check_run") |
| 447 | |
| 448 | resp, err := http.DefaultClient.Do(req) |
| 449 | if err != nil { |
| 450 | t.Fatalf("request failed: %v", err) |
| 451 | } |
| 452 | resp.Body.Close() |
| 453 | |
| 454 | if resp.StatusCode != http.StatusOK { |
| 455 | t.Errorf("expected 200, got %d", resp.StatusCode) |
| 456 | } |
| 457 | |
| 458 | select { |
| 459 | case ev := <-ch: |
| 460 | if ev.PRNumber != "55" || ev.Repo != "test/repo" || ev.EventType != "check_run" { |
| 461 | t.Errorf("unexpected event: %+v", ev) |
| 462 | } |
| 463 | case <-time.After(time.Second): |
| 464 | t.Fatal("timed out waiting for event") |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | func TestServerRejectsGet(t *testing.T) { |
| 469 | ch := make(chan Event, 10) |
| 470 | srv := NewServer(0, "/webhook/github", ch) |
| 471 | |
| 472 | mux := http.NewServeMux() |
| 473 | mux.HandleFunc("/webhook/github", srv.handleWebhook) |
| 474 | ts := httptest.NewServer(mux) |
| 475 | defer ts.Close() |
| 476 | |
| 477 | resp, err := http.Get(ts.URL + "/webhook/github") |
| 478 | if err != nil { |
| 479 | t.Fatalf("request failed: %v", err) |
| 480 | } |
| 481 | resp.Body.Close() |
| 482 | |
| 483 | if resp.StatusCode != http.StatusMethodNotAllowed { |
| 484 | t.Errorf("expected 405, got %d", resp.StatusCode) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | func TestServerRejectsMissingEventHeader(t *testing.T) { |
| 489 | ch := make(chan Event, 10) |
| 490 | srv := NewServer(0, "/webhook/github", ch) |
| 491 | |
| 492 | mux := http.NewServeMux() |
| 493 | mux.HandleFunc("/webhook/github", srv.handleWebhook) |
| 494 | ts := httptest.NewServer(mux) |
| 495 | defer ts.Close() |
| 496 | |
| 497 | req, _ := http.NewRequest(http.MethodPost, ts.URL+"/webhook/github", bytes.NewBufferString("{}")) |
| 498 | req.Header.Set("Content-Type", "application/json") |
| 499 | // No X-GitHub-Event header |
| 500 | |
| 501 | resp, err := http.DefaultClient.Do(req) |
| 502 | if err != nil { |
| 503 | t.Fatalf("request failed: %v", err) |
| 504 | } |
| 505 | resp.Body.Close() |
| 506 | |
| 507 | if resp.StatusCode != http.StatusBadRequest { |
| 508 | t.Errorf("expected 400, got %d", resp.StatusCode) |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | func TestServerListenThenServe(t *testing.T) { |
| 513 | ch := make(chan Event, 10) |
| 514 | srv := NewServer(0, "/webhook/github", ch) |
| 515 | |
| 516 | if err := srv.Listen(); err != nil { |
| 517 | t.Fatalf("Listen failed: %v", err) |
| 518 | } |
| 519 | |
| 520 | // Addr is available immediately after Listen, no race. |
| 521 | addr := srv.Addr() |
| 522 | if addr == "" { |
| 523 | t.Fatal("expected non-empty address after Listen") |
| 524 | } |
| 525 | |
| 526 | go func() { |
| 527 | _ = srv.Serve() |
| 528 | }() |
| 529 | defer srv.Shutdown(context.Background()) |
| 530 | |
| 531 | // Send a webhook to the live server. |
| 532 | payload := `{ |
| 533 | "action": "completed", |
| 534 | "check_run": { |
| 535 | "conclusion": "success", |
| 536 | "pull_requests": [{"number": 77}] |
| 537 | }, |
| 538 | "repository": {"full_name": "test/repo"} |
| 539 | }` |
| 540 | url := "http://" + addr + "/webhook/github" |
| 541 | req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(payload)) |
| 542 | req.Header.Set("Content-Type", "application/json") |
| 543 | req.Header.Set("X-GitHub-Event", "check_run") |
| 544 | |
| 545 | resp, err := http.DefaultClient.Do(req) |
| 546 | if err != nil { |
| 547 | t.Fatalf("request failed: %v", err) |
| 548 | } |
| 549 | resp.Body.Close() |
| 550 | |
| 551 | if resp.StatusCode != http.StatusOK { |
| 552 | t.Errorf("expected 200, got %d", resp.StatusCode) |
| 553 | } |
| 554 | |
| 555 | select { |
| 556 | case ev := <-ch: |
| 557 | if ev.PRNumber != "77" || ev.EventType != "check_run" { |
| 558 | t.Errorf("unexpected event: %+v", ev) |
| 559 | } |
| 560 | case <-time.After(time.Second): |
| 561 | t.Fatal("timed out waiting for event") |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | func TestServerGracefulShutdownReleasesPort(t *testing.T) { |
| 566 | ch := make(chan Event, 10) |
| 567 | srv := NewServer(0, "/webhook/github", ch) |
| 568 | |
| 569 | if err := srv.Listen(); err != nil { |
| 570 | t.Fatalf("Listen failed: %v", err) |
| 571 | } |
| 572 | addr := srv.Addr() |
| 573 | |
| 574 | go func() { |
| 575 | _ = srv.Serve() |
| 576 | }() |
| 577 | |
| 578 | // Verify the server is accepting connections. |
| 579 | url := "http://" + addr + "/webhook/github" |
| 580 | resp, err := http.Get(url) |
| 581 | if err != nil { |
| 582 | t.Fatalf("server not reachable: %v", err) |
| 583 | } |
| 584 | resp.Body.Close() |
| 585 | |
| 586 | // Shut down with a deadline. |
| 587 | shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 588 | defer cancel() |
| 589 | if err := srv.Shutdown(shutdownCtx); err != nil { |
| 590 | t.Fatalf("Shutdown failed: %v", err) |
| 591 | } |
| 592 | |
| 593 | // The port should be released โ we can bind a new server on the same address. |
| 594 | srv2 := NewServer(0, "/webhook/github", ch) |
| 595 | // Extract the port from the original address to reuse it. |
| 596 | _, port, _ := net.SplitHostPort(addr) |
| 597 | ln, err := net.Listen("tcp", "127.0.0.1:"+port) |
| 598 | if err != nil { |
| 599 | t.Fatalf("port not released after shutdown: %v", err) |
| 600 | } |
| 601 | ln.Close() |
| 602 | _ = srv2 // suppress unused warning |
| 603 | } |
| 604 | |
| 605 | func TestEventIsInvalidationSignal(t *testing.T) { |
| 606 | // Verify that webhook events are pure invalidation signals: they carry |
| 607 | // only PRNumber, Repo, and EventType โ no status data. This is the |
| 608 | // core design invariant that prevents webhook/polling divergence bugs. |
| 609 | tests := []struct { |
| 610 | name string |
| 611 | eventType string |
| 612 | payload string |
| 613 | }{ |
| 614 | { |
| 615 | name: "check_run success", |
| 616 | eventType: "check_run", |
| 617 | payload: `{ |
| 618 | "action": "completed", |
| 619 | "check_run": { |
| 620 | "conclusion": "success", |
| 621 | "pull_requests": [{"number": 42}] |
| 622 | }, |
| 623 | "repository": {"full_name": "owner/repo"} |
| 624 | }`, |
| 625 | }, |
| 626 | { |
| 627 | name: "check_run failure", |
| 628 | eventType: "check_run", |
| 629 | payload: `{ |
| 630 | "action": "completed", |
| 631 | "check_run": { |
| 632 | "conclusion": "failure", |
| 633 | "pull_requests": [{"number": 42}] |
| 634 | }, |
| 635 | "repository": {"full_name": "owner/repo"} |
| 636 | }`, |
| 637 | }, |
| 638 | { |
| 639 | name: "check_suite", |
| 640 | eventType: "check_suite", |
| 641 | payload: `{ |
| 642 | "action": "completed", |
| 643 | "check_suite": { |
| 644 | "conclusion": "success", |
| 645 | "pull_requests": [{"number": 10}] |
| 646 | }, |
| 647 | "repository": {"full_name": "owner/repo"} |
| 648 | }`, |
| 649 | }, |
| 650 | { |
| 651 | name: "pull_request opened", |
| 652 | eventType: "pull_request", |
| 653 | payload: `{ |
| 654 | "action": "opened", |
| 655 | "pull_request": {"number": 5, "state": "open", "merged": false, "mergeable": true}, |
| 656 | "repository": {"full_name": "owner/repo"} |
| 657 | }`, |
| 658 | }, |
| 659 | { |
| 660 | name: "pull_request merged", |
| 661 | eventType: "pull_request", |
| 662 | payload: `{ |
| 663 | "action": "closed", |
| 664 | "pull_request": {"number": 5, "state": "closed", "merged": true}, |
| 665 | "repository": {"full_name": "owner/repo"} |
| 666 | }`, |
| 667 | }, |
| 668 | { |
| 669 | name: "pull_request_review approved", |
| 670 | eventType: "pull_request_review", |
| 671 | payload: `{ |
| 672 | "action": "submitted", |
| 673 | "review": {"state": "approved"}, |
| 674 | "pull_request": {"number": 7}, |
| 675 | "repository": {"full_name": "owner/repo"} |
| 676 | }`, |
| 677 | }, |
| 678 | } |
| 679 | |
| 680 | for _, tt := range tests { |
| 681 | t.Run(tt.name, func(t *testing.T) { |
| 682 | events := parseEvent(tt.eventType, json.RawMessage(tt.payload)) |
| 683 | if len(events) == 0 { |
| 684 | t.Fatal("expected at least one event") |
| 685 | } |
| 686 | for _, ev := range events { |
| 687 | if ev.PRNumber == "" { |
| 688 | t.Error("expected non-empty PRNumber") |
| 689 | } |
| 690 | if ev.Repo == "" { |
| 691 | t.Error("expected non-empty Repo") |
| 692 | } |
| 693 | if ev.EventType == "" { |
| 694 | t.Error("expected non-empty EventType") |
| 695 | } |
| 696 | if ev.EventType != tt.eventType { |
| 697 | t.Errorf("expected EventType=%q, got %q", tt.eventType, ev.EventType) |
| 698 | } |
| 699 | } |
| 700 | }) |
| 701 | } |
| 702 | } |