Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix branch name detection #1642

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions mage/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"sort"
"strings"
"sync"

Expand Down Expand Up @@ -78,24 +79,53 @@ func getVersion() string {
return "v0.0.0"
}

// Get the name of the current branch, or the branch that contains the current tag
// Return either "main", "v*", or "dev" for all other branches.
func getBranchName() string {
// pull request
if branch, ok := os.LookupEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH"); ok {
return branch
gitOutput, _ := must.OutputS("git", "for-each-ref", "--contains", "HEAD", "--format=%(refname)")
refs := strings.Split(gitOutput, "\n")

return pickBranchName(refs)
}

// Return either "main", "v*", or "dev" for all other branches.
func pickBranchName(refs []string) string {
var branch string

if b, ok := os.LookupEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH"); ok {
// pull request
branch = b
} else if b, ok := os.LookupEnv("BUILD_SOURCEBRANCHNAME"); ok {
// branch build
branch = b
} else {
// tag build
// Detect if this was a tag on main or a release
sort.Strings(refs) // put main ahead of release/v*
for _, ref := range refs {
// Ignore tags
if strings.HasSuffix(ref, "refs/tags") {
continue
}

// Only match main and release/v* branches
if strings.HasSuffix(ref, "/main") || strings.Contains(ref, "/release/v") {
branch = ref
break
}
}
}

// branch build
if branch, ok := os.LookupEnv("BUILD_SOURCEBRANCHNAME"); ok {
return branch
// Convert the ref name into a branch name, e.g. refs/heads/main -> main
branch = strings.NewReplacer("refs/heads/", "", "refs/remotes/origin/", "").Replace(branch)

// Only use the following branch names "main", "release/v*", and "dev" for everything else
if branch != "main" && !strings.HasPrefix(branch, "release/v") {
branch = "dev"
}

// tag build
// Use the first branch that contains the current commit
matches, _ := must.OutputS("git", "for-each-ref", "--contains", "HEAD", "--format=%(refname:short)")
firstMatchingBranch := strings.Split(matches, "\n")[0]
// The matching branch may be a remote branch, just get its name
return strings.Replace(firstMatchingBranch, "origin/", "", 1)
// Convert release/v1 -> v1
branch = strings.ReplaceAll(branch, "release/", "")
return branch
}

func getPermalink() (string, bool) {
Expand All @@ -116,7 +146,7 @@ func getPermalink() (string, bool) {
// Get the current branch name, or the name of the branch we tagged from
branch := getBranchName()

// Build a permalink such as "canary", "latest", "latest-v1", or "canary-v1"
// Build a permalink such as "canary", "latest", "latest-v1", or "dev-canary"
switch branch {
case "main":
return permalinkPrefix, taggedRelease
Expand Down
89 changes: 89 additions & 0 deletions mage/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package mage

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPickBranchName(t *testing.T) {
// These aren't set locally but are set on a CI run
os.Unsetenv("SYSTEM_PULLREQUEST_SOURCEBRANCH")
os.Unsetenv("BUILD_SOURCEBRANCHNAME")

t.Run("origin/main", func(t *testing.T) {
refs := []string{
"refs/heads/foo",
"refs/remotes/origin/main",
"refs/remotes/origin/8252b6e4b1983702c7387ece7f971ef74047b746",
"refs/tags/v0.38.3",
}
branch := pickBranchName(refs)
assert.Equal(t, "main", branch)
})

t.Run("main", func(t *testing.T) {
refs := []string{
"refs/heads/foo",
"refs/heads/main",
"refs/remotes/origin/8252b6e4b1983702c7387ece7f971ef74047b746",
"refs/tags/v0.38.3",
}
branch := pickBranchName(refs)
assert.Equal(t, "main", branch)
})

t.Run("pull request", func(t *testing.T) {
os.Setenv("SYSTEM_PULLREQUEST_SOURCEBRANCH", "patch-1")
defer os.Unsetenv("SYSTEM_PULLREQUEST_SOURCEBRANCH")

refs := []string{
"refs/remotes/origin/foo",
"refs/remotes/origin/8252b6e4b1983702c7387ece7f971ef74047b746",
}
branch := pickBranchName(refs)
assert.Equal(t, "dev", branch)
})

t.Run("branch build", func(t *testing.T) {
os.Setenv("BUILD_SOURCEBRANCHNAME", "foo")
defer os.Unsetenv("BUILD_SOURCEBRANCHNAME")

refs := []string{
"refs/remotes/origin/foo",
"refs/remotes/origin/8252b6e4b1983702c7387ece7f971ef74047b746",
}
branch := pickBranchName(refs)
assert.Equal(t, "dev", branch)
})

t.Run("tagged release on v1", func(t *testing.T) {
refs := []string{
"refs/heads/release/v1",
"refs/remotes/origin/8252b6e4b1983702c7387ece7f971ef74047b746",
"refs/tags/v1.0.0-alpha.1",
}
branch := pickBranchName(refs)
assert.Equal(t, "v1", branch)
})

t.Run("tagged release on main", func(t *testing.T) {
refs := []string{
"refs/heads/release/v1",
"refs/heads/main",
"refs/remotes/origin/8252b6e4b1983702c7387ece7f971ef74047b746",
"refs/tags/v0.38.3",
}
branch := pickBranchName(refs)
assert.Equal(t, "main", branch)
})

t.Run("local branch", func(t *testing.T) {
refs := []string{
"refs/heads/foo",
}
branch := pickBranchName(refs)
assert.Equal(t, "dev", branch)
})
}