2024-08-01 23:00:56 +02:00
|
|
|
package rp
|
|
|
|
|
|
|
|
|
|
import (
|
2024-08-23 22:17:38 +02:00
|
|
|
"fmt"
|
2024-08-23 22:02:58 +02:00
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
2024-08-01 23:00:56 +02:00
|
|
|
|
2024-08-23 22:02:58 +02:00
|
|
|
var (
|
|
|
|
|
GenericUpdaterSemVerRegex = regexp.MustCompile(`\d+\.\d+\.\d+(-[\w.]+)?(.*x-releaser-pleaser-version)`)
|
2024-08-23 22:17:38 +02:00
|
|
|
ChangelogUpdaterHeaderRegex = regexp.MustCompile(`^# Changelog\n`)
|
2024-08-01 23:00:56 +02:00
|
|
|
)
|
|
|
|
|
|
2024-08-23 22:02:58 +02:00
|
|
|
type ReleaseInfo struct {
|
|
|
|
|
Version string
|
|
|
|
|
ChangelogEntry string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Updater interface {
|
|
|
|
|
UpdateContent(content string, info ReleaseInfo) (string, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GenericUpdater struct{}
|
|
|
|
|
|
|
|
|
|
func (u *GenericUpdater) UpdateContent(content string, info ReleaseInfo) (string, error) {
|
|
|
|
|
// We strip the "v" prefix to avoid adding/removing it from the users input.
|
|
|
|
|
version := strings.TrimPrefix(info.Version, "v")
|
|
|
|
|
|
|
|
|
|
return GenericUpdaterSemVerRegex.ReplaceAllString(content, version+"${2}"), nil
|
2024-08-01 23:00:56 +02:00
|
|
|
}
|
2024-08-23 22:17:38 +02:00
|
|
|
|
|
|
|
|
type ChangelogUpdater struct{}
|
|
|
|
|
|
|
|
|
|
func (u *ChangelogUpdater) UpdateContent(content string, info ReleaseInfo) (string, error) {
|
|
|
|
|
headerIndex := ChangelogUpdaterHeaderRegex.FindStringIndex(content)
|
|
|
|
|
if headerIndex == nil && len(content) != 0 {
|
|
|
|
|
return "", fmt.Errorf("unexpected format of CHANGELOG.md, header does not match")
|
|
|
|
|
}
|
|
|
|
|
if headerIndex != nil {
|
|
|
|
|
// Remove the header from the content
|
|
|
|
|
content = content[headerIndex[1]:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content = ChangelogHeader + "\n\n" + info.ChangelogEntry + content
|
|
|
|
|
|
|
|
|
|
return content, nil
|
|
|
|
|
}
|