@@ -42,17 +42,8 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
42
42
) ,
43
43
{ :ok , ticket } <-
44
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 ( )
45
+ # TODO: update existing command response if it exists
46
+ create_command_response ( response , command_source , command_id , ticket . id )
56
47
end
57
48
else
58
49
Logger . info ( """
@@ -91,26 +82,69 @@ defmodule Algora.Bounties.Jobs.NotifyBounty do
91
82
Thank you for contributing to #{ ticket_ref [ "owner" ] } /#{ ticket_ref [ "repo" ] } !
92
83
"""
93
84
94
- with { :ok , response } <-
95
- Github . create_issue_comment (
85
+ ensure_command_response ( token , ticket_ref , command_id , command_source , ticket , body )
86
+ end
87
+ end
88
+
89
+ defp ensure_command_response ( token , ticket_ref , command_id , command_source , ticket , body ) do
90
+ case Workspace . fetch_command_response ( "github" , command_id , command_source ) do
91
+ { :ok , response } ->
92
+ case Github . update_issue_comment (
96
93
token ,
97
94
ticket_ref [ "owner" ] ,
98
95
ticket_ref [ "repo" ] ,
99
- ticket_ref [ "number" ] ,
96
+ response . provider_response_id ,
100
97
body
101
98
) 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
99
+ { :ok , _comment } ->
100
+ try_update_command_response ( response , body )
101
+
102
+ { :error , "404 Not Found" } ->
103
+ with { :ok , _ } <- Workspace . delete_command_response ( response . id ) do
104
+ post_response ( token , ticket_ref , command_id , command_source , ticket , body )
105
+ end
106
+
107
+ { :error , reason } ->
108
+ Logger . error ( "Failed to update command response #{ response . id } with body #{ body } " )
109
+ { :error , reason }
110
+ end
111
+
112
+ { :error , _reason } ->
113
+ post_response ( token , ticket_ref , command_id , command_source , ticket , body )
114
+ end
115
+ end
116
+
117
+ defp post_response ( token , ticket_ref , command_id , command_source , ticket , body ) do
118
+ with { :ok , comment } <-
119
+ Github . create_issue_comment ( token , ticket_ref [ "owner" ] , ticket_ref [ "repo" ] , ticket_ref [ "number" ] , body ) do
120
+ create_command_response ( comment , command_source , command_id , ticket . id )
121
+ end
122
+ end
123
+
124
+ defp create_command_response ( comment , command_source , command_id , ticket_id ) do
125
+ % CommandResponse { }
126
+ |> CommandResponse . changeset ( % {
127
+ provider: "github" ,
128
+ provider_meta: Util . normalize_struct ( comment ) ,
129
+ provider_command_id: to_string ( command_id ) ,
130
+ provider_response_id: to_string ( comment [ "id" ] ) ,
131
+ command_source: command_source ,
132
+ command_type: :bounty ,
133
+ ticket_id: ticket_id
134
+ } )
135
+ |> Repo . insert ( )
136
+ end
137
+
138
+ defp try_update_command_response ( command_response , body ) do
139
+ case command_response
140
+ |> CommandResponse . changeset ( % { provider_meta: Util . normalize_struct ( body ) } )
141
+ |> Repo . update ( ) do
142
+ { :ok , command_response } ->
143
+ { :ok , command_response }
144
+
145
+ { :error , _reason } ->
146
+ Logger . error ( "Failed to update command response #{ command_response . id } " )
147
+ { :ok , command_response }
114
148
end
115
149
end
116
150
end
0 commit comments