Skip to content

Commit 1716c40

Browse files
committed
bb support
1 parent aa74fed commit 1716c40

File tree

8 files changed

+199
-14
lines changed

8 files changed

+199
-14
lines changed

backend/controllers/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (d DiggerController) UpdateRepoCache(c *gin.Context) {
6767

6868
// update the cache here, do it async for immediate response
6969
go func() {
70-
err = utils.CloneGitRepoAndDoAction(cloneUrl, branch, "", *token, func(dir string) error {
70+
err = utils.CloneGitRepoAndDoAction(cloneUrl, branch, "", *token, "", func(dir string) error {
7171
diggerYmlBytes, err := os.ReadFile(path.Join(dir, "digger.yml"))
7272
diggerYmlStr = string(diggerYmlBytes)
7373
config, _, _, err = dg_configuration.LoadDiggerConfig(dir, true, nil)
@@ -76,7 +76,7 @@ func (d DiggerController) UpdateRepoCache(c *gin.Context) {
7676
return err
7777
}
7878
return nil
79-
}, "")
79+
})
8080

8181
if err != nil {
8282
log.Printf("could not load digger config :%v", err)

backend/controllers/github.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int6
595595
var diggerYmlStr string
596596
var dependencyGraph graph.Graph[string, dg_configuration.Project]
597597

598-
err = utils.CloneGitRepoAndDoAction(cloneUrl, branch, "", *token, func(dir string) error {
598+
err = utils.CloneGitRepoAndDoAction(cloneUrl, branch, "", *token, "", func(dir string) error {
599599
diggerYmlStr, err = dg_configuration.ReadDiggerYmlFileContents(dir)
600600
if err != nil {
601601
log.Printf("could not load digger config: %v", err)
@@ -607,7 +607,7 @@ func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int6
607607
return err
608608
}
609609
return nil
610-
}, "")
610+
})
611611
if err != nil {
612612
log.Printf("Error cloning and loading config: %v", err)
613613
return "", nil, nil, nil, fmt.Errorf("error cloning and loading config %v", err)

backend/models/scheduler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type DiggerVCSType string
2020

2121
const DiggerVCSGithub DiggerVCSType = "github"
2222
const DiggerVCSGitlab DiggerVCSType = "gitlab"
23+
const DiggerVCSBitbucket DiggerVCSType = "bitbucket"
2324

2425
type DiggerBatch struct {
2526
ID uuid.UUID `gorm:"primary_key"`

backend/utils/bitbucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GetDiggerConfigForBitbucketBranch(bb BitbucketProvider, token string, repoF
5959
return "", nil, nil, fmt.Errorf("error getting changed files")
6060
}
6161

62-
err = CloneGitRepoAndDoAction(cloneUrl, branch, "", token, "", func(dir string) error {
62+
err = CloneGitRepoAndDoAction(cloneUrl, branch, "", token, "x-token-auth", func(dir string) error {
6363
diggerYmlBytes, err := os.ReadFile(path.Join(dir, "digger.yml"))
6464
diggerYmlStr = string(diggerYmlBytes)
6565
config, _, dependencyGraph, err = dg_configuration.LoadDiggerConfig(dir, true, changedFiles)

backend/utils/github_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ func init() {
1414

1515
func TestGithubCloneWithInvalidTokenThrowsErr(t *testing.T) {
1616
f := func(d string) error { return nil }
17-
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/private-repo", "main", "", "invalid-token", f, "")
17+
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/private-repo", "main", "", "invalid-token", "", f)
1818
assert.NotNil(t, err)
1919
}
2020

2121
func TestGithubCloneWithPublicRepoThrowsNoError(t *testing.T) {
2222
token := os.Getenv("GITHUB_PAT_TOKEN")
2323
f := func(d string) error { return nil }
24-
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/digger", "develop", "", token, f, "")
24+
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/digger", "develop", "", token, "", f)
2525
assert.Nil(t, err)
2626
}
2727

@@ -32,13 +32,13 @@ func TestGithubCloneWithPrivateRepoAndValidTokenThrowsNoError(t *testing.T) {
3232
return
3333
}
3434
f := func(d string) error { return nil }
35-
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/infra-gcp", "main", "", token, f, "")
35+
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/infra-gcp", "main", "", token, "", f)
3636
assert.Nil(t, err)
3737
}
3838

3939
func TestGithubCloneWithInvalidBranchThrowsError(t *testing.T) {
4040
token := os.Getenv("GITHUB_PAT_TOKEN")
4141
f := func(d string) error { return nil }
42-
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/digger", "not-a-branch", "", token, f, "")
42+
err := CloneGitRepoAndDoAction("https://github.com/diggerhq/digger", "not-a-branch", "", token, "", f)
4343
assert.NotNil(t, err)
4444
}

backend/utils/gitshell.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"log"
87
"net/url"
98
"os"
109
"os/exec"
@@ -129,7 +128,6 @@ func (g *GitShell) Clone(repoURL, branch string) error {
129128
args = append(args, "-b", branch)
130129
}
131130

132-
log.Printf("auth url: %v", authURL)
133131
args = append(args, "--depth", "1")
134132
args = append(args, "--single-branch", authURL, g.workDir)
135133

libs/ci/bitbucket/bitbucket.go

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io"
8+
"io/ioutil"
79
"log"
810
"net/http"
911
"strconv"
@@ -81,6 +83,26 @@ func (b BitbucketAPI) GetChangedFiles(prNumber int) ([]string, error) {
8183
return files, nil
8284
}
8385

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+
84106
func (b BitbucketAPI) PublishComment(prNumber int, comment string) (*ci.Comment, error) {
85107
url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d/comments", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber)
86108

@@ -105,7 +127,25 @@ func (b BitbucketAPI) PublishComment(prNumber int, comment string) (*ci.Comment,
105127
return nil, fmt.Errorf("failed to publish comment. Status code: %d", resp.StatusCode)
106128
}
107129

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
109149
}
110150

111151
func (svc BitbucketAPI) ListIssues() ([]*ci.Issue, error) {
@@ -123,8 +163,10 @@ func (svc BitbucketAPI) UpdateIssue(ID int64, title string, body string) (int64,
123163
func (b BitbucketAPI) EditComment(prNumber int, id string, comment string) error {
124164
url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d/comments/%s", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber, id)
125165

126-
commentBody := map[string]string{
127-
"content": comment,
166+
commentBody := map[string]interface{}{
167+
"content": map[string]string{
168+
"raw": comment,
169+
},
128170
}
129171

130172
commentJSON, err := json.Marshal(commentBody)
@@ -475,6 +517,137 @@ func (b BitbucketAPI) GetUserTeams(organisation string, user string) ([]string,
475517
return nil, fmt.Errorf("not implemented")
476518
}
477519

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+
478651
func FindImpactedProjectsInBitbucket(diggerConfig *configuration.DiggerConfig, prNumber int, prService ci.PullRequestService) ([]configuration.Project, error) {
479652
changedFiles, err := prService.GetChangedFiles(prNumber)
480653

libs/spec/providers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/aws/aws-sdk-go-v2/service/sts"
1212
backend2 "github.com/diggerhq/digger/libs/backendapi"
1313
"github.com/diggerhq/digger/libs/ci"
14+
"github.com/diggerhq/digger/libs/ci/bitbucket"
1415
"github.com/diggerhq/digger/libs/ci/github"
1516
"github.com/diggerhq/digger/libs/ci/gitlab"
1617
"github.com/diggerhq/digger/libs/comment_utils/reporting"
@@ -195,6 +196,18 @@ func (v VCSProviderBasic) GetPrService(vcsSpec VcsSpec) (ci.PullRequestService,
195196
return nil, fmt.Errorf("failed to get gitlab service, could not parse context: %v", err)
196197
}
197198
return gitlab.NewGitLabService(token, context, "")
199+
case "bitbucket":
200+
token := os.Getenv("DIGGER_BITBUCKET_ACCESS_TOKEN")
201+
if token == "" {
202+
return nil, fmt.Errorf("failed to get bitbucket service: GITLAB_TOKEN not specified")
203+
}
204+
return bitbucket.BitbucketAPI{
205+
AuthToken: token,
206+
HttpClient: http.Client{},
207+
RepoWorkspace: vcsSpec.RepoOwner,
208+
RepoName: vcsSpec.RepoName,
209+
}, nil
210+
198211
default:
199212
return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType)
200213
}

0 commit comments

Comments
 (0)