@@ -51,50 +51,59 @@ func extractPatchAndRC(tag string) (string, string, error) {
51
51
return patch , rc , nil
52
52
}
53
53
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
+ }
61
60
}
61
+ return validTags
62
+ }
62
63
64
+ func removeNewerOrEqualTags (proposedTag string , tags []string ) []string {
65
+ var validTags []string
63
66
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 )
67
69
}
70
+ }
71
+ return validTags
72
+ }
68
73
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 )
76
80
}
81
+ }
82
+ return validTags
83
+ }
77
84
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
96
90
}
97
91
}
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 )
98
107
if previousTag == "" {
99
108
return "" , fmt .Errorf ("no matching tag found for tags: " + strings .Join (tags , ", " ))
100
109
}
0 commit comments