Skip to content

Commit ccf6b89

Browse files
committed
fix: first spike of integration of GraphQL query
1 parent 6c44502 commit ccf6b89

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ require (
55
github.com/google/go-cmp v0.3.0
66
github.com/h2non/gock v1.0.9
77
github.com/pkg/errors v0.8.1
8+
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260
9+
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
810
github.com/stretchr/testify v1.3.0
911
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
1012
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
3636
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3737
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3838
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
39+
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260 h1:xKXiRdBUtMVp64NaxACcyX4kvfmHJ9KrLU+JvyB1mdM=
40+
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
41+
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk=
42+
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
3943
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
4044
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4145
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=

scm/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ type (
7575
Size int
7676
}
7777

78+
// GraphQLService the API to performing GraphQL queries
79+
GraphQLService interface {
80+
Query(ctx context.Context, q interface{}, vars map[string]interface{}) error
81+
}
82+
7883
// Client manages communication with a version control
7984
// system API.
8085
Client struct {
@@ -97,6 +102,7 @@ type (
97102
Reviews ReviewService
98103
Users UserService
99104
Webhooks WebhookService
105+
Query GraphQLService
100106

101107
// DumpResponse optionally specifies a function to
102108
// dump the the response body for debugging purposes.

scm/driver/github/github.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ import (
99
"bytes"
1010
"context"
1111
"encoding/json"
12+
"fmt"
13+
"net/http"
1214
"net/url"
1315
"strconv"
1416
"strings"
17+
"time"
1518

1619
"github.com/jenkins-x/go-scm/scm"
20+
githubql "github.com/shurcooL/githubv4"
1721
)
1822

23+
// Abort requests that don't return in 5 mins. Longest graphql calls can
24+
// take up to 2 minutes. This limit should ensure all successful calls return
25+
// but will prevent an indefinite stall if GitHub never responds.
26+
const maxRequestTime = 5 * time.Minute
27+
1928
// New returns a new GitHub API client.
2029
func New(uri string) (*scm.Client, error) {
2130
base, err := url.Parse(uri)
@@ -38,9 +47,37 @@ func New(uri string) (*scm.Client, error) {
3847
client.Reviews = &reviewService{client}
3948
client.Users = &userService{client}
4049
client.Webhooks = &webhookService{client}
50+
51+
graphqlEndpoint := scm.UrlJoin(uri, "/graphql")
52+
client.Query = &dynamicGraphQLClient{client, graphqlEndpoint}
53+
4154
return client.Client, nil
4255
}
4356

57+
type dynamicGraphQLClient struct {
58+
wrapper *wrapper
59+
graphqlEndpoint string
60+
}
61+
62+
func (d *dynamicGraphQLClient) Query(ctx context.Context, q interface{}, vars map[string]interface{}) error {
63+
httpClient := d.wrapper.Client.Client
64+
if httpClient != nil {
65+
66+
transport := httpClient.Transport
67+
if transport != nil {
68+
query := githubql.NewEnterpriseClient(
69+
d.graphqlEndpoint,
70+
&http.Client{
71+
Timeout: maxRequestTime,
72+
Transport: transport,
73+
})
74+
return query.Query(ctx, q, vars)
75+
}
76+
}
77+
fmt.Println("WARNING: no http transport configured for GraphQL and GitHub")
78+
return nil
79+
}
80+
4481
// NewDefault returns a new GitHub API client using the
4582
// default api.github.com address.
4683
func NewDefault() *scm.Client {

0 commit comments

Comments
 (0)