From 32a931ea54e64a414f213bcd513da748803d5fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Pr=C3=A9vost?= Date: Tue, 2 Apr 2019 10:40:32 -0400 Subject: [PATCH] =?UTF-8?q?Make=20pull=20request=20=E2=80=9Cbody=20stacks?= =?UTF-8?q?=E2=80=9D=20work=20even=20without=20default=20stacks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/dispatch/dispatch.ex | 4 +++- test/dispatch/dispatch_test.exs | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/dispatch/dispatch.ex b/lib/dispatch/dispatch.ex index 44fbd4d..9ab01bf 100644 --- a/lib/dispatch/dispatch.ex +++ b/lib/dispatch/dispatch.ex @@ -83,7 +83,9 @@ defmodule Dispatch do @doc """ Extracts stacks from a Webhook payload received from GitHub """ - def extract_from_params(%{"pull_request" => %{"body" => body}, "stacks" => default_stacks}) when is_binary(default_stacks) do + def extract_from_params(%{"pull_request" => %{"body" => body}} = params) do + default_stacks = Map.get(params, "stacks", "") + ~r/#dispatch\/(\w+)/i |> Regex.scan(body, capture: :all_but_first) |> (fn diff --git a/test/dispatch/dispatch_test.exs b/test/dispatch/dispatch_test.exs index f109d2a..3fa7545 100644 --- a/test/dispatch/dispatch_test.exs +++ b/test/dispatch/dispatch_test.exs @@ -261,4 +261,18 @@ defmodule DispatchTest do assert stacks == ["hcl", "ruby"] end + + test "extract_from_params/1 with stacks in pull request body but no default ones, it should return them" do + body = """ + This is my pull request! It adds a Terraform configuration file. Absolutely no GraphQL stuff to review. + + Also, there are no default stacks in the webhook URL. + + #dispatch/hcl #dispatch/Ruby + """ + + stacks = Dispatch.extract_from_params(%{"pull_request" => %{"body" => body}}) + + assert stacks == ["hcl", "ruby"] + end end