|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "golang.org/x/mod/semver" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +/** |
| 14 | +This script is used to determine the previous version of a release based on the current version. It is used to help |
| 15 | +generate release notes for a new release. |
| 16 | +*/ |
| 17 | + |
| 18 | +func main() { |
| 19 | + if len(os.Args) < 2 { |
| 20 | + fmt.Println("Usage: go run get-previous-version-for-release-notes.go <version being released>") |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + proposedTag := os.Args[1] |
| 25 | + |
| 26 | + tags, err := getGitTags() |
| 27 | + if err != nil { |
| 28 | + fmt.Printf("Error getting git tags: %v\n", err) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + previousTag, err := findPreviousTag(proposedTag, tags) |
| 33 | + if err != nil { |
| 34 | + fmt.Printf("Error finding previous tag: %v\n", err) |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + fmt.Printf("%s\n", previousTag) |
| 39 | +} |
| 40 | + |
| 41 | +func extractPatchAndRC(tag string) (string, string, error) { |
| 42 | + re := regexp.MustCompile(`^v\d+\.\d+\.(\d+)(?:-rc(\d+))?$`) |
| 43 | + matches := re.FindStringSubmatch(tag) |
| 44 | + if len(matches) < 2 { |
| 45 | + return "", "", fmt.Errorf("invalid tag format: %s", tag) |
| 46 | + } |
| 47 | + patch := matches[1] |
| 48 | + rc := "0" |
| 49 | + if len(matches) == 3 && matches[2] != "" { |
| 50 | + rc = matches[2] |
| 51 | + } |
| 52 | + return patch, rc, nil |
| 53 | +} |
| 54 | + |
| 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 |
| 63 | + } |
| 64 | + |
| 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) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + 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 |
| 85 | + } |
| 86 | + |
| 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 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + if previousTag == "" { |
| 111 | + return "", fmt.Errorf("no matching tag found for tags: " + strings.Join(tags, ", ")) |
| 112 | + } |
| 113 | + return previousTag, nil |
| 114 | +} |
| 115 | + |
| 116 | +func getGitTags() ([]string, error) { |
| 117 | + cmd := exec.Command("git", "tag", "--sort=-v:refname") |
| 118 | + output, err := cmd.Output() |
| 119 | + if err != nil { |
| 120 | + return nil, fmt.Errorf("error executing git command: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + tags := strings.Split(string(output), "\n") |
| 124 | + var semverTags []string |
| 125 | + for _, tag := range tags { |
| 126 | + if semver.IsValid(tag) { |
| 127 | + semverTags = append(semverTags, tag) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return semverTags, nil |
| 132 | +} |
0 commit comments