Skip to content

Commit

Permalink
Merge pull request #288 from suzuki-shunsuke/feat/get-pull-request-wi…
Browse files Browse the repository at this point in the history
…th-commit

feat: get the associated pull request with commit
  • Loading branch information
suzuki-shunsuke committed May 7, 2022
2 parents 81a42f7 + cc90277 commit 70d0d39
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 50 deletions.
24 changes: 8 additions & 16 deletions pkg/notifier/github/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,22 @@ package github
import (
"context"
"errors"
"strconv"
"strings"
)

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

func (g *CommitsService) MergedPRNumber(ctx context.Context, revision string) (int, error) {
commit, _, err := g.client.API.RepositoriesGetCommit(ctx, revision)
func (g *CommitsService) MergedPRNumber(ctx context.Context, sha string) (int, error) {
prs, _, err := g.client.API.PullRequestsListPullRequestsWithCommit(ctx, sha, nil)
if err != nil {
return 0, err
}

message := commit.Commit.GetMessage()
if !strings.HasPrefix(message, "Merge pull request #") {
return 0, errors.New("not a merge commit")
}

message = strings.TrimPrefix(message, "Merge pull request #")
i := strings.Index(message, " from")
if i >= 0 {
return strconv.Atoi(message[0:i])
for _, pr := range prs {
if pr.GetState() != "closed" {
continue
}
return pr.GetNumber(), nil
}

return 0, errors.New("not a merge commit")
return 0, errors.New("associated pull request isn't found")
}
19 changes: 2 additions & 17 deletions pkg/notifier/github/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,9 @@ func TestMergedPRNumber(t *testing.T) {
revision string
}{
{
prNumber: 1,
prNumber: 2,
ok: true,
revision: "Merge pull request #1 from suzuki-shunsuke/tfcmt",
},
{
prNumber: 123,
ok: true,
revision: "Merge pull request #123 from suzuki-shunsuke/tfcmt",
},
{
prNumber: 0,
ok: false,
revision: "destroyed the world",
},
{
prNumber: 0,
ok: false,
revision: "Merge pull request #string from suzuki-shunsuke/tfcmt",
revision: "xxx",
},
}

Expand Down
13 changes: 3 additions & 10 deletions pkg/notifier/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type API interface {
IssuesRemoveLabel(ctx context.Context, number int, label string) (*github.Response, error)
IssuesUpdateLabel(ctx context.Context, label, color string) (*github.Label, *github.Response, error)
RepositoriesCreateComment(ctx context.Context, sha string, comment *github.RepositoryComment) (*github.RepositoryComment, *github.Response, error)
RepositoriesListCommits(ctx context.Context, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error)
RepositoriesGetCommit(ctx context.Context, sha string) (*github.RepositoryCommit, *github.Response, error)
PullRequestsListPullRequestsWithCommit(ctx context.Context, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error)
}

// GitHub represents the attribute information necessary for requesting GitHub API
Expand Down Expand Up @@ -62,12 +61,6 @@ func (g *GitHub) RepositoriesCreateComment(ctx context.Context, sha string, comm
return g.Client.Repositories.CreateComment(ctx, g.owner, g.repo, sha, comment)
}

// RepositoriesListCommits is a wrapper of https://godoc.org/github.com/google/go-github/github#RepositoriesService.ListCommits
func (g *GitHub) RepositoriesListCommits(ctx context.Context, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) {
return g.Client.Repositories.ListCommits(ctx, g.owner, g.repo, opt)
}

// RepositoriesGetCommit is a wrapper of https://godoc.org/github.com/google/go-github/github#RepositoriesService.GetCommit
func (g *GitHub) RepositoriesGetCommit(ctx context.Context, sha string) (*github.RepositoryCommit, *github.Response, error) {
return g.Client.Repositories.GetCommit(ctx, g.owner, g.repo, sha, nil)
func (g *GitHub) PullRequestsListPullRequestsWithCommit(ctx context.Context, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) {
return g.Client.PullRequests.ListPullRequestsWithCommit(ctx, g.owner, g.repo, sha, opt)
}
31 changes: 24 additions & 7 deletions pkg/notifier/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (

type fakeAPI struct {
API
FakeIssuesCreateComment func(ctx context.Context, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
FakeIssuesListLabels func(ctx context.Context, number int, opts *github.ListOptions) ([]*github.Label, *github.Response, error)
FakeIssuesAddLabels func(ctx context.Context, number int, labels []string) ([]*github.Label, *github.Response, error)
FakeIssuesRemoveLabel func(ctx context.Context, number int, label string) (*github.Response, error)
FakeRepositoriesCreateComment func(ctx context.Context, sha string, comment *github.RepositoryComment) (*github.RepositoryComment, *github.Response, error)
FakeRepositoriesListCommits func(ctx context.Context, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error)
FakeRepositoriesGetCommit func(ctx context.Context, sha string) (*github.RepositoryCommit, *github.Response, error)
FakeIssuesCreateComment func(ctx context.Context, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
FakeIssuesListLabels func(ctx context.Context, number int, opts *github.ListOptions) ([]*github.Label, *github.Response, error)
FakeIssuesAddLabels func(ctx context.Context, number int, labels []string) ([]*github.Label, *github.Response, error)
FakeIssuesRemoveLabel func(ctx context.Context, number int, label string) (*github.Response, error)
FakeRepositoriesCreateComment func(ctx context.Context, sha string, comment *github.RepositoryComment) (*github.RepositoryComment, *github.Response, error)
FakeRepositoriesListCommits func(ctx context.Context, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error)
FakeRepositoriesGetCommit func(ctx context.Context, sha string) (*github.RepositoryCommit, *github.Response, error)
FakePullRequestsListPullRequestsWithCommit func(ctx context.Context, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error)
}

func (g *fakeAPI) IssuesCreateComment(ctx context.Context, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
Expand Down Expand Up @@ -46,6 +47,10 @@ func (g *fakeAPI) RepositoriesGetCommit(ctx context.Context, sha string) (*githu
return g.FakeRepositoriesGetCommit(ctx, sha)
}

func (g *fakeAPI) PullRequestsListPullRequestsWithCommit(ctx context.Context, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) {
return g.FakePullRequestsListPullRequestsWithCommit(ctx, sha, opt)
}

func newFakeAPI() fakeAPI {
return fakeAPI{
FakeIssuesCreateComment: func(ctx context.Context, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
Expand Down Expand Up @@ -102,6 +107,18 @@ func newFakeAPI() fakeAPI {
},
}, nil, nil
},
FakePullRequestsListPullRequestsWithCommit: func(ctx context.Context, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) {
return []*github.PullRequest{
{
State: github.String("open"),
Number: github.Int(1),
},
{
State: github.String("closed"),
Number: github.Int(2),
},
}, nil, nil
},
}
}

Expand Down

0 comments on commit 70d0d39

Please sign in to comment.