Skip to content

Commit

Permalink
Added body parser
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Oct 17, 2017
1 parent 320e496 commit f8deb99
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/code_corps/github/event/pull_request/body_parser.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule CodeCorps.GitHub.Event.PullRequest.BodyParser do
@moduledoc ~S"""
In charge of extracting ids from markdown content, paired to a predefined list
of keywords.
"""

@doc ~S"""
Searchs for GitHub closing keyword format inside a content string. Returns all
unique ids matched, as integers.
"""
@spec extract_closing_ids(String.t) :: list(integer)
def extract_closing_ids(content) when is_binary(content) do
~w(close closes closed fix fixes fixed resolve resolves resolved)
|> matching_regex()
|> Regex.scan(content) # [["closes #1", "closes", "1"], ["fixes #2", "fixes", "2"]]
|> Enum.map(&List.last/1) # ["1", "2"]
|> Enum.map(&String.to_integer/1) # [1, 2]
|> Enum.uniq
end

defp matching_regex(keywords) do
matches = keywords |> Enum.join("|")
~r/(?:(#{matches}))\s+#(\d+)/i
end
end
32 changes: 32 additions & 0 deletions test/lib/code_corps/github/event/pull_request/body_parser_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule CodeCorps.GitHub.Event.PullRequest.BodyParserTest do
@moduledoc false

use ExUnit.Case, async: true


alias CodeCorps.{
GitHub.Event.PullRequest.BodyParser
}

describe "extract_closing_ids/1" do
test "correctly extracts ids using supported closing keywords" do
content =
"""
close #2, closes #3 closed #4: fixed #5 fixes #6 fix #7.
resolve #8 resolves #9 #resolved #10
"""

assert content |> BodyParser.extract_closing_ids == 2..10 |> Enum.to_list
end

test "only returns unique results" do
content =
"""
close #2, closes #2 closed #3: fixed #4 fixes #5 fix #6.
resolve #7 resolves #8 #resolved #8
"""

assert content |> BodyParser.extract_closing_ids == 2..8 |> Enum.to_list
end
end
end

0 comments on commit f8deb99

Please sign in to comment.