...

Source file src/github.com/google/go-github/v33/example/appengine/app.go

Documentation: github.com/google/go-github/v33/example/appengine

     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  // Package demo provides an app that shows how to use the github package on
     7  // Google App Engine.
     8  package demo
     9  
    10  import (
    11  	"fmt"
    12  	"net/http"
    13  	"os"
    14  
    15  	"github.com/google/go-github/v33/github"
    16  	"golang.org/x/oauth2"
    17  	"google.golang.org/appengine"
    18  	"google.golang.org/appengine/log"
    19  )
    20  
    21  func init() {
    22  	http.HandleFunc("/", handler)
    23  }
    24  
    25  func handler(w http.ResponseWriter, r *http.Request) {
    26  	if r.URL.Path != "/" {
    27  		http.NotFound(w, r)
    28  		return
    29  	}
    30  
    31  	ctx := appengine.NewContext(r)
    32  	ts := oauth2.StaticTokenSource(
    33  		&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
    34  	)
    35  	tc := oauth2.NewClient(ctx, ts)
    36  	client := github.NewClient(tc)
    37  
    38  	commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil)
    39  	if err != nil {
    40  		log.Errorf(ctx, "ListCommits: %v", err)
    41  		http.Error(w, err.Error(), http.StatusInternalServerError)
    42  		return
    43  	}
    44  
    45  	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    46  	for _, commit := range commits {
    47  		fmt.Fprintln(w, commit.GetHTMLURL())
    48  	}
    49  }
    50  

View as plain text