Skip to content

Commit b008273

Browse files
authoredJun 6, 2024
Support package version update on any release branch (elastic#4843)
After we create release branches we still need to maintain the right package and test versions using the Github action on demand.
1 parent db2d247 commit b008273

File tree

2 files changed

+95
-29
lines changed

2 files changed

+95
-29
lines changed
 

‎magefile.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2049,8 +2049,13 @@ func (Integration) UpdateVersions(ctx context.Context) error {
20492049
func (Integration) UpdatePackageVersion(ctx context.Context) error {
20502050
const packageVersionFilename = ".package-version"
20512051

2052+
currentReleaseBranch, err := git.GetCurrentReleaseBranch(ctx)
2053+
if err != nil {
2054+
return fmt.Errorf("failed to identify the current release branch: %w", err)
2055+
}
2056+
20522057
sc := snapshots.NewSnapshotsClient()
2053-
versions, err := sc.FindLatestSnapshots(ctx, []string{"master"})
2058+
versions, err := sc.FindLatestSnapshots(ctx, []string{currentReleaseBranch})
20542059
if err != nil {
20552060
return fmt.Errorf("failed to fetch a manifest for the latest snapshot: %w", err)
20562061
}

‎pkg/testing/tools/git/git.go

+89-28
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,126 @@ package git
66

77
import (
88
"bufio"
9+
"bytes"
910
"context"
11+
"errors"
1012
"fmt"
13+
"io"
1114
"os/exec"
1215
"regexp"
1316
)
1417

1518
var (
19+
ErrNotReleaseBranch = errors.New("this is not a release branch")
1620
releaseBranchRegexp = regexp.MustCompile(`.*(\d+\.\d+)$`)
1721
)
1822

23+
type outputReader func(io.Reader) error
24+
1925
// GetReleaseBranches returns a list of release branches of the
2026
// current repository ordered descending by creation date.
2127
// e.g. 8.13, 8.12, etc.
2228
func GetReleaseBranches(ctx context.Context) ([]string, error) {
23-
var seen = map[string]struct{}{}
24-
branchList := []string{}
25-
2629
c := exec.CommandContext(ctx, "git", "branch", "-r", "--list", "*/[0-9]*.*[0-9]", "--sort=-creatordate")
2730

28-
r, err := c.StdoutPipe()
31+
branchList := []string{}
32+
err := runCommand(c, releaseBranchReader(&branchList))
2933
if err != nil {
30-
return nil, fmt.Errorf("failed to create the stdout pipe: %w", err)
34+
return nil, err
3135
}
32-
defer r.Close()
3336

34-
err = c.Start()
37+
return branchList, nil
38+
}
39+
40+
// GetCurrentReleaseBranch returns the current branch of the repository
41+
func GetCurrentReleaseBranch(ctx context.Context) (string, error) {
42+
c := exec.CommandContext(ctx, "git", "symbolic-ref", "--short", "HEAD")
43+
44+
var branch string
45+
err := runCommand(c, fullOutputReader(&branch))
3546
if err != nil {
36-
return nil, fmt.Errorf("failed to start git command: %w", err)
47+
return "", err
3748
}
3849

39-
scanner := bufio.NewScanner(r)
40-
for scanner.Scan() {
41-
branch := scanner.Text()
42-
if !releaseBranchRegexp.MatchString(branch) {
43-
continue
50+
// in the APIs the release branch is still called `master`
51+
if branch == "main" {
52+
return "master", nil
53+
}
54+
55+
return extractReleaseBranch(branch)
56+
}
57+
58+
func fullOutputReader(out *string) outputReader {
59+
return func(r io.Reader) error {
60+
b, err := io.ReadAll(r)
61+
if err != nil {
62+
return fmt.Errorf("failed to read the entire output: %w", err)
4463
}
64+
*out = string(bytes.TrimSpace(b))
65+
return nil
66+
}
67+
}
4568

46-
matches := releaseBranchRegexp.FindStringSubmatch(branch)
47-
if len(matches) != 2 {
48-
continue
69+
func releaseBranchReader(out *[]string) outputReader {
70+
return func(r io.Reader) error {
71+
var seen = map[string]struct{}{}
72+
scanner := bufio.NewScanner(r)
73+
for scanner.Scan() {
74+
branch := scanner.Text()
75+
branch, err := extractReleaseBranch(branch)
76+
if err != nil {
77+
continue
78+
}
79+
_, exists := seen[branch]
80+
if exists {
81+
continue
82+
}
83+
seen[branch] = struct{}{}
84+
// appending to the list right away instead of
85+
// collecting from the map later preserves the order
86+
*out = append(*out, branch)
4987
}
50-
branch = matches[1]
51-
_, exists := seen[branch]
52-
if exists {
53-
continue
88+
if scanner.Err() != nil {
89+
return fmt.Errorf("failed to scan the output: %w", scanner.Err())
5490
}
55-
seen[branch] = struct{}{}
56-
// appending to the list right away instead of
57-
// collecting from the map later preserves the order
58-
branchList = append(branchList, branch)
91+
92+
return nil
5993
}
60-
if scanner.Err() != nil {
61-
return nil, fmt.Errorf("failed to scan the output: %w", err)
94+
}
95+
96+
func extractReleaseBranch(branch string) (string, error) {
97+
if !releaseBranchRegexp.MatchString(branch) {
98+
return "", fmt.Errorf("failed to process branch %q: %w", branch, ErrNotReleaseBranch)
99+
}
100+
101+
matches := releaseBranchRegexp.FindStringSubmatch(branch)
102+
if len(matches) != 2 {
103+
return "", fmt.Errorf("failed to process branch %q: expected 2 matches, got %d", branch, len(matches))
104+
}
105+
return matches[1], nil
106+
}
107+
108+
func runCommand(c *exec.Cmd, or outputReader) error {
109+
r, err := c.StdoutPipe()
110+
if err != nil {
111+
return fmt.Errorf("failed to create the stdout pipe: %w", err)
112+
}
113+
defer r.Close()
114+
115+
err = c.Start()
116+
if err != nil {
117+
return fmt.Errorf("failed to start git command: %w", err)
118+
}
119+
120+
err = or(r)
121+
if err != nil {
122+
return fmt.Errorf("failed to process the git command output: %w", err)
62123
}
63124

64125
err = c.Wait()
65126
if err != nil {
66-
return nil, fmt.Errorf("failed to wait for the git command to finish: %w", err)
127+
return fmt.Errorf("failed to wait for the git command to finish: %w", err)
67128
}
68129

69-
return branchList, nil
130+
return nil
70131
}

0 commit comments

Comments
 (0)