Skip to content

Commit 5cdbd4e

Browse files
committed
implement edits
1 parent 7bb2157 commit 5cdbd4e

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

lib/algora/accounts/schemas/user.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Algora.Accounts.User do
1212
alias Algora.Organizations.Member
1313
alias Algora.Types.Money
1414
alias Algora.Workspace.Installation
15+
alias AlgoraWeb.Endpoint
1516

1617
@derive {Inspect, except: [:provider_meta]}
1718
typed_schema "users" do
@@ -322,8 +323,9 @@ defmodule Algora.Accounts.User do
322323
def handle(%{handle: handle}) when is_binary(handle), do: handle
323324
def handle(%{provider_login: handle}), do: handle
324325

325-
def url(%{handle: handle, type: :individual}) when is_binary(handle), do: "/@/#{handle}"
326-
def url(%{handle: handle, type: :organization}), do: "/org/#{handle}"
326+
def url(%{handle: handle, type: :individual}) when is_binary(handle), do: "#{Endpoint.url()}/@/#{handle}"
327+
def url(%{handle: handle, type: :organization}), do: "#{Endpoint.url()}/org/#{handle}"
328+
def url(%{handle: handle}) when is_binary(handle), do: "#{Endpoint.url()}/org/#{handle}"
327329
def url(%{provider_login: handle}), do: "https://github.com/#{handle}"
328330

329331
def last_context(%{last_context: last_context}), do: last_context || default_context()

lib/algora/bounties/bounties.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ defmodule Algora.Bounties do
2626

2727
@type criterion ::
2828
{:limit, non_neg_integer()}
29-
| {:owner_id, integer()}
29+
| {:ticket_id, String.t()}
30+
| {:owner_id, String.t()}
3031
| {:status, :open | :paid}
3132
| {:tech_stack, [String.t()]}
3233

@@ -113,7 +114,7 @@ defmodule Algora.Bounties do
113114
Repo.transact(fn ->
114115
with {:ok, token} <- token_res,
115116
{:ok, ticket} <- Workspace.ensure_ticket(token, repo_owner, repo_name, number),
116-
existing = Repo.get_by(Bounty, ticket_id: ticket.id),
117+
existing = Repo.get_by(Bounty, owner_id: owner.id, ticket_id: ticket.id),
117118
{:ok, strategy} <- strategy_to_action(existing, opts[:strategy]),
118119
{:ok, bounty} <-
119120
(case strategy do
@@ -624,6 +625,9 @@ defmodule Algora.Bounties do
624625
{:limit, limit}, query ->
625626
from([b] in query, limit: ^limit)
626627

628+
{:ticket_id, ticket_id}, query ->
629+
from([b] in query, where: b.ticket_id == ^ticket_id)
630+
627631
{:owner_id, owner_id}, query ->
628632
from([b] in query, where: b.owner_id == ^owner_id)
629633

lib/algora/bounties/jobs/notify_bounty.ex

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
44
queue: :notify_bounty,
55
max_attempts: 1
66

7-
alias Algora.Accounts
87
alias Algora.Accounts.User
8+
alias Algora.Bounties
99
alias Algora.Github
1010
alias Algora.Repo
1111
alias Algora.Util
@@ -58,22 +58,24 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
5858
@impl Oban.Worker
5959
def perform(%Oban.Job{
6060
args: %{
61-
"amount" => amount,
61+
"amount" => _amount,
6262
"ticket_ref" => ticket_ref,
6363
"installation_id" => installation_id,
6464
"command_id" => command_id,
6565
"command_source" => command_source
6666
}
6767
}) do
6868
with {:ok, token} <- Github.get_installation_token(installation_id),
69-
{:ok, installation} <-
70-
Workspace.fetch_installation_by(provider: "github", provider_id: to_string(installation_id)),
71-
{:ok, owner} <- Accounts.fetch_user_by(id: installation.connected_user_id),
72-
{:ok, _} <-
73-
Github.add_labels(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"], ["💎 Bounty"]),
74-
{:ok, ticket} <- Workspace.ensure_ticket(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"]) do
69+
{:ok, ticket} <- Workspace.ensure_ticket(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"]),
70+
bounties when bounties != [] <- Bounties.list_bounties(ticket_id: ticket.id),
71+
{:ok, _} <- Github.add_labels(token, ticket_ref["owner"], ticket_ref["repo"], ticket_ref["number"], ["💎 Bounty"]) do
72+
header =
73+
Enum.map_join(bounties, "\n", fn bounty ->
74+
"## 💎 #{bounty.amount} bounty [• #{bounty.owner.name}](#{User.url(bounty.owner)})"
75+
end)
76+
7577
body = """
76-
## 💎 #{amount} bounty [• #{owner.name}](#{User.url(owner)})
78+
#{header}
7779
### Steps to solve:
7880
1. **Start working**: Comment `/attempt ##{ticket_ref["number"]}` with your implementation plan
7981
2. **Submit work**: Create a pull request including `/claim ##{ticket_ref["number"]}` in the PR body to claim the bounty
@@ -96,16 +98,16 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
9698
response.provider_response_id,
9799
body
98100
) do
99-
{:ok, _comment} ->
100-
try_update_command_response(response, body)
101+
{:ok, comment} ->
102+
try_update_command_response(response, comment)
101103

102104
{:error, "404 Not Found"} ->
103105
with {:ok, _} <- Workspace.delete_command_response(response.id) do
104106
post_response(token, ticket_ref, command_id, command_source, ticket, body)
105107
end
106108

107109
{:error, reason} ->
108-
Logger.error("Failed to update command response #{response.id} with body #{body}")
110+
Logger.error("Failed to update command response #{response.id}: #{inspect(reason)}")
109111
{:error, reason}
110112
end
111113

@@ -142,8 +144,8 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
142144
{:ok, command_response} ->
143145
{:ok, command_response}
144146

145-
{:error, _reason} ->
146-
Logger.error("Failed to update command response #{command_response.id}")
147+
{:error, reason} ->
148+
Logger.error("Failed to update command response #{command_response.id}: #{inspect(reason)}")
147149
{:ok, command_response}
148150
end
149151
end

lib/algora_web/controllers/webhooks/github_controller.ex

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ defmodule AlgoraWeb.Webhooks.GithubController do
55
alias Algora.Bounties
66
alias Algora.Github
77
alias Algora.Github.Webhook
8+
alias Algora.Repo
89
alias Algora.Workspace
10+
alias Algora.Workspace.CommandResponse
911

1012
require Logger
1113

@@ -53,18 +55,29 @@ defmodule AlgoraWeb.Webhooks.GithubController do
5355
defp execute_command(event_action, {:bounty, args}, author, params)
5456
when event_action in ["issues.opened", "issues.edited", "issue_comment.created", "issue_comment.edited"] do
5557
[event, _action] = String.split(event_action, ".")
58+
5659
amount = args[:amount]
5760
repo = params["repository"]
5861
issue = params["issue"]
5962
installation_id = params["installation"]["id"]
6063

6164
{command_source, command_id} =
6265
case event do
63-
"issue_comment" ->
64-
{:comment, params["comment"]["id"]}
66+
"issue_comment" -> {:comment, params["comment"]["id"]}
67+
_ -> {:ticket, issue["id"]}
68+
end
6569

66-
_ ->
67-
{:ticket, issue["id"]}
70+
# TODO: perform compensating action if needed
71+
# ❌ comment1.created (:set) -> comment2.created (:increase) -> comment2.edited (:increase)
72+
# ✅ comment1.created (:set) -> comment2.created (:increase) -> comment2.edited (:decrease + :increase)
73+
strategy =
74+
case Repo.get_by(CommandResponse,
75+
provider: "github",
76+
provider_command_id: to_string(command_id),
77+
command_source: command_source
78+
) do
79+
nil -> :increase
80+
_ -> :set
6881
end
6982

7083
# TODO: community bounties?
@@ -81,6 +94,7 @@ defmodule AlgoraWeb.Webhooks.GithubController do
8194
amount: amount,
8295
ticket_ref: %{owner: repo["owner"]["login"], repo: repo["name"], number: issue["number"]}
8396
},
97+
strategy: strategy,
8498
installation_id: installation_id,
8599
command_id: command_id,
86100
command_source: command_source

0 commit comments

Comments
 (0)