Skip to content

Commit

Permalink
Merge pull request #19 from k1LoW/latest-merged
Browse files Browse the repository at this point in the history
Put comment to latest merged pull request ( Add `--latest-merged` )
  • Loading branch information
k1LoW authored Oct 5, 2021
2 parents cd7d099 + eead090 commit ea65ea7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ $ echo 'This is comment !!' | GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput issue-comment
$ echo 'This is comment !!' | GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput pr-comment --owner k1LoW --repo myrepo --number 2
```

**Put comment to latest merged pull request:**

``` console
$ echo 'Hello merged pull request !!' | GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput pr-comment --owner k1LoW --repo myrepo --latest-merged
```

**Put issue to repo:**

``` console
Expand Down
17 changes: 13 additions & 4 deletions cmd/prComment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var prCommentCmd = &cobra.Command{
Short: "Put comment to pull request",
Long: `Put comment to pull request.`,
Args: func(cmd *cobra.Command, args []string) error {
if number > 0 && latestMerged {
return errors.New("specify one of --number and --latest-merged")
}
if number == 0 && !latestMerged {
return errors.New("specify one of --number and --latest-merged")
}
fi, err := os.Stdin.Stat()
if err != nil {
return err
Expand All @@ -58,6 +64,12 @@ func runPrComment(stdin io.Reader, stdout io.Writer) error {
if err != nil {
return err
}
if latestMerged {
number, err = g.FetchLatestMergedPullRequest(ctx)
if err != nil {
return err
}
}
b, err := g.IsPullRequest(ctx, number)
if err != nil {
return err
Expand Down Expand Up @@ -91,10 +103,7 @@ func init() {
os.Exit(1)
}
prCommentCmd.Flags().IntVarP(&number, "number", "", 0, "pull request number")
if err := prCommentCmd.MarkFlagRequired("number"); err != nil {
prCommentCmd.PrintErrln(err)
os.Exit(1)
}
prCommentCmd.Flags().BoolVarP(&latestMerged, "latest-merged", "", false, "latest merged pull request")
prCommentCmd.Flags().StringVarP(&header, "header", "", "", "comment header")
prCommentCmd.Flags().StringVarP(&footer, "footer", "", "", "comment footer")
prCommentCmd.Flags().StringVarP(&key, "key", "", "", "key for uniquely identifying the comment")
Expand Down
31 changes: 16 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ import (
)

var (
owner string
repo string
number int
header string
footer string
key string
branch string
file string
path string
message string
public bool
filename string
title string
assignees []string
closeTitle string
owner string
repo string
number int
header string
footer string
key string
branch string
file string
path string
message string
public bool
filename string
title string
assignees []string
closeTitle string
latestMerged bool
)

// rootCmd represents the base command when called without any subcommands
Expand Down
44 changes: 44 additions & 0 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -97,6 +98,49 @@ func (g *Gh) CommentFooter() string {
return fmt.Sprintf(footerFormat, key)
}

func (g *Gh) FetchLatestMergedPullRequest(ctx context.Context) (int, error) {
commits, _, err := g.client.Repositories.ListCommits(ctx, g.owner, g.repo, &github.CommitsListOptions{
ListOptions: github.ListOptions{
Page: 1,
PerPage: 100,
},
})
if err != nil {
return 0, err
}
for _, c := range commits {
m := c.GetCommit().GetMessage()
if strings.HasPrefix(m, "Merge pull request #") {
splitted := strings.Split(strings.TrimPrefix(m, "Merge pull request #"), " ")
if len(splitted) < 1 {
break
}
n, err := strconv.Atoi(splitted[0])
if err != nil {
break
}
return n, nil
}
}
// fallback
q := fmt.Sprintf("type:pr is:merged sort:updated-desc repo:%s/%s", g.owner, g.repo)
prs, _, err := g.client.Search.Issues(ctx, q, &github.SearchOptions{
Sort: "updated",
Order: "desc",
ListOptions: github.ListOptions{
Page: 1,
PerPage: 1,
},
})
if err != nil {
return 0, err
}
if len(prs.Issues) == 0 {
return 0, err
}
return prs.Issues[0].GetNumber(), nil
}

func (g Gh) IsPullRequest(ctx context.Context, n int) (bool, error) {
i, _, err := g.client.Issues.Get(ctx, g.owner, g.repo, n)
if err != nil {
Expand Down

0 comments on commit ea65ea7

Please sign in to comment.