Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bitbucket gitops config fixes #2768

Merged
merged 17 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
20 changes: 19 additions & 1 deletion api/bean/GitOpsConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,23 @@ type GitOpsConfigDto struct {
AzureProjectName string `json:"azureProjectName"`
BitBucketWorkspaceId string `json:"bitBucketWorkspaceId"`
BitBucketProjectKey string `json:"bitBucketProjectKey"`
UserId int32 `json:"-"`

GitRepoName string `json:"gitRepoName"`
UserEmailId string `json:"userEmailId"`
Description string `json:"description"`
UserId int32 `json:"-"`
}

type GitRepoRequestDto struct {
Host string `json:"host"`
Provider string `json:"provider"`
GitRepoName string `json:"gitRepoName"`
Username string `json:"username"`
UserEmailId string `json:"userEmailId"`
Token string `json:"token"`
GitLabGroupId string `json:"gitLabGroupId"`
GitHubOrgId string `json:"gitHubOrgId"`
AzureProjectName string `json:"azureProjectName"`
BitBucketWorkspaceId string `json:"bitBucketWorkspaceId"`
BitBucketProjectKey string `json:"bitBucketProjectKey"`
}
47 changes: 44 additions & 3 deletions internal/util/ChartService.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"fmt"
repository3 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/devtron-labs/devtron/api/bean"
repository4 "github.com/devtron-labs/devtron/client/argocdServer/repository"
"github.com/devtron-labs/devtron/internal/sql/repository"
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
repository2 "github.com/devtron-labs/devtron/pkg/user/repository"
"github.com/devtron-labs/devtron/util"
"github.com/go-pg/pg"
"io/ioutil"
"math/rand"
"net/http"
Expand Down Expand Up @@ -222,9 +224,25 @@ func (impl ChartTemplateServiceImpl) CreateGitRepositoryForApp(gitOpsRepoName, b
space := regexp.MustCompile(`\s+`)
gitOpsRepoName = space.ReplaceAllString(gitOpsRepoName, "-")

gitOpsConfigBitbucket, err := impl.gitOpsConfigRepository.GetGitOpsConfigByProvider(BITBUCKET_PROVIDER)
if err != nil {
if err == pg.ErrNoRows {
gitOpsConfigBitbucket.BitBucketWorkspaceId = ""
} else {
return nil, err
}
}
//getting user name & emailId for commit author data
userEmailId, userName := impl.GetUserEmailIdAndNameForGitOpsCommit(userId)
repoUrl, _, detailedError := impl.gitFactory.Client.CreateRepository(gitOpsRepoName, fmt.Sprintf("helm chart for "+gitOpsRepoName), userName, userEmailId)
gitRepoRequest := &bean.GitOpsConfigDto{
GitRepoName: gitOpsRepoName,
Description: fmt.Sprintf("helm chart for " + gitOpsRepoName),
Username: userName,
UserEmailId: userEmailId,
BitBucketWorkspaceId: gitOpsConfigBitbucket.BitBucketWorkspaceId,
BitBucketProjectKey: gitOpsConfigBitbucket.BitBucketProjectKey,
}
repoUrl, _, detailedError := impl.gitFactory.Client.CreateRepository(gitRepoRequest)
for _, err := range detailedError.StageErrorMap {
if err != nil {
impl.logger.Errorw("error in creating git project", "name", gitOpsRepoName, "err", err)
Expand Down Expand Up @@ -487,9 +505,25 @@ func (impl ChartTemplateServiceImpl) createAndPushToGitChartProxy(appStoreName,
gitOpsRepoName := impl.GetGitOpsRepoName(installAppVersionRequest.AppName)
installAppVersionRequest.GitOpsRepoName = gitOpsRepoName
}
gitOpsConfigBitbucket, err := impl.gitOpsConfigRepository.GetGitOpsConfigByProvider(BITBUCKET_PROVIDER)
if err != nil {
if err == pg.ErrNoRows {
gitOpsConfigBitbucket.BitBucketWorkspaceId = ""
} else {
return nil, err
}
}
//getting user name & emailId for commit author data
userEmailId, userName := impl.GetUserEmailIdAndNameForGitOpsCommit(installAppVersionRequest.UserId)
repoUrl, _, detailedError := impl.gitFactory.Client.CreateRepository(installAppVersionRequest.GitOpsRepoName, "helm chart for "+installAppVersionRequest.GitOpsRepoName, userName, userEmailId)
gitRepoRequest := &bean.GitOpsConfigDto{
GitRepoName: installAppVersionRequest.GitOpsRepoName,
Description: "helm chart for " + installAppVersionRequest.GitOpsRepoName,
Username: userName,
UserEmailId: userEmailId,
BitBucketWorkspaceId: gitOpsConfigBitbucket.BitBucketWorkspaceId,
BitBucketProjectKey: gitOpsConfigBitbucket.BitBucketProjectKey,
}
repoUrl, _, detailedError := impl.gitFactory.Client.CreateRepository(gitRepoRequest)
for _, err := range detailedError.StageErrorMap {
if err != nil {
impl.logger.Errorw("error in creating git project", "name", installAppVersionRequest.GitOpsRepoName, "err", err)
Expand Down Expand Up @@ -641,7 +675,14 @@ func (impl ChartTemplateServiceImpl) GetByteArrayRefChart(chartMetaData *chart.M

func (impl ChartTemplateServiceImpl) CreateReadmeInGitRepo(gitOpsRepoName string, userId int32) error {
userEmailId, userName := impl.GetUserEmailIdAndNameForGitOpsCommit(userId)
_, err := impl.gitFactory.Client.CreateReadme(gitOpsRepoName, userName, userEmailId)
gitOpsConfig, err := impl.gitOpsConfigRepository.GetGitOpsConfigActive()
config := &bean.GitOpsConfigDto{
Username: userName,
UserEmailId: userEmailId,
GitRepoName: gitOpsRepoName,
BitBucketWorkspaceId: gitOpsConfig.BitBucketWorkspaceId,
}
_, err = impl.gitFactory.Client.CreateReadme(config)
if err != nil {
return err
}
Expand Down
32 changes: 17 additions & 15 deletions internal/util/GitService.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ const (
)

type GitClient interface {
CreateRepository(name, description, userName, userEmailId string) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
CommitValues(config *ChartConfig) (commitHash string, commitTime time.Time, err error)
GetRepoUrl(projectName string) (repoUrl string, err error)
DeleteRepository(name string) error
CreateReadme(name, userName, userEmailId string) (string, error)
CreateRepository(config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
CommitValues(config *ChartConfig, bitBucketWorkspaceId string) (commitHash string, commitTime time.Time, err error)
GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error)
DeleteRepository(config *bean2.GitOpsConfigDto) error
CreateReadme(config *bean2.GitOpsConfigDto) (string, error)
GetCommits(repoName, projectName string) ([]*GitCommitDto, error)
}

Expand Down Expand Up @@ -132,15 +132,17 @@ func (factory *GitFactory) GetGitLabGroupPath(gitOpsConfig *bean2.GitOpsConfigDt

func (factory *GitFactory) NewClientForValidation(gitOpsConfig *bean2.GitOpsConfigDto) (GitClient, *GitServiceImpl, error) {
cfg := &GitConfig{
GitlabGroupId: gitOpsConfig.GitLabGroupId,
GitToken: gitOpsConfig.Token,
GitUserName: gitOpsConfig.Username,
GitWorkingDir: GIT_WORKING_DIR,
GithubOrganization: gitOpsConfig.GitHubOrgId,
GitProvider: gitOpsConfig.Provider,
GitHost: gitOpsConfig.Host,
AzureToken: gitOpsConfig.Token,
AzureProject: gitOpsConfig.AzureProjectName,
GitlabGroupId: gitOpsConfig.GitLabGroupId,
GitToken: gitOpsConfig.Token,
GitUserName: gitOpsConfig.Username,
GitWorkingDir: GIT_WORKING_DIR,
GithubOrganization: gitOpsConfig.GitHubOrgId,
GitProvider: gitOpsConfig.Provider,
GitHost: gitOpsConfig.Host,
AzureToken: gitOpsConfig.Token,
AzureProject: gitOpsConfig.AzureProjectName,
BitbucketWorkspaceId: gitOpsConfig.BitBucketWorkspaceId,
BitbucketProjectKey: gitOpsConfig.BitBucketProjectKey,
}
gitService := NewGitServiceImpl(cfg, logger, factory.gitCliUtil)
//factory.gitService = gitService
Expand All @@ -150,7 +152,7 @@ func (factory *GitFactory) NewClientForValidation(gitOpsConfig *bean2.GitOpsConf
}

//factory.Client = client
logger.Infow("client changed successfully")
logger.Infow("client changed successfully", "cfg", cfg)
return client, gitService, nil
}

Expand Down
67 changes: 29 additions & 38 deletions internal/util/GitServiceAzure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package util
import (
"context"
"fmt"
bean2 "github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/go-pg/pg"
"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
"go.uber.org/zap"
Expand All @@ -20,12 +20,12 @@ type GitAzureClient struct {
gitOpsConfigRepository repository.GitOpsConfigRepository
}

func (impl GitAzureClient) GetRepoUrl(repoName string) (repoUrl string, err error) {
url, exists, err := impl.repoExists(repoName, impl.project)
func (impl GitAzureClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
url, exists, err := impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
return "", err
} else if !exists {
return "", fmt.Errorf("%s :repo not found", repoName)
return "", fmt.Errorf("%s :repo not found", config.GitRepoName)
} else {
return url, nil
}
Expand All @@ -49,35 +49,26 @@ func NewGitAzureClient(token string, host string, project string, logger *zap.Su
gitOpsConfigRepository: gitOpsConfigRepository,
}, err
}
func (impl GitAzureClient) DeleteRepository(name string) error {
gitOpsConfigBitbucket, err := impl.gitOpsConfigRepository.GetGitOpsConfigByProvider(BITBUCKET_PROVIDER)
if err != nil {
if err == pg.ErrNoRows {
gitOpsConfigBitbucket.AzureProject = ""
} else {
impl.logger.Errorw("error in fetching gitOps bitbucket config", "err", err)
return err
}
}
func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
clientAzure := *impl.client
gitRepository, err := clientAzure.GetRepository(context.Background(), git.GetRepositoryArgs{
RepositoryId: &name,
Project: &gitOpsConfigBitbucket.AzureProject,
RepositoryId: &config.GitRepoName,
Project: &config.AzureProjectName,
})
if err != nil || gitRepository == nil {
impl.logger.Errorw("error in fetching repo azure", "project", name, "err", err)
impl.logger.Errorw("error in fetching repo azure", "project", config.GitRepoName, "err", err)
return err
}
err = clientAzure.DeleteRepository(context.Background(), git.DeleteRepositoryArgs{RepositoryId: gitRepository.Id, Project: &impl.project})
if err != nil {
impl.logger.Errorw("error in deleting repo azure", "project", name, "err", err)
impl.logger.Errorw("error in deleting repo azure", "project", config.GitRepoName, "err", err)
}
return err
}
func (impl GitAzureClient) CreateRepository(name, description, userName, userEmailId string) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
func (impl GitAzureClient) CreateRepository(config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
ctx := context.Background()
url, repoExists, err := impl.repoExists(name, impl.project)
url, repoExists, err := impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
impl.logger.Errorw("error in communication with azure", "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
Expand All @@ -88,73 +79,73 @@ func (impl GitAzureClient) CreateRepository(name, description, userName, userEma
return url, false, detailedErrorGitOpsConfigActions
}
gitRepositoryCreateOptions := git.GitRepositoryCreateOptions{
Name: &name,
Name: &config.GitRepoName,
}
clientAzure := *impl.client
operationReference, err := clientAzure.CreateRepository(ctx, git.CreateRepositoryArgs{
GitRepositoryToCreate: &gitRepositoryCreateOptions,
Project: &impl.project,
})
if err != nil {
impl.logger.Errorw("error in creating repo azure", "project", name, "err", err)
impl.logger.Errorw("error in creating repo azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateRepoStage] = err
return "", true, detailedErrorGitOpsConfigActions
}
logger.Infow("repo created ", "r", operationReference.WebUrl)
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateRepoStage)
validated, err := impl.ensureProjectAvailabilityOnHttp(name)
validated, err := impl.ensureProjectAvailabilityOnHttp(config.GitRepoName)
if err != nil {
impl.logger.Errorw("error in ensuring project availability azure", "project", name, "err", err)
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", name)
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(name, userName, userEmailId)
_, err = impl.CreateReadme(config)
if err != nil {
impl.logger.Errorw("error in creating readme azure", "project", name, "err", err)
impl.logger.Errorw("error in creating readme azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateReadmeStage] = err
return *operationReference.WebUrl, true, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CreateReadmeStage)

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

func (impl GitAzureClient) CreateReadme(repoName, userName, userEmailId string) (string, error) {
func (impl GitAzureClient) CreateReadme(config *bean2.GitOpsConfigDto) (string, error) {
cfg := &ChartConfig{
ChartName: repoName,
ChartName: config.GitRepoName,
ChartLocation: "",
FileName: "README.md",
FileContent: "@devtron",
ReleaseMessage: "readme",
ChartRepoName: repoName,
UserName: userName,
UserEmailId: userEmailId,
ChartRepoName: config.GitRepoName,
UserName: config.Username,
UserEmailId: config.UserEmailId,
}
hash, _, err := impl.CommitValues(cfg)
hash, _, err := impl.CommitValues(cfg, "")
if err != nil {
impl.logger.Errorw("error in creating readme azure", "repo", repoName, "err", err)
impl.logger.Errorw("error in creating readme azure", "repo", config.GitRepoName, "err", err)
}
return hash, err
}

func (impl GitAzureClient) CommitValues(config *ChartConfig) (commitHash string, commitTime time.Time, err error) {
func (impl GitAzureClient) CommitValues(config *ChartConfig, bitBucketWorkspaceId string) (commitHash string, commitTime time.Time, err error) {
branch := "master"
branchfull := "refs/heads/master"
path := filepath.Join(config.ChartLocation, config.FileName)
Expand Down
Loading