@@ -6,12 +6,15 @@ import (
66 "log"
77 "net/url"
88 "path"
9+ "strings"
910
1011 "github.com/jenkins-x/go-scm/scm"
1112
1213 "github.com/rhd-gitops-example/services/pkg/git"
1314 "github.com/rhd-gitops-example/services/pkg/local"
1415 "github.com/rhd-gitops-example/services/pkg/util"
16+
17+ "github.com/google/uuid"
1518)
1619
1720type ServiceManager struct {
@@ -87,6 +90,22 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName stri
8790 }
8891 }(keepCache , & reposToDelete )
8992
93+ var localSource git.Source
94+ var errorSource error
95+ if fromLocalRepo (fromURL ) {
96+ localSource = s .localFactory (fromURL , s .debug )
97+ } else {
98+ source , errorSource = s .checkoutSourceRepo (fromURL , fromBranch )
99+ if errorSource != nil {
100+ return fmt .Errorf ("failed to checkout repo: %w" , errorSource )
101+ }
102+ reposToDelete = append (reposToDelete , source )
103+ }
104+
105+ if newBranchName == "" {
106+ newBranchName = generateBranch (source )
107+ }
108+
90109 destination , err := s .checkoutDestinationRepo (toURL , newBranchName )
91110 if err != nil {
92111 return fmt .Errorf ("failed to checkout repo: %w" , err )
@@ -95,23 +114,17 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName stri
95114
96115 var copied []string
97116 if fromLocalRepo (fromURL ) {
98- localSource := s .localFactory (fromURL , s .debug )
99117 copied , err = local .CopyConfig (serviceName , localSource , destination )
100118 if err != nil {
101119 return fmt .Errorf ("failed to setup local repo: %w" , err )
102120 }
103121 } else {
104- source , err = s .checkoutSourceRepo (fromURL , fromBranch )
105- if err != nil {
106- return fmt .Errorf ("failed to checkout repo: %w" , err )
107- }
108- reposToDelete = append (reposToDelete , source )
109-
110122 copied , err = git .CopyService (serviceName , source , destination )
111123 if err != nil {
112124 return fmt .Errorf ("failed to copy service: %w" , err )
113125 }
114126 }
127+
115128 if err := destination .StageFiles (copied ... ); err != nil {
116129 return fmt .Errorf ("failed to stage files: %w" , err )
117130 }
@@ -212,3 +225,11 @@ func fromLocalRepo(s string) bool {
212225 }
213226 return false
214227}
228+
229+ func generateBranch (repo git.Repo ) string {
230+ uniqueString := uuid .New ()
231+ runes := []rune (uniqueString .String ())
232+ branchName := repo .GetName () + "-" + repo .GetCommitID () + "-" + string (runes [0 :5 ])
233+ branchName = strings .Replace (branchName , "\n " ,"" ,- 1 )
234+ return branchName
235+ }
0 commit comments