Skip to content

Commit 7dd524e

Browse files
committed
branch name logic
1 parent 4ac60fd commit 7dd524e

2 files changed

Lines changed: 54 additions & 21 deletions

File tree

internal/gitsemver/gitsemver.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,36 +159,43 @@ func (vs *GitSemVer) GetTag(repo string) (tag string, match bool, err error) {
159159
}
160160

161161
func (vs *GitSemVer) getBranchGitHub(repo string) (branchName string, err error) {
162-
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_BASE_REF")); branchName == "" {
163-
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_NAME")); branchName != "" {
164-
if strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_TYPE")) == "tag" {
165-
var branches []string
166-
if branches, err = vs.Git.GetBranchesFromTag(repo, branchName); err == nil {
167-
for _, branchName = range branches {
168-
if vs.IsReleaseBranch(branchName) {
169-
return
170-
}
171-
}
172-
}
162+
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_BASE_REF")); branchName != "" {
163+
return
164+
}
165+
refName := strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_NAME"))
166+
if refName == "" {
167+
return
168+
}
169+
if strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_TYPE")) != "tag" {
170+
return refName, nil
171+
}
172+
var branches []string
173+
if branches, err = vs.Git.GetBranchesFromTag(repo, refName); err == nil {
174+
for _, candidate := range branches {
175+
if vs.IsReleaseBranch(candidate) {
176+
return candidate, nil
173177
}
174178
}
175179
}
176180
return
177181
}
178182

179183
func (vs *GitSemVer) getBranchGitLab(repo string) (branchName string, err error) {
180-
if branchName = strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_REF_NAME")); branchName != "" {
181-
if strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_TAG")) == branchName {
182-
var branches []string
183-
if branches, err = vs.Git.GetBranchesFromTag(repo, branchName); err == nil {
184-
for _, branchName = range branches {
185-
if vs.IsReleaseBranch(branchName) {
186-
return
187-
}
188-
}
184+
if branchName = strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_REF_NAME")); branchName == "" {
185+
return
186+
}
187+
if strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_TAG")) != branchName {
188+
return branchName, nil
189+
}
190+
var branches []string
191+
if branches, err = vs.Git.GetBranchesFromTag(repo, branchName); err == nil {
192+
for _, candidate := range branches {
193+
if vs.IsReleaseBranch(candidate) {
194+
return candidate, nil
189195
}
190196
}
191197
}
198+
branchName = ""
192199
return
193200
}
194201

internal/gitsemver/gitsemver_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,33 @@ func Test_VersionStringer_GetBranchFromTag_GitHub(t *testing.T) {
225225
env["GITHUB_REF_NAME"] = "v1"
226226
name, err = vs.GetBranch(".")
227227
isEqual(t, err, nil)
228-
isEqual(t, "onepointoh", name)
228+
isEqual(t, "", name)
229+
}
230+
231+
func Test_VersionStringer_GetBranchFromTag_GitHub_NoContainingBranch(t *testing.T) {
232+
env := MockEnvironment{}
233+
git := &MockGitter{}
234+
vs := gitsemver.GitSemVer{Git: git, Env: env}
235+
236+
git.branch = "detached"
237+
env["GITHUB_REF_TYPE"] = "tag"
238+
env["GITHUB_REF_NAME"] = "v2.5.0"
239+
name, err := vs.GetBranch(".")
240+
isEqual(t, err, nil)
241+
isEqual(t, "", name)
242+
}
243+
244+
func Test_VersionStringer_GetBranchFromTag_GitLab_NoReleaseBranch(t *testing.T) {
245+
env := MockEnvironment{}
246+
git := &MockGitter{}
247+
vs := gitsemver.GitSemVer{Git: git, Env: env}
248+
249+
git.branch = "detached"
250+
env["CI_COMMIT_TAG"] = "v1"
251+
env["CI_COMMIT_REF_NAME"] = "v1"
252+
name, err := vs.GetBranch(".")
253+
isEqual(t, err, nil)
254+
isEqual(t, "", name)
229255
}
230256

231257
func Test_VersionStringer_GetBuild(t *testing.T) {

0 commit comments

Comments
 (0)