Skip to content

Commit 25a56de

Browse files
committed
feat: bounty notifications via Oban job (#27)
1 parent 9d1b0b2 commit 25a56de

File tree

8 files changed

+70
-82
lines changed

8 files changed

+70
-82
lines changed

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ config :algora, Oban,
3131
queues: [
3232
event_consumers: 1,
3333
comment_consumers: 1,
34-
github_og_image: 5
34+
github_og_image: 5,
35+
notify_bounty: 1
3536
]
3637

3738
# Configures the mailer

lib/algora/bounties/bounties.ex

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ defmodule Algora.Bounties do
77
alias Algora.Accounts.User
88
alias Algora.Bounties.Bounty
99
alias Algora.Bounties.Claim
10+
alias Algora.Bounties.Jobs
1011
alias Algora.Bounties.Tip
1112
alias Algora.FeeTier
12-
alias Algora.Github
1313
alias Algora.MoneyUtils
1414
alias Algora.Organizations.Member
1515
alias Algora.Payments
@@ -37,9 +37,9 @@ defmodule Algora.Bounties do
3737
Phoenix.PubSub.subscribe(Algora.PubSub, "bounties:all")
3838
end
3939

40-
@spec create_bounty(%{creator: User.t(), owner: User.t(), amount: Money.t(), ticket: Ticket.t()}) ::
40+
@spec do_create_bounty(%{creator: User.t(), owner: User.t(), amount: Money.t(), ticket: Ticket.t()}) ::
4141
{:ok, Bounty.t()} | {:error, atom()}
42-
def create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket}) do
42+
defp do_create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket}) do
4343
changeset =
4444
Bounty.changeset(%Bounty{}, %{
4545
amount: amount,
@@ -72,46 +72,30 @@ defmodule Algora.Bounties do
7272
creator: creator,
7373
owner: owner,
7474
amount: amount,
75-
ticket_ref: %{owner: repo_owner, repo: repo_name, number: number}
75+
ticket_ref: %{owner: repo_owner, repo: repo_name, number: number} = ticket_ref
7676
}) do
77-
with {:ok, token} <- Accounts.get_access_token(creator),
78-
{:ok, ticket} <- Workspace.ensure_ticket(token, repo_owner, repo_name, number) do
79-
create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket})
80-
else
81-
{:error, _reason} = error -> error
82-
end
83-
end
84-
85-
def notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}) do
86-
# TODO: post comment in a separate job
87-
body = """
88-
💎 **#{owner.provider_login}** is offering a **#{Money.to_string!(bounty.amount, no_fraction_if_integer: true)}** bounty for this issue
89-
90-
👉 Got a pull request resolving this? Claim the bounty by commenting `/claim ##{ticket_ref.number}` in your PR and joining swift.algora.io
91-
"""
92-
93-
Task.start(fn ->
94-
if Github.pat_enabled() do
95-
Github.create_issue_comment(
96-
Github.pat(),
97-
ticket_ref.owner,
98-
ticket_ref.repo,
99-
ticket_ref.number,
100-
body
101-
)
77+
Repo.transact(fn ->
78+
with {:ok, token} <- Accounts.get_access_token(creator),
79+
{:ok, ticket} <- Workspace.ensure_ticket(token, repo_owner, repo_name, number),
80+
{:ok, bounty} <- do_create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket}),
81+
{:ok, _job} <- notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}) do
82+
{:ok, bounty}
10283
else
103-
Logger.info("""
104-
Github.create_issue_comment(Github.pat(), "#{ticket_ref.owner}", "#{ticket_ref.repo}", #{ticket_ref.number},
105-
\"\"\"
106-
#{body}
107-
\"\"\")
108-
""")
109-
110-
:ok
84+
{:error, _reason} = error -> error
11185
end
11286
end)
11387
end
11488

89+
def notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}) do
90+
%{
91+
owner_login: owner.provider_login,
92+
amount: Money.to_string!(bounty.amount, no_fraction_if_integer: true),
93+
ticket_ref: %{owner: ticket_ref.owner, repo: ticket_ref.repo, number: ticket_ref.number}
94+
}
95+
|> Jobs.NotifyBounty.new()
96+
|> Oban.insert()
97+
end
98+
11599
@spec create_tip(%{creator: User.t(), owner: User.t(), recipient: User.t(), amount: Money.t()}) ::
116100
{:ok, String.t()} | {:error, atom()}
117101
def create_tip(%{creator: creator, owner: owner, recipient: recipient, amount: amount}) do
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Algora.Bounties.Jobs.NotifyBounty do
2+
@moduledoc false
3+
use Oban.Worker, queue: :notify_bounty
4+
5+
alias Algora.Github
6+
7+
require Logger
8+
9+
@impl Oban.Worker
10+
def perform(%Oban.Job{args: %{"owner_login" => owner_login, "amount" => amount, "ticket_ref" => ticket_ref}}) do
11+
body = """
12+
💎 **#{owner_login}** is offering a **#{amount}** bounty for this issue
13+
14+
👉 Got a pull request resolving this? Claim the bounty by commenting `/claim ##{ticket_ref["number"]}` in your PR and joining swift.algora.io
15+
"""
16+
17+
if Github.pat_enabled() do
18+
Github.create_issue_comment(
19+
Github.pat(),
20+
ticket_ref["owner"],
21+
ticket_ref["repo"],
22+
ticket_ref["number"],
23+
body
24+
)
25+
else
26+
Logger.info("""
27+
Github.create_issue_comment(Github.pat(), "#{ticket_ref["owner"]}", "#{ticket_ref["repo"]}", #{ticket_ref["number"]},
28+
\"\"\"
29+
#{body}
30+
\"\"\")
31+
""")
32+
end
33+
end
34+
end

lib/algora/integrations/github/poller/comment_consumer.ex

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ defmodule Algora.Github.Poller.CommentConsumer do
66
alias Algora.Bounties
77
alias Algora.Github
88
alias Algora.Util
9-
alias Algora.Workspace
109

1110
require Logger
1211

@@ -51,42 +50,14 @@ defmodule Algora.Github.Poller.CommentConsumer do
5150
end
5251

5352
defp run_command({:bounty, args}, ticket_ref, comment) do
54-
owner = Keyword.get(ticket_ref, :owner)
55-
repo = Keyword.get(ticket_ref, :repo)
56-
number = Keyword.get(ticket_ref, :number)
57-
5853
with {:ok, user} <- Accounts.fetch_user_by(provider_id: to_string(comment["user"]["id"])),
59-
{:ok, token} <- Accounts.get_access_token(user),
60-
{:ok, amount} <- Keyword.fetch(args, :amount),
61-
{:ok, ticket} <- Workspace.ensure_ticket(token, owner, repo, number),
62-
{:ok, _bounty} <-
63-
Bounties.create_bounty(%{
64-
creator: user,
65-
owner: user,
66-
amount: amount,
67-
ticket: ticket
68-
}) do
69-
# TODO: post comment in a separate job
70-
body = """
71-
💎 **#{user.provider_login}** is offering a **#{Money.to_string!(amount, no_fraction_if_integer: true)}** bounty for this issue
72-
73-
👉 Got a pull request resolving this? Claim the bounty by commenting `/claim ##{number}` in your PR and joining swift.algora.io
74-
"""
75-
76-
if Github.pat_enabled() do
77-
Github.create_issue_comment(Github.pat(), owner, repo, number, body)
78-
else
79-
Logger.info("""
80-
Github.create_issue_comment(Github.pat(), "#{owner}", "#{repo}", #{number},
81-
\"\"\"
82-
#{body}
83-
\"\"\")
84-
""")
85-
86-
:ok
87-
end
88-
89-
:ok
54+
{:ok, amount} <- Keyword.fetch(args, :amount) do
55+
Bounties.create_bounty(%{
56+
creator: user,
57+
owner: user,
58+
amount: amount,
59+
ticket_ref: %{owner: ticket_ref[:owner], repo: ticket_ref[:repo], number: ticket_ref[:number]}
60+
})
9061
else
9162
{:error, _reason} = error ->
9263
Logger.error("Failed to create bounty: #{inspect(error)}")

lib/algora_web/live/community/dashboard_live.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,13 @@ defmodule AlgoraWeb.Community.DashboardLive do
220220
ticket_ref = get_field(changeset, :ticket_ref)
221221

222222
with %{valid?: true} <- changeset,
223-
{:ok, bounty} <-
223+
{:ok, _bounty} <-
224224
Bounties.create_bounty(%{
225225
creator: socket.assigns.current_user,
226226
owner: socket.assigns.current_user,
227227
amount: amount,
228228
ticket_ref: ticket_ref
229229
}) do
230-
Bounties.notify_bounty(%{owner: socket.assigns.current_user, bounty: bounty, ticket_ref: ticket_ref})
231-
232230
{:noreply,
233231
socket
234232
|> assign_achievements()

lib/algora_web/live/org/bounty_hook.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ defmodule AlgoraWeb.Org.BountyHook do
1717

1818
# TODO: use AlgoraWeb.Community.DashboardLive.BountyForm
1919
with {:ok, [ticket_ref: ticket_ref], _, _, _, _} <- Parser.full_ticket_ref(url),
20-
{:ok, _bounty} <- Bounties.create_bounty(%{creator: creator, owner: owner, ticket: ticket_ref, amount: amount}) do
20+
{:ok, _bounty} <-
21+
Bounties.create_bounty(%{creator: creator, owner: owner, ticket_ref: ticket_ref, amount: amount}) do
2122
{:halt,
2223
socket
2324
|> put_flash(:info, "Bounty created successfully")

lib/algora_web/live/org/create_bounty_live.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ defmodule AlgoraWeb.Org.CreateBountyLive do
458458
url = get_change(changeset, :ticket_url)
459459

460460
with {:ok, [ticket_ref: ticket_ref], _, _, _, _} <- Parser.full_ticket_ref(url),
461-
{:ok, _bounty} <- Bounties.create_bounty(%{creator: creator, owner: owner, ticket: ticket_ref, amount: amount}) do
461+
{:ok, _bounty} <-
462+
Bounties.create_bounty(%{creator: creator, owner: owner, ticket_ref: ticket_ref, amount: amount}) do
462463
{:noreply,
463464
socket
464465
|> put_flash(:info, "Bounty created successfully")

lib/algora_web/live/swift_bounties_live.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,7 @@ defmodule AlgoraWeb.SwiftBountiesLive do
607607
amount: amount,
608608
ticket_ref: ticket_ref
609609
}) do
610-
{:ok, bounty} ->
611-
Bounties.notify_bounty(%{owner: socket.assigns.current_user, bounty: bounty, ticket_ref: ticket_ref})
612-
610+
{:ok, _bounty} ->
613611
{:noreply,
614612
socket
615613
|> put_flash(:info, "Bounty created")

0 commit comments

Comments
 (0)