diff --git a/lib/slax.ex b/lib/slax.ex index 6fcbd553..98ff1f80 100644 --- a/lib/slax.ex +++ b/lib/slax.ex @@ -1,4 +1,5 @@ defmodule Slax do + @moduledoc false use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html diff --git a/lib/slax/channels/channel.ex b/lib/slax/channels/channel.ex index adce7561..02dbeafe 100644 --- a/lib/slax/channels/channel.ex +++ b/lib/slax/channels/channel.ex @@ -1,4 +1,5 @@ defmodule Slax.Channel do + @moduledoc false use Slax.Schema @type t :: %__MODULE__{} diff --git a/lib/slax/channels/channels.ex b/lib/slax/channels/channels.ex index 506380c9..961492b1 100644 --- a/lib/slax/channels/channels.ex +++ b/lib/slax/channels/channels.ex @@ -1,13 +1,17 @@ defmodule Slax.Channels do + @moduledoc false use Slax.Context alias Slax.Channel def create_or_update_channel(channel_id, %{name: name} = attrs) do - case Repo.get_by(Channel, %{channel_id: channel_id, name: name}) do - nil -> %Channel{channel_id: channel_id, name: name} - channel -> channel - end + channel = + case Repo.get_by(Channel, %{channel_id: channel_id, name: name}) do + nil -> %Channel{channel_id: channel_id, name: name} + channel -> channel + end + + channel |> Channel.changeset(attrs) |> Repo.insert_or_update() end @@ -37,10 +41,13 @@ defmodule Slax.Channels do end def set_default_repo(%{"id" => channel_id, "name" => channel_name}, attrs) do - case get_by_channel_id(channel_id) do - nil -> %Channel{channel_id: channel_id, name: channel_name} - channel -> channel - end + channel = + case get_by_channel_id(channel_id) do + nil -> %Channel{channel_id: channel_id, name: channel_name} + channel -> channel + end + + channel |> Channel.changeset(attrs) |> Repo.insert_or_update() end diff --git a/lib/slax/commands/context.ex b/lib/slax/commands/context.ex index bd373ec3..8ff244eb 100644 --- a/lib/slax/commands/context.ex +++ b/lib/slax/commands/context.ex @@ -1,4 +1,5 @@ defmodule Slax.Commands.Context do + @moduledoc false defstruct user: nil @type t :: %__MODULE__{} end diff --git a/lib/slax/commands/github_commands.ex b/lib/slax/commands/github_commands.ex index e4c22bf8..1a56a99c 100644 --- a/lib/slax/commands/github_commands.ex +++ b/lib/slax/commands/github_commands.ex @@ -24,7 +24,8 @@ defmodule Slax.Commands.GithubCommands do def parse_project_name(results, text) do case Regex.run(~r/^[a-zA-Z0-9\-_]{3,21}$/, text) do [project_name] -> - Map.put(results, :project_name, project_name) + results + |> Map.put(:project_name, project_name) |> Map.update(:success, %{}, fn x -> Map.put(x, :project_name, "Project Name Parsed") end) _ -> @@ -36,7 +37,7 @@ defmodule Slax.Commands.GithubCommands do @doc """ Pulls all issue templates from a story repo that are included in `story_paths`. - It then uses these templates to create issues in a newly created github repository, + It then uses these templates to create issues in a newly created github repository, If there is no github repository the function will exit early. The sleep avoids github rate limit abuse err. """ def create_reusable_stories( @@ -60,26 +61,9 @@ defmodule Slax.Commands.GithubCommands do errors = tree_errors ++ parse_errors ++ github_errors - results = - if length(errors) > 0 do - errors = - Enum.map(errors, fn {:error, path, message} -> "#{path}: #{message}" end) - |> Enum.join("\n") - - Map.update(results, :errors, %{}, fn x -> Map.put(x, :reusable_stories, errors) end) - else - results - end - - results = - if length(issue_ids) > 0 do - Map.put(results, :reusable_stories, true) - |> Map.update(:success, %{}, fn x -> - Map.put(x, :reusable_stories, "Reuseable Stories Created") - end) - else - results - end + results = update_results_with_errors(errors, results) + + results = update_results_with_issues(issue_ids, results) results @@ -90,10 +74,36 @@ defmodule Slax.Commands.GithubCommands do def create_reusable_stories(results, _, _, _, _), do: results + defp update_results_with_errors(errors, results) do + if length(errors) > 0 do + errors = + Enum.map_join(errors, "\n", fn {:error, path, message} -> "#{path}: #{message}" end) + + Map.update(results, :errors, %{}, fn x -> Map.put(x, :reusable_stories, errors) end) + else + results + end + end + + defp update_results_with_issues(issue_ids, results) do + if length(issue_ids) > 0 do + results + |> Map.put(:reusable_stories, true) + |> Map.update(:success, %{}, fn x -> + Map.put(x, :reusable_stories, "Reuseable Stories Created") + end) + else + results + end + end + defp process_tree(data, story_repo, story_paths, github_access_token) do - Map.get(data, "tree", []) - |> Enum.filter(fn x -> x["type"] == "blob" && String.ends_with?(x["path"], ".md") end) - |> Enum.filter(fn x -> String.starts_with?(x["path"], Keyword.values(story_paths)) end) + data + |> Map.get("tree", []) + |> Enum.filter(fn x -> + x["type"] == "blob" && String.ends_with?(x["path"], ".md") && + String.starts_with?(x["path"], Keyword.values(story_paths)) + end) |> Enum.map(fn x -> case Github.fetch_blob(%{ access_token: github_access_token, @@ -164,8 +174,7 @@ defmodule Slax.Commands.GithubCommands do @spec format_results(map) :: binary def format_results(results) do @steps - |> Enum.map(&format_result(results, &1)) - |> Enum.join("\n") + |> Enum.map_join("\n", &format_result(results, &1)) end defp format_result(results, key) do diff --git a/lib/slax/commands/github_commands/behaviour.ex b/lib/slax/commands/github_commands/behaviour.ex index 014c8c66..30a46ec8 100644 --- a/lib/slax/commands/github_commands/behaviour.ex +++ b/lib/slax/commands/github_commands/behaviour.ex @@ -1,4 +1,5 @@ defmodule Slax.Commands.GithubCommands.Behaviour do + @moduledoc false @typep results :: map() @typep github_access_token :: String.t() @typep org_name :: String.t() diff --git a/lib/slax/commands/latency.ex b/lib/slax/commands/latency.ex index 6617e52c..07d9c282 100644 --- a/lib/slax/commands/latency.ex +++ b/lib/slax/commands/latency.ex @@ -29,15 +29,17 @@ defmodule Slax.Commands.Latency do status_labels = ["in progress", "in review", "qa", "uat", "up next"] issue_events - |> Enum.filter(&Enum.member?(["labeled", "unlabeled"], &1["action"])) - |> Enum.filter(fn event -> - label_name = - event["label"]["name"] - |> String.downcase() - |> String.trim() - - Enum.member?(status_labels, label_name) - end) + |> Enum.filter( + &(Enum.member?(["labeled", "unlabeled"], &1["action"]) && + fn event -> + label_name = + event["label"]["name"] + |> String.downcase() + |> String.trim() + + Enum.member?(status_labels, label_name) + end) + ) end defp add_events_to_issues(issues, issues_events) do @@ -62,8 +64,7 @@ defmodule Slax.Commands.Latency do formatted_list = results |> Enum.filter(&issue_is_latent/1) - |> Enum.map(&format_issue(&1)) - |> Enum.join("") + |> Enum.map_join("", &format_issue(&1)) date = DateTime.utc_now() @@ -129,23 +130,27 @@ defmodule Slax.Commands.Latency do |> Map.get("created_at") end - update_status_from_new_event = fn event, status -> - case event["action"] do - "labeled" -> - [event["label"]["name"], event["created_at"]] - - "unlabeled" -> - [old_status, _] = status - - if old_status == event["label"]["name"] do - [nil, event["created_at"]] - else - status - end - end + status = fn event, status -> + maybe_update_status_from_new_event(event, status) end events - |> Enum.reduce([nil, first_timestamp], update_status_from_new_event) + |> Enum.reduce([nil, first_timestamp], status) + end + + defp maybe_update_status_from_new_event(event, status) do + case event["action"] do + "labeled" -> + [event["label"]["name"], event["created_at"]] + + "unlabeled" -> + [old_status, _] = status + + if old_status == event["label"]["name"] do + [nil, event["created_at"]] + else + status + end + end end end diff --git a/lib/slax/commands/project.ex b/lib/slax/commands/project.ex index 41a25156..ac5df157 100644 --- a/lib/slax/commands/project.ex +++ b/lib/slax/commands/project.ex @@ -65,7 +65,8 @@ defmodule Slax.Commands.NewProject do org_name: org_name }) do {:ok, repo_url} -> - Map.put(results, :github_repo, repo_url) + results + |> Map.put(:github_repo, repo_url) |> Map.update(:success, %{}, fn x -> Map.put(x, :github_repo, "Github Repo Found or Created: <#{repo_url}>") end) @@ -86,7 +87,8 @@ defmodule Slax.Commands.NewProject do channel_id = channel["id"] formatted_channel_name = "<##{channel_id}|#{channel_name}>" - Map.put(results, :slack_channel, channel_name) + results + |> Map.put(:slack_channel, channel_name) |> Map.update(:success, %{}, fn x -> Map.put(x, :slack_channel, "Channel Created: #{formatted_channel_name}") end) @@ -113,8 +115,7 @@ defmodule Slax.Commands.NewProject do results = if length(errors) > 0 do errors = - Enum.map(errors, fn {:error, team, message} -> "#{team}: #{message}" end) - |> Enum.join("\n") + Enum.map_join(errors, "\n", fn {:error, team, message} -> "#{team}: #{message}" end) Map.update(results, :errors, %{}, fn x -> Map.put(x, :github_org_teams, errors) end) else @@ -123,7 +124,8 @@ defmodule Slax.Commands.NewProject do results = if length(team_ids) > 0 do - Map.put(results, :github_org_teams, true) + results + |> Map.put(:github_org_teams, true) |> Map.update(:success, %{}, fn x -> Map.put(x, :github_org_teams, "Github Teams Added") end) @@ -164,7 +166,8 @@ defmodule Slax.Commands.NewProject do ) do case Github.create_webhook(params) do {:ok, _} -> - Map.put(results, webhook_key, true) + results + |> Map.put(webhook_key, true) |> Map.update(:success, %{}, fn x -> Map.put(x, webhook_key, success_message) end) {:error, message} -> diff --git a/lib/slax/context.ex b/lib/slax/context.ex index 92d68307..d615fdae 100644 --- a/lib/slax/context.ex +++ b/lib/slax/context.ex @@ -1,4 +1,5 @@ defmodule Slax.Context do + @moduledoc false defmacro __using__(_) do quote do import Ecto.Query, warn: false diff --git a/lib/slax/event_sink.ex b/lib/slax/event_sink.ex index 188320cf..cf0bd247 100644 --- a/lib/slax/event_sink.ex +++ b/lib/slax/event_sink.ex @@ -18,8 +18,7 @@ defmodule Slax.EventSink do def fetch_issues_events(params, issues) when is_list(issues) do issue_ids = issues - |> Enum.map(& &1["number"]) - |> Enum.join(",") + |> Enum.map_join(",", & &1["number"]) signature = :crypto.hash( diff --git a/lib/slax/github.ex b/lib/slax/github.ex index ee304418..0c36de7b 100644 --- a/lib/slax/github.ex +++ b/lib/slax/github.ex @@ -158,17 +158,7 @@ defmodule Slax.Github do end) {:error, %{status_code: 301, body: _body, headers: headers}} -> - location = - headers - |> Enum.find_value(fn {header, value} -> - case header do - "Location" -> - value - - _ -> - false - end - end) + location = find_header_value(headers) fetch_issues_from_url(location, params) @@ -563,6 +553,19 @@ defmodule Slax.Github do end end + defp find_header_value(headers) do + headers + |> Enum.find_value(fn {header, value} -> + case header do + "Location" -> + value + + _ -> + false + end + end) + end + defp retrieve_token(repo) do case ProjectRepos.get_by_repo(repo) do %{token: token} when not is_nil(token) -> diff --git a/lib/slax/helpers/text.ex b/lib/slax/helpers/text.ex index c3363cb2..f58a664a 100644 --- a/lib/slax/helpers/text.ex +++ b/lib/slax/helpers/text.ex @@ -1,5 +1,5 @@ defmodule Slax.Helpers.Text do - @doc """ + @moduledoc """ Takes in a list and joins the elements by comma and the word and ## Examples diff --git a/lib/slax/http.ex b/lib/slax/http.ex index 8b956818..6aca2376 100644 --- a/lib/slax/http.ex +++ b/lib/slax/http.ex @@ -1,4 +1,5 @@ defmodule Slax.Http do + @moduledoc false def post(url, body, headers \\ [], options \\ []) do url |> http_adapter().post(body, headers, options) @@ -66,9 +67,8 @@ defmodule Slax.Http do do: {:error, reason} defp parse_body(body) do - with {:ok, body} <- Jason.decode(body) do - body - else + case Jason.decode(body) do + {:ok, decoded_body} -> decoded_body _ -> body end end diff --git a/lib/slax/http/behaviour.ex b/lib/slax/http/behaviour.ex index a35e13c1..cf3c51f3 100644 --- a/lib/slax/http/behaviour.ex +++ b/lib/slax/http/behaviour.ex @@ -1,4 +1,5 @@ defmodule Slax.Http.Behaviour do + @moduledoc false @typep url :: binary() @typep body :: {:form, [{atom(), any()}]} @typep headers :: [{atom, binary}] | [{binary, binary}] | %{binary => binary} diff --git a/lib/slax/poker.ex b/lib/slax/poker.ex index 89fefdb9..daae323e 100644 --- a/lib/slax/poker.ex +++ b/lib/slax/poker.ex @@ -1,4 +1,5 @@ defmodule Slax.Poker do + @moduledoc false use Slax.Context alias Slax.{Github, Poker.Round, ProjectRepos} @@ -39,11 +40,14 @@ defmodule Slax.Poker do Closes all open rounds for a channel """ def end_current_round_for_channel(channel_name) do - from( - round in Round, - where: round.closed == false, - where: round.channel == ^channel_name - ) + round = + from( + round in Round, + where: round.closed == false, + where: round.channel == ^channel_name + ) + + round |> Repo.update_all(set: [closed: true]) |> case do {number_updated, _} -> {:ok, number_updated} @@ -52,11 +56,14 @@ defmodule Slax.Poker do end def get_current_round_for_channel(channel_name) do - from( - round in Round, - where: round.closed == false, - where: round.channel == ^channel_name - ) + round = + from( + round in Round, + where: round.closed == false, + where: round.channel == ^channel_name + ) + + round |> preload([:estimates]) |> Repo.one() end @@ -91,7 +98,9 @@ defmodule Slax.Poker do _label -> false end) - if not is_nil(label) do + if is_nil(label) do + :ok + else case Tentacat.Issues.Labels.remove(client, org, repo, issue, URI.encode(label["name"])) do {200, _issue, _http_response} -> :ok @@ -99,8 +108,6 @@ defmodule Slax.Poker do {response_code, message, http_response} -> {response_code, message, http_response} end - else - :ok end end end diff --git a/lib/slax/poker/estimates.ex b/lib/slax/poker/estimates.ex index 01af222c..a2275a57 100644 --- a/lib/slax/poker/estimates.ex +++ b/lib/slax/poker/estimates.ex @@ -1,4 +1,5 @@ defmodule Slax.Poker.Estimates do + @moduledoc false use Slax.Context alias Slax.Poker.Estimate @@ -14,10 +15,13 @@ defmodule Slax.Poker.Estimates do end def create_or_update_estimate(round_id, %{user: user} = attrs) do - case Repo.get_by(Estimate, %{round_id: round_id, user: user}) do - nil -> %Estimate{round_id: round_id} - estimate -> estimate - end + estimate = + case Repo.get_by(Estimate, %{round_id: round_id, user: user}) do + nil -> %Estimate{round_id: round_id} + estimate -> estimate + end + + estimate |> Estimate.changeset(attrs) |> Repo.insert_or_update() end diff --git a/lib/slax/projects/project_repo.ex b/lib/slax/projects/project_repo.ex index d5237c6c..c1ef9719 100644 --- a/lib/slax/projects/project_repo.ex +++ b/lib/slax/projects/project_repo.ex @@ -16,7 +16,8 @@ defmodule Slax.ProjectRepo do end def changeset(params \\ %{}) do - cast(%__MODULE__{}, params, [:org_name, :repo_name, :project_id]) + %__MODULE__{} + |> cast(params, [:org_name, :repo_name, :project_id]) |> cast_assoc(:default_channels) end diff --git a/lib/slax/projects/project_repos.ex b/lib/slax/projects/project_repos.ex index e3b957b3..4e7e9fb2 100644 --- a/lib/slax/projects/project_repos.ex +++ b/lib/slax/projects/project_repos.ex @@ -1,4 +1,5 @@ defmodule Slax.ProjectRepos do + @moduledoc false use Slax.Context alias Slax.{ProjectRepo, ProjectChannel, Projects} diff --git a/lib/slax/projects/projects.ex b/lib/slax/projects/projects.ex index 66f94088..c14c6064 100644 --- a/lib/slax/projects/projects.ex +++ b/lib/slax/projects/projects.ex @@ -1,4 +1,5 @@ defmodule Slax.Projects do + @moduledoc false use Slax.Context alias Slax.{Project, ProjectChannel} diff --git a/lib/slax/schema.ex b/lib/slax/schema.ex index 9920babb..378a8c62 100644 --- a/lib/slax/schema.ex +++ b/lib/slax/schema.ex @@ -1,4 +1,5 @@ defmodule Slax.Schema do + @moduledoc false defmacro __using__(_) do quote do use Ecto.Schema diff --git a/lib/slax/sprints/sprints.ex b/lib/slax/sprints/sprints.ex index 56f8efe8..ba1fb9db 100644 --- a/lib/slax/sprints/sprints.ex +++ b/lib/slax/sprints/sprints.ex @@ -85,11 +85,10 @@ defmodule Slax.Sprints do Note: This downloads all milestones for a repo and can be slow """ def find_milestone_by_title(repo, title, user) do - Github.fetch_milestones(%{ - repo: repo, - access_token: user.github_access_token - }) - |> case do + case Github.fetch_milestones(%{ + repo: repo, + access_token: user.github_access_token + }) do {:ok, milestones} -> Enum.find(milestones, &(Map.get(&1, "title") == title)) diff --git a/lib/slax/tentacat/issues.ex b/lib/slax/tentacat/issues.ex index a3fb5b6d..4665514a 100644 --- a/lib/slax/tentacat/issues.ex +++ b/lib/slax/tentacat/issues.ex @@ -1,4 +1,5 @@ defmodule Slax.Tentacat.Issues do + @moduledoc false def find(client, org, repo, issue) do tentacat().find(client, org, repo, issue) end diff --git a/lib/slax/tentacat/issues_behaviour.ex b/lib/slax/tentacat/issues_behaviour.ex index 66745b47..20c812f8 100644 --- a/lib/slax/tentacat/issues_behaviour.ex +++ b/lib/slax/tentacat/issues_behaviour.ex @@ -1,3 +1,4 @@ defmodule Slax.Tentacat.IssuesBehaviour do + @moduledoc false @callback find(map(), String.t(), String.t(), String.t()) :: {integer(), map(), map()} end diff --git a/lib/slax/tentacat/prs.ex b/lib/slax/tentacat/prs.ex index c66b64f6..6d501179 100644 --- a/lib/slax/tentacat/prs.ex +++ b/lib/slax/tentacat/prs.ex @@ -1,4 +1,5 @@ defmodule Slax.Tentacat.Prs do + @moduledoc false def find(client, org, repo, issue) do tentacat().find(client, org, repo, issue) end diff --git a/lib/slax/tentacat/prs_behaviour.ex b/lib/slax/tentacat/prs_behaviour.ex index 448526e1..5b190c95 100644 --- a/lib/slax/tentacat/prs_behaviour.ex +++ b/lib/slax/tentacat/prs_behaviour.ex @@ -1,3 +1,4 @@ defmodule Slax.Tentacat.PrsBehaviour do + @moduledoc false @callback find(map(), String.t(), String.t(), String.t()) :: {integer(), map(), map()} end diff --git a/lib/slax/users/user.ex b/lib/slax/users/user.ex index 07998c54..10a5d864 100644 --- a/lib/slax/users/user.ex +++ b/lib/slax/users/user.ex @@ -1,4 +1,5 @@ defmodule Slax.User do + @moduledoc false use SlaxWeb, :model schema "users" do diff --git a/lib/slax/users/users.ex b/lib/slax/users/users.ex index 6db6415d..77854e7b 100644 --- a/lib/slax/users/users.ex +++ b/lib/slax/users/users.ex @@ -1,4 +1,5 @@ defmodule Slax.Users do + @moduledoc false import Ecto.{Query, Changeset}, warn: false alias Slax.{Repo, User} @@ -7,10 +8,13 @@ defmodule Slax.Users do end def create_or_update_user(slack_id, attrs) do - case Repo.get_by(User, slack_id: slack_id) do - nil -> %User{slack_id: slack_id} - user -> user - end + user = + case Repo.get_by(User, slack_id: slack_id) do + nil -> %User{slack_id: slack_id} + user -> user + end + + user |> User.changeset(attrs) |> Repo.insert_or_update() end diff --git a/lib/slax_web/controllers/project_controller.ex b/lib/slax_web/controllers/project_controller.ex index 313e0aef..c8252725 100644 --- a/lib/slax_web/controllers/project_controller.ex +++ b/lib/slax_web/controllers/project_controller.ex @@ -46,7 +46,9 @@ defmodule SlaxWeb.ProjectController do def handle_new_project_request(github_access_token, repo, response_url) do formatted_response = - NewProject.new_project(String.trim(repo), github_access_token) + repo + |> String.trim() + |> NewProject.new_project(github_access_token) |> GithubCommands.format_results() Slack.send_message(response_url, %{ @@ -57,7 +59,9 @@ defmodule SlaxWeb.ProjectController do def handle_reuseable_stories_request(github_access_token, repo, response_url) do formatted_response = - ReuseableStories.reuseable_stories(String.trim(repo), github_access_token) + repo + |> String.trim() + |> ReuseableStories.reuseable_stories(github_access_token) |> GithubCommands.format_results() Slack.send_message(response_url, %{ diff --git a/lib/slax_web/controllers/sprint_controller.ex b/lib/slax_web/controllers/sprint_controller.ex index 08a0ba8d..9111cb6b02 100644 --- a/lib/slax_web/controllers/sprint_controller.ex +++ b/lib/slax_web/controllers/sprint_controller.ex @@ -26,19 +26,15 @@ defmodule SlaxWeb.SprintController do {_year, week} = :calendar.iso_week_number() - Sprints.create_sprint_commitment(%{ - repo: repo, - issue_numbers: issue_numbers, - week: week, - user: conn.assigns.current_user - }) - |> case do - {:error, messages, _} -> - text(conn, Enum.join(messages, "\n")) - - {:ok, _, _} -> - text(conn, "Sprint commitment set for week #{week}.") - end + maybe_handle_sprint_commitment_errors( + %{ + repo: repo, + issue_numbers: issue_numbers, + week: week, + user: conn.assigns.current_user + }, + conn + ) end end end @@ -49,4 +45,14 @@ defmodule SlaxWeb.SprintController do /sprint commitment - Create a new sprint commitment """) end + + defp maybe_handle_sprint_commitment_errors(params, conn) do + case Sprints.create_sprint_commitment(params) do + {:error, messages, _} -> + text(conn, Enum.join(messages, "\n")) + + {:ok, _, _} -> + text(conn, "Sprint commitment set for week #{params[:week]}.") + end + end end diff --git a/lib/slax_web/plugs/verify_token.ex b/lib/slax_web/plugs/verify_token.ex index 159cb9a3..26fc696e 100644 --- a/lib/slax_web/plugs/verify_token.ex +++ b/lib/slax_web/plugs/verify_token.ex @@ -1,4 +1,5 @@ defmodule Slax.Plugs.VerifySlackToken do + @moduledoc false import Phoenix.Controller, only: [text: 2] import Plug.Conn, only: [halt: 1, get_req_header: 2] @@ -29,14 +30,14 @@ defmodule Slax.Plugs.VerifySlackToken do conn false -> - text(conn, "Invalid slack signing secret.") |> halt + conn |> text("Invalid slack signing secret.") |> halt end else _token when token == :auth -> conn _ -> - text(conn, "Invalid params.") |> halt + conn |> text("Invalid params.") |> halt end end diff --git a/lib/slax_web/plugs/verify_user.ex b/lib/slax_web/plugs/verify_user.ex index 49cff6c4..0480a094 100644 --- a/lib/slax_web/plugs/verify_user.ex +++ b/lib/slax_web/plugs/verify_user.ex @@ -1,4 +1,5 @@ defmodule Slax.Plugs.VerifyUser do + @moduledoc false import Phoenix.Controller, only: [text: 2] import Plug.Conn, only: [assign: 3, halt: 1] @@ -8,7 +9,7 @@ defmodule Slax.Plugs.VerifyUser do def call(%Plug.Conn{params: %{"user_id" => user_id}} = conn, _) do case Slax.Users.get_user(user_id) do - nil -> text(conn, "You need to authenticate! use `/auth github`") |> halt + nil -> conn |> text("You need to authenticate! use `/auth github`") |> halt user -> assign(conn, :current_user, user) end end diff --git a/lib/slax_web/websocket_listener.ex b/lib/slax_web/websocket_listener.ex index e853ad62..b29b9a97 100644 --- a/lib/slax_web/websocket_listener.ex +++ b/lib/slax_web/websocket_listener.ex @@ -1,4 +1,5 @@ defmodule SlaxWeb.WebsocketListener do + @moduledoc false use GenServer require Logger @@ -28,7 +29,7 @@ defmodule SlaxWeb.WebsocketListener do {:ok, pid} = :gun.open(:binary.bin_to_list("wss-primary.slack.com"), 443, %{ - connect_timeout: 60000, + connect_timeout: 60_000, retry: 10, retry_timeout: 300, transport: :tls, @@ -54,9 +55,10 @@ defmodule SlaxWeb.WebsocketListener do end def handle_info({_, pid, stream_ref, {:text, event}}, socket) do - with {:ok, decoded_event} <- Jason.decode(event) do - handle_message(pid, stream_ref, decoded_event) - else + case Jason.decode(event) do + {:ok, decoded_event} -> + handle_message(pid, stream_ref, decoded_event) + _ -> nil end @@ -83,11 +85,9 @@ defmodule SlaxWeb.WebsocketListener do if is_nil(channel) or !channel.disabled do Issue.handle_event(event) - with {:ok, response} <- Jason.encode(%{envelope_id: envelope_id}) do - :gun.ws_send(pid, stream_ref, {:text, response}) - else - _ -> - nil + case Jason.encode(%{envelope_id: envelope_id}) do + {:ok, response} -> :gun.ws_send(pid, stream_ref, {:text, response}) + _ -> nil end end end @@ -97,12 +97,9 @@ defmodule SlaxWeb.WebsocketListener do "envelope_id" => envelope_id, "payload" => payload }) do - with {:ok, response} <- - Jason.encode(%{envelope_id: envelope_id, payload: Poker.start(payload)}) do - :gun.ws_send(pid, stream_ref, {:text, response}) - else - _ -> - nil + case Jason.encode(%{envelope_id: envelope_id, payload: Poker.start(payload)}) do + {:ok, response} -> :gun.ws_send(pid, stream_ref, {:text, response}) + _ -> nil end end diff --git a/lib/slax_web/websockets/default_repo.ex b/lib/slax_web/websockets/default_repo.ex index e0a7ba05..d1628b78 100644 --- a/lib/slax_web/websockets/default_repo.ex +++ b/lib/slax_web/websockets/default_repo.ex @@ -32,7 +32,8 @@ defmodule SlaxWeb.DefaultRepo do "channels_select_action" => %{"selected_channel" => channel_id} } <- parse_state_values(values) do slack_channel = - Slack.get_channels(%{trigger_id: trigger_id}) + %{trigger_id: trigger_id} + |> Slack.get_channels() |> Enum.find(&(&1["id"] == channel_id)) Channels.set_default_repo(slack_channel, %{default_project_repo_id: selected_repo}) diff --git a/lib/slax_web/websockets/issue.ex b/lib/slax_web/websockets/issue.ex index 3ac8534e..3e54f9a9 100644 --- a/lib/slax_web/websockets/issue.ex +++ b/lib/slax_web/websockets/issue.ex @@ -1,4 +1,5 @@ defmodule SlaxWeb.Issue do + @moduledoc false require Logger alias Slax.Channels alias Slax.{Github, Slack} @@ -38,27 +39,30 @@ defmodule SlaxWeb.Issue do defp load_prs_and_issues_from_scan(repo_and_issues, channel) do repo_and_issues |> Enum.uniq() - |> Enum.map(fn [repo_and_issue, _, repo_name, issue_number] -> + |> Enum.map_join("\n", fn [repo_and_issue, _, repo_name, issue_number] -> case repo_name do "" -> default_repo = Channels.maybe_get_default_repo(channel) - case default_repo do - nil -> - "No default repo set for this channel" - - _ -> - org_name = default_repo.org_name - repo_name = default_repo.repo_name - issue_number = String.slice(issue_number, 1..-1) - load_pr_or_issue_from_github("#{org_name}/#{repo_name}##{issue_number}") - end + maybe_load_from_default_repo(default_repo, issue_number) _ -> load_pr_or_issue_from_github(repo_and_issue) end end) - |> Enum.join("\n") + end + + defp maybe_load_from_default_repo(default_repo, issue_number) do + case default_repo do + nil -> + "No default repo set for this channel" + + _ -> + org_name = default_repo.org_name + repo_name = default_repo.repo_name + issue_number = String.slice(issue_number, 1..-1) + load_pr_or_issue_from_github("#{org_name}/#{repo_name}##{issue_number}") + end end defp load_pr_or_issue_from_github(repo_and_issue) do diff --git a/lib/slax_web/websockets/poker.ex b/lib/slax_web/websockets/poker.ex index dbc7742e..856b06da 100644 --- a/lib/slax_web/websockets/poker.ex +++ b/lib/slax_web/websockets/poker.ex @@ -1,4 +1,5 @@ defmodule SlaxWeb.Poker do + @moduledoc false alias Slax.{Github, Poker, Slack} alias Slax.Poker.Estimates alias Slax.Helpers.Text diff --git a/lib/slax_web/websockets/token.ex b/lib/slax_web/websockets/token.ex index f063c058..c774ddf0 100644 --- a/lib/slax_web/websockets/token.ex +++ b/lib/slax_web/websockets/token.ex @@ -65,21 +65,21 @@ defmodule SlaxWeb.Token do } } }) do - with %{ - "project_name_input" => %{"value" => nil}, - "repo_name_input" => repo_name_input, - "project_select" => project_select - } <- - parse_state_values(values) do - # don't create project, create repo and attach it to project - {org_name, repo_name} = Github.parse_repo_org(repo_name_input["value"]) + case parse_state_values(values) do + %{ + "project_name_input" => %{"value" => nil}, + "repo_name_input" => repo_name_input, + "project_select" => project_select + } -> + # don't create project, create repo and attach it to project + {org_name, repo_name} = Github.parse_repo_org(repo_name_input["value"]) + + ProjectRepos.create(%{ + org_name: org_name, + repo_name: repo_name, + project_id: String.to_integer(project_select["selected_option"]["value"]) + }) - ProjectRepos.create(%{ - org_name: org_name, - repo_name: repo_name, - project_id: String.to_integer(project_select["selected_option"]["value"]) - }) - else %{ "project_name_input" => %{"value" => project_name}, "repo_name_input" => repo_name_input, diff --git a/mix.lock b/mix.lock index 13d768be..dd2f7c3a 100644 --- a/mix.lock +++ b/mix.lock @@ -8,7 +8,7 @@ "cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"}, "cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"}, - "credo": {:hex, :credo, "1.7.5", "643213503b1c766ec0496d828c90c424471ea54da77c8a168c725686377b9545", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f799e9b5cd1891577d8c773d245668aa74a2fcd15eb277f51a0131690ebfb3fd"}, + "credo": {:hex, :credo, "1.7.7", "771445037228f763f9b2afd612b6aa2fd8e28432a95dbbc60d8e03ce71ba4446", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8bc87496c9aaacdc3f90f01b7b0582467b69b4bd2441fe8aae3109d843cc2f2e"}, "crontab": {:hex, :crontab, "1.1.10", "dc9bb1f4299138d47bce38341f5dcbee0aa6c205e864fba7bc847f3b5cb48241", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "1347d889d1a0eda997990876b4894359e34bfbbd688acbb0ba28a2795ca40685"}, "db_connection": {:hex, :db_connection, "2.6.0", "77d835c472b5b67fc4f29556dee74bf511bbafecdcaf98c27d27fa5918152086", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2f992d15725e721ec7fbc1189d4ecdb8afef76648c746a8e1cad35e3b8a35f3"}, "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, diff --git a/test/support/factory.ex b/test/support/factory.ex index f539e87e..4c3df6fd 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -1,4 +1,5 @@ defmodule Slax.Factory do + @moduledoc false alias Slax.{User, ProjectRepo, Project, ProjectChannel, Repo, Channel} use ExMachina.Ecto, repo: Repo diff --git a/test/support/model_case.ex b/test/support/model_case.ex index 58451bd9..1f37ddab 100644 --- a/test/support/model_case.ex +++ b/test/support/model_case.ex @@ -59,7 +59,8 @@ defmodule Slax.ModelCase do true """ def errors_on(struct, data) do - struct.__struct__.changeset(struct, data) + struct + |> struct.__struct__.changeset(data) |> Ecto.Changeset.traverse_errors(&SlaxWeb.ErrorHelpers.translate_error/1) |> Enum.flat_map(fn {key, errors} -> for msg <- errors, do: {key, msg} end) end