Skip to content

Commit 0c406c4

Browse files
committed
in midst of implementing edits
1 parent 7efbba1 commit 0c406c4

File tree

6 files changed

+87
-28
lines changed

6 files changed

+87
-28
lines changed

lib/algora/bounties/jobs/notify_bounty.ex

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,8 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
4242
),
4343
{:ok, ticket} <-
4444
Workspace.ensure_ticket(Github.pat(), ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"]) do
45-
%CommandResponse{}
46-
|> CommandResponse.changeset(%{
47-
provider: "github",
48-
provider_meta: Util.normalize_struct(response),
49-
provider_command_id: to_string(command_id),
50-
provider_response_id: to_string(response["id"]),
51-
command_source: command_source,
52-
command_type: :bounty,
53-
ticket_id: ticket.id
54-
})
55-
|> Repo.insert()
45+
# TODO: update existing command response if it exists
46+
create_command_response(response, command_source, command_id, ticket.id)
5647
end
5748
else
5849
Logger.info("""
@@ -91,26 +82,69 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
9182
Thank you for contributing to #{ticket_ref["owner"]}/#{ticket_ref["repo"]}!
9283
"""
9384

94-
with {:ok, response} <-
95-
Github.create_issue_comment(
85+
ensure_command_response(token, ticket_ref, command_id, command_source, ticket, body)
86+
end
87+
end
88+
89+
defp ensure_command_response(token, ticket_ref, command_id, command_source, ticket, body) do
90+
case Workspace.fetch_command_response("github", command_id, command_source) do
91+
{:ok, response} ->
92+
case Github.update_issue_comment(
9693
token,
9794
ticket_ref["owner"],
9895
ticket_ref["repo"],
99-
ticket_ref["number"],
96+
response.provider_response_id,
10097
body
10198
) do
102-
%CommandResponse{}
103-
|> CommandResponse.changeset(%{
104-
provider: "github",
105-
provider_meta: Util.normalize_struct(response),
106-
provider_command_id: to_string(command_id),
107-
provider_response_id: to_string(response["id"]),
108-
command_source: command_source,
109-
command_type: :bounty,
110-
ticket_id: ticket.id
111-
})
112-
|> Repo.insert()
113-
end
99+
{:ok, _comment} ->
100+
try_update_command_response(response, body)
101+
102+
{:error, "404 Not Found"} ->
103+
with {:ok, _} <- Workspace.delete_command_response(response.id) do
104+
post_response(token, ticket_ref, command_id, command_source, ticket, body)
105+
end
106+
107+
{:error, reason} ->
108+
Logger.error("Failed to update command response #{response.id} with body #{body}")
109+
{:error, reason}
110+
end
111+
112+
{:error, _reason} ->
113+
post_response(token, ticket_ref, command_id, command_source, ticket, body)
114+
end
115+
end
116+
117+
defp post_response(token, ticket_ref, command_id, command_source, ticket, body) do
118+
with {:ok, comment} <-
119+
Github.create_issue_comment(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"], body) do
120+
create_command_response(comment, command_source, command_id, ticket.id)
121+
end
122+
end
123+
124+
defp create_command_response(comment, command_source, command_id, ticket_id) do
125+
%CommandResponse{}
126+
|> CommandResponse.changeset(%{
127+
provider: "github",
128+
provider_meta: Util.normalize_struct(comment),
129+
provider_command_id: to_string(command_id),
130+
provider_response_id: to_string(comment["id"]),
131+
command_source: command_source,
132+
command_type: :bounty,
133+
ticket_id: ticket_id
134+
})
135+
|> Repo.insert()
136+
end
137+
138+
defp try_update_command_response(command_response, body) do
139+
case command_response
140+
|> CommandResponse.changeset(%{provider_meta: Util.normalize_struct(body)})
141+
|> Repo.update() do
142+
{:ok, command_response} ->
143+
{:ok, command_response}
144+
145+
{:error, _reason} ->
146+
Logger.error("Failed to update command response #{command_response.id}")
147+
{:ok, command_response}
114148
end
115149
end
116150
end

lib/algora/integrations/github/behaviour.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule Algora.Github.Behaviour do
1717
@callback find_installation(token(), integer(), integer()) :: response
1818
@callback get_installation_token(integer()) :: response
1919
@callback create_issue_comment(token(), String.t(), String.t(), integer(), String.t()) :: response
20+
@callback update_issue_comment(token(), String.t(), String.t(), integer(), String.t()) :: response
2021
@callback list_repository_events(token(), String.t(), String.t(), keyword()) :: response
2122
@callback list_repository_comments(token(), String.t(), String.t(), keyword()) :: response
2223
@callback add_labels(token(), String.t(), String.t(), integer(), [String.t()]) :: response

lib/algora/integrations/github/client.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ defmodule Algora.Github.Client do
176176
)
177177
end
178178

179+
@impl true
180+
def update_issue_comment(access_token, owner, repo, comment_id, body) do
181+
fetch(
182+
access_token,
183+
"/repos/#{owner}/#{repo}/issues/comments/#{comment_id}",
184+
"PATCH",
185+
%{body: body}
186+
)
187+
end
188+
179189
@impl true
180190
def list_repository_events(access_token, owner, repo, opts \\ []) do
181191
fetch(access_token, "/repos/#{owner}/#{repo}/events#{build_query(opts)}")

lib/algora/integrations/github/github.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ defmodule Algora.Github do
8484
def create_issue_comment(token, owner, repo, number, body),
8585
do: client().create_issue_comment(token, owner, repo, number, body)
8686

87+
@impl true
88+
def update_issue_comment(token, owner, repo, comment_id, body),
89+
do: client().update_issue_comment(token, owner, repo, comment_id, body)
90+
8791
@impl true
8892
def list_repository_events(token, owner, repo, opts \\ []),
8993
do: client().list_repository_events(token, owner, repo, opts)

lib/algora/workspace/workspace.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Algora.Workspace do
55
alias Algora.Accounts.User
66
alias Algora.Github
77
alias Algora.Repo
8+
alias Algora.Workspace.CommandResponse
89
alias Algora.Workspace.Installation
910
alias Algora.Workspace.Jobs
1011
alias Algora.Workspace.Repository
@@ -153,4 +154,15 @@ defmodule Algora.Workspace do
153154
def list_user_installations(user_id) do
154155
Repo.all(from(i in Installation, where: i.owner_id == ^user_id, preload: [:connected_user]))
155156
end
157+
158+
def fetch_command_response(provider, command_id, command_source) do
159+
Repo.fetch_one(
160+
from cr in CommandResponse,
161+
where: cr.provider == ^provider,
162+
where: cr.provider_command_id == ^to_string(command_id),
163+
where: cr.command_source == ^command_source
164+
)
165+
end
166+
167+
def delete_command_response(id), do: Repo.delete(Repo.get(CommandResponse, id))
156168
end

lib/algora_web/controllers/webhooks/github_controller.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ defmodule AlgoraWeb.Webhooks.GithubController do
6767
{:ticket, issue["id"]}
6868
end
6969

70-
dbg({command_source, command_id})
71-
7270
# TODO: community bounties?
7371
with {:ok, "admin"} <- get_permissions(author, params),
7472
{:ok, token} <- Github.get_installation_token(installation_id),

0 commit comments

Comments
 (0)