Skip to content

Commit 9e5ea4f

Browse files
authored
Generate unique branch if one is not specified (#32)
1 parent e04fd1c commit 9e5ea4f

File tree

8 files changed

+71
-8
lines changed

8 files changed

+71
-8
lines changed

cmd/services/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
&cli.StringFlag{
5757
Name: branchNameFlag,
5858
Usage: "the name to use for the newly created branch",
59-
Value: "test-branch",
59+
Value: "",
6060
},
6161
&cli.StringFlag{
6262
Name: cacheDirFlag,

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.13
55
require (
66
github.com/golang/protobuf v1.3.2 // indirect
77
github.com/google/go-cmp v0.3.0
8+
github.com/google/uuid v1.1.1
89
github.com/jenkins-x/go-scm v1.5.77
910
github.com/mitchellh/go-homedir v1.1.0
1011
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
2020
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
2121
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2222
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
23+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
2324
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2425
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
2526
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=

pkg/avancement/service_manager.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1720
type 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+
}

pkg/avancement/service_manager_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88
"testing"
9+
"regexp"
910

1011
"github.com/jenkins-x/go-scm/scm"
1112
fakescm "github.com/jenkins-x/go-scm/scm/driver/fake"
@@ -174,6 +175,20 @@ func TestPromoteWithCacheDeletionFailure(t *testing.T) {
174175
devRepo.AssertDeletedFromCache(t)
175176
}
176177

178+
func TestGenerateBranchWithSuccess(t *testing.T) {
179+
repo := mock.New("/dev", "master")
180+
GenerateBranchWithSuccess(t, repo)
181+
}
182+
183+
func GenerateBranchWithSuccess(t *testing.T, repo git.Repo) {
184+
branch := generateBranch(repo)
185+
nameRegEx := "^([0-9A-Za-z]+)-([0-9a-z]{7})-([0-9A-Za-z]{5})$"
186+
_, err := regexp.Match(nameRegEx, []byte(branch))
187+
if err != nil {
188+
t.Fatalf("failed to generate a branch name matching pattern %s", nameRegEx)
189+
}
190+
}
191+
177192
type mockSource struct {
178193
files []string
179194
localPath string

pkg/git/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type Source interface {
2020
type Repo interface {
2121
Destination
2222
Source
23+
GetName() string
24+
GetCommitID() string
2325
Clone() error
2426
Checkout(branch string) error
2527
CheckoutAndCreate(branch string) error

pkg/git/mock/mock.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Repository struct {
3434

3535
deleted bool
3636
DeleteErr error
37+
38+
commitID string
39+
40+
repoName string
3741
}
3842

3943
// New creates and returns a new git.Cache implementation that operates entirely
@@ -50,6 +54,16 @@ func (m *Repository) Checkout(branch string) error {
5054
return m.checkoutErr
5155
}
5256

57+
func (m *Repository) GetName() string {
58+
m.repoName = "fakeRepoName"
59+
return m.repoName
60+
}
61+
62+
func (m *Repository) GetCommitID() string {
63+
m.commitID = "fakeCommitString"
64+
return m.commitID
65+
}
66+
5367
// CheckoutAndCreate fulfils the git.Repo interface.
5468
func (m *Repository) CheckoutAndCreate(branch string) error {
5569
if m.branchesCreated == nil {

pkg/git/repository.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func (r *Repository) CheckoutAndCreate(branch string) error {
6060
return err
6161
}
6262

63+
func (r *Repository) GetName() string {
64+
return r.repoName
65+
}
66+
67+
func (r *Repository) GetCommitID() string {
68+
commitID, _ := r.execGit(r.repoPath(), nil, "rev-parse", "--short", "HEAD")
69+
return string(commitID)
70+
}
71+
6372
func (r *Repository) Walk(base string, cb func(prefix, name string) error) error {
6473
repoBase := r.repoPath(base)
6574
prefix := filepath.Dir(repoBase) + "/"

0 commit comments

Comments
 (0)