|
| 1 | +defmodule CodeCorps.GitHub.Event.PullRequest do |
| 2 | + @moduledoc ~S""" |
| 3 | + In charge of handling a GitHub Webhook payload for the PullRequest event type |
| 4 | +
|
| 5 | + [https://developer.github.com/v3/activity/events/types/#pullrequestevent](https://developer.github.com/v3/activity/events/types/#pullrequestevent) |
| 6 | + """ |
| 7 | + |
| 8 | + @behaviour CodeCorps.GitHub.Event.Handler |
| 9 | + |
| 10 | + alias CodeCorps.{ |
| 11 | + GitHub.Event.Common.RepoFinder, |
| 12 | + GitHub.Event.PullRequest.PullRequestLinker, |
| 13 | + GitHub.Event.PullRequest.TaskSyncer, |
| 14 | + GitHub.Event.PullRequest.UserLinker, |
| 15 | + GitHub.Event.PullRequest.Validator, |
| 16 | + Repo, |
| 17 | + Task |
| 18 | + } |
| 19 | + alias Ecto.Multi |
| 20 | + |
| 21 | + @type outcome :: {:ok, list(Task.t)} | |
| 22 | + {:error, :not_fully_implemented} | |
| 23 | + {:error, :unexpected_action} | |
| 24 | + {:error, :unexpected_payload} | |
| 25 | + {:error, :repository_not_found} | |
| 26 | + {:error, :validation_error_on_inserting_user} | |
| 27 | + {:error, :multiple_github_users_matched_same_cc_user} | |
| 28 | + {:error, :validation_error_on_syncing_tasks} | |
| 29 | + {:error, :unexpected_transaction_outcome} |
| 30 | + |
| 31 | + @doc ~S""" |
| 32 | + Handles the "PullRequest" GitHub webhook |
| 33 | +
|
| 34 | + The process is as follows |
| 35 | + - validate the payload is structured as expected |
| 36 | + - validate the action is properly supported |
| 37 | + - match payload with affected `CodeCorps.GithubRepo` record using |
| 38 | + `CodeCorps.GitHub.Event.Common.RepoFinder` |
| 39 | + - match with a `CodeCorps.User` using |
| 40 | + `CodeCorps.GitHub.Event.PullRequest.UserLinker` |
| 41 | + - for each `CodeCorps.ProjectGithubRepo` belonging to matched repo |
| 42 | + - match and update, or create a `CodeCorps.Task` on the associated |
| 43 | + `CodeCorps.Project` |
| 44 | +
|
| 45 | + If the process runs all the way through, the function will return an `:ok` |
| 46 | + tuple with a list of affected (created or updated) tasks. |
| 47 | +
|
| 48 | + If it fails, it will instead return an `:error` tuple, where the second |
| 49 | + element is the atom indicating a reason. |
| 50 | + """ |
| 51 | + @spec handle(map) :: outcome |
| 52 | + def handle(payload) do |
| 53 | + Multi.new |
| 54 | + |> Multi.run(:payload, fn _ -> payload |> validate_payload() end) |
| 55 | + |> Multi.run(:action, fn _ -> payload |> validate_action() end) |
| 56 | + |> Multi.run(:repo, fn _ -> RepoFinder.find_repo(payload) end) |
| 57 | + |> Multi.run(:pull_request, fn %{repo: github_repo} -> link_pull_request(github_repo, payload) end) |
| 58 | + |> Multi.run(:user, fn %{pull_request: github_pull_request} -> UserLinker.find_or_create_user(github_pull_request, payload) end) |
| 59 | + |> Multi.run(:tasks, fn %{pull_request: github_pull_request, user: user} -> github_pull_request |> TaskSyncer.sync_all(user, payload) end) |
| 60 | + |> Repo.transaction |
| 61 | + |> marshall_result() |
| 62 | + end |
| 63 | + |
| 64 | + @spec link_pull_request(GithubRepo.t, map) :: {:ok, GithubIssue.t} | {:error, Ecto.Changeset.t} |
| 65 | + defp link_pull_request(github_repo, %{"pull_request" => attrs}) do |
| 66 | + PullRequestLinker.create_or_update_pull_request(github_repo, attrs) |
| 67 | + end |
| 68 | + |
| 69 | + @spec marshall_result(tuple) :: tuple |
| 70 | + defp marshall_result({:ok, %{tasks: tasks}}), do: {:ok, tasks} |
| 71 | + defp marshall_result({:error, :payload, :invalid, _steps}), do: {:error, :unexpected_payload} |
| 72 | + defp marshall_result({:error, :action, :not_fully_implemented, _steps}), do: {:error, :not_fully_implemented} |
| 73 | + defp marshall_result({:error, :action, :unexpected_action, _steps}), do: {:error, :unexpected_action} |
| 74 | + defp marshall_result({:error, :repo, :unmatched_project, _steps}), do: {:ok, []} |
| 75 | + defp marshall_result({:error, :repo, :unmatched_repository, _steps}), do: {:error, :repository_not_found} |
| 76 | + defp marshall_result({:error, :user, %Ecto.Changeset{}, _steps}), do: {:error, :validation_error_on_inserting_user} |
| 77 | + defp marshall_result({:error, :user, :multiple_users, _steps}), do: {:error, :multiple_github_users_matched_same_cc_user} |
| 78 | + defp marshall_result({:error, :tasks, {_tasks, _errors}, _steps}), do: {:error, :validation_error_on_syncing_tasks} |
| 79 | + defp marshall_result({:error, _errored_step, _error_response, _steps}), do: {:error, :unexpected_transaction_outcome} |
| 80 | + |
| 81 | + @implemented_actions ~w(opened closed edited reopened) |
| 82 | + @unimplemented_actions ~w(assigned unassigned review_requested review_request_removed labeled unlabeled) |
| 83 | + |
| 84 | + @spec validate_action(map) :: {:ok, :implemented} | {:error, :not_fully_implemented | :unexpected_action} |
| 85 | + defp validate_action(%{"action" => action}) when action in @implemented_actions, do: {:ok, :implemented} |
| 86 | + defp validate_action(%{"action" => action}) when action in @unimplemented_actions, do: {:error, :not_fully_implemented} |
| 87 | + defp validate_action(_payload), do: {:error, :unexpected_action} |
| 88 | + |
| 89 | + @spec validate_payload(map) :: {:ok, :valid} | {:error, :invalid} |
| 90 | + defp validate_payload(%{} = payload) do |
| 91 | + case payload |> Validator.valid? do |
| 92 | + true -> {:ok, :valid} |
| 93 | + false -> {:error, :invalid} |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments