Skip to content

Commit

Permalink
Merge pull request #390 from micnncim/commit-comments
Browse files Browse the repository at this point in the history
feat: support PR comments for push events
  • Loading branch information
suzuki-shunsuke committed Aug 7, 2022
2 parents d65bb22 + d359a60 commit e1df4c8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/notifier/github/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (g *NotifyService) Apply(ctx context.Context, param *notifier.ParamExec) (i
return result.ExitCode, err
}
if cfg.PR.Number == 0 {
if prNumber, err := g.client.Commits.MergedPRNumber(ctx, cfg.PR.Revision); err == nil {
if prNumber, err := g.client.Commits.PRNumber(ctx, cfg.PR.Revision, PullRequestStateClosed); err == nil {
cfg.PR.Number = prNumber
}
}
Expand Down
26 changes: 18 additions & 8 deletions pkg/notifier/github/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@ package github
import (
"context"
"errors"

"github.com/google/go-github/v43/github"
)

// CommitsService handles communication with the commits related
// methods of GitHub API
type CommitsService service

func (g *CommitsService) MergedPRNumber(ctx context.Context, sha string) (int, error) {
prs, _, err := g.client.API.PullRequestsListPullRequestsWithCommit(ctx, sha, nil)
type PullRequestState string

const (
PullRequestStateOpen = "open"
PullRequestStateClosed = "closed"
PullRequestStateAll = "all"
)

func (g *CommitsService) PRNumber(ctx context.Context, sha string, state PullRequestState) (int, error) {
prs, _, err := g.client.API.PullRequestsListPullRequestsWithCommit(ctx, sha, &github.PullRequestListOptions{
State: string(state),
Sort: "updated",
})
if err != nil {
return 0, err
}
for _, pr := range prs {
if pr.GetState() != "closed" {
continue
}
return pr.GetNumber(), nil
if len(prs) == 0 {
return 0, errors.New("associated pull request isn't found")
}
return 0, errors.New("associated pull request isn't found")
return prs[0].GetNumber(), nil
}
8 changes: 4 additions & 4 deletions pkg/notifier/github/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"testing"
)

func TestMergedPRNumber(t *testing.T) {
func TestPRNumber(t *testing.T) {
t.Parallel()
testCases := []struct {
prNumber int
ok bool
revision string
}{
{
prNumber: 2,
prNumber: 1,
ok: true,
revision: "xxx",
},
Expand All @@ -27,12 +27,12 @@ func TestMergedPRNumber(t *testing.T) {
}
api := newFakeAPI()
client.API = &api
prNumber, err := client.Commits.MergedPRNumber(context.Background(), testCase.revision)
prNumber, err := client.Commits.PRNumber(context.Background(), testCase.revision, PullRequestStateOpen)
if (err == nil) != testCase.ok {
t.Errorf("got error %q", err)
}
if prNumber != testCase.prNumber {
t.Errorf("got %q but want %q", prNumber, testCase.prNumber)
t.Errorf("got %d but want %d", prNumber, testCase.prNumber)
}
}
}
5 changes: 5 additions & 0 deletions pkg/notifier/github/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (g *NotifyService) Plan(ctx context.Context, param *notifier.ParamExec) (in
if err != nil {
return result.ExitCode, err
}
if cfg.PR.Number == 0 && cfg.PR.Revision != "" {
if prNumber, err := g.client.Commits.PRNumber(ctx, cfg.PR.Revision, PullRequestStateOpen); err == nil {
cfg.PR.Number = prNumber
}
}

logE := logrus.WithFields(logrus.Fields{
"program": "tfcmt",
Expand Down

0 comments on commit e1df4c8

Please sign in to comment.