Skip to content

Commit 18d3cd5

Browse files
committed
update claim notif
1 parent de8ccb6 commit 18d3cd5

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

lib/algora/bounties/bounties.ex

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ defmodule Algora.Bounties do
132132
provider: "github",
133133
provider_id: to_string(pull_request["id"]),
134134
provider_meta: Util.normalize_struct(pull_request),
135+
type: :pull_request,
135136
title: pull_request["title"],
136137
url: pull_request["html_url"],
137138
merged_at: Util.to_date(pull_request["merged_at"])
@@ -177,7 +178,7 @@ defmodule Algora.Bounties do
177178
with {:ok, token} <- token_res,
178179
{:ok, ticket} <- Workspace.ensure_ticket(token, repo_owner, repo_name, number),
179180
{:ok, claim} <- do_claim_bounty(%{user: user, ticket: ticket, pull_request: pull_request}),
180-
{:ok, _job} <- notify_claim(%{ticket_ref: ticket_ref}, installation_id: installation_id) do
181+
{:ok, _job} <- notify_claim(%{claim: claim}, installation_id: installation_id) do
181182
broadcast()
182183
{:ok, claim}
183184
else
@@ -187,15 +188,12 @@ defmodule Algora.Bounties do
187188
end
188189

189190
@spec notify_claim(
190-
%{ticket_ref: %{owner: String.t(), repo: String.t(), number: integer()}},
191+
%{claim: Claim.t()},
191192
opts :: [installation_id: integer()]
192193
) ::
193194
{:ok, Oban.Job.t()} | {:error, atom()}
194-
def notify_claim(%{ticket_ref: ticket_ref}, opts \\ []) do
195-
%{
196-
ticket_ref: %{owner: ticket_ref.owner, repo: ticket_ref.repo, number: ticket_ref.number},
197-
installation_id: opts[:installation_id]
198-
}
195+
def notify_claim(%{claim: claim}, opts \\ []) do
196+
%{claim_id: claim.id, installation_id: opts[:installation_id]}
199197
|> Jobs.NotifyClaim.new()
200198
|> Oban.insert()
201199
end

lib/algora/bounties/jobs/notify_claim.ex

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@ defmodule Algora.Bounties.Jobs.NotifyClaim do
22
@moduledoc false
33
use Oban.Worker, queue: :notify_claim
44

5+
alias Algora.Bounties.Claim
56
alias Algora.Github
7+
alias Algora.Repo
68

79
require Logger
810

911
@impl Oban.Worker
10-
def perform(%Oban.Job{args: %{"ticket_ref" => _ticket_ref, "installation_id" => nil}}) do
12+
def perform(%Oban.Job{args: %{"claim_id" => _claim_id, "installation_id" => nil}}) do
1113
:ok
1214
end
1315

1416
@impl Oban.Worker
15-
def perform(%Oban.Job{args: %{"ticket_ref" => ticket_ref, "installation_id" => installation_id}}) do
16-
with {:ok, token} <- Github.get_installation_token(installation_id) do
17-
# TODO: update message
18-
body = "Claimed!"
17+
def perform(%Oban.Job{args: %{"claim_id" => claim_id, "installation_id" => installation_id}}) do
18+
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: [])
21+
22+
# TODO: implement
23+
claim_reward_url = "#{AlgoraWeb.Endpoint.url()}/claims/#{claim.id}"
1924

2025
Github.create_issue_comment(
2126
token,
22-
ticket_ref["owner"],
23-
ticket_ref["repo"],
24-
ticket_ref["number"],
25-
body
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."
2631
)
2732
end
2833
end

lib/algora/bounties/schemas/claim.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ defmodule Algora.Bounties.Claim do
1010
field :provider_id, :string, null: false
1111
field :provider_meta, :map, null: false
1212

13+
field :type, Ecto.Enum, values: [:pull_request, :review, :video, :design, :article]
14+
1315
field :merged_at, :utc_datetime_usec
1416
field :approved_at, :utc_datetime_usec
1517
field :rejected_at, :utc_datetime_usec
@@ -41,6 +43,7 @@ defmodule Algora.Bounties.Claim do
4143
:rejected_at,
4244
:charged_at,
4345
:paid_at,
46+
:type,
4447
:title,
4548
:description,
4649
:url,
@@ -69,6 +72,13 @@ defmodule Algora.Bounties.Claim do
6972
end
7073
end
7174

75+
def type_label(:pull_request), do: "a pull request"
76+
def type_label(:review), do: "a review"
77+
def type_label(:video), do: "a video"
78+
def type_label(:design), do: "a design"
79+
def type_label(:article), do: "an article"
80+
def type_label(nil), do: "a URL"
81+
7282
def rewarded(query \\ Claim) do
7383
from c in query,
7484
where: c.state == :approved and not is_nil(c.charged_at)

priv/repo/migrations/20250112164132_recreate_claims.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule Algora.Repo.Migrations.RecreateClaims do
1818
add :charged_at, :utc_datetime_usec
1919
add :paid_at, :utc_datetime_usec
2020

21+
add :type, :string, null: false
2122
add :title, :string, null: false
2223
add :description, :string
2324
add :url, :string, null: false

0 commit comments

Comments
 (0)