Skip to content

Commit cbeb72e

Browse files
anubhakushwahadmitshur
authored andcommitted
Support preview GraphQL API v4 Node IDs. (google#817)
Add NodeID field to objects that now expose it. Reference: https://developer.github.com/changes/2017-12-19-graphql-node-id/.
1 parent 218ecc2 commit cbeb72e

22 files changed

+354
-36
lines changed

github/gists.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Gist struct {
3030
GitPushURL *string `json:"git_push_url,omitempty"`
3131
CreatedAt *time.Time `json:"created_at,omitempty"`
3232
UpdatedAt *time.Time `json:"updated_at,omitempty"`
33+
NodeID *string `json:"node_id,omitempty"`
3334
}
3435

3536
func (g Gist) String() string {
@@ -60,6 +61,7 @@ type GistCommit struct {
6061
User *User `json:"user,omitempty"`
6162
ChangeStatus *CommitStats `json:"change_status,omitempty"`
6263
CommittedAt *Timestamp `json:"committed_at,omitempty"`
64+
NodeID *string `json:"node_id,omitempty"`
6365
}
6466

6567
func (gc GistCommit) String() string {
@@ -73,6 +75,7 @@ type GistFork struct {
7375
ID *string `json:"id,omitempty"`
7476
CreatedAt *Timestamp `json:"created_at,omitempty"`
7577
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
78+
NodeID *string `json:"node_id,omitempty"`
7679
}
7780

7881
func (gf GistFork) String() string {
@@ -111,6 +114,9 @@ func (s *GistsService) List(ctx context.Context, user string, opt *GistListOptio
111114
return nil, nil, err
112115
}
113116

117+
// TODO: remove custom Accept header when this API fully launches.
118+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
119+
114120
var gists []*Gist
115121
resp, err := s.client.Do(ctx, req, &gists)
116122
if err != nil {
@@ -134,6 +140,9 @@ func (s *GistsService) ListAll(ctx context.Context, opt *GistListOptions) ([]*Gi
134140
return nil, nil, err
135141
}
136142

143+
// TODO: remove custom Accept header when this API fully launches.
144+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
145+
137146
var gists []*Gist
138147
resp, err := s.client.Do(ctx, req, &gists)
139148
if err != nil {
@@ -157,6 +166,9 @@ func (s *GistsService) ListStarred(ctx context.Context, opt *GistListOptions) ([
157166
return nil, nil, err
158167
}
159168

169+
// TODO: remove custom Accept header when this API fully launches.
170+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
171+
160172
var gists []*Gist
161173
resp, err := s.client.Do(ctx, req, &gists)
162174
if err != nil {
@@ -175,6 +187,10 @@ func (s *GistsService) Get(ctx context.Context, id string) (*Gist, *Response, er
175187
if err != nil {
176188
return nil, nil, err
177189
}
190+
191+
// TODO: remove custom Accept header when this API fully launches.
192+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
193+
178194
gist := new(Gist)
179195
resp, err := s.client.Do(ctx, req, gist)
180196
if err != nil {
@@ -193,6 +209,10 @@ func (s *GistsService) GetRevision(ctx context.Context, id, sha string) (*Gist,
193209
if err != nil {
194210
return nil, nil, err
195211
}
212+
213+
// TODO: remove custom Accept header when this API fully launches.
214+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
215+
196216
gist := new(Gist)
197217
resp, err := s.client.Do(ctx, req, gist)
198218
if err != nil {
@@ -211,6 +231,10 @@ func (s *GistsService) Create(ctx context.Context, gist *Gist) (*Gist, *Response
211231
if err != nil {
212232
return nil, nil, err
213233
}
234+
235+
// TODO: remove custom Accept header when this API fully launches.
236+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
237+
214238
g := new(Gist)
215239
resp, err := s.client.Do(ctx, req, g)
216240
if err != nil {
@@ -229,6 +253,10 @@ func (s *GistsService) Edit(ctx context.Context, id string, gist *Gist) (*Gist,
229253
if err != nil {
230254
return nil, nil, err
231255
}
256+
257+
// TODO: remove custom Accept header when this API fully launches.
258+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
259+
232260
g := new(Gist)
233261
resp, err := s.client.Do(ctx, req, g)
234262
if err != nil {
@@ -253,6 +281,9 @@ func (s *GistsService) ListCommits(ctx context.Context, id string, opt *ListOpti
253281
return nil, nil, err
254282
}
255283

284+
// TODO: remove custom Accept header when this API fully launches.
285+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
286+
256287
var gistCommits []*GistCommit
257288
resp, err := s.client.Do(ctx, req, &gistCommits)
258289
if err != nil {
@@ -322,6 +353,9 @@ func (s *GistsService) Fork(ctx context.Context, id string) (*Gist, *Response, e
322353
return nil, nil, err
323354
}
324355

356+
// TODO: remove custom Accept header when this API fully launches.
357+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
358+
325359
g := new(Gist)
326360
resp, err := s.client.Do(ctx, req, g)
327361
if err != nil {
@@ -341,6 +375,9 @@ func (s *GistsService) ListForks(ctx context.Context, id string) ([]*GistFork, *
341375
return nil, nil, err
342376
}
343377

378+
// TODO: remove custom Accept header when this API fully launches.
379+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
380+
344381
var gistForks []*GistFork
345382
resp, err := s.client.Do(ctx, req, &gistForks)
346383
if err != nil {

github/gists_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestGistsService_List_specifiedUser(t *testing.T) {
2323

2424
mux.HandleFunc("/users/u/gists", func(w http.ResponseWriter, r *http.Request) {
2525
testMethod(t, r, "GET")
26+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
2627
testFormValues(t, r, values{
2728
"since": since,
2829
})
@@ -47,6 +48,7 @@ func TestGistsService_List_authenticatedUser(t *testing.T) {
4748

4849
mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) {
4950
testMethod(t, r, "GET")
51+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
5052
fmt.Fprint(w, `[{"id": "1"}]`)
5153
})
5254

@@ -77,6 +79,7 @@ func TestGistsService_ListAll(t *testing.T) {
7779

7880
mux.HandleFunc("/gists/public", func(w http.ResponseWriter, r *http.Request) {
7981
testMethod(t, r, "GET")
82+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
8083
testFormValues(t, r, values{
8184
"since": since,
8285
})
@@ -103,6 +106,7 @@ func TestGistsService_ListStarred(t *testing.T) {
103106

104107
mux.HandleFunc("/gists/starred", func(w http.ResponseWriter, r *http.Request) {
105108
testMethod(t, r, "GET")
109+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
106110
testFormValues(t, r, values{
107111
"since": since,
108112
})
@@ -127,6 +131,7 @@ func TestGistsService_Get(t *testing.T) {
127131

128132
mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
129133
testMethod(t, r, "GET")
134+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
130135
fmt.Fprint(w, `{"id": "1"}`)
131136
})
132137

@@ -155,6 +160,7 @@ func TestGistsService_GetRevision(t *testing.T) {
155160

156161
mux.HandleFunc("/gists/1/s", func(w http.ResponseWriter, r *http.Request) {
157162
testMethod(t, r, "GET")
163+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
158164
fmt.Fprint(w, `{"id": "1"}`)
159165
})
160166

@@ -194,6 +200,7 @@ func TestGistsService_Create(t *testing.T) {
194200
json.NewDecoder(r.Body).Decode(v)
195201

196202
testMethod(t, r, "POST")
203+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
197204
if !reflect.DeepEqual(v, input) {
198205
t.Errorf("Request body = %+v, want %+v", v, input)
199206
}
@@ -246,6 +253,7 @@ func TestGistsService_Edit(t *testing.T) {
246253
json.NewDecoder(r.Body).Decode(v)
247254

248255
testMethod(t, r, "PATCH")
256+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
249257
if !reflect.DeepEqual(v, input) {
250258
t.Errorf("Request body = %+v, want %+v", v, input)
251259
}
@@ -300,6 +308,7 @@ func TestGistsService_ListCommits(t *testing.T) {
300308

301309
mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
302310
testMethod(t, r, "GET")
311+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
303312
testFormValues(t, r, nil)
304313
fmt.Fprint(w, `
305314
[
@@ -347,6 +356,7 @@ func TestGistsService_ListCommits_withOptions(t *testing.T) {
347356

348357
mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
349358
testMethod(t, r, "GET")
359+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
350360
testFormValues(t, r, values{
351361
"page": "2",
352362
})
@@ -475,6 +485,7 @@ func TestGistsService_Fork(t *testing.T) {
475485

476486
mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
477487
testMethod(t, r, "POST")
488+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
478489
fmt.Fprint(w, `{"id": "2"}`)
479490
})
480491

@@ -495,6 +506,7 @@ func TestGistsService_ListForks(t *testing.T) {
495506

496507
mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
497508
testMethod(t, r, "GET")
509+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
498510
testFormValues(t, r, nil)
499511
fmt.Fprint(w, `
500512
[

github/git_blobs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Blob struct {
1717
SHA *string `json:"sha,omitempty"`
1818
Size *int `json:"size,omitempty"`
1919
URL *string `json:"url,omitempty"`
20+
NodeID *string `json:"node_id,omitempty"`
2021
}
2122

2223
// GetBlob fetchs a blob from a repo given a SHA.
@@ -29,6 +30,9 @@ func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha
2930
return nil, nil, err
3031
}
3132

33+
// TODO: remove custom Accept header when this API fully launches.
34+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
35+
3236
blob := new(Blob)
3337
resp, err := s.client.Do(ctx, req, blob)
3438
return blob, resp, err
@@ -44,6 +48,9 @@ func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string,
4448
return nil, nil, err
4549
}
4650

51+
// TODO: remove custom Accept header when this API fully launches.
52+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
53+
4754
t := new(Blob)
4855
resp, err := s.client.Do(ctx, req, t)
4956
return t, resp, err

github/git_blobs_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func TestGitService_GetBlob(t *testing.T) {
1919
defer teardown()
2020

2121
mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
22-
if m := "GET"; m != r.Method {
23-
t.Errorf("Request method = %v, want %v", r.Method, m)
24-
}
22+
testMethod(t, r, "GET")
23+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
24+
2525
fmt.Fprint(w, `{
2626
"sha": "s",
2727
"content": "blob content"
@@ -66,9 +66,8 @@ func TestGitService_CreateBlob(t *testing.T) {
6666
v := new(Blob)
6767
json.NewDecoder(r.Body).Decode(v)
6868

69-
if m := "POST"; m != r.Method {
70-
t.Errorf("Request method = %v, want %v", r.Method, m)
71-
}
69+
testMethod(t, r, "POST")
70+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
7271

7372
want := input
7473
if !reflect.DeepEqual(v, want) {

github/git_commits.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package github
88
import (
99
"context"
1010
"fmt"
11+
"strings"
1112
"time"
1213
)
1314

@@ -31,6 +32,7 @@ type Commit struct {
3132
HTMLURL *string `json:"html_url,omitempty"`
3233
URL *string `json:"url,omitempty"`
3334
Verification *SignatureVerification `json:"verification,omitempty"`
35+
NodeID *string `json:"node_id,omitempty"`
3436

3537
// CommentCount is the number of GitHub comments on the commit. This
3638
// is only populated for requests that fetch GitHub data like
@@ -67,8 +69,9 @@ func (s *GitService) GetCommit(ctx context.Context, owner string, repo string, s
6769
return nil, nil, err
6870
}
6971

70-
// TODO: remove custom Accept header when this API fully launches.
71-
req.Header.Set("Accept", mediaTypeGitSigningPreview)
72+
// TODO: remove custom Accept headers when APIs fully launch.
73+
acceptHeaders := []string{mediaTypeGitSigningPreview, mediaTypeGraphQLNodeIDPreview}
74+
req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
7275

7376
c := new(Commit)
7477
resp, err := s.client.Do(ctx, req, c)
@@ -123,6 +126,9 @@ func (s *GitService) CreateCommit(ctx context.Context, owner string, repo string
123126
return nil, nil, err
124127
}
125128

129+
// TODO: remove custom Accept header when this API fully launches.
130+
req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
131+
126132
c := new(Commit)
127133
resp, err := s.client.Do(ctx, req, c)
128134
if err != nil {

github/git_commits_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import (
1111
"fmt"
1212
"net/http"
1313
"reflect"
14+
"strings"
1415
"testing"
1516
)
1617

1718
func TestGitService_GetCommit(t *testing.T) {
1819
client, mux, _, teardown := setup()
1920
defer teardown()
2021

22+
acceptHeaders := []string{mediaTypeGitSigningPreview, mediaTypeGraphQLNodeIDPreview}
2123
mux.HandleFunc("/repos/o/r/git/commits/s", func(w http.ResponseWriter, r *http.Request) {
2224
testMethod(t, r, "GET")
23-
testHeader(t, r, "Accept", mediaTypeGitSigningPreview)
25+
testHeader(t, r, "Accept", strings.Join(acceptHeaders, ", "))
2426
fmt.Fprint(w, `{"sha":"s","message":"m","author":{"name":"n"}}`)
2527
})
2628

@@ -58,6 +60,7 @@ func TestGitService_CreateCommit(t *testing.T) {
5860
json.NewDecoder(r.Body).Decode(v)
5961

6062
testMethod(t, r, "POST")
63+
testHeader(t, r, "Accept", mediaTypeGraphQLNodeIDPreview)
6164

6265
want := &createCommit{
6366
Message: input.Message,

0 commit comments

Comments
 (0)