-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create GenServer process to merge errors together #134
Open
SimonLab
wants to merge
3
commits into
master
Choose a base branch
from
combine
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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,60 @@ | ||
defmodule Dwylbot.JoinRulesTest do | ||
alias Dwylbot.Rules.Join | ||
use ExUnit.Case | ||
doctest Dwylbot.Rules.Join.EstimationAssignee, import: true | ||
|
||
test "Join errors" do | ||
errors = [ | ||
%{ | ||
actions: [ | ||
%{comment: "conment_1", | ||
url: "https://api.github.com/repos/SimonLab/github_app/issues/26/comments" | ||
} | ||
], | ||
error_type: "issue_no_estimation", | ||
id: "SimonLab/github_app/26", | ||
token: 42, | ||
}, | ||
%{ | ||
actions: [ | ||
%{comment: "conment_2", | ||
url: "https://api.github.com/repos/SimonLab/github_app/issues/26/comments" | ||
}], | ||
error_type: "issue_inprogress_noassignees", | ||
id: "SimonLab/github_app/26", | ||
token: 42, | ||
}, | ||
%{ | ||
actions: [ | ||
%{comment: "conment_3", | ||
url: "https://api.github.com/repos/SimonLab/github_app/issues/25/comments" | ||
}], | ||
error_type: "another_error", | ||
id: nil, | ||
token: 42, | ||
}, | ||
] | ||
|
||
expected = [ | ||
%{ | ||
actions: [ | ||
%{comment: "conment_2\n\nconment_1\n", | ||
url: "https://api.github.com/repos/SimonLab/github_app/issues/26/comments" | ||
} | ||
], | ||
token: 42, | ||
}, | ||
%{ | ||
actions: [ | ||
%{comment: "conment_3", | ||
url: "https://api.github.com/repos/SimonLab/github_app/issues/25/comments" | ||
}], | ||
error_type: "another_error", | ||
id: nil, | ||
token: 42, | ||
}, | ||
] | ||
|
||
assert expected == Join.join(errors) | ||
end | ||
end |
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,20 @@ | ||
defmodule Dwylbot.MergeErrorsTest do | ||
use ExUnit.Case | ||
|
||
test "send error" do | ||
error = %{ | ||
id: nil, | ||
token: 42, | ||
error_type: "issue_inprogress_noassignees", | ||
actions: [ | ||
%{ | ||
comment: "test comment", | ||
url: "" | ||
}, | ||
] | ||
} | ||
assert :ok == Dwylbot.MergeErrors.send_error(error) | ||
Process.sleep(1000) | ||
end | ||
|
||
end |
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,46 @@ | ||
defmodule Dwylbot.MergeErrors do | ||
@moduledoc """ | ||
GenServer, receive, merge and reports errors every 5s | ||
""" | ||
use GenServer | ||
alias Dwylbot.Rules.Join | ||
@github_api Application.get_env(:dwylbot, :github_api) | ||
@time Application.get_env(:dwylbot, :time_merge_errors) | ||
|
||
def start_link(errors) do | ||
GenServer.start_link(__MODULE__, errors, name: __MODULE__) | ||
end | ||
|
||
def init(errors) do | ||
process_errors() | ||
{:ok, errors} | ||
end | ||
|
||
def process_errors do | ||
Process.send(__MODULE__, :errors, []) | ||
end | ||
|
||
def send_error(error) do | ||
GenServer.cast(__MODULE__, {:error, error}) | ||
end | ||
|
||
def handle_info(:errors, errors_state) do | ||
GenServer.cast(__MODULE__, {:report_errors, errors_state}) | ||
Process.send_after(__MODULE__, :errors, @time) | ||
{:noreply, []} | ||
end | ||
|
||
def handle_cast({:report_errors, errors}, errors_state) do | ||
joined_errors = Join.join(errors) | ||
joined_errors | ||
|> Enum.each(fn(err) -> | ||
@github_api.report_error(err.token, err) | ||
end) | ||
|
||
{:noreply, errors_state} | ||
end | ||
|
||
def handle_cast({:error, error}, errors_state) do | ||
{:noreply, [error | errors_state]} | ||
end | ||
end |
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
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
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
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
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
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
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,115 @@ | ||
defmodule Dwylbot.Rules.Join.EstimationAssignee do | ||
@moduledoc """ | ||
This module merge the no assignees and no estimation errors together: | ||
- create and add a new error with a new comment action | ||
- delete the two original errors from the list of errors | ||
""" | ||
|
||
def noassignee_noestimation(errors) do | ||
no_assignee = get_error_by_type(errors, "issue_inprogress_noassignees") | ||
no_estimation = get_error_by_type(errors, "issue_no_estimation") | ||
|
||
merge_errors? = no_assignee != nil | ||
&& no_estimation != nil | ||
&& no_assignee.id == no_estimation.id | ||
&& no_assignee.id != nil | ||
|
||
if merge_errors? do | ||
actions = filter_actions(no_estimation.actions) | ||
++ filter_actions(no_assignee.actions) | ||
|
||
comment_action = merge_comment_actions(no_assignee, no_estimation) | ||
|
||
new_error = %{ | ||
token: no_assignee.token, | ||
actions: [comment_action| actions] | ||
} | ||
|
||
replace_errors(errors, no_assignee, no_estimation, new_error) | ||
else | ||
errors | ||
end | ||
end | ||
|
||
@doc """ | ||
iex>get_error_by_type([%{error_type: "type_1"}], "no_match") | ||
nil | ||
iex>get_error_by_type([%{error_type: "type_1"}], "type_1") | ||
%{error_type: "type_1"} | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^^^^ I like this. Very helpful 👍 |
||
def get_error_by_type(errors, error_type) do | ||
errors | ||
|> Enum.find(fn(err) -> | ||
err.error_type == error_type | ||
end) | ||
end | ||
|
||
@doc """ | ||
iex>get_comment_action(%{actions: [%{assgin: "bob"}]}) | ||
%{comment: "", url: ""} | ||
iex>get_comment_action(%{actions: [ | ||
...>%{assign: "bob"}, | ||
...>%{comment: "hello", url: "/comment"}]}) | ||
%{comment: "hello", url: "/comment"} | ||
""" | ||
def get_comment_action(error) do | ||
error.actions | ||
|> Enum.find(%{comment: "", url: ""}, fn(action) -> | ||
Map.has_key?(action, :comment) | ||
end) | ||
end | ||
|
||
@doc """ | ||
iex>merge_comment_actions( | ||
...>%{actions: [%{comment: "comment 1", url: "/comment"}]}, | ||
...>%{actions: [%{comment: "comment 2", url: "/comment"}]} | ||
...>) | ||
%{comment: "comment 1\\n\\ncomment 2\\n", url: "/comment"} | ||
""" | ||
def merge_comment_actions(err_1, err_2) do | ||
action_comment_1 = get_comment_action(err_1) | ||
action_comment_2 = get_comment_action(err_2) | ||
|
||
comment = """ | ||
#{action_comment_1.comment} | ||
|
||
#{action_comment_2.comment} | ||
""" | ||
%{ | ||
comment: comment, | ||
url: action_comment_1.url | ||
} | ||
end | ||
|
||
@doc """ | ||
iex>replace_errors( | ||
...>[%{err: "err 1"}, %{err: "err 2"}, %{err: "err 3"}], | ||
...>%{err: "err 1"}, | ||
...>%{err: "err 2"}, | ||
...>%{err: "new error"} | ||
...>) | ||
[%{err: "new error"}, %{err: "err 3"}] | ||
""" | ||
def replace_errors(errors, err_1, err_2, new_error) do | ||
delete_errors = errors | ||
|> List.delete(err_1) | ||
|> List.delete(err_2) | ||
|
||
[new_error | delete_errors] | ||
end | ||
|
||
@doc """ | ||
iex>filter_actions([ | ||
...>%{comment: "comment acction", url: "/comment" }, | ||
...>%{assign: "bob", url: "/assign"} | ||
...>]) | ||
[%{assign: "bob", url: "/assign"}] | ||
""" | ||
def filter_actions(actions) do | ||
actions | ||
|> Enum.filter(fn(action) -> | ||
!Map.has_key?(action, :comment) | ||
end) | ||
end | ||
|
||
end |
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,13 @@ | ||
defmodule Dwylbot.Rules.Join do | ||
@moduledoc """ | ||
join rules together when necesssary: | ||
- in-progress label added: combine no_assignee with no_estimation | ||
- multiple reviewers added: combine add assignees rule | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll implement this (combine reviewers errors) on another PR, this one is full enough I think |
||
""" | ||
alias Dwylbot.Rules.Join.EstimationAssignee | ||
def join(rules) do | ||
rules | ||
|> EstimationAssignee.noassignee_noestimation() | ||
end | ||
|
||
end |
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of guard 👍