-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
64 lines (55 loc) · 1.7 KB
/
client.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
package webhook
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"github.com/brigadecore/brigade/pkg/brigade"
)
// ghClient gets a new GitHub client object.
//
// It authenticates with an OAUTH2 token.
//
// If an enterpriseHost base URL is provided, this will attempt to connect to
// that instead of the hosted GitHub API server.
func ghClient(gh brigade.Github) (*github.Client, error) {
t := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: gh.Token})
c := context.Background()
tc := oauth2.NewClient(c, t)
if gh.BaseURL != "" {
return github.NewEnterpriseClient(gh.BaseURL, gh.UploadURL, tc)
}
return github.NewClient(tc), nil
}
// GetFileContents returns the contents for a particular file in the project.
func GetFileContents(proj *brigade.Project, ref, path string) ([]byte, error) {
c := context.Background()
var client *github.Client
if proj.Github.Token != "" { // GitHub project configured with Auth Token
var err error
client, err = ghClient(proj.Github)
if err != nil {
return []byte{}, err
}
} else { // OSS project
netClient := &http.Client{ // https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
Timeout: time.Second * 30, // 30 seconds timeout should be enough
}
client = github.NewClient(netClient)
}
parts := strings.SplitN(proj.Repo.Name, "/", 3)
if len(parts) != 3 {
return nil, fmt.Errorf("project name %q is malformed", proj.Repo.Name)
}
opts := &github.RepositoryContentGetOptions{Ref: ref}
r, err := client.Repositories.DownloadContents(c, parts[1], parts[2], path, opts)
if err != nil {
return nil, err
}
defer r.Close()
return ioutil.ReadAll(r)
}