@@ -127,42 +127,96 @@ defmodule Algora.Bounties do
127
127
end
128
128
129
129
@ spec do_claim_bounty ( % {
130
- user: User . t ( ) ,
130
+ provider_login: String . t ( ) ,
131
+ token: String . t ( ) ,
131
132
target: Ticket . t ( ) ,
132
133
source: Ticket . t ( ) ,
134
+ group_id: String . t ( ) | nil ,
135
+ group_share: Decimal . t ( ) ,
133
136
status: :pending | :approved | :rejected | :paid ,
134
137
type: :pull_request | :review | :video | :design | :article
135
138
} ) ::
136
139
{ :ok , Claim . t ( ) } | { :error , atom ( ) }
137
- defp do_claim_bounty ( % { user: user , target: target , source: source , status: status , type: type } ) do
138
- # TODO: ensure user is pull request author
139
- changeset =
140
- Claim . changeset ( % Claim { } , % {
141
- target_id: target . id ,
142
- source_id: source . id ,
143
- user_id: user . id ,
144
- type: type ,
145
- status: status ,
146
- url: source . url
147
- } )
148
-
149
- activity_attrs = % { type: :claim_submitted , notify_users: [ user . id ] }
150
-
151
- case Repo . insert_with_activity ( changeset , activity_attrs ) do
152
- { :ok , claim } ->
153
- { :ok , claim }
154
-
140
+ defp do_claim_bounty ( % {
141
+ provider_login: provider_login ,
142
+ token: token ,
143
+ target: target ,
144
+ source: source ,
145
+ group_id: group_id ,
146
+ group_share: group_share ,
147
+ status: status ,
148
+ type: type
149
+ } ) do
150
+ with { :ok , user } <- Workspace . ensure_user ( token , provider_login ) ,
151
+ activity_attrs = % { type: :claim_submitted , notify_users: [ user . id ] } ,
152
+ { :ok , claim } <-
153
+ Repo . insert_with_activity (
154
+ Claim . changeset ( % Claim { } , % {
155
+ target_id: target . id ,
156
+ source_id: source . id ,
157
+ user_id: user . id ,
158
+ type: type ,
159
+ status: status ,
160
+ url: source . url ,
161
+ group_id: group_id ,
162
+ group_share: group_share
163
+ } ) ,
164
+ activity_attrs
165
+ ) do
166
+ { :ok , claim }
167
+ else
155
168
{ :error , % { errors: [ target_id: { _ , [ constraint: :unique , constraint_name: _ ] } ] } } ->
156
169
{ :error , :already_exists }
157
170
158
- { :error , _changeset } = error ->
171
+ { :error , _reason } = error ->
159
172
error
160
173
end
161
174
end
162
175
176
+ @ spec do_claim_bounties ( % {
177
+ provider_logins: [ String . t ( ) ] ,
178
+ token: String . t ( ) ,
179
+ target: Ticket . t ( ) ,
180
+ source: Ticket . t ( ) ,
181
+ status: :pending | :approved | :rejected | :paid ,
182
+ type: :pull_request | :review | :video | :design | :article
183
+ } ) ::
184
+ { :ok , [ Claim . t ( ) ] } | { :error , atom ( ) }
185
+ defp do_claim_bounties ( % {
186
+ provider_logins: provider_logins ,
187
+ token: token ,
188
+ target: target ,
189
+ source: source ,
190
+ status: status ,
191
+ type: type
192
+ } ) do
193
+ Enum . reduce_while ( provider_logins , { :ok , [ ] } , fn provider_login , { :ok , acc } ->
194
+ group_id =
195
+ case List . last ( acc ) do
196
+ nil -> nil
197
+ primary_claim -> primary_claim . group_id
198
+ end
199
+
200
+ case do_claim_bounty ( % {
201
+ provider_login: provider_login ,
202
+ token: token ,
203
+ target: target ,
204
+ source: source ,
205
+ status: status ,
206
+ type: type ,
207
+ group_id: group_id ,
208
+ group_share: Decimal . div ( 1 , length ( provider_logins ) )
209
+ } ) do
210
+ { :ok , claim } -> { :cont , { :ok , [ claim | acc ] } }
211
+ error -> { :halt , error }
212
+ end
213
+ end )
214
+ end
215
+
163
216
@ spec claim_bounty (
164
217
% {
165
218
user: User . t ( ) ,
219
+ coauthor_provider_logins: [ String . t ( ) ] ,
166
220
target_ticket_ref: % { owner: String . t ( ) , repo: String . t ( ) , number: integer ( ) } ,
167
221
source_ticket_ref: % { owner: String . t ( ) , repo: String . t ( ) , number: integer ( ) } ,
168
222
status: :pending | :approved | :rejected | :paid ,
@@ -174,6 +228,7 @@ defmodule Algora.Bounties do
174
228
def claim_bounty (
175
229
% {
176
230
user: user ,
231
+ coauthor_provider_logins: coauthor_provider_logins ,
177
232
target_ticket_ref: % { owner: target_repo_owner , repo: target_repo_name , number: target_number } ,
178
233
source_ticket_ref: % { owner: source_repo_owner , repo: source_repo_name , number: source_number } ,
179
234
status: status ,
@@ -192,7 +247,15 @@ defmodule Algora.Bounties do
192
247
with { :ok , token } <- token_res ,
193
248
{ :ok , target } <- Workspace . ensure_ticket ( token , target_repo_owner , target_repo_name , target_number ) ,
194
249
{ :ok , source } <- Workspace . ensure_ticket ( token , source_repo_owner , source_repo_name , source_number ) ,
195
- { :ok , claim } <- do_claim_bounty ( % { user: user , target: target , source: source , status: status , type: type } ) ,
250
+ { :ok , [ claim | _ ] } <-
251
+ do_claim_bounties ( % {
252
+ provider_logins: [ user . provider_login | coauthor_provider_logins ] ,
253
+ token: token ,
254
+ target: target ,
255
+ source: source ,
256
+ status: status ,
257
+ type: type
258
+ } ) ,
196
259
{ :ok , _job } <- notify_claim ( % { claim: claim } , installation_id: installation_id ) do
197
260
broadcast ( )
198
261
{ :ok , claim }
@@ -208,7 +271,7 @@ defmodule Algora.Bounties do
208
271
) ::
209
272
{ :ok , Oban.Job . t ( ) } | { :error , atom ( ) }
210
273
def notify_claim ( % { claim: claim } , opts \\ [ ] ) do
211
- % { claim_id : claim . id , installation_id: opts [ :installation_id ] }
274
+ % { claim_group_id : claim . group_id , installation_id: opts [ :installation_id ] }
212
275
|> Jobs.NotifyClaim . new ( )
213
276
|> Oban . insert ( )
214
277
end
0 commit comments