feat: add changelog heading links for unreleased and releases
Some checks failed
Push Validation / validate (push) Failing after 42s
Some checks failed
Push Validation / validate (push) Failing after 42s
- Link Unreleased heading to repository main branch. - Link release headings to release tag pages. - Derive repository URL from CI metadata or origin git remote. - Keep plain headings when repository URL cannot be resolved. - Update tests and README usage docs for linked heading behavior.
This commit is contained in:
@@ -16,6 +16,9 @@ const (
|
||||
)
|
||||
|
||||
var releasedSectionRe = regexp.MustCompile(`(?m)^## \[(\d+\.\d+\.\d+)\] - `)
|
||||
var linkedReleasedSectionRe = regexp.MustCompile(`(?m)^## \[(\d+\.\d+\.\d+)\](?:\([^\n)]*\))? - `)
|
||||
var unreleasedHeadingRe = regexp.MustCompile(`(?m)^## \[Unreleased\](?:\([^\n)]*\))?\n`)
|
||||
var releaseHeadingRe = regexp.MustCompile(`(?m)^## \[(\d+\.\d+\.\d+)\](?:\([^\n)]*\))? - `)
|
||||
|
||||
type Options struct {
|
||||
VersionFile string
|
||||
@@ -202,6 +205,10 @@ func updateChangelog(rootDir, version, releaseDate, changelogPath string) error
|
||||
}
|
||||
|
||||
updated := text[:afterHeader] + "\n" + newSection + text[nextSectionStart:]
|
||||
repoURL, ok := deriveRepositoryURL(rootDir)
|
||||
if ok {
|
||||
updated = addChangelogLinks(updated, repoURL)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(updated), 0o644); err != nil {
|
||||
return fmt.Errorf("write changelog: %w", err)
|
||||
}
|
||||
@@ -245,13 +252,12 @@ func readChangelogState(rootDir, changelogPath string) (string, string, int, int
|
||||
}
|
||||
|
||||
text := string(contents)
|
||||
unreleasedHeader := "## [Unreleased]\n"
|
||||
start := strings.Index(text, unreleasedHeader)
|
||||
if start == -1 {
|
||||
headerLoc := unreleasedHeadingRe.FindStringIndex(text)
|
||||
if headerLoc == nil {
|
||||
return "", "", 0, 0, "", fmt.Errorf("unreleased section not found in changelog")
|
||||
}
|
||||
|
||||
afterHeader := start + len(unreleasedHeader)
|
||||
afterHeader := headerLoc[1]
|
||||
nextSectionRelative := strings.Index(text[afterHeader:], "\n## [")
|
||||
if nextSectionRelative == -1 {
|
||||
nextSectionRelative = len(text[afterHeader:])
|
||||
@@ -269,13 +275,127 @@ func readLatestChangelogVersion(rootDir, changelogPath string) (string, bool, er
|
||||
return "", false, fmt.Errorf("read changelog: %w", err)
|
||||
}
|
||||
|
||||
match := releasedSectionRe.FindStringSubmatch(string(contents))
|
||||
match := linkedReleasedSectionRe.FindStringSubmatch(string(contents))
|
||||
if match == nil {
|
||||
return "", false, nil
|
||||
}
|
||||
return match[1], true, nil
|
||||
}
|
||||
|
||||
func deriveRepositoryURL(rootDir string) (string, bool) {
|
||||
serverURL := strings.TrimSpace(os.Getenv("GITHUB_SERVER_URL"))
|
||||
repository := strings.TrimSpace(os.Getenv("GITHUB_REPOSITORY"))
|
||||
if serverURL != "" && repository != "" {
|
||||
return strings.TrimSuffix(serverURL, "/") + "/" + strings.TrimPrefix(repository, "/"), true
|
||||
}
|
||||
|
||||
gitConfigPath := filepath.Join(rootDir, ".git", "config")
|
||||
contents, err := os.ReadFile(gitConfigPath)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
remoteURL, ok := originRemoteURLFromGitConfig(string(contents))
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
repoURL, ok := normalizeRepoURL(remoteURL)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return repoURL, true
|
||||
}
|
||||
|
||||
func originRemoteURLFromGitConfig(config string) (string, bool) {
|
||||
inOrigin := false
|
||||
for _, line := range strings.Split(config, "\n") {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") {
|
||||
inOrigin = trimmed == `[remote "origin"]`
|
||||
continue
|
||||
}
|
||||
|
||||
if !inOrigin {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(trimmed, "url") {
|
||||
parts := strings.SplitN(trimmed, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
url := strings.TrimSpace(parts[1])
|
||||
if url != "" {
|
||||
return url, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
func normalizeRepoURL(remoteURL string) (string, bool) {
|
||||
remoteURL = strings.TrimSpace(remoteURL)
|
||||
if remoteURL == "" {
|
||||
return "", false
|
||||
}
|
||||
|
||||
if strings.HasPrefix(remoteURL, "http://") || strings.HasPrefix(remoteURL, "https://") {
|
||||
return strings.TrimSuffix(remoteURL, ".git"), true
|
||||
}
|
||||
|
||||
if strings.HasPrefix(remoteURL, "ssh://") {
|
||||
withoutScheme := strings.TrimPrefix(remoteURL, "ssh://")
|
||||
at := strings.Index(withoutScheme, "@")
|
||||
if at == -1 {
|
||||
return "", false
|
||||
}
|
||||
hostAndPath := withoutScheme[at+1:]
|
||||
host, path, ok := strings.Cut(hostAndPath, "/")
|
||||
if !ok || host == "" || path == "" {
|
||||
return "", false
|
||||
}
|
||||
return "https://" + host + "/" + strings.TrimSuffix(path, ".git"), true
|
||||
}
|
||||
|
||||
if strings.Contains(remoteURL, "@") && strings.Contains(remoteURL, ":") {
|
||||
afterAt, ok := strings.CutPrefix(remoteURL, strings.Split(remoteURL, "@")[0]+"@")
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
host, path, ok := strings.Cut(afterAt, ":")
|
||||
if !ok || host == "" || path == "" {
|
||||
return "", false
|
||||
}
|
||||
return "https://" + host + "/" + strings.TrimSuffix(path, ".git"), true
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
func addChangelogLinks(text, repoURL string) string {
|
||||
repoURL = strings.TrimSuffix(strings.TrimSpace(repoURL), "/")
|
||||
if repoURL == "" {
|
||||
return text
|
||||
}
|
||||
|
||||
mainURL := repoURL + "/src/branch/main"
|
||||
text = unreleasedHeadingRe.ReplaceAllString(text, fmt.Sprintf("## [Unreleased](%s)\n", mainURL))
|
||||
|
||||
text = releaseHeadingRe.ReplaceAllStringFunc(text, func(match string) string {
|
||||
parts := releaseHeadingRe.FindStringSubmatch(match)
|
||||
if len(parts) < 2 {
|
||||
return match
|
||||
}
|
||||
version := parts[1]
|
||||
return fmt.Sprintf("## [%s](%s/releases/tag/v%s) - ", version, repoURL, version)
|
||||
})
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
func parseSemver(version string) (semver, error) {
|
||||
parts := strings.Split(strings.TrimSpace(version), ".")
|
||||
if len(parts) != 3 {
|
||||
|
||||
Reference in New Issue
Block a user