@@ -19,7 +19,9 @@ package util
1919
2020import (
2121 "fmt"
22+ "github.com/devtron-labs/devtron/internal/sql/repository"
2223 appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
24+ repository2 "github.com/devtron-labs/devtron/pkg/user/repository"
2325 "github.com/devtron-labs/devtron/util"
2426 "io/ioutil"
2527 "math/rand"
@@ -42,21 +44,24 @@ import (
4244type ChartWorkingDir string
4345
4446type ChartTemplateService interface {
45- CreateChart (chartMetaData * chart.Metadata , refChartLocation string , templateName string ) (* ChartValues , * ChartGitAttribute , error )
47+ CreateChart (chartMetaData * chart.Metadata , refChartLocation string , templateName string , userId int32 ) (* ChartValues , * ChartGitAttribute , error )
4648 GetChartVersion (location string ) (string , error )
4749 CreateChartProxy (chartMetaData * chart.Metadata , refChartLocation string , templateName string , version string , envName string , installAppVersionRequest * appStoreBean.InstallAppVersionDTO ) (string , * ChartGitAttribute , error )
4850 GitPull (clonedDir string , repoUrl string , appStoreName string ) error
4951 GetDir () string
52+ GetUserEmailIdAndNameForGitOpsCommit (userId int32 ) (emailId , name string )
5053 GetGitOpsRepoName (appName string ) string
5154 GetGitOpsRepoNameFromUrl (gitRepoUrl string ) string
5255}
5356type ChartTemplateServiceImpl struct {
54- randSource rand.Source
55- logger * zap.SugaredLogger
56- chartWorkingDir ChartWorkingDir
57- gitFactory * GitFactory
58- client * http.Client
59- globalEnvVariables * util.GlobalEnvVariables
57+ randSource rand.Source
58+ logger * zap.SugaredLogger
59+ chartWorkingDir ChartWorkingDir
60+ gitFactory * GitFactory
61+ client * http.Client
62+ globalEnvVariables * util.GlobalEnvVariables
63+ gitOpsConfigRepository repository.GitOpsConfigRepository
64+ userRepository repository2.UserRepository
6065}
6166
6267type ChartValues struct {
@@ -71,14 +76,18 @@ type ChartValues struct {
7176func NewChartTemplateServiceImpl (logger * zap.SugaredLogger ,
7277 chartWorkingDir ChartWorkingDir ,
7378 client * http.Client ,
74- gitFactory * GitFactory , globalEnvVariables * util.GlobalEnvVariables ) * ChartTemplateServiceImpl {
79+ gitFactory * GitFactory , globalEnvVariables * util.GlobalEnvVariables ,
80+ gitOpsConfigRepository repository.GitOpsConfigRepository ,
81+ userRepository repository2.UserRepository ) * ChartTemplateServiceImpl {
7582 return & ChartTemplateServiceImpl {
76- randSource : rand .NewSource (time .Now ().UnixNano ()),
77- logger : logger ,
78- chartWorkingDir : chartWorkingDir ,
79- client : client ,
80- gitFactory : gitFactory ,
81- globalEnvVariables : globalEnvVariables ,
83+ randSource : rand .NewSource (time .Now ().UnixNano ()),
84+ logger : logger ,
85+ chartWorkingDir : chartWorkingDir ,
86+ client : client ,
87+ gitFactory : gitFactory ,
88+ globalEnvVariables : globalEnvVariables ,
89+ gitOpsConfigRepository : gitOpsConfigRepository ,
90+ userRepository : userRepository ,
8291 }
8392}
8493
@@ -107,7 +116,7 @@ func (impl ChartTemplateServiceImpl) GetChartVersion(location string) (string, e
107116 return chartContent .Version , nil
108117}
109118
110- func (impl ChartTemplateServiceImpl ) CreateChart (chartMetaData * chart.Metadata , refChartLocation string , templateName string ) (* ChartValues , * ChartGitAttribute , error ) {
119+ func (impl ChartTemplateServiceImpl ) CreateChart (chartMetaData * chart.Metadata , refChartLocation string , templateName string , userId int32 ) (* ChartValues , * ChartGitAttribute , error ) {
111120 chartMetaData .ApiVersion = "v1" // ensure always v1
112121 dir := impl .GetDir ()
113122 chartDir := filepath .Join (string (impl .chartWorkingDir ), dir )
@@ -137,7 +146,7 @@ func (impl ChartTemplateServiceImpl) CreateChart(chartMetaData *chart.Metadata,
137146 }
138147 values .Values = valuesYaml
139148 gitOpsRepoName := impl .GetGitOpsRepoName (chartMetaData .Name )
140- chartGitAttr , err := impl .createAndPushToGit (gitOpsRepoName , templateName , chartMetaData .Version , chartDir )
149+ chartGitAttr , err := impl .createAndPushToGit (gitOpsRepoName , templateName , chartMetaData .Version , chartDir , userId )
141150 if err != nil {
142151 impl .logger .Errorw ("error in pushing chart to git " , "path" , archivePath , "err" , err )
143152 return nil , nil , err
@@ -155,7 +164,7 @@ type ChartGitAttribute struct {
155164 RepoUrl , ChartLocation string
156165}
157166
158- func (impl ChartTemplateServiceImpl ) createAndPushToGit (gitOpsRepoName , baseTemplateName , version , tmpChartLocation string ) (chartGitAttribute * ChartGitAttribute , err error ) {
167+ func (impl ChartTemplateServiceImpl ) createAndPushToGit (gitOpsRepoName , baseTemplateName , version , tmpChartLocation string , userId int32 ) (chartGitAttribute * ChartGitAttribute , err error ) {
159168 //baseTemplateName replace whitespace
160169 space := regexp .MustCompile (`\s+` )
161170 gitOpsRepoName = space .ReplaceAllString (gitOpsRepoName , "-" )
@@ -170,7 +179,9 @@ func (impl ChartTemplateServiceImpl) createAndPushToGit(gitOpsRepoName, baseTemp
170179 return nil , err
171180 }
172181 }
173- repoUrl , _ , detailedError := impl .gitFactory .Client .CreateRepository (gitOpsRepoName , fmt .Sprintf ("helm chart for " + gitOpsRepoName ), gitOpsConfigBitbucket .BitBucketWorkspaceId , gitOpsConfigBitbucket .BitBucketProjectKey )
182+ //getting user name & emailId for commit author data
183+ userEmailId , userName := impl .GetUserEmailIdAndNameForGitOpsCommit (userId )
184+ repoUrl , _ , detailedError := impl .gitFactory .Client .CreateRepository (gitOpsRepoName , fmt .Sprintf ("helm chart for " + gitOpsRepoName ), gitOpsConfigBitbucket .BitBucketWorkspaceId , gitOpsConfigBitbucket .BitBucketProjectKey , userName , userEmailId )
174185
175186 for _ , err := range detailedError .StageErrorMap {
176187 if err != nil {
@@ -205,7 +216,7 @@ func (impl ChartTemplateServiceImpl) createAndPushToGit(gitOpsRepoName, baseTemp
205216 impl .logger .Errorw ("error copying dir" , "err" , err )
206217 return nil , nil
207218 }
208- commit , err := impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" )
219+ commit , err := impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" , userName , userEmailId )
209220 if err != nil {
210221 impl .logger .Errorw ("error in pushing git" , "err" , err )
211222 impl .logger .Warn ("re-trying, taking pull and then push again" )
@@ -218,7 +229,7 @@ func (impl ChartTemplateServiceImpl) createAndPushToGit(gitOpsRepoName, baseTemp
218229 impl .logger .Errorw ("error copying dir" , "err" , err )
219230 return nil , err
220231 }
221- commit , err = impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" )
232+ commit , err = impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" , userName , userEmailId )
222233 if err != nil {
223234 impl .logger .Errorw ("error in pushing git" , "err" , err )
224235 return nil , err
@@ -411,7 +422,9 @@ func (impl ChartTemplateServiceImpl) createAndPushToGitChartProxy(appStoreName,
411422 return nil , err
412423 }
413424 }
414- repoUrl , _ , detailedError := impl .gitFactory .Client .CreateRepository (installAppVersionRequest .GitOpsRepoName , "helm chart for " + installAppVersionRequest .GitOpsRepoName , gitOpsConfigBitbucket .BitBucketWorkspaceId , gitOpsConfigBitbucket .BitBucketProjectKey )
425+ //getting user name & emailId for commit author data
426+ userEmailId , userName := impl .GetUserEmailIdAndNameForGitOpsCommit (installAppVersionRequest .UserId )
427+ repoUrl , _ , detailedError := impl .gitFactory .Client .CreateRepository (installAppVersionRequest .GitOpsRepoName , "helm chart for " + installAppVersionRequest .GitOpsRepoName , gitOpsConfigBitbucket .BitBucketWorkspaceId , gitOpsConfigBitbucket .BitBucketProjectKey , userName , userEmailId )
415428 for _ , err := range detailedError .StageErrorMap {
416429 if err != nil {
417430 impl .logger .Errorw ("error in creating git project" , "name" , installAppVersionRequest .GitOpsRepoName , "err" , err )
@@ -445,7 +458,7 @@ func (impl ChartTemplateServiceImpl) createAndPushToGitChartProxy(appStoreName,
445458 impl .logger .Errorw ("error copying dir" , "err" , err )
446459 return nil , err
447460 }
448- commit , err := impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" )
461+ commit , err := impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" , userName , userEmailId )
449462 if err != nil {
450463 impl .logger .Errorw ("error in pushing git" , "err" , err )
451464 impl .logger .Warn ("re-trying, taking pull and then push again" )
@@ -458,7 +471,7 @@ func (impl ChartTemplateServiceImpl) createAndPushToGitChartProxy(appStoreName,
458471 impl .logger .Errorw ("error copying dir" , "err" , err )
459472 return nil , err
460473 }
461- commit , err = impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" )
474+ commit , err = impl .gitFactory .gitService .CommitAndPushAllChanges (clonedDir , "first commit" , userName , userEmailId )
462475 if err != nil {
463476 impl .logger .Errorw ("error in pushing git" , "err" , err )
464477 return nil , err
@@ -483,6 +496,30 @@ func (impl ChartTemplateServiceImpl) GitPull(clonedDir string, repoUrl string, a
483496 return nil
484497}
485498
499+ func (impl * ChartTemplateServiceImpl ) GetUserEmailIdAndNameForGitOpsCommit (userId int32 ) (string , string ) {
500+ 501+ name := "devtron bot"
502+ //getting emailId associated with user
503+ userDetail , _ := impl .userRepository .GetById (userId )
504+ if userDetail != nil && userDetail .EmailId != "admin" && userDetail .EmailId != "system" && len (userDetail .EmailId ) > 0 {
505+ emailId = userDetail .EmailId
506+ } else {
507+ emailIdGitOps , err := impl .gitOpsConfigRepository .GetEmailIdFromActiveGitOpsConfig ()
508+ if err != nil {
509+ impl .logger .Errorw ("error in getting emailId from active gitOps config" , "err" , err )
510+ } else if len (emailIdGitOps ) > 0 {
511+ emailId = emailIdGitOps
512+ }
513+ }
514+ //we are getting name from emailId(replacing special characters in <user-name part of email> with space)
515+ emailComponents := strings .Split (emailId , "@" )
516+ regex , _ := regexp .Compile (`[^\w]` )
517+ if regex != nil {
518+ name = regex .ReplaceAllString (emailComponents [0 ], " " )
519+ }
520+ return emailId , name
521+ }
522+
486523func (impl ChartTemplateServiceImpl ) GetGitOpsRepoName (appName string ) string {
487524 var repoName string
488525 if len (impl .globalEnvVariables .GitOpsRepoPrefix ) == 0 {
0 commit comments