4
4
"bytes"
5
5
"fmt"
6
6
"io/ioutil"
7
- "net/http"
8
7
"reflect"
9
8
"strings"
10
9
"time"
@@ -24,11 +23,14 @@ type Receiver struct {
24
23
25
24
// NewReceiver creates a Receiver using the provided configuration and template.
26
25
func NewReceiver (c * ReceiverConfig , t * Template ) (* Receiver , error ) {
27
- client , err := jira .NewClient (http .DefaultClient , c .APIURL )
26
+ tp := jira.BasicAuthTransport {
27
+ Username : c .User ,
28
+ Password : string (c .Password ),
29
+ }
30
+ client , err := jira .NewClient (tp .Client (), c .APIURL )
28
31
if err != nil {
29
32
return nil , err
30
33
}
31
- client .Authentication .SetBasicAuth (c .User , string (c .Password ))
32
34
33
35
return & Receiver {conf : c , tmpl : t , client : client }, nil
34
36
}
@@ -62,14 +64,8 @@ func (r *Receiver) Notify(data *alertmanager.Data) (bool, error) {
62
64
return false , nil
63
65
}
64
66
65
- reopenDuration , err := time .ParseDuration (r .conf .ReopenDuration )
66
- if err != nil {
67
- log .Errorf ("Unable to parse reopen duration: %v" , err )
68
- return false , err
69
- }
70
-
71
67
resolutionTime := time .Time (issue .Fields .Resolutiondate )
72
- if resolutionTime .Add (reopenDuration ).After (time .Now ()) {
68
+ if resolutionTime .Add (time . Duration ( * r . conf . ReopenDuration ) ).After (time .Now ()) {
73
69
log .Infof ("Issue %s for %s was resolved on %s, reopening" , issue .Key , issueLabel , resolutionTime .Format (time .UnixDate ))
74
70
return r .reopen (issue .Key )
75
71
}
@@ -96,7 +92,7 @@ func (r *Receiver) Notify(data *alertmanager.Data) (bool, error) {
96
92
if len (r .conf .Components ) > 0 {
97
93
issue .Fields .Components = make ([]* jira.Component , 0 , len (r .conf .Components ))
98
94
for _ , component := range r .conf .Components {
99
- issue .Fields .Components = append (issue .Fields .Components , & jira.Component {Name : component })
95
+ issue .Fields .Components = append (issue .Fields .Components , & jira.Component {Name : r . tmpl . Execute ( component , data ) })
100
96
}
101
97
}
102
98
@@ -178,7 +174,7 @@ func (r *Receiver) search(project, issueLabel string) (*jira.Issue, bool, error)
178
174
query := fmt .Sprintf ("project=\" %s\" and labels=%q order by key" , project , issueLabel )
179
175
options := & jira.SearchOptions {
180
176
Fields : []string {"summary" , "status" , "resolution" , "resolutiondate" },
181
- MaxResults : 50 ,
177
+ MaxResults : 2 ,
182
178
}
183
179
log .V (1 ).Infof ("search: query=%v options=%+v" , query , options )
184
180
issues , resp , err := r .client .Issue .Search (query , options )
@@ -188,13 +184,12 @@ func (r *Receiver) search(project, issueLabel string) (*jira.Issue, bool, error)
188
184
}
189
185
if len (issues ) > 0 {
190
186
if len (issues ) > 1 {
191
- // Swallow it, but log an error .
187
+ // Swallow it, but log a message .
192
188
log .Infof ("More than one issue matched %s, will only update last issue: %+v" , query , issues )
193
189
}
194
190
195
- lastIndex := len (issues ) - 1
196
- log .V (1 ).Infof (" found: %+v" , issues [lastIndex ])
197
- return & issues [lastIndex ], false , nil
191
+ log .V (1 ).Infof (" found: %+v" , issues [0 ])
192
+ return & issues [0 ], false , nil
198
193
}
199
194
log .V (1 ).Infof (" no results" )
200
195
return nil , false , nil
@@ -221,10 +216,11 @@ func (r *Receiver) reopen(issueKey string) (bool, error) {
221
216
222
217
func (r * Receiver ) create (issue * jira.Issue ) (bool , error ) {
223
218
log .V (1 ).Infof ("create: issue=%v" , * issue )
224
- issue , resp , err := r .client .Issue .Create (issue )
219
+ newIssue , resp , err := r .client .Issue .Create (issue )
225
220
if err != nil {
226
221
return handleJiraError ("Issue.Create" , resp , err )
227
222
}
223
+ * issue = * newIssue
228
224
229
225
log .V (1 ).Infof (" done: key=%s ID=%s" , issue .Key , issue .ID )
230
226
return false , nil
0 commit comments