-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathbitbucket.go
141 lines (122 loc) · 3.88 KB
/
bitbucket.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
package utils
import (
"fmt"
"log/slog"
"net/http"
"os"
"path"
orchestrator_bitbucket "github.com/diggerhq/digger/libs/ci/bitbucket"
dg_configuration "github.com/diggerhq/digger/libs/digger_config"
"github.com/dominikbraun/graph"
"github.com/ktrysmt/go-bitbucket"
)
type BitbucketProvider interface {
NewClient(token string) (*bitbucket.Client, error)
}
type BitbucketClientProvider struct{}
func (b BitbucketClientProvider) NewClient(token string) (*bitbucket.Client, error) {
client := bitbucket.NewOAuthbearerToken(token)
return client, nil
}
func GetBitbucketService(bb BitbucketProvider, token string, repoOwner string, repoName string, prNumber int) (*orchestrator_bitbucket.BitbucketAPI, error) {
slog.Debug("Creating Bitbucket service",
slog.Group("repository",
slog.String("owner", repoOwner),
slog.String("name", repoName),
),
"prNumber", prNumber,
)
//token := os.Getenv("DIGGER_BITBUCKET_ACCESS_TOKEN")
//client, err := bb.NewClient(token)
//if err != nil {
// return nil, fmt.Errorf("could not get bitbucket client: %v", err)
//}
//context := orchestrator_bitbucket.BitbucketContext{
// RepositoryName: repoName,
// RepositoryFullName: repoFullName,
// PullRequestID: &prNumber,
//}
service := orchestrator_bitbucket.BitbucketAPI{
AuthToken: token,
RepoWorkspace: repoOwner,
RepoName: repoName,
HttpClient: http.Client{},
}
return &service, nil
}
func GetDiggerConfigForBitbucketBranch(bb BitbucketProvider, token string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string, prNumber int) (string, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) {
slog.Info("Getting Digger config for Bitbucket branch",
slog.Group("repository",
slog.String("fullName", repoFullName),
slog.String("owner", repoOwner),
slog.String("name", repoName),
slog.String("cloneUrl", cloneUrl),
),
"branch", branch,
"prNumber", prNumber,
)
service, err := GetBitbucketService(bb, token, repoOwner, repoName, prNumber)
if err != nil {
slog.Error("Could not get Bitbucket service",
"repoFullName", repoFullName,
"error", err,
)
return "", nil, nil, fmt.Errorf("could not get bitbucket service: %v", err)
}
var config *dg_configuration.DiggerConfig
var diggerYmlStr string
var dependencyGraph graph.Graph[string, dg_configuration.Project]
changedFiles, err := service.GetChangedFiles(prNumber)
if err != nil {
slog.Error("Error getting changed files",
"repoFullName", repoFullName,
"prNumber", prNumber,
"error", err,
)
return "", nil, nil, fmt.Errorf("error getting changed files")
}
slog.Debug("Retrieved changed files",
"repoFullName", repoFullName,
"prNumber", prNumber,
"changedFilesCount", len(changedFiles),
)
err = CloneGitRepoAndDoAction(cloneUrl, branch, "", token, "x-token-auth", func(dir string) error {
diggerYmlPath := path.Join(dir, "digger.yml")
diggerYmlBytes, err := os.ReadFile(diggerYmlPath)
if err != nil {
slog.Error("Error reading digger.yml file",
"path", diggerYmlPath,
"error", err,
)
return fmt.Errorf("error reading digger.yml: %w", err)
}
diggerYmlStr = string(diggerYmlBytes)
config, _, dependencyGraph, err = dg_configuration.LoadDiggerConfig(dir, true, changedFiles)
if err != nil {
slog.Error("Error loading Digger config",
"repoFullName", repoFullName,
"dir", dir,
"error", err,
)
return err
}
return nil
})
if err != nil {
slog.Error("Error cloning and loading config",
"repoFullName", repoFullName,
"branch", branch,
"error", err,
)
return "", nil, nil, fmt.Errorf("error cloning and loading config")
}
projectCount := 0
if config != nil {
projectCount = len(config.Projects)
}
slog.Info("Digger config loaded successfully",
"repoFullName", repoFullName,
"projectCount", projectCount,
)
return diggerYmlStr, config, dependencyGraph, nil
}