-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
82 lines (71 loc) · 2.02 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
71
72
73
74
75
76
77
78
79
80
81
82
package V2BuildAssist
import (
"context"
"io"
"io/ioutil"
"net/http"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
func CreateFileIfNotExist(accessToken, owner, repo, path, message string, content []byte) (string, error) {
ctx := context.Background()
client := getclient(accessToken, ctx)
f, _, err := client.Repositories.CreateFile(ctx, owner, repo, path, &github.RepositoryContentFileOptions{Content: content, Message: &message})
if err != nil {
return "", err
}
url := f.GetURL()
return url, nil
}
func getclient(accessToken string, ctx context.Context) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client
}
func GetReleaseFile(accessToken, owner, repo, tag, path string) ([]byte, int64, error) {
ctx := context.Background()
client := getclient(accessToken, ctx)
rele, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
if err != nil {
return nil, 0, err
}
id := *rele.ID
for _, v := range rele.Assets {
if *v.Name == path {
url := v.GetBrowserDownloadURL()
resp, err := http.Get(url)
if err != nil {
return nil, 0, err
}
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}
return d, id, nil
}
}
return nil, 0, io.EOF
}
func CreateCommentForCommit(accessToken, owner, repo, sha, message string) (string, error) {
ctx := context.Background()
client := getclient(accessToken, ctx)
f, _, err := client.Repositories.CreateComment(ctx, owner, repo, sha, &github.RepositoryComment{Body: &message})
if err != nil {
return "", err
}
url := f.GetURL()
return url, nil
}
func CreateCommentForPR(accessToken, owner, repo, message string, number int) (string, error) {
ctx := context.Background()
client := getclient(accessToken, ctx)
f, _, err := client.Issues.CreateComment(ctx, owner, repo, number, &github.IssueComment{Body: &message})
if err != nil {
return "", err
}
url := f.GetURL()
return url, nil
}