Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create as many changelogs as possible if missing #35

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
versionLinkReg = regexp.MustCompile(`\*\*Full Changelog\*\*: (https://.*)$`)
versionLinkReg = regexp.MustCompile(`\n\*\*Full Changelog\*\*: (https://.*)$`)
semverFromLinkReg = regexp.MustCompile(`.*[./](v?[0-9]+\.[0-9]+\.[0-9]+)`)
newContribReg = regexp.MustCompile(`(?ms)## New Contributors.*\z`)
)
Expand Down Expand Up @@ -45,9 +45,14 @@ func exists(filename string) bool {
return err == nil
}

func insertNewChangelog(orig []byte, section string) string {
var changelogReg = regexp.MustCompile(`(?i)^# Change\s?log`)

func insertNewChangelog(orig string, section string) string {
orig = strings.TrimSpace(orig) + "\n"
section = strings.TrimSpace(section) + "\n"

var bf bytes.Buffer
lineSnr := bufio.NewScanner(bytes.NewReader(orig))
lineSnr := bufio.NewScanner(strings.NewReader(orig))
inserted := false
for lineSnr.Scan() {
line := lineSnr.Text()
Expand Down
23 changes: 20 additions & 3 deletions rcpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,33 @@ func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) err

changelog := convertKeepAChangelogFormat(releases.Body)
changelogMd := "CHANGELOG.md"

var content string
if exists(changelogMd) {
byt, err := os.ReadFile(changelogMd)
if err != nil {
return err
}
content = insertNewChangelog(byt, changelog)
} else {
content = "# Changelog\n\n" + changelog
content = strings.TrimSpace(string(byt)) + "\n"
}

// If the changelog is not in "keep a changelog" format, or if the file does not exist, re-create everything. Is it rough...?
if !changelogReg.MatchString(content) {
// We are concerned that depending on the release history, API requests may become more frequent.
vers := (&gitsemvers.Semvers{}).VersionStrings()
logs := []string{"# Changelog\n"}
for i := len(vers) - 1; i >= 0 || len(vers)-i < 10; i-- { // Up to 10 most recent versions
ver := vers[i]
releases, _, _ := rp.gh.Repositories.GenerateReleaseNotes(
ctx, rp.owner, rp.repo, &github.GenerateNotesOptions{
TagName: ver,
})
logs = append(logs, strings.TrimSpace(convertKeepAChangelogFormat(releases.Body))+"\n")
}
content = strings.Join(logs, "\n")
}

content = insertNewChangelog(content, changelog)
if err := os.WriteFile(changelogMd, []byte(content), 0644); err != nil {
return err
}
Expand Down