diff --git a/example/appengine/app.go b/example/appengine/app.go new file mode 100644 index 00000000000..d14176d006a --- /dev/null +++ b/example/appengine/app.go @@ -0,0 +1,48 @@ +// Copyright 2017 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// The demo app shows how to use the github package on Google App Engine. +package demo + +import ( + "fmt" + "net/http" + "os" + + "github.com/google/go-github/github" + "golang.org/x/oauth2" + "google.golang.org/appengine" + "google.golang.org/appengine/log" +) + +func init() { + http.HandleFunc("/", handler) +} + +func handler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + + ctx := appengine.NewContext(r) + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil) + if err != nil { + log.Errorf(ctx, "ListCommits: %v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + for _, commit := range commits { + fmt.Fprintln(w, commit.GetHTMLURL()) + } +} diff --git a/example/appengine/app.yaml b/example/appengine/app.yaml new file mode 100644 index 00000000000..dca235faeeb --- /dev/null +++ b/example/appengine/app.yaml @@ -0,0 +1,14 @@ +# Copyright 2017 The go-github AUTHORS. All rights reserved. +# +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +runtime: go +api_version: go1 + +handlers: +- url: /.* + script: _go_app + +env_variables: + GITHUB_AUTH_TOKEN: "-your-auth-token-here-" diff --git a/github/github.go b/github/github.go index 7faebad4960..05edfd0be85 100644 --- a/github/github.go +++ b/github/github.go @@ -399,7 +399,7 @@ func parseRate(r *http.Response) Rate { // The provided ctx must be non-nil. If it is canceled or times out, // ctx.Err() will be returned. func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) { - ctx, req = withContext(ctx, req) + req = withContext(ctx, req) rateLimitCategory := category(req.URL.Path) diff --git a/github/repos_contents.go b/github/repos_contents.go index f9adaf7a03c..ffb56b90df2 100644 --- a/github/repos_contents.go +++ b/github/repos_contents.go @@ -248,7 +248,7 @@ func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo st } var resp *http.Response // Use http.DefaultTransport if no custom Transport is configured - ctx, req = withContext(ctx, req) + req = withContext(ctx, req) if s.client.client.Transport == nil { resp, err = http.DefaultTransport.RoundTrip(req) } else { diff --git a/github/repos_releases.go b/github/repos_releases.go index 5c27565510b..1ee7ecf9948 100644 --- a/github/repos_releases.go +++ b/github/repos_releases.go @@ -244,7 +244,7 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r } defer func() { s.client.client.CheckRedirect = saveRedirect }() - ctx, req = withContext(ctx, req) + req = withContext(ctx, req) resp, err := s.client.client.Do(req) if err != nil { if !strings.Contains(err.Error(), "disable redirect") { diff --git a/github/with_appengine.go b/github/with_appengine.go index 957c4d30797..87a228ad7be 100644 --- a/github/with_appengine.go +++ b/github/with_appengine.go @@ -17,10 +17,9 @@ package github import ( "context" "net/http" - - "google.golang.org/appengine" ) -func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) { - return appengine.WithContext(ctx, req), req +func withContext(ctx context.Context, req *http.Request) *http.Request { + // No-op because App Engine adds context to a request differently. + return req } diff --git a/github/without_appengine.go b/github/without_appengine.go index b0edc04db1d..6f8fdac5603 100644 --- a/github/without_appengine.go +++ b/github/without_appengine.go @@ -14,6 +14,6 @@ import ( "net/http" ) -func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) { - return ctx, req.WithContext(ctx) +func withContext(ctx context.Context, req *http.Request) *http.Request { + return req.WithContext(ctx) }