-
Notifications
You must be signed in to change notification settings - Fork 5
/
jira.go
670 lines (585 loc) · 21.2 KB
/
jira.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package jira
import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
jira "github.com/andygrunwald/go-jira/v2/cloud"
gogh "github.com/google/go-github/v48/github"
"github.com/sirupsen/logrus"
"github.com/uwu-tools/gh-jira-issue-sync/internal/config"
"github.com/uwu-tools/gh-jira-issue-sync/internal/github"
synchttp "github.com/uwu-tools/gh-jira-issue-sync/internal/http"
"github.com/uwu-tools/gh-jira-issue-sync/internal/jira/auth"
"github.com/uwu-tools/gh-jira-issue-sync/internal/options"
)
const (
// commentDateFormat is the format used in the headers of JIRA comments.
commentDateFormat = "15:04 PM, January 2 2006"
// maxJQLIssueLength is the maximum number of GitHub issues we can
// use before we need to stop using JQL and filter issues ourself.
maxJQLIssueLength = 100
// maxIssueSearchResults is the maximum number of items that a page can
// return. Each operation can have a different limit for the number of items
// returned, and these limits may change without notice. To find the maximum
// number of items that an operation could return, set maxResults to a large
// number—for example, over 1000—and if the returned value of maxResults is
// less than the requested value, the returned value is the maximum.
//
// ref: https://developer.atlassian.com/cloud/jira/platform/rest/v2/intro/#pagination
maxIssueSearchResults = 1000
)
// getErrorBody reads the HTTP response body of a JIRA API response,
// logs it as an error, and returns an error object with the contents
// of the body. If an error occurs during reading, that error is
// instead printed and returned. This function closes the body for
// further reading.
func getErrorBody(cfg *config.Config, res *jira.Response) error {
log := cfg.GetLogger()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Errorf("Error occurred trying to read error body: %+v", err)
return fmt.Errorf("reading error body: %w", err)
}
log.Debugf("Error body: %+v", body)
return fmt.Errorf("reading error body: %s", string(body)) //nolint:goerr113
}
// Client is a wrapper around the JIRA API clients library we
// use. It allows us to hide implementation details such as backoff
// as well as swap in other implementations, such as for dry run
// or test mocking.
type Client interface {
ListIssues(ids []int) ([]jira.Issue, error)
GetIssue(key string) (*jira.Issue, error)
// TODO: Remove unnecessary return values; consider only returning error
CreateIssue(issue *jira.Issue) (jira.Issue, error)
// TODO: Remove unnecessary return values; consider only returning error
UpdateIssue(issue *jira.Issue) (jira.Issue, error)
// TODO: Remove unnecessary return values; consider only returning error
CreateComment(
issue *jira.Issue, comment *gogh.IssueComment, githubClient github.Client,
) (jira.Comment, error)
// TODO: Remove unnecessary return values; consider only returning error
// TODO: Re-arrange arguments
UpdateComment(
issue *jira.Issue, id string, comment *gogh.IssueComment, githubClient github.Client,
) (jira.Comment, error)
}
// New creates a new Client and configures it with
// the config object provided. The type of clients created depends
// on the configuration; currently, it creates either a standard
// clients, or a dry-run clients.
func New(cfg *config.Config) (Client, error) {
log := cfg.GetLogger()
var j Client
var tp http.Client
var err error
if !cfg.IsBasicAuth() {
oauth, err := auth.NewJiraHTTPClient(cfg)
if err != nil {
log.Errorf("Error getting OAuth config: %+v", err)
return nil, fmt.Errorf("initializing Jira client: %w", err)
}
tp = *oauth
} else {
basicAuth := jira.BasicAuthTransport{
Username: cfg.GetConfigString(options.ConfigKeyJiraUser),
APIToken: strings.TrimSpace(cfg.GetConfigString(options.ConfigKeyJiraPassword)),
}
tp.Transport = &basicAuth
}
client, err := jira.NewClient(strings.TrimSpace(cfg.GetConfigString(options.ConfigKeyJiraURI)), &tp)
if err != nil {
log.Errorf("Error initializing JIRA clients; check your base URI. Error: %+v", err)
return nil, fmt.Errorf("initializing Jira client: %w", err)
}
log.Debug("JIRA clients initialized")
err = cfg.LoadJIRAConfig(client)
if err != nil {
return nil, fmt.Errorf("loading Jira configuration: %w", err)
}
if cfg.IsDryRun() {
j = &dryrunJIRAClient{
cfg: cfg,
client: client,
}
} else {
j = &realJIRAClient{
cfg: cfg,
client: client,
}
}
return j, nil
}
func getJQLQuery(projectKey, fieldID string, ids []int) string {
idStrs := make([]string, len(ids))
for i, v := range ids {
idStrs[i] = fmt.Sprint(v)
}
// If the list of IDs is too long, we get a 414 Request-URI Too Large, so in that case,
// we'll need to do the filtering ourselves.
var jql string
if len(ids) < maxJQLIssueLength {
jql = fmt.Sprintf(
"project='%s' AND cf[%s] in (%s)",
projectKey,
fieldID,
strings.Join(idStrs, ","),
)
} else {
jql = fmt.Sprintf("project='%s'", projectKey)
}
logrus.Debugf("JQL query used: %s", jql)
return jql
}
// realJIRAClient is a standard JIRA clients, which actually makes
// of the requests against the JIRA REST API. It is the canonical
// implementation of JIRAClient.
type realJIRAClient struct {
cfg *config.Config
client *jira.Client
}
// ListIssues returns a list of JIRA issues on the configured project which
// have GitHub IDs in the provided list. `ids` should be a comma-separated
// list of GitHub IDs.
func (j *realJIRAClient) ListIssues(ids []int) ([]jira.Issue, error) {
log := j.cfg.GetLogger()
jql := getJQLQuery(
j.cfg.GetProjectKey(),
j.cfg.GetFieldID(config.GitHubID),
ids,
)
// TODO(backoff): Consider restoring backoff logic here
// TODO(j-v2): Parameterize all query options
searchOpts := &jira.SearchOptions{
MaxResults: maxIssueSearchResults,
}
jiraIssues, res, err := j.client.Issue.Search(j.cfg.Context(), jql, searchOpts)
if err != nil {
log.Errorf("Error retrieving JIRA issues: %+v", err)
return nil, getErrorBody(j.cfg, res)
}
var issues []jira.Issue
if len(ids) < maxJQLIssueLength {
// The issues were already filtered by our JQL, so use as is
issues = jiraIssues
} else {
// Filter only issues which have a defined GitHub ID in the list of IDs
for _, v := range jiraIssues {
if id, err := v.Fields.Unknowns.Int(j.cfg.GetFieldKey(config.GitHubID)); err == nil {
for _, idOpt := range ids {
if id == int64(idOpt) {
issues = append(issues, v)
break
}
}
}
}
}
return issues, nil
}
// GetIssue returns a single JIRA issue within the configured project
// according to the issue key (e.g. "PROJ-13").
func (j *realJIRAClient) GetIssue(key string) (*jira.Issue, error) {
log := j.cfg.GetLogger()
i, res, err := j.request(func() (interface{}, *jira.Response, error) {
// TODO(j-v2): Add query options
return j.client.Issue.Get(j.cfg.Context(), key, nil) //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error retrieving JIRA issue: %+v", err)
return nil, getErrorBody(j.cfg, res)
}
issue, ok := i.(*jira.Issue)
if !ok {
log.Errorf("Get JIRA issue did not return issue! Got %v", i)
return nil, fmt.Errorf("get JIRA issue failed: expected *jira.Issue; got %T", i) //nolint:goerr113
}
return issue, nil
}
// CreateIssue creates a new JIRA issue according to the fields provided in
// the provided issue object. It returns the created issue, with all the
// fields provided (including e.g. ID and Key).
func (j *realJIRAClient) CreateIssue(issue *jira.Issue) (jira.Issue, error) {
log := j.cfg.GetLogger()
i, res, err := j.request(func() (interface{}, *jira.Response, error) {
return j.client.Issue.Create(j.cfg.Context(), issue) //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error creating JIRA issue: %+v", err)
return jira.Issue{}, getErrorBody(j.cfg, res)
}
is, ok := i.(*jira.Issue)
if !ok {
log.Errorf("Create JIRA issue did not return issue! Got: %v", i)
return jira.Issue{}, fmt.Errorf("create JIRA issue failed: expected *jira.Issue; got %T", i) //nolint:goerr113
}
return *is, nil
}
// UpdateIssue updates a given issue (identified by the Key field of the provided
// issue object) with the fields on the provided issue. It returns the updated
// issue as it exists on JIRA.
func (j *realJIRAClient) UpdateIssue(issue *jira.Issue) (jira.Issue, error) {
log := j.cfg.GetLogger()
i, res, err := j.request(func() (interface{}, *jira.Response, error) {
// TODO(j-v2): Add query options
return j.client.Issue.Update(j.cfg.Context(), issue, nil) //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error updating JIRA issue %s: %v", issue.Key, err)
return jira.Issue{}, getErrorBody(j.cfg, res)
}
is, ok := i.(*jira.Issue)
if !ok {
log.Errorf("Update JIRA issue did not return issue! Got: %v", i)
return jira.Issue{}, fmt.Errorf("update JIRA issue failed: expected *jira.Issue; got %T", i) //nolint:goerr113
}
return *is, nil
}
// maxBodyLength is the maximum length of a JIRA comment body, which is currently
// 2^15-1.
const maxBodyLength = 1 << 15
// CreateComment adds a comment to the provided JIRA issue using the fields from
// the provided GitHub comment. It then returns the created comment.
func (j *realJIRAClient) CreateComment(
issue *jira.Issue,
comment *gogh.IssueComment,
githubClient github.Client,
) (jira.Comment, error) {
log := j.cfg.GetLogger()
user, err := githubClient.GetUser(comment.User.GetLogin())
if err != nil {
return jira.Comment{}, fmt.Errorf("getting GitHub user: %w", err)
}
body := fmt.Sprintf("Comment [(ID %d)|%s]", comment.GetID(), comment.GetHTMLURL())
body = fmt.Sprintf("%s from GitHub user [%s|%s]", body, user.GetLogin(), user.GetHTMLURL())
if user.GetName() != "" {
body = fmt.Sprintf("%s (%s)", body, user.GetName())
}
body = fmt.Sprintf(
"%s at %s:\n\n%s",
body,
comment.CreatedAt.Format(commentDateFormat),
comment.GetBody(),
)
if len(body) > maxBodyLength {
body = body[:maxBodyLength]
}
jComment := jira.Comment{
Body: body,
}
com, res, err := j.request(func() (interface{}, *jira.Response, error) {
return j.client.Issue.AddComment(j.cfg.Context(), issue.ID, &jComment) //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error creating JIRA comment on issue %s. Error: %v", issue.Key, err)
return jira.Comment{}, getErrorBody(j.cfg, res)
}
co, ok := com.(*jira.Comment)
if !ok {
log.Errorf("Create JIRA comment did not return comment! Got: %v", com)
return jira.Comment{}, fmt.Errorf("create JIRA comment failed: expected *jira.Comment; got %T", com) //nolint:goerr113
}
return *co, nil
}
// UpdateComment updates a comment (identified by the `id` parameter) on a given
// JIRA with a new body from the fields of the given GitHub comment. It returns
// the updated comment.
func (j *realJIRAClient) UpdateComment(
issue *jira.Issue,
id string,
comment *gogh.IssueComment,
githubClient github.Client,
) (jira.Comment, error) {
log := j.cfg.GetLogger()
user, err := githubClient.GetUser(comment.User.GetLogin())
if err != nil {
return jira.Comment{}, fmt.Errorf("getting GitHub user: %w", err)
}
body := fmt.Sprintf("Comment [(ID %d)|%s]", comment.GetID(), comment.GetHTMLURL())
body = fmt.Sprintf("%s from GitHub user [%s|%s]", body, user.GetLogin(), user.GetHTMLURL())
if user.GetName() != "" {
body = fmt.Sprintf("%s (%s)", body, user.GetName())
}
body = fmt.Sprintf(
"%s at %s:\n\n%s",
body,
comment.CreatedAt.Format(commentDateFormat),
comment.GetBody(),
)
if len(body) > maxBodyLength {
body = body[:maxBodyLength]
}
// As it is, the JIRA API we're using doesn't have any way to update comments natively.
// So, we have to build the request ourselves.
request := struct {
Body string `json:"body"`
}{
Body: body,
}
req, err := j.client.NewRequest(
j.cfg.Context(),
"PUT",
fmt.Sprintf("rest/api/2/issue/%s/comment/%s", issue.Key, id),
request,
)
if err != nil {
log.Errorf("Error creating comment update request: %s", err)
return jira.Comment{}, fmt.Errorf("creating comment update request: %w", err)
}
com, res, err := j.request(func() (interface{}, *jira.Response, error) {
res, err := j.client.Do(req, nil)
return nil, res, err //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error updating comment: %+v", err)
return jira.Comment{}, getErrorBody(j.cfg, res)
}
co, ok := com.(*jira.Comment)
if !ok {
log.Errorf("Update JIRA comment did not return comment! Got: %v", com)
return jira.Comment{}, fmt.Errorf("update JIRA comment failed: expected *jira.Comment; got %T", com) //nolint:goerr113
}
return *co, nil
}
// request executes a Jira request with exponential backoff, using the real
// client.
func (j *realJIRAClient) request(f func() (interface{}, *jira.Response, error)) (interface{}, *jira.Response, error) {
ret, resp, err := synchttp.NewJiraRequest(f, j.cfg.GetLogger(), j.cfg.GetTimeout())
if err != nil {
return ret, resp, fmt.Errorf("request error: %w", err)
}
return ret, resp, nil
}
// dryrunJIRAClient is an implementation of JIRAClient which performs all
// GET requests the same as the realJIRAClient, but does not perform any
// unsafe requests which may modify server data, instead printing out the
// actions it is asked to perform without making the request.
type dryrunJIRAClient struct {
cfg *config.Config
client *jira.Client
}
// newlineReplaceRegex is a regex to match both "\r\n" and just "\n" newline styles,
// in order to allow us to escape both sequences cleanly in the output of a dry run.
var newlineReplaceRegex = regexp.MustCompile("\r?\n")
// truncate is a utility function to replace all the newlines in
// the string with the characters "\n", then truncate it to no
// more than 50 characters.
func truncate(s string, length int) string {
if s == "" {
return "empty"
}
s = newlineReplaceRegex.ReplaceAllString(s, "\\n")
if len(s) <= length {
return s
}
return fmt.Sprintf("%s...", s[0:length])
}
// ListIssues returns a list of JIRA issues on the configured project which
// have GitHub IDs in the provided list. `ids` should be a comma-separated
// list of GitHub IDs.
//
// This function is identical to that in realJIRAClient.
func (j *dryrunJIRAClient) ListIssues(ids []int) ([]jira.Issue, error) {
log := j.cfg.GetLogger()
jql := getJQLQuery(
j.cfg.GetProjectKey(),
j.cfg.GetFieldID(config.GitHubID),
ids,
)
ji, res, err := j.request(func() (interface{}, *jira.Response, error) {
// TODO(j-v2): Add query options
return j.client.Issue.Search(j.cfg.Context(), jql, nil) //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error retrieving JIRA issues: %+v", err)
return nil, getErrorBody(j.cfg, res)
}
jiraIssues, ok := ji.([]jira.Issue)
if !ok {
log.Errorf("Get JIRA issues did not return issues! Got: %v", ji)
return nil, fmt.Errorf("get JIRA issues failed: expected []jira.Issue; got %T", ji) //nolint:goerr113
}
var issues []jira.Issue
if len(ids) < maxJQLIssueLength {
// The issues were already filtered by our JQL, so use as is
issues = jiraIssues
} else {
// Filter only issues which have a defined GitHub ID in the list of IDs
for _, v := range jiraIssues {
if id, err := v.Fields.Unknowns.Int(j.cfg.GetFieldKey(config.GitHubID)); err == nil {
for _, idOpt := range ids {
if id == int64(idOpt) {
issues = append(issues, v)
break
}
}
}
}
}
return issues, nil
}
// GetIssue returns a single JIRA issue within the configured project
// according to the issue key (e.g. "PROJ-13").
//
// This function is identical to that in realJIRAClient.
func (j *dryrunJIRAClient) GetIssue(key string) (*jira.Issue, error) {
log := j.cfg.GetLogger()
i, res, err := j.request(func() (interface{}, *jira.Response, error) {
// TODO(j-v2): Add query options
return j.client.Issue.Get(j.cfg.Context(), key, nil) //nolint:wrapcheck
})
if err != nil {
log.Errorf("Error retrieving JIRA issue: %+v", err)
return nil, getErrorBody(j.cfg, res)
}
issue, ok := i.(*jira.Issue)
if !ok {
log.Errorf("Get JIRA issue did not return issue! Got %v", i)
return nil, fmt.Errorf("get JIRA issue failed: expected *jira.Issue; got %T", i) //nolint:goerr113
}
return issue, nil
}
// CreateIssue prints out the fields that would be set on a new issue were
// it to be created according to the provided issue object. It returns the
// provided issue object as-is.
func (j *dryrunJIRAClient) CreateIssue(issue *jira.Issue) (jira.Issue, error) {
log := j.cfg.GetLogger()
fields := issue.Fields
log.Info("")
log.Info("Create new JIRA issue:")
log.Infof(" Summary: %s", fields.Summary)
log.Infof(" Description: %s", truncate(fields.Description, 50))
log.Infof(" GitHub ID: %d", fields.Unknowns[j.cfg.GetFieldKey(config.GitHubID)])
log.Infof(" GitHub Number: %d", fields.Unknowns[j.cfg.GetFieldKey(config.GitHubNumber)])
log.Infof(" Labels: %s", fields.Unknowns[j.cfg.GetFieldKey(config.GitHubLabels)])
log.Infof(" State: %s", fields.Unknowns[j.cfg.GetFieldKey(config.GitHubStatus)])
log.Infof(" Reporter: %s", fields.Unknowns[j.cfg.GetFieldKey(config.GitHubReporter)])
log.Info("")
return *issue, nil
}
// UpdateIssue prints out the fields that would be set on a JIRA issue
// (identified by issue.Key) were it to be updated according to the issue
// object. It then returns the provided issue object as-is.
func (j *dryrunJIRAClient) UpdateIssue(issue *jira.Issue) (jira.Issue, error) {
log := j.cfg.GetLogger()
fields := issue.Fields
log.Info("")
log.Infof("Update JIRA issue %s:", issue.Key)
log.Infof(" Summary: %s", fields.Summary)
log.Infof(" Description: %s", truncate(fields.Description, 50))
key := j.cfg.GetFieldKey(config.GitHubLabels)
if labels, err := fields.Unknowns.String(key); err == nil {
log.Infof(" Labels: %s", labels)
}
key = j.cfg.GetFieldKey(config.GitHubStatus)
if state, err := fields.Unknowns.String(key); err == nil {
log.Infof(" State: %s", state)
}
log.Info("")
return *issue, nil
}
// CreateComment prints the body that would be set on a new comment if it were
// to be created according to the fields of the provided GitHub comment. It then
// returns a comment object containing the body that would be used.
func (j *dryrunJIRAClient) CreateComment(
issue *jira.Issue,
comment *gogh.IssueComment,
githubClient github.Client,
) (jira.Comment, error) {
log := j.cfg.GetLogger()
user, err := githubClient.GetUser(comment.User.GetLogin())
if err != nil {
return jira.Comment{}, fmt.Errorf("getting GitHub user: %w", err)
}
body := fmt.Sprintf("Comment (ID %d) from GitHub user %s", comment.GetID(), user.GetLogin())
if user.GetName() != "" {
body = fmt.Sprintf("%s (%s)", body, user.GetName())
}
body = fmt.Sprintf(
"%s at %s:\n\n%s",
body,
comment.CreatedAt.Format(commentDateFormat),
comment.GetBody(),
)
log.Info("")
log.Infof("Create comment on JIRA issue %s:", issue.Key)
log.Infof(" GitHub ID: %d", comment.GetID())
if user.GetName() != "" {
log.Infof(" User: %s (%s)", user.GetLogin(), user.GetName())
} else {
log.Infof(" User: %s", user.GetLogin())
}
log.Infof(" Posted at: %s", comment.CreatedAt.Format(commentDateFormat))
log.Infof(" Body: %s", truncate(comment.GetBody(), 100))
log.Info("")
return jira.Comment{
Body: body,
}, nil
}
// UpdateComment prints the body that would be set on a comment were it to be
// updated according to the provided GitHub comment. It then returns a comment
// object containing the body that would be used.
func (j *dryrunJIRAClient) UpdateComment(
issue *jira.Issue,
id string,
comment *gogh.IssueComment,
githubClient github.Client,
) (jira.Comment, error) {
log := j.cfg.GetLogger()
user, err := githubClient.GetUser(comment.User.GetLogin())
if err != nil {
return jira.Comment{}, fmt.Errorf("getting GitHub user: %w", err)
}
body := fmt.Sprintf("Comment (ID %d) from GitHub user %s", comment.GetID(), user.GetLogin())
if user.GetName() != "" {
body = fmt.Sprintf("%s (%s)", body, user.GetName())
}
body = fmt.Sprintf(
"%s at %s:\n\n%s",
body,
comment.CreatedAt.Format(commentDateFormat),
comment.GetBody(),
)
log.Info("")
log.Infof("Update JIRA comment %s on issue %s:", id, issue.Key)
log.Infof(" GitHub ID: %d", comment.GetID())
if user.GetName() != "" {
log.Infof(" User: %s (%s)", user.GetLogin(), user.GetName())
} else {
log.Infof(" User: %s", user.GetLogin())
}
log.Infof(" Posted at: %s", comment.CreatedAt.Format(commentDateFormat))
log.Infof(" Body: %s", truncate(comment.GetBody(), 100))
log.Info("")
return jira.Comment{
ID: id,
Body: body,
}, nil
}
// request executes a Jira request with exponential backoff, using the dry-run
// client.
func (j *dryrunJIRAClient) request(f func() (interface{}, *jira.Response, error)) (interface{}, *jira.Response, error) {
ret, resp, err := synchttp.NewJiraRequest(f, j.cfg.GetLogger(), j.cfg.GetTimeout())
if err != nil {
return ret, resp, fmt.Errorf("request error: %w", err)
}
return ret, resp, nil
}