diff --git a/server/events/vcs/github_client.go b/server/events/vcs/github_client.go index 2645b56429..65b8f44a7b 100644 --- a/server/events/vcs/github_client.go +++ b/server/events/vcs/github_client.go @@ -30,7 +30,6 @@ import ( "github.com/runatlantis/atlantis/server/events/vcs/common" "github.com/runatlantis/atlantis/server/logging" "github.com/shurcooL/githubv4" - "golang.org/x/oauth2" ) // maxCommentLength is the maximum number of chars allowed in a single comment @@ -42,7 +41,7 @@ type GithubClient struct { user string client *github.Client v4MutateClient *graphql.Client - v4QueryClient *githubv4.Client + v4QueryClient GithubGraphQLClient ctx context.Context logger logging.SimpleLogging config GithubConfig @@ -95,16 +94,11 @@ func NewGithubClient(hostname string, credentials GithubCredentials, config Gith transport, graphql.WithHeader("Accept", "application/vnd.github.queen-beryl-preview+json"), ) - token, err := credentials.GetToken() - if err != nil { - return nil, errors.Wrap(err, "Failed to get GitHub token") + + ghGraphQLClient := GithubGraphQLClient{ + credentials: credentials, + graphqlURL: graphqlURL, } - src := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - httpClient := oauth2.NewClient(context.Background(), src) - // Use the client from shurcooL's githubv4 library for queries. - v4QueryClient := githubv4.NewEnterpriseClient(graphqlURL, httpClient) user, err := credentials.GetUser() logger.Debug("GH User: %s", user) @@ -116,7 +110,7 @@ func NewGithubClient(hostname string, credentials GithubCredentials, config Gith user: user, client: client, v4MutateClient: v4MutateClient, - v4QueryClient: v4QueryClient, + v4QueryClient: ghGraphQLClient, ctx: context.Background(), logger: logger, config: config, @@ -344,7 +338,6 @@ func (g *GithubClient) GetCombinedStatusMinusApply(repo models.Repo, pull *githu //iterate over check completed check suites - return false if we find one that doesnt have conclusion = "success" for _, c := range checksuites.CheckSuites { if *c.Status == "completed" { - fmt.Printf("Looking at suite %v\n", *c.ID) //iterate over the runs inside the suite suite, _, err := g.client.Checks.ListCheckRunsCheckSuite(context.Background(), repo.Owner, repo.Name, *c.ID, nil) if err != nil { @@ -352,20 +345,15 @@ func (g *GithubClient) GetCombinedStatusMinusApply(repo models.Repo, pull *githu } for _, r := range suite.CheckRuns { - fmt.Printf("Looking at check run %s\n", *r.Name) //check to see if the check is required if isRequiredCheck(*r.Name, required.RequiredStatusChecks.Contexts) { - fmt.Println("Check is required") if *c.Conclusion == "success" { - fmt.Println("Check is successful") continue } else { - fmt.Println("Check is failed") return false, nil } } else { //ignore checks that arent required - fmt.Println("Check is not required") continue } @@ -432,8 +420,6 @@ func (g *GithubClient) PullIsMergeable(repo models.Repo, pull models.PullRequest return false, errors.Wrap(err, "getting pull request status") } - fmt.Printf("Status was %v\n", status) - //check to see if pr is approved using reviewDecision approved, err := g.GetPullReviewDecision(repo, pull) if err != nil { diff --git a/server/events/vcs/github_graphql_client.go b/server/events/vcs/github_graphql_client.go new file mode 100644 index 0000000000..93c978a1e9 --- /dev/null +++ b/server/events/vcs/github_graphql_client.go @@ -0,0 +1,34 @@ +package vcs + +import ( + "context" + + "github.com/pkg/errors" + "github.com/shurcooL/githubv4" + "golang.org/x/oauth2" +) + +type GithubGraphQLClient struct { + credentials GithubCredentials + graphqlURL string +} + +func (gcl GithubGraphQLClient) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error { + token, err := gcl.credentials.GetToken() + if err != nil { + return errors.Wrap(err, "Failed to get GitHub token") + } + src := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + httpClient := oauth2.NewClient(context.Background(), src) + // Use the client from shurcooL's githubv4 library for queries. + v4QueryClient := githubv4.NewEnterpriseClient(gcl.graphqlURL, httpClient) + + err = v4QueryClient.Query(ctx, q, variables) + if err != nil { + return errors.Wrap(err, "making graphql query") + } + + return nil +}