Skip to content

Commit

Permalink
Add prow transfer-issue plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Eddie Zaneski <eddiezane@gmail.com>
  • Loading branch information
eddiezane committed Aug 26, 2021
1 parent 2100a3d commit ce9ffe9
Show file tree
Hide file tree
Showing 12 changed files with 497 additions and 1 deletion.
20 changes: 20 additions & 0 deletions prow/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ type Client interface {

Throttle(hourlyTokens, burst int, org ...string) error
QueryWithGitHubAppsSupport(ctx context.Context, q interface{}, vars map[string]interface{}, org string) error
MutateWithGitHubAppsSupport(ctx context.Context, m interface{}, input githubql.Input, vars map[string]interface{}, org string) error

SetMax404Retries(int)

Expand Down Expand Up @@ -381,6 +382,7 @@ type httpClient interface {
// Interface for how prow interacts with the graphql client, which we may throttle.
type gqlClient interface {
QueryWithGitHubAppsSupport(ctx context.Context, q interface{}, vars map[string]interface{}, org string) error
MutateWithGitHubAppsSupport(ctx context.Context, m interface{}, input githubql.Input, vars map[string]interface{}, org string) error
forUserAgent(userAgent string) gqlClient
}

Expand Down Expand Up @@ -507,6 +509,13 @@ func (t *throttler) QueryWithGitHubAppsSupport(ctx context.Context, q interface{
return t.graph.QueryWithGitHubAppsSupport(ctx, q, vars, org)
}

func (t *throttler) MutateWithGitHubAppsSupport(ctx context.Context, m interface{}, input githubql.Input, vars map[string]interface{}, org string) error {
if err := t.Wait(ctx, extractOrgFromContext(ctx)); err != nil {
return err
}
return t.graph.MutateWithGitHubAppsSupport(ctx, m, input, vars, org)
}

func (t *throttler) forUserAgent(userAgent string) gqlClient {
return &throttler{
graph: t.graph.forUserAgent(userAgent),
Expand Down Expand Up @@ -689,6 +698,12 @@ func (c *graphQLGitHubAppsAuthClientWrapper) QueryWithGitHubAppsSupport(ctx cont
return c.Client.Query(ctx, q, vars)
}

func (c *graphQLGitHubAppsAuthClientWrapper) MutateWithGitHubAppsSupport(ctx context.Context, m interface{}, input githubql.Input, vars map[string]interface{}, org string) error {
ctx = context.WithValue(ctx, githubOrgHeaderKey, org)
ctx = context.WithValue(ctx, userAgentContextKey, c.userAgent)
return c.Client.Mutate(ctx, m, input, vars)
}

func (c *graphQLGitHubAppsAuthClientWrapper) forUserAgent(userAgent string) gqlClient {
return &graphQLGitHubAppsAuthClientWrapper{
Client: c.Client,
Expand Down Expand Up @@ -3350,6 +3365,11 @@ func (c *client) QueryWithGitHubAppsSupport(ctx context.Context, q interface{},
return c.gqlc.QueryWithGitHubAppsSupport(ctx, q, vars, org)
}

// MutateWithGitHubAppsSupport runs a GraphQL mutation using shurcooL/githubql's client.
func (c *client) MutateWithGitHubAppsSupport(ctx context.Context, m interface{}, input githubql.Input, vars map[string]interface{}, org string) error {
return c.gqlc.MutateWithGitHubAppsSupport(ctx, m, input, vars, org)
}

// CreateTeam adds a team with name to the org, returning a struct with the new ID.
//
// See https://developer.github.com/v3/teams/#create-team
Expand Down
18 changes: 18 additions & 0 deletions prow/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3065,6 +3065,9 @@ func setValue(target *reflect.Value, typeOverrides []func(typeName string) (over
if target.Type().String() == "interface {}" {
target.Set(reflect.ValueOf(map[string]interface{}{}))
}
if target.Type().String() == "githubv4.Input" {
target.Set(reflect.ValueOf(struct{}{}))
}
}

func TestBotUserChecker(t *testing.T) {
Expand Down Expand Up @@ -3142,6 +3145,9 @@ func TestV4ClientSetsUserAgent(t *testing.T) {
if err := client.QueryWithGitHubAppsSupport(context.Background(), struct{}{}, nil, ""); err != nil {
t.Error(err)
}
if err := client.MutateWithGitHubAppsSupport(context.Background(), struct{}{}, githubv4.Input(struct{}{}), nil, ""); err != nil {
t.Error(err)
}
})

t.Run("ForPlugin changes the user agent accordingly", func(t *testing.T) {
Expand All @@ -3150,13 +3156,19 @@ func TestV4ClientSetsUserAgent(t *testing.T) {
if err := client.QueryWithGitHubAppsSupport(context.Background(), struct{}{}, nil, ""); err != nil {
t.Error(err)
}
if err := client.MutateWithGitHubAppsSupport(context.Background(), struct{}{}, githubv4.Input(struct{}{}), nil, ""); err != nil {
t.Error(err)
}
})

t.Run("The ForPlugin call doesn't manipulate the original client", func(t *testing.T) {
expectedUserAgent = "unset/0"
if err := client.QueryWithGitHubAppsSupport(context.Background(), struct{}{}, nil, ""); err != nil {
t.Error(err)
}
if err := client.MutateWithGitHubAppsSupport(context.Background(), struct{}{}, githubv4.Input(struct{}{}), nil, ""); err != nil {
t.Error(err)
}
})

t.Run("ForSubcomponent changes the user agent accordingly", func(t *testing.T) {
Expand All @@ -3165,13 +3177,19 @@ func TestV4ClientSetsUserAgent(t *testing.T) {
if err := client.QueryWithGitHubAppsSupport(context.Background(), struct{}{}, nil, ""); err != nil {
t.Error(err)
}
if err := client.MutateWithGitHubAppsSupport(context.Background(), struct{}{}, githubv4.Input(struct{}{}), nil, ""); err != nil {
t.Error(err)
}
})

t.Run("The ForSubcomponent call doesn't manipulate the original client", func(t *testing.T) {
expectedUserAgent = "unset/0"
if err := client.QueryWithGitHubAppsSupport(context.Background(), struct{}{}, nil, ""); err != nil {
t.Error(err)
}
if err := client.MutateWithGitHubAppsSupport(context.Background(), struct{}{}, githubv4.Input(struct{}{}), nil, ""); err != nil {
t.Error(err)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions prow/github/fakegithub/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
importpath = "k8s.io/test-infra/prow/github/fakegithub",
deps = [
"//prow/github:go_default_library",
"@com_github_shurcool_githubv4//:go_default_library",
"@io_k8s_apimachinery//pkg/util/sets:go_default_library",
],
)
Expand Down
12 changes: 12 additions & 0 deletions prow/github/fakegithub/fakegithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"sync"

githubql "github.com/shurcooL/githubv4"

"k8s.io/apimachinery/pkg/util/sets"

"k8s.io/test-infra/prow/github"
Expand Down Expand Up @@ -131,6 +133,9 @@ type FakeClient struct {
// Error will be returned if set. Currently only implemented for CreateStatus
Error error

// GetRepoError will be returned if set when GetRepo is called
GetRepoError error

// WasLabelAddedByHumanVal determines the return of the method with the same name
WasLabelAddedByHumanVal bool

Expand Down Expand Up @@ -849,6 +854,9 @@ func (f *FakeClient) GetRepos(org string, isUser bool) ([]github.Repo, error) {
}

func (f *FakeClient) GetRepo(owner, name string) (github.FullRepo, error) {
if f.GetRepoError != nil {
return github.FullRepo{}, f.GetRepoError
}
return github.FullRepo{
Repo: github.Repo{
Owner: github.User{Login: owner},
Expand Down Expand Up @@ -1050,3 +1058,7 @@ func (f *FakeClient) ListCurrentUserOrgInvitations() ([]github.UserOrgInvitation

return ret, nil
}

func (f *FakeClient) MutateWithGitHubAppsSupport(ctx context.Context, m interface{}, input githubql.Input, vars map[string]interface{}, org string) error {
return nil
}
8 changes: 7 additions & 1 deletion prow/github/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ const (
// PullRequest contains information about a PullRequest.
type PullRequest struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
Number int `json:"number"`
HTMLURL string `json:"html_url"`
User User `json:"user"`
Expand Down Expand Up @@ -347,6 +348,7 @@ type Repo struct {
HasIssues bool `json:"has_issues"`
HasProjects bool `json:"has_projects"`
HasWiki bool `json:"has_wiki"`
NodeID string `json:"node_id"`
// Permissions reflect the permission level for the requester, so
// on a repository GET call this will be for the user whose token
// is being used, if listing a team's repos this will be for the
Expand Down Expand Up @@ -743,6 +745,7 @@ type IssueCommentEvent struct {
// Issue represents general info about an issue.
type Issue struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
User User `json:"user"`
Number int `json:"number"`
Title string `json:"title"`
Expand Down Expand Up @@ -904,6 +907,7 @@ const (
// Review describes a Pull Request review.
type Review struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
User User `json:"user"`
Body string `json:"body"`
State ReviewState `json:"state"`
Expand Down Expand Up @@ -950,6 +954,7 @@ const (
// ReviewComment describes a Pull Request review.
type ReviewComment struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
ReviewID int `json:"pull_request_review_id"`
User User `json:"user"`
Body string `json:"body"`
Expand Down Expand Up @@ -1168,7 +1173,8 @@ const (
// Issue and PR "closed" events are not coerced to the "deleted" Action and do not trigger
// a GenericCommentEvent because these events don't actually remove the comment content from GH.
type GenericCommentEvent struct {
ID int `json:"id"`
ID int `json:"id"`
NodeID string `json:"node_id"`
CommentID *int
IsPR bool
Action GenericCommentEventAction
Expand Down
5 changes: 5 additions & 0 deletions prow/hook/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s *Server) handleReviewEvent(l *logrus.Entry, re github.ReviewEvent) {
l,
&github.GenericCommentEvent{
GUID: re.GUID,
NodeID: re.Review.NodeID,
IsPR: true,
Action: action,
Body: re.Review.Body,
Expand Down Expand Up @@ -163,6 +164,7 @@ func (s *Server) handleReviewCommentEvent(l *logrus.Entry, rce github.ReviewComm
l,
&github.GenericCommentEvent{
GUID: rce.GUID,
NodeID: rce.Comment.NodeID,
IsPR: true,
CommentID: intPtr(rce.Comment.ID),
Action: action,
Expand Down Expand Up @@ -221,6 +223,7 @@ func (s *Server) handlePullRequestEvent(l *logrus.Entry, pr github.PullRequestEv
l,
&github.GenericCommentEvent{
ID: pr.PullRequest.ID,
NodeID: pr.PullRequest.NodeID,
GUID: pr.GUID,
IsPR: true,
Action: action,
Expand Down Expand Up @@ -304,6 +307,7 @@ func (s *Server) handleIssueEvent(l *logrus.Entry, i github.IssueEvent) {
l,
&github.GenericCommentEvent{
ID: i.Issue.ID,
NodeID: i.Issue.NodeID,
GUID: i.GUID,
IsPR: i.Issue.IsPullRequest(),
Action: action,
Expand Down Expand Up @@ -360,6 +364,7 @@ func (s *Server) handleIssueCommentEvent(l *logrus.Entry, ic github.IssueComment
l,
&github.GenericCommentEvent{
ID: ic.Issue.ID,
NodeID: ic.Issue.NodeID,
CommentID: intPtr(ic.Comment.ID),
GUID: ic.GUID,
IsPR: ic.Issue.IsPullRequest(),
Expand Down
1 change: 1 addition & 0 deletions prow/hook/plugin-imports/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ go_library(
"//prow/plugins/skip:go_default_library",
"//prow/plugins/slackevents:go_default_library",
"//prow/plugins/stage:go_default_library",
"//prow/plugins/transfer-issue:go_default_library",
"//prow/plugins/trick-or-treat:go_default_library",
"//prow/plugins/trigger:go_default_library",
"//prow/plugins/updateconfig:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions prow/hook/plugin-imports/plugin-imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
_ "k8s.io/test-infra/prow/plugins/skip"
_ "k8s.io/test-infra/prow/plugins/slackevents"
_ "k8s.io/test-infra/prow/plugins/stage"
_ "k8s.io/test-infra/prow/plugins/transfer-issue"
_ "k8s.io/test-infra/prow/plugins/trick-or-treat"
_ "k8s.io/test-infra/prow/plugins/trigger"
_ "k8s.io/test-infra/prow/plugins/updateconfig"
Expand Down
1 change: 1 addition & 0 deletions prow/plugins/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ filegroup(
"//prow/plugins/skip:all-srcs",
"//prow/plugins/slackevents:all-srcs",
"//prow/plugins/stage:all-srcs",
"//prow/plugins/transfer-issue:all-srcs",
"//prow/plugins/trick-or-treat:all-srcs",
"//prow/plugins/trigger:all-srcs",
"//prow/plugins/updateconfig:all-srcs",
Expand Down
46 changes: 46 additions & 0 deletions prow/plugins/transfer-issue/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package(default_visibility = ["//visibility:public"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)

go_test(
name = "go_default_test",
srcs = ["transfer-issue_test.go"],
embed = [":go_default_library"],
deps = [
"//prow/github:go_default_library",
"//prow/github/fakegithub:go_default_library",
"@com_github_shurcool_githubv4//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

go_library(
name = "go_default_library",
srcs = ["transfer-issue.go"],
importpath = "k8s.io/test-infra/prow/plugins/transfer-issue",
deps = [
"//prow/config:go_default_library",
"//prow/github:go_default_library",
"//prow/pluginhelp:go_default_library",
"//prow/plugins:go_default_library",
"@com_github_shurcool_githubv4//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
Loading

0 comments on commit ce9ffe9

Please sign in to comment.