Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ config :algora, Oban,
queues: [
event_consumers: 1,
comment_consumers: 1,
github_og_image: 5
github_og_image: 5,
notify_bounty: 1
]

# Configures the mailer
Expand Down
58 changes: 21 additions & 37 deletions lib/algora/bounties/bounties.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ defmodule Algora.Bounties do
alias Algora.Accounts.User
alias Algora.Bounties.Bounty
alias Algora.Bounties.Claim
alias Algora.Bounties.Jobs
alias Algora.Bounties.Tip
alias Algora.FeeTier
alias Algora.Github
alias Algora.MoneyUtils
alias Algora.Organizations.Member
alias Algora.Payments
Expand Down Expand Up @@ -37,9 +37,9 @@ defmodule Algora.Bounties do
Phoenix.PubSub.subscribe(Algora.PubSub, "bounties:all")
end

@spec create_bounty(%{creator: User.t(), owner: User.t(), amount: Money.t(), ticket: Ticket.t()}) ::
@spec do_create_bounty(%{creator: User.t(), owner: User.t(), amount: Money.t(), ticket: Ticket.t()}) ::
{:ok, Bounty.t()} | {:error, atom()}
def create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket}) do
defp do_create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket}) do
changeset =
Bounty.changeset(%Bounty{}, %{
amount: amount,
Expand Down Expand Up @@ -72,46 +72,30 @@ defmodule Algora.Bounties do
creator: creator,
owner: owner,
amount: amount,
ticket_ref: %{owner: repo_owner, repo: repo_name, number: number}
ticket_ref: %{owner: repo_owner, repo: repo_name, number: number} = ticket_ref
}) do
with {:ok, token} <- Accounts.get_access_token(creator),
{:ok, ticket} <- Workspace.ensure_ticket(token, repo_owner, repo_name, number) do
create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket})
else
{:error, _reason} = error -> error
end
end

def notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}) do
# TODO: post comment in a separate job
body = """
💎 **#{owner.provider_login}** is offering a **#{Money.to_string!(bounty.amount, no_fraction_if_integer: true)}** bounty for this issue

👉 Got a pull request resolving this? Claim the bounty by commenting `/claim ##{ticket_ref.number}` in your PR and joining swift.algora.io
"""

Task.start(fn ->
if Github.pat_enabled() do
Github.create_issue_comment(
Github.pat(),
ticket_ref.owner,
ticket_ref.repo,
ticket_ref.number,
body
)
Repo.transact(fn ->
with {:ok, token} <- Accounts.get_access_token(creator),
{:ok, ticket} <- Workspace.ensure_ticket(token, repo_owner, repo_name, number),
{:ok, bounty} <- do_create_bounty(%{creator: creator, owner: owner, amount: amount, ticket: ticket}),
{:ok, _job} <- notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}) do
{:ok, bounty}
else
Logger.info("""
Github.create_issue_comment(Github.pat(), "#{ticket_ref.owner}", "#{ticket_ref.repo}", #{ticket_ref.number},
\"\"\"
#{body}
\"\"\")
""")

:ok
{:error, _reason} = error -> error
end
end)
end

def notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}) do
%{
owner_login: owner.provider_login,
amount: Money.to_string!(bounty.amount, no_fraction_if_integer: true),
ticket_ref: %{owner: ticket_ref.owner, repo: ticket_ref.repo, number: ticket_ref.number}
}
|> Jobs.NotifyBounty.new()
|> Oban.insert()
end

@spec create_tip(%{creator: User.t(), owner: User.t(), recipient: User.t(), amount: Money.t()}) ::
{:ok, String.t()} | {:error, atom()}
def create_tip(%{creator: creator, owner: owner, recipient: recipient, amount: amount}) do
Expand Down
34 changes: 34 additions & 0 deletions lib/algora/bounties/jobs/notify_bounty.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Algora.Bounties.Jobs.NotifyBounty do
@moduledoc false
use Oban.Worker, queue: :notify_bounty

alias Algora.Github

require Logger

@impl Oban.Worker
def perform(%Oban.Job{args: %{"owner_login" => owner_login, "amount" => amount, "ticket_ref" => ticket_ref}}) do
body = """
💎 **#{owner_login}** is offering a **#{amount}** bounty for this issue

👉 Got a pull request resolving this? Claim the bounty by commenting `/claim ##{ticket_ref["number"]}` in your PR and joining swift.algora.io
"""

if Github.pat_enabled() do
Github.create_issue_comment(
Github.pat(),
ticket_ref["owner"],
ticket_ref["repo"],
ticket_ref["number"],
body
)
else
Logger.info("""
Github.create_issue_comment(Github.pat(), "#{ticket_ref["owner"]}", "#{ticket_ref["repo"]}", #{ticket_ref["number"]},
\"\"\"
#{body}
\"\"\")
""")
end
end
end
43 changes: 7 additions & 36 deletions lib/algora/integrations/github/poller/comment_consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Algora.Github.Poller.CommentConsumer do
alias Algora.Bounties
alias Algora.Github
alias Algora.Util
alias Algora.Workspace

require Logger

Expand Down Expand Up @@ -51,42 +50,14 @@ defmodule Algora.Github.Poller.CommentConsumer do
end

defp run_command({:bounty, args}, ticket_ref, comment) do
owner = Keyword.get(ticket_ref, :owner)
repo = Keyword.get(ticket_ref, :repo)
number = Keyword.get(ticket_ref, :number)

with {:ok, user} <- Accounts.fetch_user_by(provider_id: to_string(comment["user"]["id"])),
{:ok, token} <- Accounts.get_access_token(user),
{:ok, amount} <- Keyword.fetch(args, :amount),
{:ok, ticket} <- Workspace.ensure_ticket(token, owner, repo, number),
{:ok, _bounty} <-
Bounties.create_bounty(%{
creator: user,
owner: user,
amount: amount,
ticket: ticket
}) do
# TODO: post comment in a separate job
body = """
💎 **#{user.provider_login}** is offering a **#{Money.to_string!(amount, no_fraction_if_integer: true)}** bounty for this issue

👉 Got a pull request resolving this? Claim the bounty by commenting `/claim ##{number}` in your PR and joining swift.algora.io
"""

if Github.pat_enabled() do
Github.create_issue_comment(Github.pat(), owner, repo, number, body)
else
Logger.info("""
Github.create_issue_comment(Github.pat(), "#{owner}", "#{repo}", #{number},
\"\"\"
#{body}
\"\"\")
""")

:ok
end

:ok
{:ok, amount} <- Keyword.fetch(args, :amount) do
Bounties.create_bounty(%{
creator: user,
owner: user,
amount: amount,
ticket_ref: %{owner: ticket_ref[:owner], repo: ticket_ref[:repo], number: ticket_ref[:number]}
})
else
{:error, _reason} = error ->
Logger.error("Failed to create bounty: #{inspect(error)}")
Expand Down
4 changes: 1 addition & 3 deletions lib/algora_web/live/community/dashboard_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,13 @@ defmodule AlgoraWeb.Community.DashboardLive do
ticket_ref = get_field(changeset, :ticket_ref)

with %{valid?: true} <- changeset,
{:ok, bounty} <-
{:ok, _bounty} <-
Bounties.create_bounty(%{
creator: socket.assigns.current_user,
owner: socket.assigns.current_user,
amount: amount,
ticket_ref: ticket_ref
}) do
Bounties.notify_bounty(%{owner: socket.assigns.current_user, bounty: bounty, ticket_ref: ticket_ref})

{:noreply,
socket
|> assign_achievements()
Expand Down
3 changes: 2 additions & 1 deletion lib/algora_web/live/org/bounty_hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ defmodule AlgoraWeb.Org.BountyHook do

# TODO: use AlgoraWeb.Community.DashboardLive.BountyForm
with {:ok, [ticket_ref: ticket_ref], _, _, _, _} <- Parser.full_ticket_ref(url),
{:ok, _bounty} <- Bounties.create_bounty(%{creator: creator, owner: owner, ticket: ticket_ref, amount: amount}) do
{:ok, _bounty} <-
Bounties.create_bounty(%{creator: creator, owner: owner, ticket_ref: ticket_ref, amount: amount}) do
{:halt,
socket
|> put_flash(:info, "Bounty created successfully")
Expand Down
3 changes: 2 additions & 1 deletion lib/algora_web/live/org/create_bounty_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ defmodule AlgoraWeb.Org.CreateBountyLive do
url = get_change(changeset, :ticket_url)

with {:ok, [ticket_ref: ticket_ref], _, _, _, _} <- Parser.full_ticket_ref(url),
{:ok, _bounty} <- Bounties.create_bounty(%{creator: creator, owner: owner, ticket: ticket_ref, amount: amount}) do
{:ok, _bounty} <-
Bounties.create_bounty(%{creator: creator, owner: owner, ticket_ref: ticket_ref, amount: amount}) do
{:noreply,
socket
|> put_flash(:info, "Bounty created successfully")
Expand Down
4 changes: 1 addition & 3 deletions lib/algora_web/live/swift_bounties_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,7 @@ defmodule AlgoraWeb.SwiftBountiesLive do
amount: amount,
ticket_ref: ticket_ref
}) do
{:ok, bounty} ->
Bounties.notify_bounty(%{owner: socket.assigns.current_user, bounty: bounty, ticket_ref: ticket_ref})

{:ok, _bounty} ->
{:noreply,
socket
|> put_flash(:info, "Bounty created")
Expand Down
Loading