Skip to content

Commit c59749c

Browse files
authored
Support Google App Engine with recent context changes (google#582)
* Support Google App Engine with recent context changes Fixes google#578.
1 parent e07907f commit c59749c

7 files changed

+73
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ straightforward.
201201
[contributing]: CONTRIBUTING.md
202202

203203

204+
## Google App Engine ##
205+
206+
Go on App Engine Classic (which as of this writing uses Go 1.6) can not use
207+
the `"context"` import and still relies on `"golang.org/x/net/context"`.
208+
As a result, if you wish to continue to use `go-github` on App Engine Classic,
209+
you will need to rewrite all the `"context"` imports using the following command:
210+
211+
`gofmt -w -r '"context" -> "golang.org/x/net/context"' *.go`
212+
213+
See `with_appengine.go` for more details.
214+
204215
## License ##
205216

206217
This library is distributed under the BSD-style license found in the [LICENSE](./LICENSE)

github/doc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,16 @@ github.Response struct.
157157
opt.ListOptions.Page = resp.NextPage
158158
}
159159
160+
Google App Engine
161+
162+
Go on App Engine Classic (which as of this writing uses Go 1.6) can not use
163+
the "context" import and still relies on "golang.org/x/net/context".
164+
As a result, if you wish to continue to use "go-github" on App Engine Classic,
165+
you will need to rewrite all the "context" imports using the following command:
166+
167+
gofmt -w -r '"context" -> "golang.org/x/net/context"' *.go
168+
169+
See "with_appengine.go" for more details.
170+
160171
*/
161172
package github

github/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func parseRate(r *http.Response) Rate {
390390
// The provided ctx must be non-nil. If it is canceled or times out,
391391
// ctx.Err() will be returned.
392392
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
393-
req = req.WithContext(ctx)
393+
ctx, req = withContext(ctx, req)
394394

395395
rateLimitCategory := category(req.URL.Path)
396396

github/repos_contents.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,11 @@ func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo st
248248
}
249249
var resp *http.Response
250250
// Use http.DefaultTransport if no custom Transport is configured
251+
ctx, req = withContext(ctx, req)
251252
if s.client.client.Transport == nil {
252-
resp, err = http.DefaultTransport.RoundTrip(req.WithContext(ctx))
253+
resp, err = http.DefaultTransport.RoundTrip(req)
253254
} else {
254-
resp, err = s.client.client.Transport.RoundTrip(req.WithContext(ctx))
255+
resp, err = s.client.client.Transport.RoundTrip(req)
255256
}
256257
if err != nil {
257258
return nil, nil, err

github/repos_releases.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r
244244
}
245245
defer func() { s.client.client.CheckRedirect = saveRedirect }()
246246

247-
resp, err := s.client.client.Do(req.WithContext(ctx))
247+
ctx, req = withContext(ctx, req)
248+
resp, err := s.client.client.Do(req)
248249
if err != nil {
249250
if !strings.Contains(err.Error(), "disable redirect") {
250251
return nil, "", err

github/with_appengine.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
// +build appengine
7+
8+
// This file provides glue for making github work on App Engine.
9+
// In order to get the entire github package to compile with
10+
// Go 1.6, you will need to rewrite all the import "context" lines.
11+
// Fortunately, this is easy with "gofmt":
12+
//
13+
// gofmt -w -r '"context" -> "golang.org/x/net/context"' *.go
14+
15+
package github
16+
17+
import (
18+
"context"
19+
"net/http"
20+
21+
"google.golang.org/appengine"
22+
)
23+
24+
func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) {
25+
return appengine.WithContext(ctx, req), req
26+
}

github/without_appengine.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
// +build !appengine
7+
8+
// This file provides glue for making github work without App Engine.
9+
10+
package github
11+
12+
import (
13+
"context"
14+
"net/http"
15+
)
16+
17+
func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) {
18+
return ctx, req.WithContext(ctx)
19+
}

0 commit comments

Comments
 (0)