forked from google/go-github
/
app.go
49 lines (41 loc) · 1.13 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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.
// Package demo provides an app that 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())
}
}