Skip to content

Commit 7ea399b

Browse files
committed
feat: amount labels
1 parent 494f233 commit 7ea399b

File tree

8 files changed

+68
-2
lines changed

8 files changed

+68
-2
lines changed

lib/algora/admin/admin.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ defmodule Algora.Admin do
2121

2222
require Logger
2323

24+
def add_label(url, amount) do
25+
%{owner: owner, repo: repo, number: number} = parse_ticket_url(url)
26+
27+
with installation_id when not is_nil(installation_id) <- Workspace.get_installation_id_by_owner(owner),
28+
{:ok, token} <- Github.get_installation_token(installation_id) do
29+
Workspace.add_amount_label(token, owner, repo, number, amount)
30+
end
31+
end
32+
2433
def init_contributors(repo_owner, repo_name) do
2534
with {:ok, repo} <- Workspace.ensure_repository(token(), repo_owner, repo_name) do
2635
Workspace.ensure_contributors(token(), repo)

lib/algora/bounties/jobs/notify_bounty.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
6868
@impl Oban.Worker
6969
def perform(%Oban.Job{
7070
args: %{
71-
"amount" => _amount,
71+
"amount" => amount,
7272
"ticket_ref" => ticket_ref,
7373
"installation_id" => installation_id,
7474
"command_id" => command_id,
@@ -85,7 +85,9 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
8585
{:ok, ticket} <-
8686
Workspace.ensure_ticket(token, ticket_ref.owner, ticket_ref.repo, ticket_ref.number),
8787
bounties when bounties != [] <- Bounties.list_bounties(ticket_id: ticket.id),
88-
{:ok, _} <- Github.add_labels(token, ticket_ref.owner, ticket_ref.repo, ticket_ref.number, ["💎 Bounty"]) do
88+
{:ok, _} <- Github.add_labels(token, ticket_ref.owner, ticket_ref.repo, ticket_ref.number, ["💎 Bounty"]),
89+
:ok <-
90+
Workspace.add_amount_label(token, ticket_ref.owner, ticket_ref.repo, ticket_ref.number, Money.parse(amount)) do
8991
attempts = Bounties.list_attempts_for_ticket(ticket.id)
9092
claims = Bounties.list_claims([ticket.id])
9193

lib/algora/integrations/github/behaviour.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ defmodule Algora.Github.Behaviour do
3030
@callback list_repository_languages(token(), String.t(), String.t()) :: {:ok, [map()]} | {:error, String.t()}
3131
@callback list_repository_contributors(token(), String.t(), String.t()) :: {:ok, [map()]} | {:error, String.t()}
3232
@callback add_labels(token(), String.t(), String.t(), integer(), [String.t()]) :: {:ok, [map()]} | {:error, String.t()}
33+
@callback create_label(token(), String.t(), String.t(), map()) :: {:ok, [map()]} | {:error, String.t()}
3334
end

lib/algora/integrations/github/client.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,9 @@ defmodule Algora.Github.Client do
272272
labels: labels
273273
})
274274
end
275+
276+
@impl true
277+
def create_label(access_token, owner, repo, label) do
278+
fetch(access_token, "/repos/#{owner}/#{repo}/labels", "POST", label)
279+
end
275280
end

lib/algora/integrations/github/github.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@ defmodule Algora.Github do
144144

145145
@impl true
146146
def add_labels(token, owner, repo, number, labels), do: client().add_labels(token, owner, repo, number, labels)
147+
148+
@impl true
149+
def create_label(token, owner, repo, label), do: client().create_label(token, owner, repo, label)
147150
end

lib/algora/shared/util.ex

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ defmodule Algora.Util do
2828
|> :erlang.binary_to_term()
2929
end
3030

31+
def format_number_compact(number) when is_struct(number, Decimal) do
32+
number
33+
|> Decimal.to_float()
34+
|> format_number_compact()
35+
end
36+
37+
def format_number_compact(number) do
38+
n = trunc(number)
39+
40+
case n do
41+
n when n >= 1_000_000 ->
42+
"#{(n / 1_000_000) |> Float.round(1) |> trim_trailing_zero()}M"
43+
44+
n when n >= 1_000 ->
45+
"#{(n / 1_000) |> Float.round(1) |> trim_trailing_zero()}K"
46+
47+
n ->
48+
to_string(n)
49+
end
50+
end
51+
52+
defp trim_trailing_zero(number) do
53+
number
54+
|> Float.to_string()
55+
|> String.replace(~r/\.0+$/, "")
56+
end
57+
3158
def time_ago(datetime) do
3259
now = NaiveDateTime.utc_now()
3360
diff = NaiveDateTime.diff(now, datetime, :second)

lib/algora/workspace/workspace.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,18 @@ defmodule Algora.Workspace do
554554
def fetch_contributor(repository_id, user_id) do
555555
Repo.fetch_by(Contributor, repository_id: repository_id, user_id: user_id)
556556
end
557+
558+
def add_amount_label(token, owner, repo, number, amount) do
559+
label = "$#{amount |> Money.to_decimal() |> Util.format_number_compact()}"
560+
561+
with {:ok, _} <- Github.create_label(token, owner, repo, %{"name" => label, "color" => "34d399"}),
562+
{:ok, _} <- Github.add_labels(token, owner, repo, number, [label]) do
563+
:ok
564+
else
565+
{:error, reason} ->
566+
Logger.error("Failed to add label #{label} to #{owner}/#{repo}##{number}: #{inspect(reason)}")
567+
568+
:error
569+
end
570+
end
557571
end

test/support/github_mock.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,9 @@ defmodule Algora.Support.GithubMock do
170170
def add_labels(_access_token, _owner, _repo, _number, _labels) do
171171
{:ok, []}
172172
end
173+
174+
@impl true
175+
def create_label(_access_token, _owner, _repo, _label) do
176+
{:ok, %{"id" => random_id()}}
177+
end
173178
end

0 commit comments

Comments
 (0)