Skip to content

Commit

Permalink
change issue lookup to check for default channel
Browse files Browse the repository at this point in the history
  • Loading branch information
texastoast committed Apr 16, 2024
1 parent 460cb6f commit 553e9c3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 31 deletions.
16 changes: 15 additions & 1 deletion lib/slax/channels/channels.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@ defmodule Slax.Channels do

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}
nil -> %Channel{channel_id: channel_id, name: channel_name}
channel -> channel
end
|> Channel.changeset(attrs)
|> Repo.insert_or_update()
end

def maybe_get_default_repo(channel_id) do
channel = Repo.get_by(Channel, channel_id: channel_id)

case channel do
nil ->
nil

channel ->
channel
|> Ecto.assoc(:default_project_repo)
|> Repo.one()
end
end
end
16 changes: 8 additions & 8 deletions lib/slax_web/websockets/default_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ defmodule SlaxWeb.DefaultRepo do
alias Slax.Slack

def handle_payload(%{
"trigger_id" => trigger_id,
"type" => "shortcut",
"callback_id" => "set_default_repo"
}) do
"trigger_id" => trigger_id,
"type" => "shortcut",
"callback_id" => "set_default_repo"
}) do
view = build_default_repo_view()

Slack.open_modal(%{trigger_id: trigger_id, view: view})
Expand All @@ -28,12 +28,12 @@ defmodule SlaxWeb.DefaultRepo do
}
}) do
with %{
"repo_select" => %{"selected_option" => %{"value" => selected_repo}},
"repo_select" => %{"selected_option" => %{"value" => selected_repo}},
"channels_select_action" => %{"selected_channel" => channel_id}
} <- parse_state_values(values) do

slack_channel = Slack.get_channels(%{trigger_id: trigger_id})
|> Enum.find(&(&1["id"] == channel_id))
slack_channel =
Slack.get_channels(%{trigger_id: trigger_id})
|> Enum.find(&(&1["id"] == channel_id))

Channels.set_default_repo(slack_channel, %{default_project_repo_id: selected_repo})
end
Expand Down
85 changes: 65 additions & 20 deletions lib/slax_web/websockets/issue.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule SlaxWeb.Issue do
require Logger
alias Slax.Channels
alias Slax.{Github, Slack}

def handle_event(%{"subtype" => subtype})
Expand All @@ -14,7 +15,8 @@ defmodule SlaxWeb.Issue do
issues_scan = scan_text_for_issue(text)
prs_scan = scan_text_for_pr(text)

reply = load_issues_from_scan(issues_scan) <> "\n" <> load_prs_from_scan(prs_scan)
reply =
load_issues_from_scan(issues_scan, channel) <> "\n" <> load_prs_from_scan(prs_scan, channel)

unless reply == "\n" do
Slack.post_message_to_thread(%{text: reply, channel: channel, thread_ts: ts})
Expand All @@ -25,51 +27,94 @@ defmodule SlaxWeb.Issue do
issues_scan = scan_text_for_issue(text)
prs_scan = scan_text_for_pr(text)

reply = load_issues_from_scan(issues_scan) <> "\n" <> load_prs_from_scan(prs_scan)
reply =
load_issues_from_scan(issues_scan, channel) <> "\n" <> load_prs_from_scan(prs_scan, channel)

unless reply == "\n" do
Slack.post_message_to_channel(reply, channel)
end
end

def scan_text_for_issue(text) do
Regex.scan(~r{([\w-]+/)?([\w-]+)(#[0-9]+)}, text)
Regex.scan(~r{([\w-]+/)?([\w-]+)?(#[0-9]+)}, text)
end

def scan_text_for_pr(text) do
Regex.scan(~r{([\w-]+/)?([\w-]+)(\$[0-9]+)}, text)
Regex.scan(~r{([\w-]+/)?([\w-]+)?(\$[0-9]+)}, text)
end

defp load_issues_from_scan(repo_and_issues) do
defp load_issues_from_scan(repo_and_issues, channel) do
repo_and_issues
|> Enum.uniq()
|> Enum.map(fn [repo_and_issue | _] ->
case Github.load_issue(repo_and_issue) do
{:ok, issue, warning_message} ->
"<#{issue["html_url"]}|#{repo_and_issue}>: #{issue["title"]} #{labels_for_issue(issue)} #{warning_message}"

{:error, error} ->
error
|> Enum.map(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_issue_from_github("#{org_name}/#{repo_name}##{issue_number}")
end

_ ->
load_issue_from_github(repo_and_issue)
end
end)
|> Enum.join("\n")
end

defp load_prs_from_scan(repo_and_prs) do
defp load_issue_from_github(repo_and_issue) do
case Github.load_issue(repo_and_issue) do
{:ok, issue, warning_message} ->
"<#{issue["html_url"]}|#{repo_and_issue}>: #{issue["title"]} #{labels_for_issue(issue)} #{warning_message}"

{:error, error} ->
error
end
end

defp load_prs_from_scan(repo_and_prs, channel) do
repo_and_prs
|> Enum.uniq()
|> Enum.map(fn [repo_and_pr | _] ->
case Github.load_pr(repo_and_pr) do
{:ok, pr, warning_message} ->
"<#{pr["html_url"]}|#{repo_and_pr}>: [PR] #{pr["title"]} (#{pr["state"]}) #{warning_message}"

{:error, error} ->
error
|> Enum.map(fn [repo_and_pr, _, repo_name, pr_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
pr_number = String.slice(pr_number, 1..-1)
load_pr_from_github("#{org_name}/#{repo_name}##{pr_number}")
end

_ ->
load_pr_from_github(repo_and_pr)
end
end)
|> Enum.join("\n")
end

defp load_pr_from_github(repo_and_pr) do
case Github.load_pr(repo_and_pr) do
{:ok, pr, warning_message} ->
"<#{pr["html_url"]}|#{repo_and_pr}>: [PR] #{pr["title"]} (#{pr["state"]}) #{warning_message}"

{:error, error} ->
error
end
end

defp labels_for_issue(issue) do
column_label =
cond do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ defmodule Slax.Repo.Migrations.AddDefaultChannelToProjectRepos do
use Ecto.Migration

def change do

alter table(:channels) do
add(:default_project_repo_id, references(:project_repos))
end

end
end

0 comments on commit 553e9c3

Please sign in to comment.