Skip to content

Commit 226a670

Browse files
chore(ci): improve previous-version script readability, fix bug (cherry-pick argoproj#22378) (argoproj#22381)
Signed-off-by: Michael Crenshaw <[email protected]> Co-authored-by: Michael Crenshaw <[email protected]>
1 parent 2933154 commit 226a670

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

hack/get-previous-release/get-previous-version-for-release-notes.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +51,59 @@ func extractPatchAndRC(tag string) (string, string, error) {
5151
return patch, rc, nil
5252
}
5353

54-
func findPreviousTag(proposedTag string, tags []string) (string, error) {
55-
var previousTag string
56-
proposedMinor := semver.MajorMinor(proposedTag)
57-
58-
proposedPatch, proposedRC, err := extractPatchAndRC(proposedTag)
59-
if err != nil {
60-
return "", err
54+
func removeInvalidTags(tags []string) []string {
55+
var validTags []string
56+
for _, tag := range tags {
57+
if _, _, err := extractPatchAndRC(tag); err == nil {
58+
validTags = append(validTags, tag)
59+
}
6160
}
61+
return validTags
62+
}
6263

64+
func removeNewerOrEqualTags(proposedTag string, tags []string) []string {
65+
var validTags []string
6366
for _, tag := range tags {
64-
// If this tag is newer than the proposed tag, skip it.
65-
if semver.Compare(tag, proposedTag) > 0 {
66-
continue
67+
if semver.Compare(tag, proposedTag) < 0 {
68+
validTags = append(validTags, tag)
6769
}
70+
}
71+
return validTags
72+
}
6873

69-
// If this tag is older than a tag we've already decided is a candidate, skip it.
70-
if semver.Compare(tag, previousTag) <= 0 {
71-
continue
72-
}
73-
tagPatch, tagRC, err := extractPatchAndRC(tag)
74-
if err != nil {
75-
continue
74+
func removeTagsFromSameMinorSeries(proposedTag string, tags []string) []string {
75+
var validTags []string
76+
proposedMinor := semver.MajorMinor(proposedTag)
77+
for _, tag := range tags {
78+
if semver.MajorMinor(tag) != proposedMinor {
79+
validTags = append(validTags, tag)
7680
}
81+
}
82+
return validTags
83+
}
7784

78-
// If it's a non-RC release...
79-
if proposedRC == "0" {
80-
if proposedPatch == "0" {
81-
// ...and we're cutting the first patch of a new minor release series, don't consider tags in the same
82-
// minor release series.
83-
if semver.MajorMinor(tag) != proposedMinor {
84-
previousTag = tag
85-
}
86-
} else {
87-
88-
previousTag = tag
89-
}
90-
} else {
91-
if tagRC != "0" && tagPatch == proposedPatch {
92-
previousTag = tag
93-
} else if tagRC == "0" {
94-
previousTag = tag
95-
}
85+
func getMostRecentTag(tags []string) string {
86+
var mostRecentTag string
87+
for _, tag := range tags {
88+
if mostRecentTag == "" || semver.Compare(tag, mostRecentTag) > 0 {
89+
mostRecentTag = tag
9690
}
9791
}
92+
return mostRecentTag
93+
}
94+
95+
func findPreviousTag(proposedTag string, tags []string) (string, error) {
96+
tags = removeInvalidTags(tags)
97+
tags = removeNewerOrEqualTags(proposedTag, tags)
98+
99+
proposedPatch, proposedRC, _ := extractPatchAndRC(proposedTag) // Ignore the error, we already filtered out invalid tags.
100+
if proposedRC == "0" && proposedPatch == "0" {
101+
// If we're cutting the first patch of a new minor release series, don't consider tags in the same minor release
102+
// series. We want to compare to the latest tag in the previous minor release series.
103+
tags = removeTagsFromSameMinorSeries(proposedTag, tags)
104+
}
105+
106+
previousTag := getMostRecentTag(tags)
98107
if previousTag == "" {
99108
return "", fmt.Errorf("no matching tag found for tags: " + strings.Join(tags, ", "))
100109
}

hack/get-previous-release/get-previous-version-for-release-notes_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func TestFindPreviousTagRules(t *testing.T) {
8181
// Rule 6: If we're releasing a major version, get the most recent tag on the previous major release series,
8282
// even if it's an RC.
8383
{"Rule 6: major version", "v3.0.0", "v2.13.0-rc3", false},
84+
// Rule 7: If the proposed tag already exists, don't return it.
85+
{"Rule 7: proposed tag already exists", "v2.12.5", "v2.12.4", false},
8486
}
8587

8688
for _, test := range tests {

0 commit comments

Comments
 (0)