Skip to content

Instantly share code, notes, and snippets.

@twyatt
Created March 12, 2024 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twyatt/28fce197fa3bf266f56b3a8fe4856521 to your computer and use it in GitHub Desktop.
Save twyatt/28fce197fa3bf266f56b3a8fe4856521 to your computer and use it in GitHub Desktop.
Reads release notes from clipboard and prints condensed version to console.
/* Reads release notes from clipboard and prints condensed version to console.
*
* **BEFORE**
*
* - Update ktlint to v1.0.1 (#92)
* - Update dependency gradle to v8.3 (#86)
* - Update dependency gradle to v8.4 (#91)
* - Update dependency org.slf4j:slf4j-simple to v2.0.9 (#90)
* - Update ktlint to v1 (major) (#89)
*
* **AFTER**
*
* - Update ktlint to v1.0.1 (#89, #92)
* - Update dependency gradle to v8.4 (#86, #91)
* - Update dependency org.slf4j:slf4j-simple to v2.0.9 (#90)
*/
package main
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/hashicorp/go-version"
"golang.design/x/clipboard"
)
func main() {
err := clipboard.Init()
if err != nil {
panic(err)
}
text := string(clipboard.Read(clipboard.FmtText))
lines := strings.Split(text, "\n")
versions := make(map[string]string)
prs := make(map[string][]int)
for i := 0; i < len(lines); i++ {
line := lines[i]
artifact, v, p := splitLine(line)
pr, _ := strconv.Atoi(p)
putLatest(&versions, artifact, v)
prs[artifact] = append(prs[artifact], pr)
}
for artifact, pr := range prs {
sort.Ints(pr)
v := versions[artifact]
fmt.Printf("- Update %s to v%s (%v)\n", artifact, v, flatten(pr))
}
}
func putLatest(versions *map[string]string, artifact string, ver string) {
new := sanitize(&ver)
p, hasPrev := (*versions)[artifact]
if !hasPrev || new.GreaterThan(sanitize(&p)) {
(*versions)[artifact] = ver
}
}
func sanitize(v *string) *version.Version {
if v == nil {
return nil
}
if idx := strings.IndexByte(*v, ' '); idx >= 0 {
ver, _ := version.NewVersion((*v)[:idx])
return ver
} else {
ver, _ := version.NewVersion(*v)
return ver
}
}
func splitLine(line string) (string, string, string) {
re := regexp.MustCompile(`- Update (.*) to v(.*) \(#(\d+)\)`)
matches := re.FindStringSubmatch(line)
return matches[1], matches[2], matches[3]
}
func flatten(prs []int) string {
text := ""
for _, pr := range prs {
if len(text) > 0 {
text += ", "
}
text += "#"
text += strconv.Itoa(pr)
}
return text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment