Skip to content

Commit abc648e

Browse files
committed
improve notif handling
1 parent 18d3cd5 commit abc648e

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

lib/algora/bounties/jobs/notify_claim.ex

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,33 @@ defmodule Algora.Bounties.Jobs.NotifyClaim do
1616
@impl Oban.Worker
1717
def perform(%Oban.Job{args: %{"claim_id" => claim_id, "installation_id" => installation_id}}) do
1818
with {:ok, token} <- Github.get_installation_token(installation_id),
19-
{:ok, claim} <- Repo.fetch(Claim, claim_id) do
20-
claim = Repo.preload(claim, ticket: [repository: [:user]], user: [])
19+
{:ok, claim} <- Repo.fetch(Claim, claim_id),
20+
claim = Repo.preload(claim, ticket: [repository: [:user]], user: []),
21+
{:ok, _} <- maybe_add_labels(token, claim),
22+
{:ok, _} <- add_comment(token, claim) do
23+
:ok
24+
end
25+
end
2126

22-
# TODO: implement
23-
claim_reward_url = "#{AlgoraWeb.Endpoint.url()}/claims/#{claim.id}"
27+
defp add_comment(token, claim) do
28+
Github.create_issue_comment(
29+
token,
30+
claim.ticket.repository.user.provider_login,
31+
claim.ticket.repository.name,
32+
claim.ticket.number,
33+
"💡 @#{claim.user.provider_login} submitted [#{Claim.type_label(claim.type)}](#{claim.url}) that claims the bounty. You can visit [Algora](#{Claim.reward_url(claim)}) to reward."
34+
)
35+
end
2436

25-
Github.create_issue_comment(
26-
token,
27-
claim.ticket.repository.user.provider_login,
28-
claim.ticket.repository.name,
29-
claim.ticket.number,
30-
"💡 @#{claim.user.provider_login} submitted [#{Claim.type_label(claim.type)}](#{claim.url}) that claims the bounty. You can visit [Algora](#{claim_reward_url}) to reward."
31-
)
32-
end
37+
defp maybe_add_labels(token, %Claim{type: :pull_request} = claim) do
38+
Github.add_labels(
39+
token,
40+
claim.provider_meta["base"]["repo"]["owner"]["login"],
41+
claim.provider_meta["base"]["repo"]["name"],
42+
claim.provider_meta["number"],
43+
["🙋 Bounty claim"])
44+
3345
end
46+
47+
defp maybe_add_labels(_token, _claim), do: {:ok, nil}
3448
end

lib/algora/bounties/schemas/claim.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ defmodule Algora.Bounties.Claim do
7979
def type_label(:article), do: "an article"
8080
def type_label(nil), do: "a URL"
8181

82+
def reward_url(claim), do: "#{AlgoraWeb.Endpoint.url()}/claims/#{claim.id}"
83+
8284
def rewarded(query \\ Claim) do
8385
from c in query,
8486
where: c.state == :approved and not is_nil(c.charged_at)

lib/algora/integrations/github/behaviour.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Algora.Github.Behaviour do
22
@moduledoc false
3+
34
@type token :: String.t()
45
@type response :: {:ok, map()} | {:error, any()}
56

@@ -15,9 +16,8 @@ defmodule Algora.Github.Behaviour do
1516
@callback list_installations(token(), integer()) :: response
1617
@callback find_installation(token(), integer(), integer()) :: response
1718
@callback get_installation_token(integer()) :: response
18-
@callback create_issue_comment(token(), String.t(), String.t(), integer(), String.t()) ::
19-
response
20-
19+
@callback create_issue_comment(token(), String.t(), String.t(), integer(), String.t()) :: response
2120
@callback list_repository_events(token(), String.t(), String.t(), keyword()) :: response
2221
@callback list_repository_comments(token(), String.t(), String.t(), keyword()) :: response
22+
@callback add_labels(token(), String.t(), String.t(), integer(), [String.t()]) :: response
2323
end

lib/algora/integrations/github/client.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,9 @@ defmodule Algora.Github.Client do
180180
def list_repository_comments(access_token, owner, repo, opts \\ []) do
181181
fetch(access_token, "/repos/#{owner}/#{repo}/issues/comments#{build_query(opts)}")
182182
end
183+
184+
@impl true
185+
def add_labels(access_token, owner, repo, number, labels) do
186+
fetch(access_token, "/repos/#{owner}/#{repo}/issues/#{number}/labels", "POST", %{labels: labels})
187+
end
183188
end

lib/algora/integrations/github/github.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,7 @@ defmodule Algora.Github do
9191
@impl true
9292
def list_repository_comments(token, owner, repo, opts \\ []),
9393
do: client().list_repository_comments(token, owner, repo, opts)
94+
95+
@impl true
96+
def add_labels(token, owner, repo, number, labels), do: client().add_labels(token, owner, repo, number, labels)
9497
end

0 commit comments

Comments
 (0)