Skip to content
This repository was archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
include PR links in changelog output (#10)
Browse files Browse the repository at this point in the history
include PR links in changelog output
  • Loading branch information
redhoteggplant authored and czimergebot committed Jun 13, 2019
1 parent 5f0c3f8 commit 1d9eaae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"syscall"
"time"
"regexp"

"gopkg.in/src-d/go-git.v4/plumbing/storer"

Expand Down Expand Up @@ -80,12 +81,26 @@ var changelogCmd = &cobra.Command{
},
}

// GetCommitLog takes a commit object and returns a commit log, for example:
// * [2847a2e6](../../commit/2847a2e624ee6736b43cc3a68acd75368d1a75d1) A commit message
// GetCommitLog takes a commit object and returns a commit log that may link to a pull request, for example:
// * [2847a2e6](../../commit/2847a2e624ee6736b43cc3a68acd75368d1a75d1) A commit message
// * [2847a2e6](../../commit/2847a2e624ee6736b43cc3a68acd75368d1a75d1) A commit message ([#100](../../pull/100))
func GetCommitLog(commit *object.Commit) string {
hash := commit.Hash.String()
if hash != "" {
return fmt.Sprintf("* [%s](../../commit/%s) %s", hash[:8], hash, strings.Split(commit.Message, "\n")[0])
var commitLog string
commitMsg := strings.Split(commit.Message, "\n")[0]
shortHash := hash[:8]
r := regexp.MustCompile("\\(#\\d+\\)$")
idx := r.FindStringIndex(commitMsg)
if idx == nil {
commitLog = fmt.Sprintf("* [%s](../../commit/%s) %s", shortHash, hash, commitMsg)
} else {
// extract message and PR number from commitMsg
message, prNum := commitMsg[:idx[0]], commitMsg[idx[0]+2:idx[1]-1]

commitLog = fmt.Sprintf("* [%s](../../commit/%s) %s([#%s](../../pull/%s))", shortHash, hash, message, prNum, prNum)
}
return commitLog
}
return ""
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func TestGetCommitLog(t *testing.T) {
},
"* [00010203](../../commit/0001020304050607080900010203040506070809) A commit message",
},
{"non-empty commit, one-line commit message with pull request number",
object.Commit{
Hash: [20]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Message: "A commit message (#100)",
},
"* [00010203](../../commit/0001020304050607080900010203040506070809) A commit message ([#100](../../pull/100))",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 1d9eaae

Please sign in to comment.