-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Songmu/convert-keep-a-changelog
implement updating CHANGELOG.md process
- Loading branch information
Showing
5 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters