/
github.go
114 lines (87 loc) · 2.98 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"context"
"fmt"
"net/http"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
const (
ReviewApprove = "APPROVE"
ReviewRequestChange = "REQUEST_CHANGES"
ReviewComment = "COMMENT"
ReviewPending = "PENDING"
)
// GitHubClient is a clint to interact with Github API
type GitHubClient struct {
Owner, Repo string
Client *github.Client
}
// NewGitHubClient creates and initializes a new GitHubClient
func NewGitHubClient(owner, repo, token string) *GitHubClient {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
})
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
return &GitHubClient{
Owner: owner,
Repo: repo,
Client: client,
}
}
// CompareCommits gets diffs between base and head
func (c *GitHubClient) CompareCommits(base, head string) (*github.CommitsComparison, error) {
cc, res, err := c.Client.Repositories.CompareCommits(context.TODO(), c.Owner, c.Repo, base, head)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Repositories.CompareCommits returns invalid status: %s", res.Status)
}
return cc, nil
}
// GetPullRequestFiles gets files edited by a PR
func (c *GitHubClient) ListPullRequestsFiles(number int, opt *github.ListOptions) ([]*github.CommitFile, error) {
cf, res, err := c.Client.PullRequests.ListFiles(context.TODO(), c.Owner, c.Repo, number, opt)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("PullRequests.ListFiles returns invalid status: %s", res.Status)
}
return cf, nil
}
// GetLatestRelease gets the latest release of the repository
func (c *GitHubClient) GetLatestRelease() (*github.RepositoryRelease, error) {
rr, res, err := c.Client.Repositories.GetLatestRelease(context.TODO(), c.Owner, c.Repo)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Repositories.GetLatestRelease returns invalid status: %s", res.Status)
}
return rr, nil
}
// GetContent gets the specified file
func (c *GitHubClient) GetContent(path string, opt *github.RepositoryContentGetOptions) (*github.RepositoryContent, []*github.RepositoryContent, error) {
fc, dc, res, err := c.Client.Repositories.GetContents(context.TODO(), c.Owner, c.Repo, path, opt)
if err != nil {
return nil, nil, err
}
if res.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("Repositories.GetContents returns invalid status: %s", res.Status)
}
return fc, dc, nil
}
// CreateReview creates a review on a given PR
func (c *GitHubClient) CreateReview(number int, review *github.PullRequestReviewRequest) (*github.PullRequestReview, error) {
prr, res, err := c.Client.PullRequests.CreateReview(context.TODO(), c.Owner, c.Repo, number, review)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("PullRequests.CreateReview returns invalid status: %s", res.Status)
}
return prr, nil
}