Skip to content

Commit 87507a3

Browse files
tallysmartinsPragTob
authored andcommitted
Fix branch name from Push event (#31)
* Fix branch name from Push event - closes #30 Signed-off-by: Tallys Martins <tallysmartins@gmail.com> * Add new test cases for WebhooksController Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
1 parent 9aa6761 commit 87507a3

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

lib/elixir_bench_web/controllers/github/webhooks_controller.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ defmodule ElixirBenchWeb.Github.WebHooks do
1717

1818
defp extract_job_attrs("push", payload) do
1919
slug = get_in(payload, ["repository", "full_name"])
20-
branch_name = get_in(payload, ["ref"])
2120
commit_sha = get_in(payload, ["after"])
2221

22+
branch_name =
23+
case get_in(payload, ["ref"]) do
24+
nil -> nil
25+
"" -> nil
26+
name -> String.replace(name, "refs/heads/", "")
27+
end
28+
2329
%{slug: slug, branch_name: branch_name, commit_sha: commit_sha}
2430
end
2531

2632
defp extract_job_attrs("pull_request", payload) do
2733
# data is fetched from sender's repository refered as "head"
2834
slug = get_in(payload, ["pull_request", "head", "repo", "full_name"])
29-
branch_name = get_in(payload, ["pull_request", "head", "ref"])
3035
commit_sha = get_in(payload, ["pull_request", "head", "sha"])
36+
branch_name = get_in(payload, ["pull_request", "head", "ref"])
3137

3238
%{slug: slug, branch_name: branch_name, commit_sha: commit_sha}
3339
end

test/elixir_bench_web/controllers/github/webhooks_controller_test.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,44 @@ defmodule ElixirBenchWeb.Github.WebHooksControllerTest do
4242
assert %{"branch_name" => "changes", "repo_slug" => "baxterthehacker/public-repo"} = data
4343
end
4444

45+
test "validate branch ref name in push events", context do
46+
insert(:repo, @github_repo_attrs)
47+
48+
payload = push_payload() |> Jason.decode!()
49+
50+
refs_head = %{payload | "ref" => "refs/heads/mybranch"}
51+
empty_string = %{payload | "ref" => ""}
52+
nil_value = %{payload | "ref" => nil}
53+
54+
assert_difference(Repo, 0) do
55+
assert_difference(Job, 1) do
56+
{:ok, %{"data" => data}} =
57+
context.conn
58+
|> set_headers("push")
59+
|> post("/hooks/handle", %{"payload" => Jason.encode!(refs_head)})
60+
|> decode_response_body
61+
end
62+
end
63+
64+
assert %{"branch_name" => "mybranch", "repo_slug" => "baxterthehacker/public-repo"} = data
65+
66+
{:ok, %{"errors" => errors}} =
67+
context.conn
68+
|> set_headers("push")
69+
|> post("/hooks/handle", %{"payload" => Jason.encode!(empty_string)})
70+
|> decode_response_body
71+
72+
assert %{"branch_name" => ["can't be blank"]} = errors
73+
74+
{:ok, %{"errors" => errors}} =
75+
context.conn
76+
|> set_headers("push")
77+
|> post("/hooks/handle", %{"payload" => Jason.encode!(nil_value)})
78+
|> decode_response_body
79+
80+
assert %{"branch_name" => ["can't be blank"]} = errors
81+
end
82+
4583
test "respond to ping event", context do
4684
response =
4785
context.conn

0 commit comments

Comments
 (0)