Skip to content

Commit fd5797b

Browse files
authored
generateBranchForLocalSource fix (#51)
generateBranchForLocalSource fix
1 parent 2d0930a commit fd5797b

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

pkg/avancement/service_manager.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName, mes
9191
isLocal := fromLocalRepo(fromURL)
9292
if isLocal {
9393
localSource = s.localFactory(fromURL, s.debug)
94+
if newBranchName == "" {
95+
newBranchName = generateBranchForLocalSource(localSource)
96+
}
9497
} else {
9598
source, errorSource = s.checkoutSourceRepo(fromURL, fromBranch)
9699
if errorSource != nil {
97100
return fmt.Errorf("failed to checkout repo: %w", errorSource)
98101
}
99102
reposToDelete = append(reposToDelete, source)
100-
}
101-
102-
if newBranchName == "" {
103-
newBranchName = generateBranch(source)
103+
if newBranchName == "" {
104+
newBranchName = generateBranch(source)
105+
}
104106
}
105107

106108
destination, err := s.checkoutDestinationRepo(toURL, newBranchName)
@@ -241,6 +243,14 @@ func generateBranch(repo git.Repo) string {
241243
return branchName
242244
}
243245

246+
func generateBranchForLocalSource(source git.Source) string {
247+
uniqueString := uuid.New()
248+
runes := []rune(uniqueString.String())
249+
branchName := source.GetName() + "-" + "local-dir" + "-" + string(runes[0:5])
250+
branchName = strings.Replace(branchName, "\n", "", -1)
251+
return branchName
252+
}
253+
244254
// generateDefaultCommitMsg constructs a default commit message based on the source information.
245255
func generateDefaultCommitMsg(sourceRepo git.Repo, serviceName, from, fromBranch string) string {
246256
commit := sourceRepo.GetCommitID()

pkg/avancement/service_manager_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,15 @@ func TestPromoteWithCacheDeletionFailure(t *testing.T) {
200200

201201
func TestGenerateBranchWithSuccess(t *testing.T) {
202202
repo := mock.New("/dev", "master")
203-
GenerateBranchWithSuccess(t, repo)
203+
generateBranchWithSuccess(t, repo)
204204
}
205205

206-
func GenerateBranchWithSuccess(t *testing.T, repo git.Repo) {
206+
func TestGenerateBranchForLocalSource(t *testing.T) {
207+
source := NewLocal("/path/to/topLevel")
208+
generateBranchForLocalWithSuccess(t, source)
209+
}
210+
211+
func generateBranchWithSuccess(t *testing.T, repo git.Repo) {
207212
branch := generateBranch(repo)
208213
nameRegEx := "^([0-9A-Za-z]+)-([0-9a-z]{7})-([0-9A-Za-z]{5})$"
209214
_, err := regexp.Match(nameRegEx, []byte(branch))
@@ -212,6 +217,15 @@ func GenerateBranchWithSuccess(t *testing.T, repo git.Repo) {
212217
}
213218
}
214219

220+
func generateBranchForLocalWithSuccess(t *testing.T, source git.Source) {
221+
branch := generateBranchForLocalSource(source)
222+
nameRegEx := "^path-to-topLevel-local-dir-([0-9A-Za-z]{5})$"
223+
_, err := regexp.Match(nameRegEx, []byte(branch))
224+
if err != nil {
225+
t.Fatalf("generated name `%s` for local case %s failed to matching pattern %s", nameRegEx, source.GetName(), nameRegEx)
226+
}
227+
}
228+
215229
type mockSource struct {
216230
files []string
217231
localPath string
@@ -248,6 +262,10 @@ func (s *mockSource) Walk(_ string, cb func(string, string) error) error {
248262
return nil
249263
}
250264

265+
func (s *mockSource) GetName() string {
266+
return "mock-source-name"
267+
}
268+
251269
func (s *mockSource) AddFiles(name string) {
252270
if s.files == nil {
253271
s.files = []string{}

pkg/git/copy_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ func (s *mockSource) Walk(base string, cb func(string, string) error) error {
129129
return nil
130130
}
131131

132+
func (s *mockSource) GetName() string {
133+
return "local-dir-repo-name-unknown"
134+
}
135+
132136
func (s *mockSource) addFile(name string) {
133137
if s.files == nil {
134138
s.files = []string{}

pkg/git/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ type Source interface {
1515
// Walk walks the repository tree, calling the callback with the prefix and
1616
// filename.
1717
Walk(path string, cb func(prefix, name string) error) error
18+
GetName() string
1819
}
1920

2021
type Repo interface {
2122
Destination
2223
Source
23-
GetName() string
24-
GetCommitID() string
2524
Clone() error
2625
Checkout(branch string) error
2726
CheckoutAndCreate(branch string) error
27+
GetCommitID() string
2828
StageFiles(filenames ...string) error
2929
Commit(msg string, author *Author) error
3030
Push(branch string) error

pkg/local/local.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ func (l *Local) Walk(_ string, cb func(prefix, name string) error) error {
5454
return cb(prefix, strings.TrimPrefix(path, prefix))
5555
})
5656
}
57+
58+
// GetName - we're using a directory that may not be a git repo, all we know is our path
59+
func (l *Local) GetName() string {
60+
path := filepath.ToSlash(l.LocalPath)
61+
name := strings.ReplaceAll(path, "/", "-")
62+
return name
63+
}

pkg/local/local_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func (s *mockSource) Walk(_ string, cb func(string, string) error) error {
6363
return nil
6464
}
6565

66+
func (s *mockSource) GetName() string {
67+
return "AlwaysTheSameName"
68+
}
69+
6670
func (s *mockSource) addFile(name string) {
6771
if s.files == nil {
6872
s.files = []string{}

0 commit comments

Comments
 (0)