Skip to content

Commit c2a2edd

Browse files
fix: harden check_suite staging-completed path to no-op if batch is missing
1 parent 12d313a commit c2a2edd

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/web/controllers/webhook_controller.ex

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,21 @@ defmodule BorsNG.WebhookController do
267267

268268
case {action, branch} do
269269
{"completed", ^staging_branch} ->
270-
Batch
271-
|> Repo.get_by!(commit: sha, project_id: project.id)
272-
|> Batch.changeset(%{last_polled: 0})
273-
|> Repo.update!()
274-
275-
batcher = Batcher.Registry.get(project.id)
276-
Batcher.poll(batcher)
270+
case Repo.get_by(Batch, commit: sha, project_id: project.id) do
271+
nil ->
272+
Logger.debug([
273+
"Ignoring check_suite completed for unknown staging commit: ",
274+
sha
275+
])
276+
277+
batch ->
278+
batch
279+
|> Batch.changeset(%{last_polled: 0})
280+
|> Repo.update!()
281+
282+
batcher = Batcher.Registry.get(project.id)
283+
Batcher.poll(batcher)
284+
end
277285

278286
{"completed", ^trying_branch} ->
279287
attemptor = Attemptor.Registry.get(project.id)

test/controllers/webhook_controller_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,24 @@ defmodule BorsNG.WebhookControllerTest do
556556
assert Enum.any?(comments, &String.contains?(&1, "now in draft mode"))
557557
end
558558

559+
test "ignore check_suite completed for unknown staging commit", %{conn: conn} do
560+
body_params = %{
561+
"repository" => %{"id" => 13},
562+
"action" => "completed",
563+
"check_suite" => %{
564+
"head_branch" => "staging",
565+
"head_sha" => "missing_batch_commit"
566+
}
567+
}
568+
569+
conn =
570+
conn
571+
|> put_req_header("x-github-event", "check_suite")
572+
|> post(webhook_path(conn, :webhook, "github"), body_params)
573+
574+
assert conn.status == 200
575+
end
576+
559577
def wait_until_other_branch_is_removed do
560578
branches =
561579
GitHub.ServerMock.get_state()

0 commit comments

Comments
 (0)