-
Notifications
You must be signed in to change notification settings - Fork 5
/
github.go
51 lines (41 loc) · 1.15 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
package dreck
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/miekg/dreck/auth"
"github.com/google/go-github/v28/github"
)
func githubIssueComment(body string) *github.IssueComment {
return &github.IssueComment{
Body: &body,
}
}
func (d Dreck) newClient(installation int) (*github.Client, context.Context, error) {
ctx := context.Background()
token, err := auth.MakeAccessTokenForInstallation(d.clientID, d.key, installation)
if err != nil {
return nil, ctx, err
}
client := auth.MakeClient(ctx, token)
return client, ctx, nil
}
// githubFile returns the file from github or an error if nothing is found.
func githubFile(owner, repository, path string) ([]byte, error) {
file := fmt.Sprintf("https://github.com/%s/%s/raw/master/%s", owner, repository, path)
client := http.Client{Timeout: 30 * time.Second}
req, _ := http.NewRequest(http.MethodGet, file, nil)
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %d while fetching: %q", res.StatusCode, file)
}
if res.Body != nil {
defer res.Body.Close()
}
return ioutil.ReadAll(res.Body)
}