Skip to content

Commit

Permalink
Merge pull request #34 from Songmu/convert-keep-a-changelog
Browse files Browse the repository at this point in the history
implement updating CHANGELOG.md process
  • Loading branch information
Songmu authored Aug 17, 2022
2 parents 80c2abc + e513d6c commit 2e74a98
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
67 changes: 67 additions & 0 deletions changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package rcpr

import (
"bufio"
"bytes"
"fmt"
"os"
"regexp"
"strings"

"github.com/Songmu/flextime"
)

var (
versionLinkReg = regexp.MustCompile(`\*\*Full Changelog\*\*: (https://.*)$`)
semverFromLinkReg = regexp.MustCompile(`.*[./](v?[0-9]+\.[0-9]+\.[0-9]+)`)
newContribReg = regexp.MustCompile(`(?ms)## New Contributors.*\z`)
)

func convertKeepAChangelogFormat(md string) string {
md = strings.TrimSpace(md)

var link string
md = versionLinkReg.ReplaceAllStringFunc(md, func(match string) string {
m := versionLinkReg.FindStringSubmatch(match)
link = m[1]
return ""
})
var semvStr string
if m := semverFromLinkReg.FindStringSubmatch(link); len(m) > 1 {
semvStr = m[1]
}
now := flextime.Now()

heading := fmt.Sprintf("## [%s](%s) - %s", semvStr, link, now.Format("2006-01-02"))
md = strings.Replace(md, "## What's Changed", heading, 1)
md = strings.ReplaceAll(md, "\n* ", "\n- ")
md = newContribReg.ReplaceAllString(md, "")

return strings.TrimSpace(md) + "\n"
}

func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}

func insertNewChangelog(orig []byte, section string) string {
var bf bytes.Buffer
lineSnr := bufio.NewScanner(bytes.NewReader(orig))
inserted := false
for lineSnr.Scan() {
line := lineSnr.Text()
if !inserted && strings.HasPrefix(line, "## ") {
bf.WriteString(section)
bf.WriteString("\n")
inserted = true
}
bf.WriteString(line)
bf.WriteString("\n")
}
if !inserted {
bf.WriteString("\n")
bf.WriteString(section)
}
return bf.String()
}
64 changes: 64 additions & 0 deletions changelog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package rcpr

import (
"testing"
"time"

"github.com/Songmu/flextime"
)

func TestConvertKeepAChangelogFormat(t *testing.T) {

input := `## What's Changed
* add github.go for github client by @Songmu in https://github.com/Songmu/rcpr/pull/1
* create rc pull request when the default branch proceeded by @Songmu in https://github.com/Songmu/rcpr/pull/2
* dogfooding by @Songmu in https://github.com/Songmu/rcpr/pull/3
* set label to the pull request by @Songmu in https://github.com/Songmu/rcpr/pull/5
* change rc branch naming convention by @Songmu in https://github.com/Songmu/rcpr/pull/6
* adjust auto commit message by @Songmu in https://github.com/Songmu/rcpr/pull/8
* apply the commits added on the RC branch with cherry-pick by @Songmu in https://github.com/Songmu/rcpr/pull/9
* unshallow if a shallow repository by @Songmu in https://github.com/Songmu/rcpr/pull/10
* fix git log by @Songmu in https://github.com/Songmu/rcpr/pull/11
* parse git URL more precise by @Songmu in https://github.com/Songmu/rcpr/pull/12
* fix parseGitURL by @Songmu in https://github.com/Songmu/rcpr/pull/13
* refactor git.go by @Songmu in https://github.com/Songmu/rcpr/pull/14
* set user.email and user.name only if they aren't set by @Songmu in https://github.com/Songmu/rcpr/pull/15
* fix api base handling by @Songmu in https://github.com/Songmu/rcpr/pull/16
* take care of v-prefix or not in tags by @Songmu in https://github.com/Songmu/rcpr/pull/17
* Detect version file and update by @Songmu in https://github.com/Songmu/rcpr/pull/18
* tagging semver to merged rcpr by @Songmu in https://github.com/Songmu/rcpr/pull/19
## New Contributors
* @Songmu made their first contribution in https://github.com/Songmu/rcpr/pull/1
**Full Changelog**: https://github.com/Songmu/rcpr/commits/v0.0.1
`

expect := `## [v0.0.1](https://github.com/Songmu/rcpr/commits/v0.0.1) - 2022-08-16
- add github.go for github client by @Songmu in https://github.com/Songmu/rcpr/pull/1
- create rc pull request when the default branch proceeded by @Songmu in https://github.com/Songmu/rcpr/pull/2
- dogfooding by @Songmu in https://github.com/Songmu/rcpr/pull/3
- set label to the pull request by @Songmu in https://github.com/Songmu/rcpr/pull/5
- change rc branch naming convention by @Songmu in https://github.com/Songmu/rcpr/pull/6
- adjust auto commit message by @Songmu in https://github.com/Songmu/rcpr/pull/8
- apply the commits added on the RC branch with cherry-pick by @Songmu in https://github.com/Songmu/rcpr/pull/9
- unshallow if a shallow repository by @Songmu in https://github.com/Songmu/rcpr/pull/10
- fix git log by @Songmu in https://github.com/Songmu/rcpr/pull/11
- parse git URL more precise by @Songmu in https://github.com/Songmu/rcpr/pull/12
- fix parseGitURL by @Songmu in https://github.com/Songmu/rcpr/pull/13
- refactor git.go by @Songmu in https://github.com/Songmu/rcpr/pull/14
- set user.email and user.name only if they aren't set by @Songmu in https://github.com/Songmu/rcpr/pull/15
- fix api base handling by @Songmu in https://github.com/Songmu/rcpr/pull/16
- take care of v-prefix or not in tags by @Songmu in https://github.com/Songmu/rcpr/pull/17
- Detect version file and update by @Songmu in https://github.com/Songmu/rcpr/pull/18
- tagging semver to merged rcpr by @Songmu in https://github.com/Songmu/rcpr/pull/19
`

restore := flextime.Fix(time.Date(2022, time.August, 16, 18, 10, 10, 0, time.UTC))
defer restore()

got := convertKeepAChangelogFormat(input)
if got != expect {
t.Errorf("error:\n %s", got)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/Songmu/flextime v0.1.0
github.com/Songmu/gitconfig v0.1.0
github.com/Songmu/gitsemvers v0.0.2
github.com/google/go-github/v45 v45.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Songmu/flextime v0.1.0 h1:sss5IALl84LbvU/cS5D1cKNd5ffT94N2BZwC+esgAJI=
github.com/Songmu/flextime v0.1.0/go.mod h1:ofUSZ/qj7f1BfQQ6rEH4ovewJ0SZmLOjBF1xa8iE87Q=
github.com/Songmu/gitconfig v0.1.0 h1:JsaQ6rh3Lnig0Xvo4t4nj7Xu6pXrMfyCOPJ6iRriuKY=
github.com/Songmu/gitconfig v0.1.0/go.mod h1:kVksEBYKkMQuZ5BumlnW7KBDBreiySMbf50dseHYOpQ=
github.com/Songmu/gitmock v0.0.2 h1:KF5GTll60LxGskZbt58QDd29y/GYLgdxqvkvnSU6RlY=
Expand Down
30 changes: 25 additions & 5 deletions rcpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
gitEmail = "github-actions[bot]@users.noreply.github.com"
defaultReleaseBranch = "main"
autoCommitMessage = "[rcpr] prepare for the next release"
autoChangelogMessage = "[rcpr] update CHANGELOG.md"
autoLableName = "rcpr"
)

Expand Down Expand Up @@ -282,8 +283,8 @@ func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) err
continue
}
commitish := m[0]
authorAndSubject := strings.TrimSpace(m[1])
if authorAndSubject != autoCommitMessage {
subject := strings.TrimSpace(m[1])
if subject != autoCommitMessage && subject != autoChangelogMessage {
cherryPicks = append(cherryPicks, commitish)
}
}
Expand All @@ -302,9 +303,6 @@ func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) err
}
}
}
if _, _, err := rp.c.gitE("push", "--force", rp.remoteName, rcBranch); err != nil {
return err
}

if vfile != "" {
nVer, _ := retrieveVersionFromFile(vfile, nextVer.vPrefix)
Expand All @@ -326,6 +324,28 @@ func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) err
return 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
}
if err := os.WriteFile(changelogMd, []byte(content), 0644); err != nil {
return err
}
rp.c.gitE("add", changelogMd)
rp.c.gitE("commit", "-m", autoChangelogMessage)

if _, _, err := rp.c.gitE("push", "--force", rp.remoteName, rcBranch); err != nil {
return err
}

// TODO: pull request template
title := fmt.Sprintf("release %s", nextVer.Tag())

Expand Down

0 comments on commit 2e74a98

Please sign in to comment.