forked from MediaMath/grim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
70 lines (55 loc) · 1.47 KB
/
github.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package grim
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"fmt"
"net/http"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type tokenSource struct {
token *oauth2.Token
}
func (t *tokenSource) Token() (*oauth2.Token, error) {
return t.token, nil
}
func getHttpClientForToken(token string) *http.Client {
if token == "" {
return nil
}
ts := &tokenSource{
&oauth2.Token{AccessToken: token},
}
return oauth2.NewClient(oauth2.NoContext, ts)
}
func getClientForToken(token string) (*github.Client, error) {
tc := getHttpClientForToken(token)
ghc := github.NewClient(tc)
if ghc == nil {
return nil, fmt.Errorf("unexpected nil while initializing github client")
}
return ghc, nil
}
func verifyHTTPCreated(res *github.Response) error {
if res == nil {
return fmt.Errorf("github client returned nil for http response")
}
if res.StatusCode != http.StatusCreated {
return fmt.Errorf("github client did not return a 'created' http response code: %v", res.StatusCode)
}
return nil
}
func pollForMergeCommitSha(token, owner, repo string, number int64) (string, error) {
for i := 1; i < 4; i++ {
<-time.After(time.Duration(i*5) * time.Second)
sha, err := getMergeCommitSha(token, owner, repo, number)
if err != nil {
return "", err
} else if sha != "" {
return sha, nil
}
}
return "", nil
}