Skip to content

Commit

Permalink
Merge pull request #301 from cpanato/add-request-reviewers
Browse files Browse the repository at this point in the history
Add pull request reviewers function
  • Loading branch information
k8s-ci-robot committed Jan 17, 2024
2 parents 8c28402 + 5b7156c commit 31b2d6e
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 17 deletions.
34 changes: 33 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strings"
"time"

"github.com/google/go-github/v56/github"
"github.com/google/go-github/v58/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"

Expand Down Expand Up @@ -151,6 +151,9 @@ type Client interface {
ListComments(
context.Context, string, string, int, *github.IssueListCommentsOptions,
) ([]*github.IssueComment, *github.Response, error)
RequestPullRequestReview(
context.Context, string, string, int, []string, []string,
) (*github.PullRequest, error)
}

// NewIssueOptions is a struct of optional fields for new issues
Expand Down Expand Up @@ -399,6 +402,23 @@ func (g *githubClient) CreatePullRequest(
return pr, nil
}

func (g *githubClient) RequestPullRequestReview(
ctx context.Context, owner, repo string, prNumber int, reviewers, teamReviewers []string,
) (*github.PullRequest, error) {
reviewersRequest := github.ReviewersRequest{
Reviewers: reviewers,
TeamReviewers: teamReviewers,
}

pr, _, err := g.PullRequests.RequestReviewers(ctx, owner, repo, prNumber, reviewersRequest)
if err != nil {
return pr, fmt.Errorf("requesting reviewers for PR %d: %w", prNumber, err)
}

logrus.Infof("Successfully added reviewers for PR #%d", pr.GetNumber())
return pr, nil
}

func (g *githubClient) CreateIssue(
ctx context.Context, owner, repo string, req *github.IssueRequest,
) (*github.Issue, error) {
Expand Down Expand Up @@ -858,6 +878,18 @@ func (g *GitHub) CreatePullRequest(
return pr, nil
}

func (g *GitHub) RequestPullRequestReview(
owner, repo string, prNumber int, reviewers, teamReviewers []string,
) (*github.PullRequest, error) {
// Use the client to create a new PR
pr, err := g.Client().RequestPullRequestReview(context.Background(), owner, repo, prNumber, reviewers, teamReviewers)
if err != nil {
return pr, err
}

return pr, nil
}

// GetMilestone returns a milestone object from its string name
func (g *GitHub) GetMilestone(owner, repo, title string) (
ms *github.Milestone, exists bool, err error,
Expand Down
29 changes: 28 additions & 1 deletion github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

gogithub "github.com/google/go-github/v56/github"
gogithub "github.com/google/go-github/v58/github"
"github.com/stretchr/testify/require"

"k8s.io/utils/ptr"
Expand Down Expand Up @@ -292,6 +292,33 @@ func TestCreatePullRequest(t *testing.T) {
require.Equal(t, fakeID, pr.GetID())
}

func TestRequestReviewers(t *testing.T) {
// Given
sut, client := newSUT()
fakeID := int64(1234)
fakeNumber := int(5678)
fakeUser := "fakeuser"
client.RequestPullRequestReviewReturns(&gogithub.PullRequest{
ID: &fakeID,
Number: &fakeNumber,
RequestedReviewers: []*gogithub.User{
{
Login: &fakeUser,
Name: &fakeUser,
},
},
}, nil)

// When requesting reviewers
updatedPr, err := sut.RequestPullRequestReview("kubernetes-fake-org", "kubernetes-fake-repo", fakeNumber, []string{fakeUser}, []string{})
require.Nil(t, err)
require.NotNil(t, updatedPr, nil)
require.Equal(t, fakeID, updatedPr.GetID())
require.Equal(t, fakeNumber, updatedPr.GetNumber())
require.Equal(t, 1, len(updatedPr.RequestedReviewers))
require.Equal(t, fakeUser, updatedPr.RequestedReviewers[0].GetName())
}

func TestGetMilestone(t *testing.T) {
sut, client := newSUT()
// Given
Expand Down
101 changes: 100 additions & 1 deletion github/githubfakes/fake_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion github/internal/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
"time"

"github.com/google/go-github/v56/github"
"github.com/google/go-github/v58/github"
"github.com/sirupsen/logrus"
)

Expand Down
2 changes: 1 addition & 1 deletion github/internal/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"github.com/google/go-github/v56/github"
"github.com/google/go-github/v58/github"
"github.com/sirupsen/logrus"

"sigs.k8s.io/release-sdk/github/internal"
Expand Down
8 changes: 7 additions & 1 deletion github/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"path/filepath"
"sync"

"github.com/google/go-github/v56/github"
"github.com/google/go-github/v58/github"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -217,6 +217,12 @@ func (c *githubNotesRecordClient) CreatePullRequest(
return &github.PullRequest{}, nil
}

func (c *githubNotesRecordClient) RequestPullRequestReview(
ctx context.Context, owner, repo string, prNumber int, reviewers, teamReviewers []string, //nolint: revive
) (*github.PullRequest, error) {
return &github.PullRequest{}, nil
}

func (c *githubNotesRecordClient) CreateIssue(
_ context.Context, owner, repo string, req *github.IssueRequest, //nolint: revive
) (*github.Issue, error) {
Expand Down
8 changes: 7 additions & 1 deletion github/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"path/filepath"
"sync"

"github.com/google/go-github/v56/github"
"github.com/google/go-github/v58/github"
)

func NewReplayer(replayDir string) Client {
Expand Down Expand Up @@ -203,6 +203,12 @@ func (c *githubNotesReplayClient) CreatePullRequest(
return &github.PullRequest{}, nil
}

func (c *githubNotesReplayClient) RequestPullRequestReview(
ctx context.Context, owner, repo string, prNumber int, reviewers, teamReviewers []string, //nolint: revive
) (*github.PullRequest, error) {
return &github.PullRequest{}, nil
}

func (c *githubNotesReplayClient) CreateIssue(
ctx context.Context, owner, repo string, req *github.IssueRequest, //nolint: revive
) (*github.Issue, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/go-git/go-git/v5 v5.11.0
github.com/google/go-containerregistry v0.17.0
github.com/google/go-github/v56 v56.0.0
github.com/google/go-github/v58 v58.0.0
github.com/jellydator/ttlcache/v3 v3.1.1
github.com/magefile/mage v1.15.0
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ github.com/google/go-containerregistry v0.17.0 h1:5p+zYs/R4VGHkhyvgWurWrpJ2hW4Vv
github.com/google/go-containerregistry v0.17.0/go.mod h1:u0qB2l7mvtWVR5kNcbFIhFY1hLbf8eeGapA+vbFDCtQ=
github.com/google/go-github/v55 v55.0.0 h1:4pp/1tNMB9X/LuAhs5i0KQAE40NmiR/y6prLNb9x9cg=
github.com/google/go-github/v55 v55.0.0/go.mod h1:JLahOTA1DnXzhxEymmFF5PP2tSS9JVNj68mSZNDwskA=
github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4=
github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0=
github.com/google/go-github/v58 v58.0.0 h1:Una7GGERlF/37XfkPwpzYJe0Vp4dt2k1kCjlxwjIvzw=
github.com/google/go-github/v58 v58.0.0/go.mod h1:k4hxDKEfoWpSqFlc8LTpGd9fu2KrV1YAa6Hi6FmDNY4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
16 changes: 9 additions & 7 deletions object/objectfakes/fake_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31b2d6e

Please sign in to comment.