|
| 1 | +defmodule Algora.Bounties.Jobs.NotifyTransfer do |
| 2 | + @moduledoc false |
| 3 | + use Oban.Worker, queue: :notify_transfer |
| 4 | + |
| 5 | + import Ecto.Query |
| 6 | + |
| 7 | + alias Algora.Bounties.Ticket |
| 8 | + alias Algora.Github |
| 9 | + alias Algora.Payments.Transaction |
| 10 | + alias Algora.Repo |
| 11 | + alias Algora.Workspace.Ticket |
| 12 | + |
| 13 | + require Logger |
| 14 | + |
| 15 | + @impl Oban.Worker |
| 16 | + def perform(%Oban.Job{args: %{"transaction_id" => transaction_id}}) do |
| 17 | + with {:ok, ticket} <- |
| 18 | + Repo.fetch_one( |
| 19 | + from t in Ticket, |
| 20 | + left_join: bounty in assoc(t, :bounties), |
| 21 | + left_join: tip in assoc(t, :tips), |
| 22 | + left_join: tx in Transaction, |
| 23 | + on: tx.bounty_id == bounty.id or tx.tip_id == tip.id, |
| 24 | + join: repo in assoc(t, :repository), |
| 25 | + join: user in assoc(repo, :user), |
| 26 | + where: tx.id == ^transaction_id, |
| 27 | + select_merge: %{ |
| 28 | + repository: %{repo | user: user} |
| 29 | + } |
| 30 | + ), |
| 31 | + ticket_ref = %{ |
| 32 | + owner: ticket.repository.user.provider_login, |
| 33 | + repo: ticket.repository.name, |
| 34 | + number: ticket.number |
| 35 | + }, |
| 36 | + {:ok, transaction} <- |
| 37 | + Repo.fetch_one( |
| 38 | + from tx in Transaction, |
| 39 | + join: user in assoc(tx, :user), |
| 40 | + where: tx.id == ^transaction_id, |
| 41 | + where: tx.type == :transfer, |
| 42 | + select_merge: %{user: user} |
| 43 | + ) do |
| 44 | + installation = Repo.get(Installation, connected_user_id: ticket.repository.user.id) |
| 45 | + body = "🎉🎈 @#{transaction.user.provider_login} has been awarded **#{transaction.net_amount}**! 🎈🎊" |
| 46 | + |
| 47 | + do_perform(ticket_ref, body, installation) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + defp do_perform(ticket_ref, body, nil) do |
| 52 | + if Github.pat_enabled() do |
| 53 | + Github.create_issue_comment( |
| 54 | + Github.pat(), |
| 55 | + ticket_ref["owner"], |
| 56 | + ticket_ref["repo"], |
| 57 | + ticket_ref["number"], |
| 58 | + body |
| 59 | + ) |
| 60 | + else |
| 61 | + Logger.info(""" |
| 62 | + Github.create_issue_comment(Github.pat(), "#{ticket_ref["owner"]}", "#{ticket_ref["repo"]}", #{ticket_ref["number"]}, |
| 63 | + \"\"\" |
| 64 | + #{body} |
| 65 | + \"\"\") |
| 66 | + """) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp do_perform(ticket_ref, body, installation) do |
| 71 | + {:ok, token} = Github.get_installation_token(installation.id) |
| 72 | + Github.create_issue_comment(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"], body) |
| 73 | + end |
| 74 | +end |
0 commit comments