Skip to content

Commit 56669f3

Browse files
authored
chore: Gitops migration api modifications (#6423)
* adding is empty repo check * isEmpty in create repository function * review changes * adding header * adding status code * removing unnecessary code * adding comments
1 parent 39e45ec commit 56669f3

File tree

10 files changed

+103
-88
lines changed

10 files changed

+103
-88
lines changed

pkg/appStore/installedApp/service/FullMode/deployment/InstalledAppGitOpsService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (impl *FullModeDeploymentServiceImpl) createGitOpsRepo(gitOpsRepoName strin
232232
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
233233
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
234234
}
235-
repoUrl, isNew, err := impl.gitOperationService.CreateRepository(context.Background(), gitRepoRequest, userId)
235+
repoUrl, isNew, _, err := impl.gitOperationService.CreateRepository(context.Background(), gitRepoRequest, userId)
236236
if err != nil {
237237
impl.Logger.Errorw("error in creating git project", "name", gitOpsRepoName, "err", err)
238238
return "", false, err

pkg/deployment/gitOps/common/bean/bean.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ package bean
2020
type ChartGitAttribute struct {
2121
RepoUrl, ChartLocation string
2222
IsNewRepo bool
23+
IsRepoEmpty bool
2324
}

pkg/deployment/gitOps/git/GitOperationService.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type GitOperationService interface {
5050
PushChartToGitRepo(ctx context.Context, gitOpsRepoName, referenceTemplate, version, tempReferenceTemplateDir, repoUrl string, userId int32) (err error)
5151
PushChartToGitOpsRepoForHelmApp(ctx context.Context, PushChartToGitRequest *bean.PushChartToGitRequestDTO, requirementsConfig *ChartConfig, valuesConfig *ChartConfig) (*commonBean.ChartGitAttribute, string, error)
5252

53-
CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, error)
53+
CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, bool, error)
5454
GetRepoUrlByRepoName(repoName string) (string, error)
5555

5656
CloneInDir(repoUrl, chartDir string) (string, error)
@@ -98,12 +98,12 @@ func (impl *GitOperationServiceImpl) CreateGitRepositoryForDevtronApp(ctx contex
9898
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
9999
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
100100
}
101-
repoUrl, isNew, err := impl.CreateRepository(ctx, gitRepoRequest, userId)
101+
repoUrl, isNew, isEmpty, err := impl.CreateRepository(ctx, gitRepoRequest, userId)
102102
if err != nil {
103103
impl.logger.Errorw("error in creating git project", "name", gitOpsRepoName, "err", err)
104104
return nil, err
105105
}
106-
return &commonBean.ChartGitAttribute{RepoUrl: repoUrl, IsNewRepo: isNew}, nil
106+
return &commonBean.ChartGitAttribute{RepoUrl: repoUrl, IsNewRepo: isNew, IsRepoEmpty: isEmpty}, nil
107107
}
108108

109109
func getChartDirPathFromCloneDir(cloneDirPath string) (string, error) {
@@ -279,21 +279,21 @@ func (impl *GitOperationServiceImpl) isRetryableGitCommitError(err error) bool {
279279
return false
280280
}
281281

282-
func (impl *GitOperationServiceImpl) CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, error) {
282+
func (impl *GitOperationServiceImpl) CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, bool, error) {
283283
//getting username & emailId for commit author data
284284
userEmailId, userName := impl.gitOpsConfigReadService.GetUserEmailIdAndNameForGitOpsCommit(userId)
285285
if dto != nil {
286286
dto.UserEmailId = userEmailId
287287
dto.Username = userName
288288
}
289-
repoUrl, isNew, detailedError := impl.gitFactory.Client.CreateRepository(ctx, dto)
289+
repoUrl, isNew, isEmpty, detailedError := impl.gitFactory.Client.CreateRepository(ctx, dto)
290290
for _, err := range detailedError.StageErrorMap {
291291
if err != nil {
292292
impl.logger.Errorw("error in creating git project", "err", err, "req", dto)
293-
return "", false, err
293+
return "", false, false, err
294294
}
295295
}
296-
return repoUrl, isNew, nil
296+
return repoUrl, isNew, isEmpty, nil
297297
}
298298

299299
func (impl *GitOperationServiceImpl) GetRepoUrlByRepoName(repoName string) (string, error) {
@@ -308,7 +308,7 @@ func (impl *GitOperationServiceImpl) GetRepoUrlByRepoName(repoName string) (stri
308308
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
309309
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
310310
}
311-
repoUrl, err = impl.gitFactory.Client.GetRepoUrl(dto)
311+
repoUrl, _, err = impl.gitFactory.Client.GetRepoUrl(dto)
312312
if err != nil {
313313
//will allow to continue to persist status on next operation
314314
impl.logger.Errorw("error in getting repo url", "err", err, "repoName", repoName)

pkg/deployment/gitOps/git/GitOpsClient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import (
2929
)
3030

3131
type GitOpsClient interface {
32-
CreateRepository(ctx context.Context, config *gitOps.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
32+
CreateRepository(ctx context.Context, config *gitOps.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
3333
CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *gitOps.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error)
34-
GetRepoUrl(config *gitOps.GitOpsConfigDto) (repoUrl string, err error)
34+
GetRepoUrl(config *gitOps.GitOpsConfigDto) (repoUrl string, isRepoEmpty bool, err error)
3535
DeleteRepository(config *gitOps.GitOpsConfigDto) error
3636
CreateReadme(ctx context.Context, config *gitOps.GitOpsConfigDto) (string, error)
3737
}

pkg/deployment/gitOps/git/GitServiceAzure.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,24 @@ type GitAzureClient struct {
3939
gitOpsHelper *GitOpsHelper
4040
}
4141

42-
func (impl GitAzureClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
42+
func (impl GitAzureClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, isRepoEmpty bool, err error) {
4343

4444
start := time.Now()
4545
defer func() {
4646
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitAzureClient", start, err)
4747
}()
4848

49-
url, exists, err := impl.repoExists(config.GitRepoName, impl.project)
49+
var (
50+
url string
51+
exists bool
52+
)
53+
url, exists, isRepoEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
5054
if err != nil {
51-
return "", err
55+
return "", isRepoEmpty, err
5256
} else if !exists {
53-
return "", fmt.Errorf("%s :repo not found", config.GitRepoName)
57+
return "", isRepoEmpty, fmt.Errorf("%s :repo not found", config.GitRepoName)
5458
} else {
55-
return url, nil
59+
return url, isRepoEmpty, nil
5660
}
5761
}
5862

@@ -96,23 +100,26 @@ func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) (err
96100
return err
97101
}
98102

99-
func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
100-
var err error
103+
func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
104+
var (
105+
err error
106+
repoExists bool
107+
)
101108
start := time.Now()
102109
defer func() {
103110
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
104111
}()
105112

106113
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
107-
url, repoExists, err := impl.repoExists(config.GitRepoName, impl.project)
114+
url, repoExists, isEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
108115
if err != nil {
109116
impl.logger.Errorw("error in communication with azure", "err", err)
110117
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
111-
return "", false, detailedErrorGitOpsConfigActions
118+
return "", false, isEmpty, detailedErrorGitOpsConfigActions
112119
}
113120
if repoExists {
114121
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, GetRepoUrlStage)
115-
return url, false, detailedErrorGitOpsConfigActions
122+
return url, false, isEmpty, detailedErrorGitOpsConfigActions
116123
}
117124
gitRepositoryCreateOptions := git.GitRepositoryCreateOptions{
118125
Name: &config.GitRepoName,
@@ -125,12 +132,12 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
125132
if err != nil {
126133
impl.logger.Errorw("error in creating repo azure", "project", config.GitRepoName, "err", err)
127134
detailedErrorGitOpsConfigActions.StageErrorMap[CreateRepoStage] = err
128-
url, repoExists, err = impl.repoExists(config.GitRepoName, impl.project)
135+
url, repoExists, isEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
129136
if err != nil {
130137
impl.logger.Errorw("error in communication with azure", "err", err)
131138
}
132139
if err != nil || !repoExists {
133-
return "", true, detailedErrorGitOpsConfigActions
140+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
134141
}
135142
}
136143
impl.logger.Infow("repo created ", "r", operationReference.WebUrl)
@@ -139,34 +146,35 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
139146
if err != nil {
140147
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
141148
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
142-
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
149+
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
143150
}
144151
if !validated {
145152
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
146-
return "", true, detailedErrorGitOpsConfigActions
153+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
147154
}
148155
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneHttpStage)
149156

150157
_, err = impl.CreateReadme(ctx, config)
151158
if err != nil {
152159
impl.logger.Errorw("error in creating readme azure", "project", config.GitRepoName, "err", err)
153160
detailedErrorGitOpsConfigActions.StageErrorMap[CreateReadmeStage] = err
154-
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
161+
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
155162
}
163+
isEmpty = false //As we have created readme, repo is no longer empty
156164
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateReadmeStage)
157165

158166
validated, err = impl.ensureProjectAvailabilityOnSsh(impl.project, *operationReference.WebUrl)
159167
if err != nil {
160168
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
161169
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
162-
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
170+
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
163171
}
164172
if !validated {
165173
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
166-
return "", true, detailedErrorGitOpsConfigActions
174+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
167175
}
168176
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneSshStage)
169-
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
177+
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
170178
}
171179

172180
func (impl GitAzureClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
@@ -296,7 +304,7 @@ func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig
296304
return commitId, commitAuthorTime, nil
297305
}
298306

299-
func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl string, exists bool, err error) {
307+
func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl string, exists, isRepoEmpty bool, err error) {
300308

301309
start := time.Now()
302310
defer func() {
@@ -313,16 +321,17 @@ func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl str
313321
notFoundStatus := 404
314322
if err != nil {
315323
if e, ok := err.(azuredevops.WrappedError); ok && *e.StatusCode == notFoundStatus {
316-
return "", false, nil
324+
return "", false, isRepoEmpty, nil
317325
} else {
318-
return "", false, err
326+
return "", false, isRepoEmpty, err
319327
}
320328

321329
}
322330
for gitRepository == nil {
323-
return "", false, nil
331+
return "", false, isRepoEmpty, nil
324332
}
325-
return *gitRepository.WebUrl, true, nil
333+
334+
return *gitRepository.WebUrl, true, *gitRepository.Size == 0, nil
326335
}
327336

328337
func (impl GitAzureClient) ensureProjectAvailabilityOnHttp(repoName string) (bool, error) {
@@ -333,7 +342,7 @@ func (impl GitAzureClient) ensureProjectAvailabilityOnHttp(repoName string) (boo
333342
}()
334343

335344
for count := 0; count < 5; count++ {
336-
_, exists, err := impl.repoExists(repoName, impl.project)
345+
_, exists, _, err := impl.repoExists(repoName, impl.project)
337346
if err == nil && exists {
338347
impl.logger.Infow("repo validated successfully on https")
339348
return true, nil

pkg/deployment/gitOps/git/GitServiceBitbucket.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (impl GitBitbucketClient) DeleteRepository(config *bean2.GitOpsConfigDto) (
8181
return err
8282
}
8383

84-
func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
84+
func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, isRepoEmpty bool, err error) {
8585
start := time.Now()
8686
defer func() {
8787
util.TriggerGitOpsMetrics("GetRepoUrl", "GitBitbucketClient", start, err)
@@ -94,16 +94,16 @@ func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUr
9494
}
9595
_, exists, err := impl.repoExists(repoOptions)
9696
if err != nil {
97-
return "", err
97+
return "", isRepoEmpty, err
9898
} else if !exists {
99-
return "", fmt.Errorf("%s :repo not found", repoOptions.RepoSlug)
99+
return "", isRepoEmpty, fmt.Errorf("%s :repo not found", repoOptions.RepoSlug)
100100
} else {
101101
repoUrl = fmt.Sprintf(BITBUCKET_CLONE_BASE_URL+"%s/%s.git", repoOptions.Owner, repoOptions.RepoSlug)
102-
return repoUrl, nil
102+
return repoUrl, isRepoEmpty, nil
103103
}
104104
}
105105

106-
func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
106+
func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
107107
var err error
108108
start := time.Now()
109109
defer func() {
@@ -127,11 +127,11 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
127127
if err != nil {
128128
impl.logger.Errorw("error in communication with bitbucket", "repoOptions", repoOptions, "err", err)
129129
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
130-
return "", false, detailedErrorGitOpsConfigActions
130+
return "", false, isEmpty, detailedErrorGitOpsConfigActions
131131
}
132132
if repoExists {
133133
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, GetRepoUrlStage)
134-
return repoUrl, false, detailedErrorGitOpsConfigActions
134+
return repoUrl, false, isEmpty, detailedErrorGitOpsConfigActions
135135
}
136136
_, err = impl.client.Repositories.Repository.Create(repoOptions)
137137
if err != nil {
@@ -142,7 +142,7 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
142142
impl.logger.Errorw("error in creating repo bitbucket", "repoOptions", repoOptions, "err", err)
143143
}
144144
if err != nil || !repoExists {
145-
return "", true, detailedErrorGitOpsConfigActions
145+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
146146
}
147147
}
148148
repoUrl = fmt.Sprintf(BITBUCKET_CLONE_BASE_URL+"%s/%s.git", repoOptions.Owner, repoOptions.RepoSlug)
@@ -153,34 +153,34 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
153153
if err != nil {
154154
impl.logger.Errorw("error in ensuring project availability bitbucket", "repoName", repoOptions.RepoSlug, "err", err)
155155
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
156-
return "", true, detailedErrorGitOpsConfigActions
156+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
157157
}
158158
if !validated {
159159
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
160-
return "", true, detailedErrorGitOpsConfigActions
160+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
161161
}
162162
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneHttpStage)
163163

164164
_, err = impl.CreateReadme(ctx, config)
165165
if err != nil {
166166
impl.logger.Errorw("error in creating readme bitbucket", "repoName", repoOptions.RepoSlug, "err", err)
167167
detailedErrorGitOpsConfigActions.StageErrorMap[CreateReadmeStage] = err
168-
return "", true, detailedErrorGitOpsConfigActions
168+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
169169
}
170170
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateReadmeStage)
171171

172172
validated, err = impl.ensureProjectAvailabilityOnSsh(repoOptions)
173173
if err != nil {
174174
impl.logger.Errorw("error in ensuring project availability bitbucket", "project", config.GitRepoName, "err", err)
175175
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
176-
return "", true, detailedErrorGitOpsConfigActions
176+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
177177
}
178178
if !validated {
179179
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
180-
return "", true, detailedErrorGitOpsConfigActions
180+
return "", true, isEmpty, detailedErrorGitOpsConfigActions
181181
}
182182
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneSshStage)
183-
return repoUrl, true, detailedErrorGitOpsConfigActions
183+
return repoUrl, true, isEmpty, detailedErrorGitOpsConfigActions
184184
}
185185

186186
func (impl GitBitbucketClient) repoExists(repoOptions *bitbucket.RepositoryOptions) (repoUrl string, exists bool, err error) {

0 commit comments

Comments
 (0)