@@ -94,6 +94,8 @@ defmodule Algora.Bounties do
9494 end
9595 end
9696
97+ def create_bounty ( _params , opts \\ [ ] )
98+
9799 @ spec create_bounty (
98100 % {
99101 creator: User . t ( ) ,
@@ -118,7 +120,7 @@ defmodule Algora.Bounties do
118120 amount: amount ,
119121 ticket_ref: % { owner: repo_owner , repo: repo_name , number: number } = ticket_ref
120122 } ,
121- opts \\ [ ]
123+ opts
122124 ) do
123125 command_id = opts [ :command_id ]
124126 shared_with = opts [ :shared_with ] || [ ]
@@ -177,6 +179,49 @@ defmodule Algora.Bounties do
177179 end )
178180 end
179181
182+ @ spec create_bounty (
183+ % {
184+ creator: User . t ( ) ,
185+ owner: User . t ( ) ,
186+ amount: Money . t ( ) ,
187+ title: String . t ( ) ,
188+ description: String . t ( )
189+ } ,
190+ opts :: [
191+ strategy: strategy ( ) ,
192+ visibility: Bounty . visibility ( ) | nil ,
193+ shared_with: [ String . t ( ) ] | nil
194+ ]
195+ ) ::
196+ { :ok , Bounty . t ( ) } | { :error , atom ( ) }
197+ def create_bounty ( % { creator: creator , owner: owner , amount: amount , title: title , description: description } , opts ) do
198+ shared_with = opts [ :shared_with ] || [ ]
199+
200+ Repo . transact ( fn ->
201+ with { :ok , ticket } <-
202+ % Ticket { type: :issue }
203+ |> Ticket . changeset ( % { title: title , description: description } )
204+ |> Repo . insert ( ) ,
205+ { :ok , bounty } <-
206+ do_create_bounty ( % {
207+ creator: creator ,
208+ owner: owner ,
209+ amount: amount ,
210+ ticket: ticket ,
211+ visibility: opts [ :visibility ] ,
212+ shared_with: shared_with
213+ } ) ,
214+ { :ok , _job } <- notify_bounty ( % { owner: owner , bounty: bounty } ) do
215+ broadcast ( )
216+ { :ok , bounty }
217+ else
218+ { :error , _reason } = error ->
219+ Algora.Admin . alert ( "Error creating bounty: #{ inspect ( error ) } " , :error )
220+ error
221+ end
222+ end )
223+ end
224+
180225 defp claim_to_solution ( claim ) do
181226 % {
182227 type: :claim ,
@@ -337,6 +382,8 @@ defmodule Algora.Bounties do
337382 end
338383 end
339384
385+ def notify_bounty ( bounty , opts \\ [ ] )
386+
340387 @ spec notify_bounty (
341388 % {
342389 owner: User . t ( ) ,
@@ -346,7 +393,7 @@ defmodule Algora.Bounties do
346393 opts :: [ installation_id: integer ( ) , command_id: integer ( ) , command_source: :ticket | :comment ]
347394 ) ::
348395 { :ok , Oban.Job . t ( ) } | { :error , atom ( ) }
349- def notify_bounty ( % { owner: owner , bounty: bounty , ticket_ref: ticket_ref } , opts \\ [ ] ) do
396+ def notify_bounty ( % { owner: owner , bounty: bounty , ticket_ref: ticket_ref } , opts ) do
350397 % {
351398 owner_login: owner . provider_login ,
352399 amount: Money . to_string! ( bounty . amount , no_fraction_if_integer: true ) ,
@@ -362,6 +409,13 @@ defmodule Algora.Bounties do
362409 |> Oban . insert ( )
363410 end
364411
412+ @ spec notify_bounty ( % { owner: User . t ( ) , bounty: Bounty . t ( ) } , opts :: [ ] ) ::
413+ { :ok , nil } | { :error , atom ( ) }
414+ def notify_bounty ( % { owner: _owner , bounty: bounty } , _opts ) do
415+ Algora.Admin . alert ( "Notify bounty: #{ inspect ( bounty ) } " , :error )
416+ { :ok , nil }
417+ end
418+
365419 @ spec do_claim_bounty ( % {
366420 provider_login: String . t ( ) ,
367421 token: String . t ( ) ,
0 commit comments