Skip to content

Commit

Permalink
Add option to add links to changelog
Browse files Browse the repository at this point in the history
Allows creating links in changelog, similar to what Github does
for markdown but works for dependencies as well.

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
  • Loading branch information
dmcgowan committed Sep 26, 2018
1 parent 2be80b4 commit 5c76170
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cmd/release-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,16 @@ This tool should be ran from the root of the project repository for a new releas
Usage: "template filepath to use in place of the default",
Value: defaultTemplateFile,
},
cli.BoolFlag{
Name: "linkify,l",
Usage: "add links to changelog",
},
}
app.Action = func(context *cli.Context) error {
var (
releasePath = context.Args().First()
tag = context.String("tag")
linkify = context.Bool("linkify")
)
if tag == "" {
tag = parseTag(releasePath)
Expand Down Expand Up @@ -150,6 +155,11 @@ This tool should be ran from the root of the project repository for a new releas
if err != nil {
return err
}
if linkify {
if err := linkifyChanges(changes, githubCommitLink(r.GithubRepo), githubPRLink(r.GithubRepo)); err != nil {
return err
}
}
if err := addContributors(r.Previous, r.Commit, contributors); err != nil {
return err
}
Expand Down Expand Up @@ -221,6 +231,16 @@ This tool should be ran from the root of the project repository for a new releas
if err := addContributors(dep.Previous, dep.Commit, contributors); err != nil {
return errors.Wrapf(err, "failed to get authors for %s", name)
}
if linkify {
if !strings.HasPrefix(dep.Name, "github.com/") {
logrus.Debugf("linkify only supported for Github, skipping %s", dep.Name)
} else {
ghname := dep.Name[11:]
if err := linkifyChanges(changes, githubCommitLink(ghname), githubPRLink(ghname)); err != nil {
return err
}
}
}

projectChanges = append(projectChanges, projectChange{
Name: name,
Expand Down
54 changes: 54 additions & 0 deletions cmd/release-tool/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -113,6 +114,26 @@ func getChangelog(previous, commit string) ([]byte, error) {
return git("log", "--oneline", gitChangeDiff(previous, commit))
}

func linkifyChanges(c []change, commit, msg func(change) (string, error)) error {
for i := range c {
commitLink, err := commit(c[i])
if err != nil {
return err
}

description, err := msg(c[i])
if err != nil {
return err
}

c[i].Commit = fmt.Sprintf("[`%s`](%s)", c[i].Commit, commitLink)
c[i].Description = description

}

return nil
}

func parseChangelog(changelog []byte) ([]change, error) {
var (
changes []change
Expand Down Expand Up @@ -282,3 +303,36 @@ func getTemplate(context *cli.Context) (string, error) {
}
return string(data), nil
}

func githubCommitLink(repo string) func(change) (string, error) {
return func(c change) (string, error) {
full, err := git("rev-parse", c.Commit)
if err != nil {
return "", err
}
commit := strings.TrimSpace(string(full))

return fmt.Sprintf("https://github.com/%s/commit/%s", repo, commit), nil
}
}

func githubPRLink(repo string) func(change) (string, error) {
r := regexp.MustCompile("^Merge pull request #[0-9]+")
return func(c change) (string, error) {
var err error
message := r.ReplaceAllStringFunc(c.Description, func(m string) string {
idx := strings.Index(m, "#")
pr := m[idx+1:]

// TODO: Validate links using github API
// TODO: Validate PR merged as commit hash
link := fmt.Sprintf("https://github.com/%s/pull/%s", repo, pr)

return fmt.Sprintf("%s [#%s](%s)", m[:idx], pr, link)
})
if err != nil {
return "", err
}
return message, nil
}
}

0 comments on commit 5c76170

Please sign in to comment.