Skip to content

Commit 583ca4b

Browse files
fix(hooks-processor): Don't retry processing of hooks of unsupported type (#413)
## 📝 Description If the webhook received from the git provider is of an unsupported type (e.g. merge requests on GitLab), we should not retry processing since that is not a transient issue. ## ✅ Checklist - [x] I have tested this change - [x] ~This change requires documentation update~ -N/A Co-authored-by: Amir Hasanbasic <[email protected]>
1 parent 7383822 commit 583ca4b

File tree

7 files changed

+196
-8
lines changed

7 files changed

+196
-8
lines changed

hooks_processor/lib/hooks_processor/hooks/processing/bitbucket_worker.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,14 @@ defmodule HooksProcessor.Hooks.Processing.BitbucketWorker do
9393
e -> e
9494
end
9595

96-
defp process_webhook(hook_type, _webhook, _project, _requester_id) do
96+
defp process_webhook(hook_type, webhook, _project, requester_id) do
97+
params = %{provider: "bitbucket", requester_id: requester_id}
98+
HooksQueries.update_webhook(webhook, params, "failed", "BAD REQUEST")
99+
97100
# Increment unsupported hook type metric
98101
Watchman.increment({"hooks.processing.bitbucket", ["unsupported_hook"]})
99102

100-
"Unsuported type of the hook: '#{hook_type}'"
103+
{:error, "Unsuported type of the hook: '#{hook_type}'"}
101104
end
102105

103106
defp perform_actions(webhook, parsed_data, hook_type, action_type)

hooks_processor/lib/hooks_processor/hooks/processing/git_worker.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ defmodule HooksProcessor.Hooks.Processing.GitWorker do
136136
e -> e
137137
end
138138

139-
defp process_webhook(hook_type, webhook, _project, _requester_id) do
140-
webhook
141-
|> LT.warn("Unsupported type of the hook: '#{hook_type}'")
139+
defp process_webhook(hook_type, webhook, _project, requester_id) do
140+
params = %{provider: "git", requester_id: requester_id}
141+
HooksQueries.update_webhook(webhook, params, "failed", "BAD REQUEST")
142142

143-
HooksQueries.update_webhook(webhook, %{}, "failed")
143+
{:error, "Unsupported type of the hook: '#{hook_type}' for webhook: #{inspect(webhook)}"}
144144
end
145145

146146
defp perform_actions(webhook, parsed_data) do

hooks_processor/lib/hooks_processor/hooks/processing/gitlab_worker.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ defmodule HooksProcessor.Hooks.Processing.GitlabWorker do
9595
e -> e
9696
end
9797

98-
defp process_webhook(hook_type, _webhook, _project, _requester_id) do
99-
"Unsuported type of the hook: '#{hook_type}'"
98+
defp process_webhook(hook_type, webhook, _project, requester_id) do
99+
params = %{provider: "gitlab", requester_id: requester_id}
100+
HooksQueries.update_webhook(webhook, params, "failed", "BAD REQUEST")
101+
102+
{:error, "Unsuported type of the hook: '#{hook_type}'"}
100103
end
101104

102105
defp should_build?(repository, hook_data, hook_type) do

hooks_processor/test/hooks/processing/bitbucket_worker_test.exs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,60 @@ defmodule HooksProcessor.Hooks.Processing.BitbucketWorkerTest do
701701

702702
GrpcMock.verify!(ProjectHubServiceMock)
703703
end
704+
705+
test "unsupported hook type => hook is recorded as failed" do
706+
params = %{
707+
received_at: DateTime.utc_now(),
708+
webhook: BitbucketHooks.pull_request_open(),
709+
repository_id: UUID.uuid4(),
710+
project_id: UUID.uuid4(),
711+
organization_id: UUID.uuid4(),
712+
provider: "bitbucket"
713+
}
714+
715+
assert {:ok, webhook} = HooksQueries.insert(params)
716+
717+
# setup mocks
718+
719+
ProjectHubServiceMock
720+
|> GrpcMock.expect(:describe, fn req, _ ->
721+
assert req.id == webhook.project_id
722+
723+
%Projecthub.DescribeResponse{
724+
project: %{
725+
metadata: %{
726+
id: req.id,
727+
org_id: UUID.uuid4()
728+
},
729+
spec: %{
730+
repository: %{
731+
pipeline_file: ".semaphore/semaphore.yml",
732+
run_on: [:BRANCHES, :TAGS],
733+
whitelist: %{tags: ["/v1.*/", "/release-.*/"]}
734+
}
735+
}
736+
},
737+
metadata: %{status: %{code: :OK}}
738+
}
739+
end)
740+
741+
# wait for worker to finish and check results
742+
743+
assert {:ok, pid} = WorkersSupervisor.start_worker_for_webhook(webhook.id)
744+
745+
Test.Helpers.wait_for_worker_to_finish(pid, 15_000)
746+
747+
assert {:ok, webhook} = HooksQueries.get_by_id(webhook.id)
748+
assert webhook.provider == "bitbucket"
749+
assert webhook.state == "failed"
750+
assert webhook.result == "BAD REQUEST"
751+
assert webhook.wf_id == nil
752+
assert webhook.ppl_id == nil
753+
assert webhook.branch_id == nil
754+
assert webhook.commit_sha == nil
755+
assert webhook.commit_author == nil
756+
assert webhook.git_ref == nil
757+
758+
GrpcMock.verify!(ProjectHubServiceMock)
759+
end
704760
end

hooks_processor/test/hooks/processing/git_worker_test.exs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,61 @@ defmodule HooksProcessor.Hooks.Processing.GitWorkerTest do
503503

504504
GrpcMock.verify!(ProjectHubServiceMock)
505505
end
506+
507+
test "unsupported hook type => hook is recorded as failed" do
508+
params = %{
509+
received_at: DateTime.utc_now(),
510+
webhook: GitHooks.unsupported_hook_type(),
511+
repository_id: UUID.uuid4(),
512+
project_id: UUID.uuid4(),
513+
organization_id: UUID.uuid4(),
514+
provider: "git"
515+
}
516+
517+
assert {:ok, webhook} = HooksQueries.insert(params)
518+
519+
# setup mocks
520+
521+
ProjectHubServiceMock
522+
|> GrpcMock.expect(:describe, fn req, _ ->
523+
assert req.id == webhook.project_id
524+
525+
%Projecthub.DescribeResponse{
526+
project: %{
527+
metadata: %{
528+
id: req.id,
529+
org_id: UUID.uuid4()
530+
},
531+
spec: %{
532+
repository: %{
533+
owner: "semaphore",
534+
name: "elixir-project",
535+
pipeline_file: ".semaphore/semaphore.yml",
536+
run_on: [:BRANCHES, :TAGS],
537+
whitelist: %{tags: ["/release-.*/"]}
538+
}
539+
}
540+
},
541+
metadata: %{status: %{code: :OK}}
542+
}
543+
end)
544+
545+
# wait for worker to finish and check results
546+
547+
assert {:ok, pid} = WorkersSupervisor.start_worker_for_webhook(webhook.id)
548+
549+
Test.Helpers.wait_for_worker_to_finish(pid, 15_000)
550+
551+
assert {:ok, webhook} = HooksQueries.get_by_id(webhook.id)
552+
assert webhook.state == "failed"
553+
assert webhook.result == "BAD REQUEST"
554+
assert webhook.wf_id == nil
555+
assert webhook.ppl_id == nil
556+
assert webhook.branch_id == nil
557+
assert webhook.commit_sha == nil
558+
assert webhook.commit_author == nil
559+
assert webhook.git_ref == nil
560+
561+
GrpcMock.verify!(ProjectHubServiceMock)
562+
end
506563
end

hooks_processor/test/hooks/processing/gitlab_worker_test.exs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,59 @@ defmodule HooksProcessor.Hooks.Processing.GitlabWorkerTest do
701701

702702
GrpcMock.verify!(ProjectHubServiceMock)
703703
end
704+
705+
test "unsupported hook type => hook is recorded as failed" do
706+
params = %{
707+
received_at: DateTime.utc_now(),
708+
webhook: GitlabHooks.merge_request_open(),
709+
repository_id: UUID.uuid4(),
710+
project_id: UUID.uuid4(),
711+
organization_id: UUID.uuid4(),
712+
provider: "gitlab"
713+
}
714+
715+
assert {:ok, webhook} = HooksQueries.insert(params)
716+
717+
# setup mocks
718+
719+
ProjectHubServiceMock
720+
|> GrpcMock.expect(:describe, fn req, _ ->
721+
assert req.id == webhook.project_id
722+
723+
%Projecthub.DescribeResponse{
724+
project: %{
725+
metadata: %{
726+
id: req.id,
727+
org_id: UUID.uuid4()
728+
},
729+
spec: %{
730+
repository: %{
731+
pipeline_file: ".semaphore/semaphore.yml",
732+
run_on: [:BRANCHES, :TAGS],
733+
whitelist: %{tags: ["/release-.*/"]}
734+
}
735+
}
736+
},
737+
metadata: %{status: %{code: :OK}}
738+
}
739+
end)
740+
741+
# wait for worker to finish and check results
742+
743+
assert {:ok, pid} = WorkersSupervisor.start_worker_for_webhook(webhook.id)
744+
745+
Test.Helpers.wait_for_worker_to_finish(pid, 15_000)
746+
747+
assert {:ok, webhook} = HooksQueries.get_by_id(webhook.id)
748+
assert webhook.state == "failed"
749+
assert webhook.result == "BAD REQUEST"
750+
assert webhook.wf_id == nil
751+
assert webhook.ppl_id == nil
752+
assert webhook.branch_id == nil
753+
assert webhook.commit_sha == nil
754+
assert webhook.commit_author == nil
755+
assert webhook.git_ref == nil
756+
757+
GrpcMock.verify!(ProjectHubServiceMock)
758+
end
704759
end

hooks_processor/test/support/git_hooks.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ defmodule Support.GitHooks do
4949
}
5050
}
5151
end
52+
53+
def unsupported_hook_type do
54+
%{
55+
"reference" => "refs/puls/123",
56+
"commit" => %{
57+
"sha" => "023becf74ae8a5d93911db4bad7967f94343b44b",
58+
"message" => "Initial commit"
59+
},
60+
"author" => %{
61+
"name" => "Radek",
62+
"email" => "[email protected]"
63+
}
64+
}
65+
end
5266
end

0 commit comments

Comments
 (0)