-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
test/lib/code_corps/github/event/pull_request/body_parser_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |