|
6 | 6 | "os"
|
7 | 7 | "os/exec"
|
8 | 8 | "regexp"
|
9 |
| - "strconv" |
10 | 9 | "strings"
|
11 | 10 | )
|
12 | 11 |
|
@@ -52,61 +51,59 @@ func extractPatchAndRC(tag string) (string, string, error) {
|
52 | 51 | return patch, rc, nil
|
53 | 52 | }
|
54 | 53 |
|
55 |
| -func findPreviousTag(proposedTag string, tags []string) (string, error) { |
56 |
| - var previousTag string |
57 |
| - proposedMajor := semver.Major(proposedTag) |
58 |
| - proposedMinor := semver.MajorMinor(proposedTag) |
59 |
| - |
60 |
| - proposedPatch, proposedRC, err := extractPatchAndRC(proposedTag) |
61 |
| - if err != nil { |
62 |
| - 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 | + } |
63 | 60 | }
|
| 61 | + return validTags |
| 62 | +} |
64 | 63 |
|
65 |
| - // If the current tag is a .0 patch release or a 1 release candidate, adjust to the previous minor release series. |
66 |
| - if (proposedPatch == "0" && proposedRC == "0") || proposedRC == "1" { |
67 |
| - proposedMinorInt, err := strconv.Atoi(strings.TrimPrefix(proposedMinor, proposedMajor+".")) |
68 |
| - if err != nil { |
69 |
| - return "", fmt.Errorf("invalid minor version: %v", err) |
70 |
| - } |
71 |
| - if proposedMinorInt > 0 { |
72 |
| - proposedMinor = fmt.Sprintf("%s.%d", proposedMajor, proposedMinorInt-1) |
| 64 | +func removeNewerOrEqualTags(proposedTag string, tags []string) []string { |
| 65 | + var validTags []string |
| 66 | + for _, tag := range tags { |
| 67 | + if semver.Compare(tag, proposedTag) < 0 { |
| 68 | + validTags = append(validTags, tag) |
73 | 69 | }
|
74 | 70 | }
|
| 71 | + return validTags |
| 72 | +} |
75 | 73 |
|
| 74 | +func removeTagsFromSameMinorSeries(proposedTag string, tags []string) []string { |
| 75 | + var validTags []string |
| 76 | + proposedMinor := semver.MajorMinor(proposedTag) |
76 | 77 | for _, tag := range tags {
|
77 |
| - if tag == proposedTag { |
78 |
| - continue |
79 |
| - } |
80 |
| - tagMajor := semver.Major(tag) |
81 |
| - tagMinor := semver.MajorMinor(tag) |
82 |
| - tagPatch, tagRC, err := extractPatchAndRC(tag) |
83 |
| - if err != nil { |
84 |
| - continue |
| 78 | + if semver.MajorMinor(tag) != proposedMinor { |
| 79 | + validTags = append(validTags, tag) |
85 | 80 | }
|
| 81 | + } |
| 82 | + return validTags |
| 83 | +} |
86 | 84 |
|
87 |
| - // Only bother considering tags with the same major and minor version. |
88 |
| - if tagMajor == proposedMajor && tagMinor == proposedMinor { |
89 |
| - // If it's a non-RC release... |
90 |
| - if proposedRC == "0" { |
91 |
| - // Only consider non-RC tags. |
92 |
| - if tagRC == "0" { |
93 |
| - if semver.Compare(tag, previousTag) > 0 { |
94 |
| - previousTag = tag |
95 |
| - } |
96 |
| - } |
97 |
| - } else { |
98 |
| - if tagRC != "0" && tagPatch == proposedPatch { |
99 |
| - if semver.Compare(tag, previousTag) > 0 { |
100 |
| - previousTag = tag |
101 |
| - } |
102 |
| - } else if tagRC == "0" { |
103 |
| - if semver.Compare(tag, previousTag) > 0 { |
104 |
| - previousTag = tag |
105 |
| - } |
106 |
| - } |
107 |
| - } |
| 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 |
108 | 90 | }
|
109 | 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) |
110 | 107 | if previousTag == "" {
|
111 | 108 | return "", fmt.Errorf("no matching tag found for tags: " + strings.Join(tags, ", "))
|
112 | 109 | }
|
|
0 commit comments