Skip to content

Commit

Permalink
Add support of the new branch setup to the version update automation (#…
Browse files Browse the repository at this point in the history
…5957)

So, we can support the new 9.0 release, 8.x branch and the rest of the
release branches.
  • Loading branch information
rdner authored Nov 6, 2024
1 parent 7978189 commit b6a608d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/testing/tools/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import (
"errors"
"fmt"
"io"
"math"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"

"github.com/elastic/elastic-agent/pkg/version"
)

var (
ErrNotReleaseBranch = errors.New("this is not a release branch")
// glob is not powerful enough to match exactly what we expect, so we have to use the regular expression too
releaseBranchRegexp = regexp.MustCompile(`.*(\d+\.(\d+|x))$`)
)

Expand All @@ -26,17 +33,44 @@ type outputReader func(io.Reader) error
// current repository ordered descending by creation date.
// e.g. 8.13, 8.12, etc.
func GetReleaseBranches(ctx context.Context) ([]string, error) {
c := exec.CommandContext(ctx, "git", "branch", "-r", "--list", "*/[0-9]*.*[0-9x]", "--sort=-creatordate")
c := exec.CommandContext(ctx, "git", "for-each-ref", "refs/remotes/origin/[0-9]*.[0-9x]*", "--format=%(refname:short)")

branchList := []string{}
err := runCommand(c, releaseBranchReader(&branchList))
if err != nil {
return nil, err
}

sort.Slice(branchList, less(branchList))

return branchList, nil
}

// we use this to emulate the maximum possible version value aliased by `.x`
var maxIntString = strconv.Itoa(math.MaxInt)

// less makes sure we have our release branches in the right order
func less(branches []string) func(i, j int) bool {
return func(i, j int) bool {
// we complete the versions, so we can parse semver and compare
var (
v1 = strings.ReplaceAll(branches[i], "x", maxIntString) + ".0"
v2 = strings.ReplaceAll(branches[j], "x", maxIntString) + ".0"
)

parsed1, err1 := version.ParseVersion(v1)
parsed2, err2 := version.ParseVersion(v2)
if err1 != nil {
return false
}
if err2 != nil {
return true
}
// descending order
return !parsed1.Less(*parsed2)
}
}

// GetCurrentReleaseBranch returns the current branch of the repository
func GetCurrentReleaseBranch(ctx context.Context) (string, error) {
c := exec.CommandContext(ctx, "git", "symbolic-ref", "--short", "HEAD")
Expand Down Expand Up @@ -71,7 +105,7 @@ func releaseBranchReader(out *[]string) outputReader {
var seen = map[string]struct{}{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
branch := scanner.Text()
branch := strings.TrimPrefix(scanner.Text(), "origin/")
branch, err := extractReleaseBranch(branch)
if err != nil {
continue
Expand Down
27 changes: 27 additions & 0 deletions pkg/testing/tools/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package git

import (
"context"
"sort"
"testing"
"time"

Expand All @@ -24,3 +25,29 @@ func TestGetReleaseBranches(t *testing.T) {
assert.Regexp(t, releaseBranchRegexp, b)
}
}

func TestLess(t *testing.T) {
branchList := []string{
"8.16",
"9.1",
"8.x",
"wrong",
"9.0",
"9.x",
"8.15",
}

expected := []string{
"9.x",
"9.1",
"9.0",
"8.x",
"8.16",
"8.15",
"wrong",
}

sort.Slice(branchList, less(branchList))

require.Equal(t, expected, branchList)
}

0 comments on commit b6a608d

Please sign in to comment.