Skip to content

Commit 11b071f

Browse files
committed
feat: add autopay_disabled flag to bounties to allow manual payment control
1 parent 81530cc commit 11b071f

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

lib/algora/bounties/schemas/bounty.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Algora.Bounties.Bounty do
99
field :amount, Algora.Types.Money
1010
field :status, Ecto.Enum, values: [:open, :cancelled, :paid]
1111
field :number, :integer, default: 0
12+
field :autopay_disabled, :boolean, default: false
1213

1314
belongs_to :ticket, Algora.Workspace.Ticket
1415
belongs_to :owner, User

lib/algora_web/controllers/webhooks/github_controller.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ defmodule AlgoraWeb.Webhooks.GithubController do
131131
autopayable_bounty =
132132
Enum.find(
133133
bounties,
134-
&(not is_nil(installation) and
134+
&(not &1.autopay_disabled and
135+
not is_nil(installation) and
135136
&1.owner.id == installation.connected_user_id and
136137
not is_nil(&1.owner.customer) and
137138
not is_nil(&1.owner.customer.default_payment_method))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Algora.Repo.Migrations.AddAutopayDisabledToBounties do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:bounties) do
6+
add :autopay_disabled, :boolean, default: false, null: false
7+
end
8+
end
9+
end

scripts/database_migration.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ defmodule DatabaseMigration do
376376
"creator_id" => row["poster_id"],
377377
"inserted_at" => row["created_at"],
378378
"updated_at" => row["updated_at"],
379-
"number" => row["number"]
379+
"number" => row["number"],
380+
"autopay_disabled" => row["autopay_disabled"]
380381
}
381382
end
382383
end

scripts/v1-progress.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
- number: 1
8181
- deleted: 0
8282
- reward_type: 0
83-
- autopay_disabled: 0
83+
- autopay_disabled: 1
8484
- manual_assignments: 0
8585
- timeouts_disabled: 0
8686
- experts_only: 0

scripts/v2-progress.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- creator_id: 1
3535
- inserted_at: 1
3636
- updated_at: 1
37+
- autopay_disabled: 1
3738
- chat_participants:
3839
- id: -1
3940
- last_read_at: -1

test/algora_web/controllers/webhooks/github_controller_test.exs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
44
use Oban.Testing, repo: Algora.Repo
55

66
import Algora.Factory
7+
import Ecto.Changeset
78
import Ecto.Query
89
import ExUnit.CaptureLog
910
import Money.Sigil
@@ -830,6 +831,46 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
830831
assert Repo.aggregate(Transaction, :count) == 0
831832
end
832833

834+
test "does not autopay when autopay is disabled", ctx do
835+
issue_number = :rand.uniform(1000)
836+
pr_number = :rand.uniform(1000)
837+
838+
customer = insert!(:customer, user: ctx[:org])
839+
_payment_method = insert!(:payment_method, customer: customer)
840+
841+
process_scenario!(ctx, [
842+
%{
843+
event_action: "issue_comment.created",
844+
user_type: :admin,
845+
body: "/bounty $100",
846+
params: %{"issue" => %{"number" => issue_number}}
847+
},
848+
%{
849+
event_action: "pull_request.opened",
850+
user_type: :unauthorized,
851+
body: "/claim #{issue_number}",
852+
params: %{"pull_request" => %{"number" => pr_number}}
853+
}
854+
])
855+
856+
Bounty |> Repo.one!() |> change(%{autopay_disabled: true}) |> Repo.update!()
857+
858+
process_scenario!(ctx, [
859+
%{
860+
event_action: "pull_request.closed",
861+
user_type: :unauthorized,
862+
body: "/claim #{issue_number}",
863+
params: %{"pull_request" => %{"number" => pr_number, "merged_at" => DateTime.to_iso8601(DateTime.utc_now())}}
864+
}
865+
])
866+
867+
bounty = Repo.one!(Bounty)
868+
claim = Repo.one!(Claim)
869+
assert claim.target_id == bounty.ticket_id
870+
assert claim.status == :approved
871+
assert Repo.aggregate(Transaction, :count) == 0
872+
end
873+
833874
test "handles autopay when claim is changed to a different bounty and PR is merged", ctx do
834875
issue_number1 = :rand.uniform(1000)
835876
issue_number2 = issue_number1 + :rand.uniform(1000)

0 commit comments

Comments
 (0)