Skip to content

Commit

Permalink
tidying and adding doc tests #140
Browse files Browse the repository at this point in the history
  • Loading branch information
naazy committed Sep 8, 2017
1 parent 40aa636 commit dbf3580
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions web/controllers/rules/pr/no_issue_reference.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,13 @@ defmodule Dwylbot.Rules.PR.NoIssueReference do
end

def check(payload, _get_data?, token) do
# "https://api.github.com/repos/naazy/dwylbot-test/issues/29"
# -> https://github.com/naazy/dwylbot-test/issues/41
issue_base_url =
payload["pull_request"]["issue_url"]
|> String.replace("//api.", "//")
|> String.split("/")
|> Enum.filter(fn(a)-> a != "repos" end)
|> Enum.drop(-1) #removes the issue number 29
|> Enum.join("/")

assignees = get_assignees_login(payload["pull_request"]["assignees"])

authorIsAssigned = Enum.member?(assignees, payload["pull_request"]["base"]["user"]["login"])

pr_contains_shorthand_issue_reference? = Regex.match?(~r/(#[0-9]+)\b/, payload["pull_request"]["body"])

compiled_issue_url_regex = case Regex.compile("(#{issue_base_url}\/[0-9]+)") do
{:ok, string} -> string
{:error, _reason} -> "error compiling regex"
end
authorIsAssigned = payload["pull_request"]["assignees"]
|> get_assignees_login
|> Enum.member?(payload["pull_request"]["base"]["user"]["login"])

pr_contains_url_issue_reference? = Regex.match?(compiled_issue_url_regex, payload["pull_request"]["body"])

if (!(pr_contains_shorthand_issue_reference? || pr_contains_url_issue_reference?) && !authorIsAssigned) do
if (!pr_contains_issue_reference?(payload["pull_request"]) && !authorIsAssigned) do
%{
error_type: @rule_name,
actions: [
Expand Down Expand Up @@ -72,6 +55,46 @@ defmodule Dwylbot.Rules.PR.NoIssueReference do
|> Enum.map(fn(a) -> a["login"] end)
end

@doc """
iex>get_issue_base_url("https://api.github.com/repos/naazy/dwylbot-test/issues/29")
"https://github.com/naazy/dwylbot-test/issues"
"""
defp get_issue_base_url(api_issue_url) do
api_issue_url
|> String.replace("//api.", "//")
|> String.split("/")
|> Enum.filter(fn(a)-> a != "repos" end)
|> Enum.drop(-1) #removes the issue number 29
|> Enum.join("/")
end

@doc """
iex>pr_contains_issue_reference(%{"body": "my comment #41")
true
iex>pr_contains_issue_reference(%{"body": "no issue ref")
false
iex>pr_contains_issue_reference(%{"body": "https://github.com/naazy/dwylbot-test/issues/29")
true
"""
defp pr_contains_issue_reference?(pr_data) do
# shorthand means with # e.g. #41
pr_contains_shorthand_issue_reference? = Regex.match?(~r/(#[0-9]+)\b/, pr_data["body"])
|| Regex.match?(~r/(#[0-9]+)\b/, pr_data["title"])

issue_base_url =
get_issue_base_url pr_data["issue_url"]

compiled_issue_url_regex = case Regex.compile("(#{issue_base_url}\/[0-9]+)") do
{:ok, string} -> string
{:error, _reason} -> "error compiling regex"
end

pr_contains_url_issue_reference? = Regex.match?(compiled_issue_url_regex, pr_data["body"])
|| Regex.match?(compiled_issue_url_regex, pr_data["title"])

pr_contains_shorthand_issue_reference? || pr_contains_url_issue_reference?
end

defp error_message(login) do
"""
@#{login}, All pull requests must reference an issue but we cannot seem to find :mag: an issue URL in your pull request comment.
Expand Down

0 comments on commit dbf3580

Please sign in to comment.