-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathGitServiceGithub.go
334 lines (306 loc) · 11.6 KB
/
GitServiceGithub.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
/*
* Copyright (c) 2024. Devtron 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.
*/
package git
import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/devtron-labs/common-lib/utils/runTime"
bean2 "github.com/devtron-labs/devtron/api/bean/gitOps"
globalUtil "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/retryFunc"
"github.com/google/go-github/github"
"go.uber.org/zap"
"golang.org/x/oauth2"
http2 "net/http"
"net/url"
"path"
"path/filepath"
"time"
)
type GitHubClient struct {
client *github.Client
logger *zap.SugaredLogger
org string
gitOpsHelper *GitOpsHelper
}
func NewGithubClient(host string, token string, org string, logger *zap.SugaredLogger,
gitOpsHelper *GitOpsHelper, tlsConfig *tls.Config) (GitHubClient, error) {
ctx := context.Background()
httpTransport := &http2.Transport{Proxy: http2.ProxyFromEnvironment, TLSClientConfig: tlsConfig}
httpClient := &http2.Client{Transport: httpTransport}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
tc := oauth2.NewClient(ctx, ts)
var client *github.Client
var err error
hostUrl, err := url.Parse(host)
if err != nil {
logger.Errorw("error in creating git client ", "host", hostUrl, "err", err)
return GitHubClient{}, err
}
if hostUrl.Host == GITHUB_HOST {
client = github.NewClient(tc)
} else {
logger.Infow("creating github EnterpriseClient with org", "host", host, "org", org)
hostUrl.Path = path.Join(hostUrl.Path, GITHUB_API_V3)
client, err = github.NewEnterpriseClient(hostUrl.String(), hostUrl.String(), tc)
}
return GitHubClient{
client: client,
org: org,
logger: logger,
gitOpsHelper: gitOpsHelper,
}, err
}
func (impl GitHubClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("DeleteRepository", "GitHubClient", start, err)
}()
_, err = impl.client.Repositories.Delete(context.Background(), config.GitHubOrgId, config.GitRepoName)
if err != nil {
impl.logger.Errorw("repo deletion failed for github", "repo", config.GitRepoName, "err", err)
return err
}
return nil
}
func IsRepoNotFound(err error) bool {
if err == nil {
return false
}
var responseErr *github.ErrorResponse
ok := errors.As(err, &responseErr)
return ok && responseErr.Response.StatusCode == 404
}
func (impl GitHubClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitHubClient", start, err)
}()
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
repoExists := true
url, err = impl.getRepoUrl(ctx, config, IsRepoNotFound)
if err != nil {
if IsRepoNotFound(err) {
repoExists = false
} else {
impl.logger.Errorw("error in creating github repo", "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
return "", false, detailedErrorGitOpsConfigActions
}
}
if repoExists {
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, GetRepoUrlStage)
return url, false, detailedErrorGitOpsConfigActions
}
private := true
// visibility := "private"
r, _, err := impl.client.Repositories.Create(ctx, impl.org,
&github.Repository{Name: &config.GitRepoName,
Description: &config.Description,
Private: &private,
// Visibility: &visibility,
})
if err != nil {
impl.logger.Errorw("error in creating github repo, ", "repo", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateRepoStage] = err
url, err = impl.GetRepoUrl(config)
if err != nil {
impl.logger.Errorw("error in getting github repo", "repo", config.GitRepoName, "err", err)
return "", true, detailedErrorGitOpsConfigActions
}
}
impl.logger.Infow("github repo created ", "r", r.CloneURL)
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateRepoStage)
validated, err := impl.ensureProjectAvailabilityOnHttp(config)
if err != nil {
impl.logger.Errorw("error in ensuring project availability github", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
return *r.CloneURL, true, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
return "", true, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneHttpStage)
_, err = impl.CreateReadme(ctx, config)
if err != nil {
impl.logger.Errorw("error in creating readme github", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateReadmeStage] = err
return *r.CloneURL, true, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateReadmeStage)
validated, err = impl.ensureProjectAvailabilityOnSsh(config.GitRepoName, *r.CloneURL)
if err != nil {
impl.logger.Errorw("error in ensuring project availability github", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
return *r.CloneURL, true, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
return "", true, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneSshStage)
//_, err = impl.createReadme(name)
return *r.CloneURL, true, detailedErrorGitOpsConfigActions
}
func (impl GitHubClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateReadme", "GitHubClient", start, err)
}()
cfg := &ChartConfig{
ChartName: config.GitRepoName,
ChartLocation: "",
FileName: "README.md",
FileContent: "@devtron",
ReleaseMessage: "readme",
ChartRepoName: config.GitRepoName,
UserName: config.Username,
UserEmailId: config.UserEmailId,
}
hash, _, err := impl.CommitValues(ctx, cfg, config)
if err != nil {
impl.logger.Errorw("error in creating readme github", "repo", config.GitRepoName, "err", err)
}
return hash, err
}
func (impl GitHubClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error) {
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitHubClient", start, err)
}()
branch := "master"
path := filepath.Join(config.ChartLocation, config.FileName)
newFile := false
fc, _, _, err := impl.client.Repositories.GetContents(ctx, impl.org, config.ChartRepoName, path, &github.RepositoryContentGetOptions{Ref: branch})
if err != nil {
responseErr, ok := err.(*github.ErrorResponse)
if !ok || responseErr.Response.StatusCode != 404 {
impl.logger.Errorw("error in creating repo github", "err", err, "config", config)
return "", time.Time{}, err
} else {
newFile = true
}
}
currentSHA := ""
if !newFile {
currentSHA = *fc.SHA
}
timeNow := time.Now()
options := &github.RepositoryContentFileOptions{
Message: &config.ReleaseMessage,
Content: []byte(config.FileContent),
SHA: ¤tSHA,
Branch: &branch,
Author: &github.CommitAuthor{
Date: &timeNow,
Email: &config.UserEmailId,
Name: &config.UserName,
},
Committer: &github.CommitAuthor{
Date: &timeNow,
Email: &config.UserEmailId,
Name: &config.UserName,
},
}
c, httpRes, err := impl.client.Repositories.CreateFile(ctx, impl.org, config.ChartRepoName, path, options)
if err != nil && httpRes != nil && httpRes.StatusCode == http2.StatusConflict {
impl.logger.Warn("conflict found in commit github", "err", err, "config", config)
return "", time.Time{}, retryFunc.NewRetryableError(err)
} else if err != nil {
impl.logger.Errorw("error in commit github", "err", err, "config", config)
return "", time.Time{}, err
}
commitTime = time.Now() // default is current time, if found then will get updated accordingly
if c != nil && c.Commit.Author != nil {
commitTime = *c.Commit.Author.Date
}
return *c.SHA, commitTime, nil
}
func (impl GitHubClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
ctx := context.Background()
return impl.getRepoUrl(ctx, config, globalUtil.AllPublishableError())
}
func (impl GitHubClient) getRepoUrl(ctx context.Context, config *bean2.GitOpsConfigDto,
isNonPublishableError globalUtil.EvalIsNonPublishableErr) (repoUrl string, err error) {
start := time.Now()
defer func() {
if isNonPublishableError(err) {
impl.logger.Debugw("found non publishable error. skipping metrics publish!", "caller method", runTime.GetCallerFunctionName(), "err", err)
return
}
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitHubClient", start, err)
}()
repo, _, err := impl.client.Repositories.Get(ctx, impl.org, config.GitRepoName)
if err != nil {
impl.logger.Errorw("error in getting repo url by repo name", "org", impl.org, "gitRepoName", config.GitRepoName, "err", err)
return "", err
}
return *repo.CloneURL, nil
}
func (impl GitHubClient) ensureProjectAvailabilityOnHttp(config *bean2.GitOpsConfigDto) (bool, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnHttp", "GitHubClient", start, err)
}()
count := 0
for count < 3 {
count = count + 1
_, err := impl.GetRepoUrl(config)
if err == nil {
return true, nil
}
responseErr, ok := err.(*github.ErrorResponse)
if !ok || responseErr.Response.StatusCode != 404 {
impl.logger.Errorw("error in validating repo github", "project", config.GitRepoName, "err", err)
return false, err
} else {
impl.logger.Errorw("error in validating repo github", "project", config.GitRepoName, "err", err)
}
time.Sleep(10 * time.Second)
}
return false, nil
}
func (impl GitHubClient) ensureProjectAvailabilityOnSsh(projectName string, repoUrl string) (bool, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnSsh", "GitHubClient", start, err)
}()
count := 0
for count < 3 {
count = count + 1
_, err := impl.gitOpsHelper.Clone(repoUrl, fmt.Sprintf("/ensure-clone/%s", projectName))
if err == nil {
impl.logger.Infow("github ensureProjectAvailability clone passed", "try count", count, "repoUrl", repoUrl)
return true, nil
}
if err != nil {
impl.logger.Errorw("github ensureProjectAvailability clone failed", "try count", count, "err", err)
}
time.Sleep(10 * time.Second)
}
return false, nil
}