-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathclient.go
More file actions
1034 lines (960 loc) · 34 KB
/
client.go
File metadata and controls
1034 lines (960 loc) · 34 KB
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 The Kubernetes Authors.
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.
*/
package bugzilla
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/prow/pkg/version"
)
const (
methodField = "method"
)
type Client interface {
Endpoint() string
// GetBug retrieves a Bug from the server
GetBug(id int) (*Bug, error)
// GetComments gets a list of comments for a specific bug ID.
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/comment.html#get-comments
GetComments(id int) ([]Comment, error)
// GetExternalBugPRsOnBug retrieves external bugs on a Bug from the server
// and returns any that reference a Pull Request in GitHub
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#get-bug
GetExternalBugPRsOnBug(id int) ([]ExternalBug, error)
// GetSubComponentsOnBug retrieves a the list of SubComponents of the bug.
// SubComponents are a Red Hat bugzilla specific extra field.
GetSubComponentsOnBug(id int) (map[string][]string, error)
// GetClones gets the list of bugs that the provided bug blocks that also have a matching summary.
GetClones(bug *Bug) ([]*Bug, error)
// CreateBug creates a new bug on the server.
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#create-bug
CreateBug(bug *BugCreate) (int, error)
// CreateComment creates a new bug on the server.
// https://bugzilla.redhat.com/docs/en/html/api/core/v1/comment.html#create-comments
CreateComment(bug *CommentCreate) (int, error)
// CloneBug clones a bug by creating a new bug with the same fields, copying the description, and updating the bug to depend on the original bug
CloneBug(bug *Bug, mutations ...func(bug *BugCreate)) (int, error)
// UpdateBug updates the fields of a bug on the server
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#update-bug
UpdateBug(id int, update BugUpdate) error
// AddPullRequestAsExternalBug attempts to add a PR to the external tracker list.
// External bugs are assumed to fall under the type identified by their hostname,
// so we will provide https://github.com/ here for the URL identifier. We return
// any error as well as whether a change was actually made.
// This will be done via JSONRPC:
// https://bugzilla.redhat.com/docs/en/html/integrating/api/Bugzilla/Extension/ExternalBugs/WebService.html#add-external-bug
AddPullRequestAsExternalBug(id int, org, repo string, num int) (bool, error)
// RemovePullRequestAsExternalBug attempts to remove a PR from the external tracker list.
// External bugs are assumed to fall under the type identified by their hostname,
// so we will provide https://github.com/ here for the URL identifier. We return
// any error as well as whether a change was actually made.
// This will be done via JSONRPC:
// https://bugzilla.redhat.com/docs/en/html/integrating/api/Bugzilla/Extension/ExternalBugs/WebService.html#remove-external-bug
RemovePullRequestAsExternalBug(id int, org, repo string, num int) (bool, error)
// GetAllClones returns all the clones of the bug including itself
// Differs from GetClones as GetClones only gets the child clones which are one level lower
GetAllClones(bug *Bug) ([]*Bug, error)
// GetRootForClone returns the original bug.
GetRootForClone(bug *Bug) (*Bug, error)
// SetRoundTripper sets a custom implementation of RoundTripper as the Transport for http.Client
SetRoundTripper(t http.RoundTripper)
// ForPlugin and ForSubcomponent allow for the logger used in the client
// to be created in a more specific manner when spawning parallel workers
ForPlugin(plugin string) Client
ForSubcomponent(subcomponent string) Client
WithFields(fields logrus.Fields) Client
Used() bool
// SearchBugs returns all bugs that meet the given criteria
SearchBugs(filters map[string]string) ([]*Bug, error)
}
// NewClient returns a bugzilla client.
func NewClient(getAPIKey func() []byte, endpoint string, githubExternalTrackerId uint, authMethod string) Client {
return &client{
logger: logrus.WithField("client", "bugzilla"),
delegate: &delegate{
client: &http.Client{},
endpoint: endpoint,
githubExternalTrackerId: githubExternalTrackerId,
getAPIKey: getAPIKey,
authMethod: authMethod,
},
}
}
// SetRoundTripper sets the Transport in http.Client to a custom RoundTripper
func (c *client) SetRoundTripper(t http.RoundTripper) {
c.client.Transport = t
}
// newBugDetailsCache is a constructor for bugDetailsCache
func newBugDetailsCache() *bugDetailsCache {
return &bugDetailsCache{cache: map[int]Bug{}}
}
// bugDetailsCache holds the already retrieved bug details
type bugDetailsCache struct {
cache map[int]Bug
lock sync.Mutex
}
// get retrieves bug details from the cache and is thread safe
func (bd *bugDetailsCache) get(key int) (bug Bug, exists bool) {
bd.lock.Lock()
defer bd.lock.Unlock()
entry, ok := bd.cache[key]
return entry, ok
}
// set stores the bug details in the cache and is thread safe
func (bd *bugDetailsCache) set(key int, value Bug) {
bd.lock.Lock()
defer bd.lock.Unlock()
bd.cache[key] = value
}
// list returns a slice of all bugs in the cache
func (bd *bugDetailsCache) list() []Bug {
bd.lock.Lock()
defer bd.lock.Unlock()
result := make([]Bug, 0, len(bd.cache))
for _, bug := range bd.cache {
result = append(result, bug)
}
return result
}
// client interacts with the Bugzilla api.
type client struct {
// If logger is non-nil, log all method calls with it.
logger *logrus.Entry
// identifier is used to add more identification to the user-agent header
identifier string
used bool
*delegate
}
// Used determines whether the client has been used
func (c *client) Used() bool {
return c.used
}
// ForPlugin clones the client, keeping the underlying delegate the same but adding
// a plugin identifier and log field
func (c *client) ForPlugin(plugin string) Client {
return c.forKeyValue("plugin", plugin)
}
// ForSubcomponent clones the client, keeping the underlying delegate the same but adding
// an identifier and log field
func (c *client) ForSubcomponent(subcomponent string) Client {
return c.forKeyValue("subcomponent", subcomponent)
}
func (c *client) forKeyValue(key, value string) Client {
return &client{
identifier: value,
logger: c.logger.WithField(key, value),
delegate: c.delegate,
}
}
func (c *client) userAgent() string {
if c.identifier != "" {
return version.UserAgentWithIdentifier(c.identifier)
}
return version.UserAgent()
}
// WithFields clones the client, keeping the underlying delegate the same but adding
// fields to the logging context
func (c *client) WithFields(fields logrus.Fields) Client {
return &client{
logger: c.logger.WithFields(fields),
delegate: c.delegate,
}
}
// delegate actually does the work to talk to Bugzilla
type delegate struct {
client *http.Client
endpoint string
githubExternalTrackerId uint
getAPIKey func() []byte
authMethod string
}
// the client is a Client impl
var _ Client = &client{}
func (c *client) Endpoint() string {
return c.endpoint
}
// GetBug retrieves a Bug from the server
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#get-bug
func (c *client) GetBug(id int) (*Bug, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "GetBug", "id": id})
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/rest/bug/%d", c.endpoint, id), nil)
if err != nil {
return nil, err
}
values := req.URL.Query()
values.Add("include_fields", "_default")
// redhat bugzilla docs claim that flags are a default field, but they are actually not returned unless added to include_fields
values.Add("include_fields", "flags")
req.URL.RawQuery = values.Encode()
raw, err := c.request(req, logger)
if err != nil {
return nil, err
}
var parsedResponse struct {
Bugs []*Bug `json:"bugs,omitempty"`
}
if err := json.Unmarshal(raw, &parsedResponse); err != nil {
return nil, fmt.Errorf("could not unmarshal response body: %w", err)
}
if len(parsedResponse.Bugs) != 1 {
return nil, fmt.Errorf("did not get one bug, but %d: %v", len(parsedResponse.Bugs), parsedResponse)
}
return parsedResponse.Bugs[0], nil
}
func getClones(c Client, bug *Bug) ([]*Bug, error) {
var errs []error
clones := []*Bug{}
for _, dependentID := range bug.Blocks {
dependent, err := c.GetBug(dependentID)
if err != nil {
errs = append(errs, fmt.Errorf("Failed to get dependent bug #%d: %w", dependentID, err))
continue
}
if dependent.Summary == bug.Summary {
clones = append(clones, dependent)
}
}
return clones, utilerrors.NewAggregate(errs)
}
// GetClones gets the list of bugs that the provided bug blocks that also have a matching summary.
func (c *client) GetClones(bug *Bug) ([]*Bug, error) {
return getClones(c, bug)
}
// getImmediateParents gets the Immediate parents of bugs with a matching summary
func getImmediateParents(c Client, bug *Bug) ([]*Bug, error) {
var errs []error
parents := []*Bug{}
// One option would be to return as soon as the first parent is found
// ideally that should be enough, although there is a check in the getRootForClone function to verify this
// Logs would need to be monitored to verify this behavior
for _, parentID := range bug.DependsOn {
parent, err := c.GetBug(parentID)
if err != nil {
errs = append(errs, fmt.Errorf("Failed to get parent bug #%d: %w", parentID, err))
continue
}
if parent.Summary == bug.Summary {
parents = append(parents, parent)
}
}
return parents, utilerrors.NewAggregate(errs)
}
func getRootForClone(c Client, bug *Bug) (*Bug, error) {
curr := bug
var errs []error
for len(curr.DependsOn) > 0 {
parent, err := getImmediateParents(c, curr)
if err != nil {
errs = append(errs, err)
}
switch l := len(parent); {
case l <= 0:
return curr, utilerrors.NewAggregate(errs)
case l == 1:
curr = parent[0]
case l > 1:
curr = parent[0]
errs = append(errs, fmt.Errorf("More than one parent found for bug #%d", curr.ID))
}
}
return curr, utilerrors.NewAggregate(errs)
}
// GetRootForClone returns the original bug.
func (c *client) GetRootForClone(bug *Bug) (*Bug, error) {
return getRootForClone(c, bug)
}
// GetAllClones returns all the clones of the bug including itself
// Differs from GetClones as GetClones only gets the child clones which are one level lower
func (c *client) GetAllClones(bug *Bug) ([]*Bug, error) {
bugCache := newBugDetailsCache()
return getAllClones(c, bug, bugCache)
}
func getAllClones(c Client, bug *Bug, bugCache *bugDetailsCache) (clones []*Bug, err error) {
clones = []*Bug{}
bugCache.set(bug.ID, *bug)
err = getAllLinkedBugs(c, bug.ID, bugCache, nil)
if err != nil {
return nil, err
}
cachedBugs := bugCache.list()
for index, node := range cachedBugs {
if node.Summary == bug.Summary {
clones = append(clones, &cachedBugs[index])
}
}
sort.SliceStable(clones, func(i, j int) bool {
return clones[i].ID < clones[j].ID
})
return clones, nil
}
// Parallel implementation for getAllClones - spawns threads to go up and down the tree
// Also parallelizes the getBug calls if bug has multiple bugs in DependsOn/Blocks
func getAllLinkedBugs(c Client, bugID int, bugCache *bugDetailsCache, errGroup *errgroup.Group) error {
var shouldWait bool
if errGroup == nil {
shouldWait = true
errGroup = new(errgroup.Group)
}
bugObj, cacheHasBug := bugCache.get(bugID)
if !cacheHasBug {
bug, err := c.GetBug(bugID)
if err != nil {
return err
}
bugObj = *bug
}
errGroup.Go(func() error {
return traverseUp(c, &bugObj, bugCache, errGroup)
})
errGroup.Go(func() error {
return traverseDown(c, &bugObj, bugCache, errGroup)
})
if shouldWait {
return errGroup.Wait()
}
return nil
}
func traverseUp(c Client, bug *Bug, bugCache *bugDetailsCache, errGroup *errgroup.Group) error {
for _, dependsOnID := range bug.DependsOn {
errGroup.Go(func() error {
_, alreadyFetched := bugCache.get(dependsOnID)
if alreadyFetched {
return nil
}
parent, err := c.GetBug(dependsOnID)
if err != nil {
return err
}
bugCache.set(parent.ID, *parent)
if bug.Summary == parent.Summary {
return getAllLinkedBugs(c, parent.ID, bugCache, errGroup)
}
return nil
})
}
return nil
}
func traverseDown(c Client, bug *Bug, bugCache *bugDetailsCache, errGroup *errgroup.Group) error {
for _, childID := range bug.Blocks {
errGroup.Go(func() error {
_, alreadyFetched := bugCache.get(childID)
if alreadyFetched {
return nil
}
child, err := c.GetBug(childID)
if err != nil {
return err
}
bugCache.set(child.ID, *child)
if bug.Summary == child.Summary {
return getAllLinkedBugs(c, child.ID, bugCache, errGroup)
}
return nil
})
}
return nil
}
// GetSubComponentsOnBug retrieves a the list of SubComponents of the bug.
// SubComponents are a Red Hat bugzilla specific extra field.
func (c *client) GetSubComponentsOnBug(id int) (map[string][]string, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "GetSubComponentsOnBug", "id": id})
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/rest/bug/%d", c.endpoint, id), nil)
if err != nil {
return nil, err
}
values := req.URL.Query()
values.Add("include_fields", "sub_components")
req.URL.RawQuery = values.Encode()
raw, err := c.request(req, logger)
if err != nil {
return nil, err
}
var parsedResponse struct {
Bugs []struct {
SubComponents map[string][]string `json:"sub_components"`
} `json:"bugs"`
}
if err := json.Unmarshal(raw, &parsedResponse); err != nil {
return nil, fmt.Errorf("could not unmarshal response body: %w", err)
}
// if there is no subcomponent, return an empty struct
if len(parsedResponse.Bugs) == 0 {
return map[string][]string{}, nil
}
// make sure there is only 1 bug
if len(parsedResponse.Bugs) != 1 {
return nil, fmt.Errorf("did not get one bug, but %d: %v", len(parsedResponse.Bugs), parsedResponse)
}
return parsedResponse.Bugs[0].SubComponents, nil
}
// GetExternalBugPRsOnBug retrieves external bugs on a Bug from the server
// and returns any that reference a Pull Request in GitHub
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#get-bug
func (c *client) GetExternalBugPRsOnBug(id int) ([]ExternalBug, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "GetExternalBugPRsOnBug", "id": id})
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/rest/bug/%d", c.endpoint, id), nil)
if err != nil {
return nil, err
}
values := req.URL.Query()
values.Add("include_fields", "external_bugs")
req.URL.RawQuery = values.Encode()
raw, err := c.request(req, logger)
if err != nil {
return nil, err
}
var parsedResponse struct {
Bugs []struct {
ExternalBugs []ExternalBug `json:"external_bugs"`
} `json:"bugs"`
}
if err := json.Unmarshal(raw, &parsedResponse); err != nil {
return nil, fmt.Errorf("could not unmarshal response body: %w", err)
}
if len(parsedResponse.Bugs) != 1 {
return nil, fmt.Errorf("did not get one bug, but %d: %v", len(parsedResponse.Bugs), parsedResponse)
}
var prs []ExternalBug
for _, bug := range parsedResponse.Bugs[0].ExternalBugs {
if bug.BugzillaBugID != id {
continue
}
if bug.Type.URL != "https://github.com/" {
// TODO: skuznets: figure out how to honor the endpoints given to the GitHub client to support enterprise here
continue
}
org, repo, num, err := PullFromIdentifier(bug.ExternalBugID)
if IsIdentifierNotForPullErr(err) {
continue
}
if err != nil {
return nil, fmt.Errorf("could not parse external identifier %q as pull: %w", bug.ExternalBugID, err)
}
bug.Org = org
bug.Repo = repo
bug.Num = num
prs = append(prs, bug)
}
return prs, nil
}
// UpdateBug updates the fields of a bug on the server
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#update-bug
func (c *client) UpdateBug(id int, update BugUpdate) error {
logger := c.logger.WithFields(logrus.Fields{methodField: "UpdateBug", "id": id, "update": update})
body, err := json.Marshal(update)
if err != nil {
return fmt.Errorf("failed to marshal update payload: %w", err)
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/rest/bug/%d", c.endpoint, id), bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
_, err = c.request(req, logger)
return err
}
// CreateBug creates a new bug on the server.
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html#create-bug
func (c *client) CreateBug(bug *BugCreate) (int, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "CreateBug", "bug": bug})
body, err := json.Marshal(bug)
if err != nil {
return 0, fmt.Errorf("failed to marshal create payload: %w", err)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/rest/bug", c.endpoint), bytes.NewBuffer(body))
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.request(req, logger)
if err != nil {
return 0, err
}
var idStruct struct {
ID int `json:"id,omitempty"`
}
err = json.Unmarshal(resp, &idStruct)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal server response: %w", err)
}
return idStruct.ID, nil
}
func (c *client) CreateComment(comment *CommentCreate) (int, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "CreateComment", "bug": comment.ID})
body, err := json.Marshal(comment)
if err != nil {
return 0, fmt.Errorf("failed to marshal create payload: %w", err)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/rest/bug/%d/comment", c.endpoint, comment.ID), bytes.NewBuffer(body))
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.request(req, logger)
if err != nil {
return 0, err
}
var idStruct struct {
ID int `json:"id,omitempty"`
}
err = json.Unmarshal(resp, &idStruct)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal server response: %w", err)
}
return idStruct.ID, nil
}
func cloneBugStruct(bug *Bug, subcomponents map[string][]string, comments []Comment) *BugCreate {
newBug := &BugCreate{
Alias: bug.Alias,
AssignedTo: bug.AssignedTo,
CC: bug.CC,
Component: bug.Component,
Flags: bug.Flags,
Groups: bug.Groups,
Keywords: bug.Keywords,
OperatingSystem: bug.OperatingSystem,
Platform: bug.Platform,
Priority: bug.Priority,
Product: bug.Product,
QAContact: bug.QAContact,
Severity: bug.Severity,
Summary: bug.Summary,
TargetMilestone: bug.TargetMilestone,
Version: bug.Version,
}
if len(subcomponents) > 0 {
newBug.SubComponents = subcomponents
}
for _, comment := range comments {
if comment.IsPrivate {
newBug.CommentIsPrivate = true
break
}
}
var newDesc strings.Builder
// The following builds a description comprising all the bug's comments formatted the same way that Bugzilla does on clone
newDesc.WriteString(fmt.Sprintf("+++ This bug was initially created as a clone of Bug #%d +++\n\n", bug.ID))
if len(comments) > 0 {
newDesc.WriteString(comments[0].Text)
}
// This is a standard time layout string for golang, which formats the time `Mon Jan 2 15:04:05 -0700 MST 2006` to the layout we want
bzTimeLayout := "2006-01-02 15:04:05 MST"
for _, comment := range comments[1:] {
// Header
newDesc.WriteString("\n\n--- Additional comment from ")
newDesc.WriteString(comment.Creator)
newDesc.WriteString(" on ")
newDesc.WriteString(comment.Time.UTC().Format(bzTimeLayout))
newDesc.WriteString(" ---\n\n")
// Comment
newDesc.WriteString(comment.Text)
}
newBug.Description = newDesc.String()
// make sure comment isn't above maximum length
if len(newBug.Description) > 65535 {
newBug.Description = fmt.Sprint(newBug.Description[:65532], "...")
}
return newBug
}
// clone handles the bz client calls for the bug cloning process and allows us to share the implementation
// between the real and fake client to prevent bugs from accidental discrepancies between the two.
func clone(c Client, bug *Bug, mutations []func(bug *BugCreate)) (int, error) {
subcomponents, err := c.GetSubComponentsOnBug(bug.ID)
if err != nil {
return 0, fmt.Errorf("failed to check if bug has subcomponents: %w", err)
}
comments, err := c.GetComments(bug.ID)
if err != nil {
return 0, fmt.Errorf("failed to get parent bug's comments: %w", err)
}
newBug := cloneBugStruct(bug, subcomponents, comments)
for _, mutation := range mutations {
mutation(newBug)
}
id, err := c.CreateBug(newBug)
if err != nil {
return id, err
}
bugUpdate := BugUpdate{
DependsOn: &IDUpdate{
Add: []int{bug.ID},
},
Whiteboard: bug.Whiteboard,
}
for _, originalBlocks := range bug.Blocks {
if bugUpdate.Blocks == nil {
bugUpdate.Blocks = &IDUpdate{}
}
bugUpdate.Blocks.Add = append(bugUpdate.Blocks.Add, originalBlocks)
}
err = c.UpdateBug(id, bugUpdate)
return id, err
}
// CloneBug clones a bug by creating a new bug with the same fields, copying the description, and updating the bug to depend on the original bug
func (c *client) CloneBug(bug *Bug, mutations ...func(bug *BugCreate)) (int, error) {
return clone(c, bug, mutations)
}
// GetComments gets a list of comments for a specific bug ID.
// https://bugzilla.readthedocs.io/en/latest/api/core/v1/comment.html#get-comments
func (c *client) GetComments(bugID int) ([]Comment, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "GetComments", "id": bugID})
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/rest/bug/%d/comment", c.endpoint, bugID), nil)
if err != nil {
return nil, err
}
raw, err := c.request(req, logger)
if err != nil {
return nil, err
}
var parsedResponse struct {
Bugs map[int]struct {
Comments []Comment `json:"comments,omitempty"`
} `json:"bugs,omitempty"`
}
if err := json.Unmarshal(raw, &parsedResponse); err != nil {
return nil, fmt.Errorf("could not unmarshal response body: %w", err)
}
if len(parsedResponse.Bugs) != 1 {
return nil, fmt.Errorf("did not get one bug, but %d: %v", len(parsedResponse.Bugs), parsedResponse)
}
return parsedResponse.Bugs[bugID].Comments, nil
}
func (c *client) request(req *http.Request, logger *logrus.Entry) ([]byte, error) {
c.used = true
if apiKey := c.getAPIKey(); len(apiKey) > 0 {
switch c.authMethod {
case "bearer":
req.Header.Set("Authorization", "Bearer "+string(apiKey))
case "query":
values := req.URL.Query()
values.Add("api_key", string(apiKey))
req.URL.RawQuery = values.Encode()
case "x-bugzilla-api-key":
req.Header.Set("X-BUGZILLA-API-KEY", string(apiKey))
default:
// If there is no auth method specified, we use a union of `query` and
// `x-bugzilla-api-key` to mimic the previous default behavior which attempted
// to satisfy different BugZilla server versions.
req.Header.Set("X-BUGZILLA-API-KEY", string(apiKey))
values := req.URL.Query()
values.Add("api_key", string(apiKey))
req.URL.RawQuery = values.Encode()
}
}
if userAgent := c.userAgent(); userAgent != "" {
req.Header.Add("User-Agent", userAgent)
}
start := time.Now()
resp, err := c.client.Do(req)
stop := time.Now()
promLabels := prometheus.Labels(map[string]string{methodField: logger.Data[methodField].(string), "status": ""})
if resp != nil {
promLabels["status"] = strconv.Itoa(resp.StatusCode)
}
requestDurations.With(promLabels).Observe(float64(stop.Sub(start).Seconds()))
if resp != nil {
logger.WithField("response", resp.StatusCode).Debug("Got response from Bugzilla.")
}
if err != nil {
code := -1
if resp != nil {
code = resp.StatusCode
}
return nil, &requestError{statusCode: code, message: err.Error()}
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.WithError(err).Warn("could not close response body")
}
}()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %w", err)
}
var error struct {
Error bool `json:"error"`
Code int `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal(raw, &error); err != nil && len(raw) > 0 {
logger.WithError(err).Debug("could not read response body as error")
}
if error.Error {
return nil, &requestError{statusCode: resp.StatusCode, bugzillaCode: error.Code, message: error.Message}
} else if resp.StatusCode != http.StatusOK {
return nil, &requestError{statusCode: resp.StatusCode, message: fmt.Sprintf("response code %d not %d", resp.StatusCode, http.StatusOK)}
}
return raw, nil
}
type requestError struct {
statusCode int
bugzillaCode int
message string
}
func (e requestError) Error() string {
if e.bugzillaCode != 0 {
return fmt.Sprintf("code %d: %s", e.bugzillaCode, e.message)
}
return e.message
}
func IsNotFound(err error) bool {
reqError, ok := err.(*requestError)
if !ok {
return false
}
return reqError.statusCode == http.StatusNotFound
}
func IsInvalidBugID(err error) bool {
reqError, ok := err.(*requestError)
if !ok {
return false
}
return reqError.bugzillaCode == 101
}
func IsAccessDenied(err error) bool {
reqError, ok := err.(*requestError)
if !ok {
return false
}
if reqError.bugzillaCode == 102 || reqError.statusCode == 401 {
return true
}
return false
}
// AddPullRequestAsExternalBug attempts to add a PR to the external tracker list.
// External bugs are assumed to fall under the type identified by their hostname,
// so we will provide https://github.com/ here for the URL identifier. We return
// any error as well as whether a change was actually made.
// This will be done via JSONRPC:
// https://bugzilla.redhat.com/docs/en/html/integrating/api/Bugzilla/Extension/ExternalBugs/WebService.html#add-external-bug
func (c *client) AddPullRequestAsExternalBug(id int, org, repo string, num int) (bool, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "AddExternalBug", "id": id, "org": org, "repo": repo, "num": num})
pullIdentifier := IdentifierForPull(org, repo, num)
bugIdentifier := ExternalBugIdentifier{
ID: pullIdentifier,
}
if c.githubExternalTrackerId != 0 {
bugIdentifier.TrackerID = int(c.githubExternalTrackerId)
} else {
bugIdentifier.Type = "https://github.com/"
}
rpcPayload := struct {
// Version is the version of JSONRPC to use. All Bugzilla servers
// support 1.0. Some support 1.1 and some support 2.0
Version string `json:"jsonrpc"`
Method string `json:"method"`
// Parameters must be specified in JSONRPC 1.0 as a structure in the first
// index of this slice
Parameters []AddExternalBugParameters `json:"params"`
ID string `json:"id"`
}{
Version: "1.0", // some Bugzilla servers support 2.0 but all support 1.0
Method: "ExternalBugs.add_external_bug",
ID: "identifier", // this is useful when fielding asynchronous responses, but not here
Parameters: []AddExternalBugParameters{{
BugIDs: []int{id},
ExternalBugs: []ExternalBugIdentifier{bugIdentifier},
}},
}
body, err := json.Marshal(rpcPayload)
if err != nil {
return false, fmt.Errorf("failed to marshal JSONRPC payload: %w", err)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/jsonrpc.cgi", c.endpoint), bytes.NewBuffer(body))
if err != nil {
return false, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.request(req, logger)
if err != nil {
return false, err
}
var response struct {
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error,omitempty"`
ID string `json:"id"`
Result *struct {
Bugs []struct {
ID int `json:"id"`
Changes struct {
ExternalBugs struct {
Added string `json:"added"`
Removed string `json:"removed"`
} `json:"ext_bz_bug_map.ext_bz_bug_id"`
} `json:"changes"`
} `json:"bugs"`
} `json:"result,omitempty"`
}
if err := json.Unmarshal(resp, &response); err != nil {
return false, fmt.Errorf("failed to unmarshal JSONRPC response: %w", err)
}
if response.Error != nil {
if response.Error.Code == 100500 && strings.Contains(response.Error.Message, `duplicate key value violates unique constraint "ext_bz_bug_map_bug_id_idx"`) {
// adding the external bug failed since it is already added, this is not an error
return false, nil
}
return false, fmt.Errorf("JSONRPC error %d: %v", response.Error.Code, response.Error.Message)
}
if response.ID != rpcPayload.ID {
return false, fmt.Errorf("JSONRPC returned mismatched identifier, expected %s but got %s", rpcPayload.ID, response.ID)
}
changed := false
if response.Result != nil {
for _, bug := range response.Result.Bugs {
if bug.ID == id {
changed = changed || strings.Contains(bug.Changes.ExternalBugs.Added, pullIdentifier)
}
}
}
return changed, nil
}
// RemovePullRequestAsExternalBug attempts to remove a PR from the external tracker list.
// External bugs are assumed to fall under the type identified by their hostname,
// so we will provide https://github.com/ here for the URL identifier. We return
// any error as well as whether a change was actually made.
// This will be done via JSONRPC:
// https://bugzilla.redhat.com/docs/en/html/integrating/api/Bugzilla/Extension/ExternalBugs/WebService.html#remove-external-bug
func (c *client) RemovePullRequestAsExternalBug(id int, org, repo string, num int) (bool, error) {
logger := c.logger.WithFields(logrus.Fields{methodField: "RemoveExternalBug", "id": id, "org": org, "repo": repo, "num": num})
pullIdentifier := IdentifierForPull(org, repo, num)
rpcPayload := struct {
// Version is the version of JSONRPC to use. All Bugzilla servers
// support 1.0. Some support 1.1 and some support 2.0
Version string `json:"jsonrpc"`
Method string `json:"method"`
// Parameters must be specified in JSONRPC 1.0 as a structure in the first
// index of this slice
Parameters []RemoveExternalBugParameters `json:"params"`
ID string `json:"id"`
}{
Version: "1.0", // some Bugzilla servers support 2.0 but all support 1.0
Method: "ExternalBugs.remove_external_bug",
ID: "identifier", // this is useful when fielding asynchronous responses, but not here
Parameters: []RemoveExternalBugParameters{{
BugIDs: []int{id},
ExternalBugIdentifier: ExternalBugIdentifier{
Type: "https://github.com/",
ID: pullIdentifier,
},
}},
}
body, err := json.Marshal(rpcPayload)
if err != nil {
return false, fmt.Errorf("failed to marshal JSONRPC payload: %w", err)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/jsonrpc.cgi", c.endpoint), bytes.NewBuffer(body))
if err != nil {
return false, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.request(req, logger)
if err != nil {
return false, err
}
var response struct {
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error,omitempty"`
ID string `json:"id"`
Result *struct {
ExternalBugs []struct {
Type string `json:"ext_type_url"`
ID string `json:"ext_bz_bug_id"`
} `json:"external_bugs"`
} `json:"result,omitempty"`
}
if err := json.Unmarshal(resp, &response); err != nil {
return false, fmt.Errorf("failed to unmarshal JSONRPC response: %w", err)
}
if response.Error != nil {
if response.Error.Code == 1006 && strings.Contains(response.Error.Message, `No external tracker bugs were found that matched your criteria`) {
// removing the external bug failed since it is already gone, this is not an error
return false, nil
}
return false, fmt.Errorf("JSONRPC error %d: %v", response.Error.Code, response.Error.Message)
}
if response.ID != rpcPayload.ID {
return false, fmt.Errorf("JSONRPC returned mismatched identifier, expected %s but got %s", rpcPayload.ID, response.ID)
}
changed := false
if response.Result != nil {
for _, bug := range response.Result.ExternalBugs {
changed = changed || bug.ID == pullIdentifier
}
}
return changed, nil
}
func IdentifierForPull(org, repo string, num int) string {
return fmt.Sprintf("%s/%s/pull/%d", org, repo, num)
}
func PullFromIdentifier(identifier string) (org, repo string, num int, err error) {
parts := strings.Split(identifier, "/")
if len(parts) >= 3 && parts[2] != "pull" {
return "", "", 0, &identifierNotForPull{identifier: identifier}
}
if len(parts) != 4 && !(len(parts) == 5 && (parts[4] == "" || parts[4] == "files")) && !(len(parts) == 6 && (parts[4] == "files" && parts[5] == "")) {
return "", "", 0, fmt.Errorf("invalid pull identifier with %d parts: %q", len(parts), identifier)
}
number, err := strconv.Atoi(parts[3])
if err != nil {
return "", "", 0, fmt.Errorf("invalid pull identifier: could not parse %s as number: %w", parts[3], err)
}
return parts[0], parts[1], number, nil
}
type identifierNotForPull struct {
identifier string
}