4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
+ "io"
8
+ "io/ioutil"
7
9
"log"
8
10
"net/http"
9
11
"strconv"
@@ -81,6 +83,26 @@ func (b BitbucketAPI) GetChangedFiles(prNumber int) ([]string, error) {
81
83
return files , nil
82
84
}
83
85
86
+ type BitbucketCommentResponse struct {
87
+ ID int `json:"id"`
88
+ Content struct {
89
+ Raw string `json:"raw"`
90
+ } `json:"content"`
91
+ Links struct {
92
+ Self struct {
93
+ Href string `json:"href"`
94
+ } `json:"self"`
95
+ HTML struct {
96
+ Href string `json:"href"`
97
+ } `json:"html"`
98
+ } `json:"links"`
99
+ CreatedOn string `json:"created_on"`
100
+ User struct {
101
+ DisplayName string `json:"display_name"`
102
+ UUID string `json:"uuid"`
103
+ } `json:"user"`
104
+ }
105
+
84
106
func (b BitbucketAPI ) PublishComment (prNumber int , comment string ) (* ci.Comment , error ) {
85
107
url := fmt .Sprintf ("%s/repositories/%s/%s/pullrequests/%d/comments" , bitbucketBaseURL , b .RepoWorkspace , b .RepoName , prNumber )
86
108
@@ -105,7 +127,25 @@ func (b BitbucketAPI) PublishComment(prNumber int, comment string) (*ci.Comment,
105
127
return nil , fmt .Errorf ("failed to publish comment. Status code: %d" , resp .StatusCode )
106
128
}
107
129
108
- return nil , nil
130
+ body , err := ioutil .ReadAll (resp .Body )
131
+ if err != nil {
132
+ fmt .Println ("Error reading response:" , err )
133
+ return nil , fmt .Errorf ("error reading response: %v" , err )
134
+ }
135
+
136
+ var commentResponse BitbucketCommentResponse
137
+ if err := json .Unmarshal (body , & commentResponse ); err != nil {
138
+ fmt .Println ("Error parsing response:" , err )
139
+ return nil , fmt .Errorf ("error parsing response: %v" , err )
140
+ }
141
+
142
+ res := ci.Comment {
143
+ Id : strconv .Itoa (commentResponse .ID ),
144
+ DiscussionId : "" ,
145
+ Body : & comment ,
146
+ Url : commentResponse .Links .HTML .Href ,
147
+ }
148
+ return & res , nil
109
149
}
110
150
111
151
func (svc BitbucketAPI ) ListIssues () ([]* ci.Issue , error ) {
@@ -123,8 +163,10 @@ func (svc BitbucketAPI) UpdateIssue(ID int64, title string, body string) (int64,
123
163
func (b BitbucketAPI ) EditComment (prNumber int , id string , comment string ) error {
124
164
url := fmt .Sprintf ("%s/repositories/%s/%s/pullrequests/%d/comments/%s" , bitbucketBaseURL , b .RepoWorkspace , b .RepoName , prNumber , id )
125
165
126
- commentBody := map [string ]string {
127
- "content" : comment ,
166
+ commentBody := map [string ]interface {}{
167
+ "content" : map [string ]string {
168
+ "raw" : comment ,
169
+ },
128
170
}
129
171
130
172
commentJSON , err := json .Marshal (commentBody )
@@ -475,6 +517,137 @@ func (b BitbucketAPI) GetUserTeams(organisation string, user string) ([]string,
475
517
return nil , fmt .Errorf ("not implemented" )
476
518
}
477
519
520
+ type PipelineResponse struct {
521
+ UUID string `json:"uuid"`
522
+ BuildNumber int `json:"build_number"`
523
+ CreatedOn string `json:"created_on"`
524
+ Creator struct {
525
+ DisplayName string `json:"display_name"`
526
+ UUID string `json:"uuid"`
527
+ AccountID string `json:"account_id"`
528
+ Nickname string `json:"nickname"`
529
+ Type string `json:"type"`
530
+ Links struct {
531
+ Self struct {
532
+ Href string `json:"href"`
533
+ } `json:"self"`
534
+ HTML struct {
535
+ Href string `json:"href"`
536
+ } `json:"html"`
537
+ Avatar struct {
538
+ Href string `json:"href"`
539
+ } `json:"avatar"`
540
+ } `json:"links"`
541
+ } `json:"creator"`
542
+ Repository struct {
543
+ Name string `json:"name"`
544
+ FullName string `json:"full_name"`
545
+ UUID string `json:"uuid"`
546
+ Type string `json:"type"`
547
+ Links struct {
548
+ Self struct {
549
+ Href string `json:"href"`
550
+ } `json:"self"`
551
+ HTML struct {
552
+ Href string `json:"href"`
553
+ } `json:"html"`
554
+ Avatar struct {
555
+ Href string `json:"href"`
556
+ } `json:"avatar"`
557
+ } `json:"links"`
558
+ } `json:"repository"`
559
+ Target struct {
560
+ Type string `json:"type"`
561
+ RefName string `json:"ref_name"`
562
+ RefType string `json:"ref_type"`
563
+ Selector struct {
564
+ Type string `json:"type"`
565
+ } `json:"selector"`
566
+ Commit struct {
567
+ Type string `json:"type"`
568
+ Hash string `json:"hash"`
569
+ } `json:"commit"`
570
+ } `json:"target"`
571
+ Trigger struct {
572
+ Name string `json:"name"`
573
+ Type string `json:"type"`
574
+ } `json:"trigger"`
575
+ State struct {
576
+ Name string `json:"name"`
577
+ Type string `json:"type"`
578
+ Stage struct {
579
+ Name string `json:"name"`
580
+ Type string `json:"type"`
581
+ } `json:"stage"`
582
+ } `json:"state"`
583
+ Variables []struct {
584
+ Key string `json:"key"`
585
+ Value string `json:"value"`
586
+ Secured bool `json:"secured"`
587
+ UUID string `json:"uuid"`
588
+ } `json:"variables"`
589
+ Links struct {
590
+ Self struct {
591
+ Href string `json:"href"`
592
+ } `json:"self"`
593
+ HTML struct {
594
+ Href string `json:"href"`
595
+ } `json:"html"`
596
+ } `json:"links"`
597
+ }
598
+
599
+ // trigger pipeline from a specific branch
600
+ func (b BitbucketAPI ) TriggerPipeline (branch string , variables []interface {}) (string , error ) {
601
+ url := fmt .Sprintf ("%s/repositories/%s/%s/pipelines" , bitbucketBaseURL , b .RepoWorkspace , b .RepoName )
602
+
603
+ log .Printf ("pipeline trigger url: %v branch %v" , url , branch )
604
+ triggerOptions := map [string ]interface {}{
605
+ "target" : map [string ]interface {}{
606
+ "ref_type" : "branch" ,
607
+ "type" : "pipeline_ref_target" ,
608
+ "ref_name" : branch ,
609
+ "selector" : map [string ]interface {}{
610
+ "type" : "custom" ,
611
+ "pattern" : "digger" ,
612
+ },
613
+ },
614
+ "variables" : variables ,
615
+ }
616
+
617
+ triggerJSON , err := json .Marshal (triggerOptions )
618
+ if err != nil {
619
+ return "" , err
620
+ }
621
+
622
+ resp , err := b .sendRequest ("POST" , url , triggerJSON )
623
+ if err != nil {
624
+ return "" , err
625
+ }
626
+ defer resp .Body .Close ()
627
+
628
+ if resp .StatusCode != http .StatusCreated {
629
+ body , _ := io .ReadAll (resp .Body )
630
+ log .Printf ("the response from bitbucket is: %v" , string (body ))
631
+ return "" , fmt .Errorf ("failed to trigger pipeline: %d" , resp .StatusCode )
632
+ }
633
+
634
+ body , err := ioutil .ReadAll (resp .Body )
635
+ if err != nil {
636
+ fmt .Println ("Error reading response:" , err )
637
+ return "" , fmt .Errorf ("error reading response: %v" , err )
638
+ }
639
+
640
+ var triggerPipelineResponse PipelineResponse
641
+ if err := json .Unmarshal (body , & triggerPipelineResponse ); err != nil {
642
+ fmt .Println ("Error parsing response:" , err )
643
+ return "" , fmt .Errorf ("error parsing response: %v" , err )
644
+ }
645
+
646
+ log .Printf (triggerPipelineResponse .Links .HTML .Href )
647
+ return "" , nil
648
+
649
+ }
650
+
478
651
func FindImpactedProjectsInBitbucket (diggerConfig * configuration.DiggerConfig , prNumber int , prService ci.PullRequestService ) ([]configuration.Project , error ) {
479
652
changedFiles , err := prService .GetChangedFiles (prNumber )
480
653
0 commit comments