Skip to content

Commit 69e7c77

Browse files
committed
Add option to add links to changelog
Allows creating links in changelog, similar to what Github does for markdown but works for dependencies as well. Signed-off-by: Derek McGowan <[email protected]>
1 parent f76a5ec commit 69e7c77

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

cmd/containerd-release/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,16 @@ This tool should be ran from the root of the project repository for a new releas
116116
Usage: "template filepath to use in place of the default",
117117
Value: defaultTemplateFile,
118118
},
119+
cli.BoolFlag{
120+
Name: "linkify,l",
121+
Usage: "add links to changelog",
122+
},
119123
}
120124
app.Action = func(context *cli.Context) error {
121125
var (
122126
releasePath = context.Args().First()
123127
tag = context.String("tag")
128+
linkify = context.Bool("linkify")
124129
)
125130
if tag == "" {
126131
tag = parseTag(releasePath)
@@ -150,6 +155,11 @@ This tool should be ran from the root of the project repository for a new releas
150155
if err != nil {
151156
return err
152157
}
158+
if linkify {
159+
if err := linkifyChanges(changes, githubCommitLink(r.GithubRepo), githubPRLink(r.GithubRepo)); err != nil {
160+
return err
161+
}
162+
}
153163
if err := addContributors(r.Previous, r.Commit, contributors); err != nil {
154164
return err
155165
}
@@ -221,6 +231,16 @@ This tool should be ran from the root of the project repository for a new releas
221231
if err := addContributors(dep.Previous, dep.Commit, contributors); err != nil {
222232
return errors.Wrapf(err, "failed to get authors for %s", name)
223233
}
234+
if linkify {
235+
if !strings.HasPrefix(dep.Name, "github.com/") {
236+
logrus.Debugf("linkify only supported for Github, skipping %s", dep.Name)
237+
} else {
238+
ghname := dep.Name[11:]
239+
if err := linkifyChanges(changes, githubCommitLink(ghname), githubPRLink(ghname)); err != nil {
240+
return err
241+
}
242+
}
243+
}
224244

225245
projectChanges = append(projectChanges, projectChange{
226246
Name: name,

cmd/containerd-release/util.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"os/exec"
2727
"path/filepath"
28+
"regexp"
2829
"sort"
2930
"strings"
3031

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

117+
func linkifyChanges(c []change, commit, msg func(change) (string, error)) error {
118+
for i := range c {
119+
commitLink, err := commit(c[i])
120+
if err != nil {
121+
return err
122+
}
123+
124+
description, err := msg(c[i])
125+
if err != nil {
126+
return err
127+
}
128+
129+
c[i].Commit = fmt.Sprintf("[`%s`](%s)", c[i].Commit, commitLink)
130+
c[i].Description = description
131+
132+
}
133+
134+
return nil
135+
}
136+
116137
func parseChangelog(changelog []byte) ([]change, error) {
117138
var (
118139
changes []change
@@ -282,3 +303,36 @@ func getTemplate(context *cli.Context) (string, error) {
282303
}
283304
return string(data), nil
284305
}
306+
307+
func githubCommitLink(repo string) func(change) (string, error) {
308+
return func(c change) (string, error) {
309+
full, err := git("rev-parse", c.Commit)
310+
if err != nil {
311+
return "", err
312+
}
313+
commit := strings.TrimSpace(string(full))
314+
315+
return fmt.Sprintf("https://github.com/%s/commit/%s", repo, commit), nil
316+
}
317+
}
318+
319+
func githubPRLink(repo string) func(change) (string, error) {
320+
r := regexp.MustCompile("^Merge pull request #[0-9]+")
321+
return func(c change) (string, error) {
322+
var err error
323+
message := r.ReplaceAllStringFunc(c.Description, func(m string) string {
324+
idx := strings.Index(m, "#")
325+
pr := m[idx+1:]
326+
327+
// TODO: Validate links using github API
328+
// TODO: Validate PR merged as commit hash
329+
link := fmt.Sprintf("https://github.com/%s/pull/%s", repo, pr)
330+
331+
return fmt.Sprintf("%s [#%s](%s)", m[:idx], pr, link)
332+
})
333+
if err != nil {
334+
return "", err
335+
}
336+
return message, nil
337+
}
338+
}

0 commit comments

Comments
 (0)