1
1
defmodule Algora.Bounties.Jobs.NotifyBounty do
2
2
@ moduledoc false
3
- use Oban.Worker , queue: :notify_bounty
3
+ use Oban.Worker ,
4
+ queue: :notify_bounty ,
5
+ max_attempts: 1
4
6
5
7
alias Algora.Accounts
6
8
alias Algora.Accounts.User
7
9
alias Algora.Github
10
+ alias Algora.Repo
11
+ alias Algora.Util
8
12
alias Algora.Workspace
13
+ alias Algora.Workspace.CommandResponse
9
14
10
15
require Logger
11
16
12
17
@ impl Oban.Worker
13
18
def perform ( % Oban.Job {
14
- args: % { "owner_login" => owner_login , "amount" => amount , "ticket_ref" => ticket_ref , "installation_id" => nil }
19
+ args: % {
20
+ "owner_login" => owner_login ,
21
+ "amount" => amount ,
22
+ "ticket_ref" => ticket_ref ,
23
+ "installation_id" => nil ,
24
+ "command_id" => command_id ,
25
+ "command_source" => command_source
26
+ }
15
27
} ) do
16
28
body = """
17
29
💎 **#{ owner_login } ** is offering a **#{ amount } ** bounty for this issue
@@ -20,13 +32,28 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
20
32
"""
21
33
22
34
if Github . pat_enabled ( ) do
23
- Github . create_issue_comment (
24
- Github . pat ( ) ,
25
- ticket_ref [ "owner" ] ,
26
- ticket_ref [ "repo" ] ,
27
- ticket_ref [ "number" ] ,
28
- body
29
- )
35
+ with { :ok , response } <-
36
+ Github . create_issue_comment (
37
+ Github . pat ( ) ,
38
+ ticket_ref [ "owner" ] ,
39
+ ticket_ref [ "repo" ] ,
40
+ ticket_ref [ "number" ] ,
41
+ body
42
+ ) ,
43
+ { :ok , ticket } <-
44
+ Workspace . ensure_ticket ( Github . pat ( ) , ticket_ref [ "owner" ] , ticket_ref [ "repo" ] , ticket_ref [ "number" ] ) do
45
+ % CommandResponse { }
46
+ |> CommandResponse . changeset ( % {
47
+ provider: "github" ,
48
+ provider_meta: Util . normalize_struct ( response ) ,
49
+ provider_command_id: to_string ( command_id ) ,
50
+ provider_response_id: to_string ( response [ "id" ] ) ,
51
+ command_source: command_source ,
52
+ command_type: :bounty ,
53
+ ticket_id: ticket . id
54
+ } )
55
+ |> Repo . insert ( )
56
+ end
30
57
else
31
58
Logger . info ( """
32
59
Github.create_issue_comment(Github.pat(), "#{ ticket_ref [ "owner" ] } ", "#{ ticket_ref [ "repo" ] } ", #{ ticket_ref [ "number" ] } ,
@@ -38,12 +65,22 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
38
65
end
39
66
40
67
@ impl Oban.Worker
41
- def perform ( % Oban.Job { args: % { "amount" => amount , "ticket_ref" => ticket_ref , "installation_id" => installation_id } } ) do
68
+ def perform ( % Oban.Job {
69
+ args: % {
70
+ "amount" => amount ,
71
+ "ticket_ref" => ticket_ref ,
72
+ "installation_id" => installation_id ,
73
+ "command_id" => command_id ,
74
+ "command_source" => command_source
75
+ }
76
+ } ) do
42
77
with { :ok , token } <- Github . get_installation_token ( installation_id ) ,
43
78
{ :ok , installation } <-
44
79
Workspace . fetch_installation_by ( provider: "github" , provider_id: to_string ( installation_id ) ) ,
45
80
{ :ok , owner } <- Accounts . fetch_user_by ( id: installation . connected_user_id ) ,
46
- { :ok , _ } <- Github . add_labels ( token , ticket_ref [ "owner" ] , ticket_ref [ "repo" ] , ticket_ref [ "number" ] , [ "💎 Bounty" ] ) do
81
+ { :ok , _ } <-
82
+ Github . add_labels ( token , ticket_ref [ "owner" ] , ticket_ref [ "repo" ] , ticket_ref [ "number" ] , [ "💎 Bounty" ] ) ,
83
+ { :ok , ticket } <- Workspace . ensure_ticket ( token , ticket_ref [ "owner" ] , ticket_ref [ "repo" ] , ticket_ref [ "number" ] ) do
47
84
body = """
48
85
## 💎 #{ amount } bounty [• #{ owner . name } ](#{ User . url ( owner ) } )
49
86
### Steps to solve:
@@ -54,13 +91,26 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
54
91
Thank you for contributing to #{ ticket_ref [ "owner" ] } /#{ ticket_ref [ "repo" ] } !
55
92
"""
56
93
57
- Github . create_issue_comment (
58
- token ,
59
- ticket_ref [ "owner" ] ,
60
- ticket_ref [ "repo" ] ,
61
- ticket_ref [ "number" ] ,
62
- body
63
- )
94
+ with { :ok , response } <-
95
+ Github . create_issue_comment (
96
+ token ,
97
+ ticket_ref [ "owner" ] ,
98
+ ticket_ref [ "repo" ] ,
99
+ ticket_ref [ "number" ] ,
100
+ body
101
+ ) do
102
+ % CommandResponse { }
103
+ |> CommandResponse . changeset ( % {
104
+ provider: "github" ,
105
+ provider_meta: Util . normalize_struct ( response ) ,
106
+ provider_command_id: to_string ( command_id ) ,
107
+ provider_response_id: to_string ( response [ "id" ] ) ,
108
+ command_source: command_source ,
109
+ command_type: :bounty ,
110
+ ticket_id: ticket . id
111
+ } )
112
+ |> Repo . insert ( )
113
+ end
64
114
end
65
115
end
66
116
end
0 commit comments