提交 9da12f4a 编写于 作者: J Jingwen Owen Ou

Merge branch 'default_msg_in_pr'

...@@ -126,30 +126,48 @@ func writePullRequestTitleAndBody(repo *github.Repo) (title, body string, err er ...@@ -126,30 +126,48 @@ func writePullRequestTitleAndBody(repo *github.Repo) (title, body string, err er
} }
func writePullRequestChanges(repo *github.Repo, messageFile string) error { func writePullRequestChanges(repo *github.Repo, messageFile string) error {
message := ` commits, err := git.RefList(repo.Base, repo.Head)
# Requesting a pull to %s from %s if err != nil {
# return err
# Write a message for this pull request. The first block }
# of the text is the title and the rest is description.%s
` var defaultMsg, commitSummary string
if len(commits) == 1 {
defaultMsg, err = git.Show(commits[0])
if err != nil {
return err
}
defaultMsg = fmt.Sprintf("%s\n", defaultMsg)
} else if len(commits) > 1 {
commitLogs, err := git.Log(repo.Base, repo.Head)
if err != nil {
return err
}
if len(commitLogs) > 0 {
startRegexp := regexp.MustCompilePOSIX("^") startRegexp := regexp.MustCompilePOSIX("^")
endRegexp := regexp.MustCompilePOSIX(" +$") endRegexp := regexp.MustCompilePOSIX(" +$")
commitLogs, _ := git.Log(repo.Base, repo.Head)
var changesMsg string
if len(commitLogs) > 0 {
commitLogs = strings.TrimSpace(commitLogs) commitLogs = strings.TrimSpace(commitLogs)
commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ") commitLogs = startRegexp.ReplaceAllString(commitLogs, "# ")
commitLogs = endRegexp.ReplaceAllString(commitLogs, "") commitLogs = endRegexp.ReplaceAllString(commitLogs, "")
changesMsg = ` commitSummary = `
# #
# Changes: # Changes:
# #
%s` %s`
changesMsg = fmt.Sprintf(changesMsg, commitLogs) commitSummary = fmt.Sprintf(commitSummary, commitLogs)
}
} }
message = fmt.Sprintf(message, repo.FullBase(), repo.FullHead(), changesMsg) message := `%s
# Requesting a pull to %s from %s
#
# Write a message for this pull request. The first block
# of the text is the title and the rest is description.%s
`
message = fmt.Sprintf(message, defaultMsg, repo.FullBase(), repo.FullHead(), commitSummary)
return ioutil.WriteFile(messageFile, []byte(message), 0644) return ioutil.WriteFile(messageFile, []byte(message), 0644)
} }
......
...@@ -68,6 +68,25 @@ func Ref(ref string) (string, error) { ...@@ -68,6 +68,25 @@ func Ref(ref string) (string, error) {
return output[0], nil return output[0], nil
} }
func RefList(a, b string) ([]string, error) {
ref := fmt.Sprintf("%s...%s", a, b)
output, err := execGitCmd("rev-list", "--cherry-pick", "--right-only", "--no-merges", ref)
if err != nil {
return nil, errors.New("Can't load rev-list for %s" + ref)
}
return output, nil
}
func Show(sha string) (string, error) {
output, err := execGitCmd("show", "-s", "--format=%w(78,0,0)%s%+b", sha)
if err != nil {
return "", errors.New("Can't show commit for %s" + sha)
}
return output[0], nil
}
func Log(sha1, sha2 string) (string, error) { func Log(sha1, sha2 string) (string, error) {
execCmd := cmd.New("git") execCmd := cmd.New("git")
execCmd.WithArg("log").WithArg("--no-color") execCmd.WithArg("log").WithArg("--no-color")
...@@ -111,8 +130,11 @@ func execGitCmd(input ...string) (outputs []string, err error) { ...@@ -111,8 +130,11 @@ func execGitCmd(input ...string) (outputs []string, err error) {
out, err := cmd.ExecOutput() out, err := cmd.ExecOutput()
for _, line := range strings.Split(out, "\n") { for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line != "" {
outputs = append(outputs, string(line)) outputs = append(outputs, string(line))
} }
}
return outputs, err return outputs, err
} }
...@@ -35,12 +35,27 @@ func TestGitHead(t *testing.T) { ...@@ -35,12 +35,27 @@ func TestGitHead(t *testing.T) {
} }
func TestGitLog(t *testing.T) { func TestGitLog(t *testing.T) {
logs, _ := Log("master", "HEAD") log, err := Log("e357a98a1a580b09d4f1d9bf613a6a51e131ef6e", "49e984e2fe86f68c386aeb133b390d39e4264ec1")
assert.T(t, len(logs) >= 0) assert.Equal(t, nil, err)
assert.NotEqual(t, "", log)
} }
func TestGitRef(t *testing.T) { func TestGitRef(t *testing.T) {
gitRef, err := Ref("master") gitRef, err := Ref("1c1077c052d32a83aa13a8afaa4a9630d2f28ef6")
assert.Equal(t, nil, err)
assert.Equal(t, "1c1077c052d32a83aa13a8afaa4a9630d2f28ef6", gitRef)
}
func TestGitRefList(t *testing.T) {
refList, err := RefList("e357a98a1a580b09d4f1d9bf613a6a51e131ef6e", "49e984e2fe86f68c386aeb133b390d39e4264ec1")
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(refList))
assert.Equal(t, "49e984e2fe86f68c386aeb133b390d39e4264ec1", refList[0])
}
func TestGitShow(t *testing.T) {
output, err := Show("ce20e63ad00751bfed5d08072b11cf1b43af1995")
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.NotEqual(t, "", gitRef) assert.Equal(t, "Add Git.RefList", output)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册