Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (impl *FullModeDeploymentServiceImpl) createGitOpsRepo(gitOpsRepoName strin
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
}
repoUrl, isNew, err := impl.gitOperationService.CreateRepository(context.Background(), gitRepoRequest, userId)
repoUrl, isNew, _, err := impl.gitOperationService.CreateRepository(context.Background(), gitRepoRequest, userId)
if err != nil {
impl.Logger.Errorw("error in creating git project", "name", gitOpsRepoName, "err", err)
return "", false, err
Expand Down
1 change: 1 addition & 0 deletions pkg/deployment/gitOps/common/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ package bean
type ChartGitAttribute struct {
RepoUrl, ChartLocation string
IsNewRepo bool
IsRepoEmpty bool
}
16 changes: 8 additions & 8 deletions pkg/deployment/gitOps/git/GitOperationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type GitOperationService interface {
PushChartToGitRepo(ctx context.Context, gitOpsRepoName, referenceTemplate, version, tempReferenceTemplateDir, repoUrl string, userId int32) (err error)
PushChartToGitOpsRepoForHelmApp(ctx context.Context, PushChartToGitRequest *bean.PushChartToGitRequestDTO, requirementsConfig *ChartConfig, valuesConfig *ChartConfig) (*commonBean.ChartGitAttribute, string, error)

CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, error)
CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, bool, error)
GetRepoUrlByRepoName(repoName string) (string, error)

CloneInDir(repoUrl, chartDir string) (string, error)
Expand Down Expand Up @@ -98,12 +98,12 @@ func (impl *GitOperationServiceImpl) CreateGitRepositoryForDevtronApp(ctx contex
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
}
repoUrl, isNew, err := impl.CreateRepository(ctx, gitRepoRequest, userId)
repoUrl, isNew, isEmpty, err := impl.CreateRepository(ctx, gitRepoRequest, userId)
if err != nil {
impl.logger.Errorw("error in creating git project", "name", gitOpsRepoName, "err", err)
return nil, err
}
return &commonBean.ChartGitAttribute{RepoUrl: repoUrl, IsNewRepo: isNew}, nil
return &commonBean.ChartGitAttribute{RepoUrl: repoUrl, IsNewRepo: isNew, IsRepoEmpty: isEmpty}, nil
}

func getChartDirPathFromCloneDir(cloneDirPath string) (string, error) {
Expand Down Expand Up @@ -279,21 +279,21 @@ func (impl *GitOperationServiceImpl) isRetryableGitCommitError(err error) bool {
return false
}

func (impl *GitOperationServiceImpl) CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, error) {
func (impl *GitOperationServiceImpl) CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, bool, error) {
//getting username & emailId for commit author data
userEmailId, userName := impl.gitOpsConfigReadService.GetUserEmailIdAndNameForGitOpsCommit(userId)
if dto != nil {
dto.UserEmailId = userEmailId
dto.Username = userName
}
repoUrl, isNew, detailedError := impl.gitFactory.Client.CreateRepository(ctx, dto)
repoUrl, isNew, isEmpty, detailedError := impl.gitFactory.Client.CreateRepository(ctx, dto)
for _, err := range detailedError.StageErrorMap {
if err != nil {
impl.logger.Errorw("error in creating git project", "err", err, "req", dto)
return "", false, err
return "", false, false, err
}
}
return repoUrl, isNew, nil
return repoUrl, isNew, isEmpty, nil
}

func (impl *GitOperationServiceImpl) GetRepoUrlByRepoName(repoName string) (string, error) {
Expand All @@ -308,7 +308,7 @@ func (impl *GitOperationServiceImpl) GetRepoUrlByRepoName(repoName string) (stri
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
}
repoUrl, err = impl.gitFactory.Client.GetRepoUrl(dto)
repoUrl, _, err = impl.gitFactory.Client.GetRepoUrl(dto)
if err != nil {
//will allow to continue to persist status on next operation
impl.logger.Errorw("error in getting repo url", "err", err, "repoName", repoName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/deployment/gitOps/git/GitOpsClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
)

type GitOpsClient interface {
CreateRepository(ctx context.Context, config *gitOps.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
CreateRepository(ctx context.Context, config *gitOps.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *gitOps.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error)
GetRepoUrl(config *gitOps.GitOpsConfigDto) (repoUrl string, err error)
GetRepoUrl(config *gitOps.GitOpsConfigDto) (repoUrl string, isRepoEmpty bool, err error)
DeleteRepository(config *gitOps.GitOpsConfigDto) error
CreateReadme(ctx context.Context, config *gitOps.GitOpsConfigDto) (string, error)
}
Expand Down
57 changes: 33 additions & 24 deletions pkg/deployment/gitOps/git/GitServiceAzure.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@ type GitAzureClient struct {
gitOpsHelper *GitOpsHelper
}

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

start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitAzureClient", start, err)
}()

url, exists, err := impl.repoExists(config.GitRepoName, impl.project)
var (
url string
exists bool
)
url, exists, isRepoEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
return "", err
return "", isRepoEmpty, err
} else if !exists {
return "", fmt.Errorf("%s :repo not found", config.GitRepoName)
return "", isRepoEmpty, fmt.Errorf("%s :repo not found", config.GitRepoName)
} else {
return url, nil
return url, isRepoEmpty, nil
}
}

Expand Down Expand Up @@ -96,23 +100,26 @@ func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) (err
return err
}

func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var err error
func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var (
err error
repoExists bool
)
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
}()

detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
url, repoExists, err := impl.repoExists(config.GitRepoName, impl.project)
url, repoExists, isEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
impl.logger.Errorw("error in communication with azure", "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
return "", false, detailedErrorGitOpsConfigActions
return "", false, isEmpty, detailedErrorGitOpsConfigActions
}
if repoExists {
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, GetRepoUrlStage)
return url, false, detailedErrorGitOpsConfigActions
return url, false, isEmpty, detailedErrorGitOpsConfigActions
}
gitRepositoryCreateOptions := git.GitRepositoryCreateOptions{
Name: &config.GitRepoName,
Expand All @@ -125,12 +132,12 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
if err != nil {
impl.logger.Errorw("error in creating repo azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateRepoStage] = err
url, repoExists, err = impl.repoExists(config.GitRepoName, impl.project)
url, repoExists, isEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
impl.logger.Errorw("error in communication with azure", "err", err)
}
if err != nil || !repoExists {
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
}
impl.logger.Infow("repo created ", "r", operationReference.WebUrl)
Expand All @@ -139,34 +146,35 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
if err != nil {
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneHttpStage)

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

validated, err = impl.ensureProjectAvailabilityOnSsh(impl.project, *operationReference.WebUrl)
if err != nil {
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneSshStage)
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}

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

func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl string, exists bool, err error) {
func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl string, exists, isRepoEmpty bool, err error) {

start := time.Now()
defer func() {
Expand All @@ -313,16 +321,17 @@ func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl str
notFoundStatus := 404
if err != nil {
if e, ok := err.(azuredevops.WrappedError); ok && *e.StatusCode == notFoundStatus {
return "", false, nil
return "", false, isRepoEmpty, nil
} else {
return "", false, err
return "", false, isRepoEmpty, err
}

}
for gitRepository == nil {
return "", false, nil
return "", false, isRepoEmpty, nil
}
return *gitRepository.WebUrl, true, nil

return *gitRepository.WebUrl, true, *gitRepository.Size == 0, nil
}

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

for count := 0; count < 5; count++ {
_, exists, err := impl.repoExists(repoName, impl.project)
_, exists, _, err := impl.repoExists(repoName, impl.project)
if err == nil && exists {
impl.logger.Infow("repo validated successfully on https")
return true, nil
Expand Down
28 changes: 14 additions & 14 deletions pkg/deployment/gitOps/git/GitServiceBitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (impl GitBitbucketClient) DeleteRepository(config *bean2.GitOpsConfigDto) (
return err
}

func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, isRepoEmpty bool, err error) {
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("GetRepoUrl", "GitBitbucketClient", start, err)
Expand All @@ -94,16 +94,16 @@ func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUr
}
_, exists, err := impl.repoExists(repoOptions)
if err != nil {
return "", err
return "", isRepoEmpty, err
} else if !exists {
return "", fmt.Errorf("%s :repo not found", repoOptions.RepoSlug)
return "", isRepoEmpty, fmt.Errorf("%s :repo not found", repoOptions.RepoSlug)
} else {
repoUrl = fmt.Sprintf(BITBUCKET_CLONE_BASE_URL+"%s/%s.git", repoOptions.Owner, repoOptions.RepoSlug)
return repoUrl, nil
return repoUrl, isRepoEmpty, nil
}
}

func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var err error
start := time.Now()
defer func() {
Expand All @@ -127,11 +127,11 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
if err != nil {
impl.logger.Errorw("error in communication with bitbucket", "repoOptions", repoOptions, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
return "", false, detailedErrorGitOpsConfigActions
return "", false, isEmpty, detailedErrorGitOpsConfigActions
}
if repoExists {
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, GetRepoUrlStage)
return repoUrl, false, detailedErrorGitOpsConfigActions
return repoUrl, false, isEmpty, detailedErrorGitOpsConfigActions
}
_, err = impl.client.Repositories.Repository.Create(repoOptions)
if err != nil {
Expand All @@ -142,7 +142,7 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
impl.logger.Errorw("error in creating repo bitbucket", "repoOptions", repoOptions, "err", err)
}
if err != nil || !repoExists {
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
}
repoUrl = fmt.Sprintf(BITBUCKET_CLONE_BASE_URL+"%s/%s.git", repoOptions.Owner, repoOptions.RepoSlug)
Expand All @@ -153,34 +153,34 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
if err != nil {
impl.logger.Errorw("error in ensuring project availability bitbucket", "repoName", repoOptions.RepoSlug, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneHttpStage)

_, err = impl.CreateReadme(ctx, config)
if err != nil {
impl.logger.Errorw("error in creating readme bitbucket", "repoName", repoOptions.RepoSlug, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateReadmeStage] = err
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateReadmeStage)

validated, err = impl.ensureProjectAvailabilityOnSsh(repoOptions)
if err != nil {
impl.logger.Errorw("error in ensuring project availability bitbucket", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
return "", true, detailedErrorGitOpsConfigActions
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneSshStage)
return repoUrl, true, detailedErrorGitOpsConfigActions
return repoUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}

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