-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgithub.go
155 lines (120 loc) · 3.57 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package resource
import (
"context"
"net/http"
"net/url"
"time"
"golang.org/x/oauth2"
"github.com/google/go-github/v28/github"
)
//go:generate counterfeiter -o fakes/fake_git_hub.go . GitHub
type GitHub interface {
ListDeployments() ([]*github.Deployment, error)
ListDeploymentStatuses(ID int64) ([]*github.DeploymentStatus, error)
GetDeployment(ID int64) (*github.Deployment, error)
CreateDeployment(request *github.DeploymentRequest) (*github.Deployment, error)
CreateDeploymentStatus(ID int64, request *github.DeploymentStatusRequest) (*github.DeploymentStatus, error)
}
type GitHubClient struct {
client *github.Client
user string
repository string
}
func NewGitHubClient(source Source) (*GitHubClient, error) {
var client *github.Client
client, err := oauthClient(source)
if err != nil {
return nil, err
}
if source.GitHubAPIURL != "" {
client.BaseURL, err = url.Parse(source.GitHubAPIURL)
if err != nil {
return nil, err
}
}
return &GitHubClient{
client: client,
user: source.User,
repository: source.Repository,
}, nil
}
func (g *GitHubClient) ListDeployments() ([]*github.Deployment, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deployments, res, err := g.client.Repositories.ListDeployments(ctx, g.user, g.repository, nil)
if err != nil {
return []*github.Deployment{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return deployments, nil
}
func (g *GitHubClient) GetDeployment(ID int64) (*github.Deployment, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deployment, res, err := g.client.Repositories.GetDeployment(ctx, g.user, g.repository, ID)
if err != nil {
return &github.Deployment{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return deployment, nil
}
func (g *GitHubClient) CreateDeployment(request *github.DeploymentRequest) (*github.Deployment, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deployment, res, err := g.client.Repositories.CreateDeployment(ctx, g.user, g.repository, request)
if err != nil {
return &github.Deployment{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return deployment, nil
}
func (g *GitHubClient) ListDeploymentStatuses(ID int64) ([]*github.DeploymentStatus, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
statuses, res, err := g.client.Repositories.ListDeploymentStatuses(ctx, g.user, g.repository, ID, nil)
if err != nil {
return []*github.DeploymentStatus{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return statuses, nil
}
func (g *GitHubClient) CreateDeploymentStatus(ID int64, request *github.DeploymentStatusRequest) (*github.DeploymentStatus, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
status, res, err := g.client.Repositories.CreateDeploymentStatus(ctx, g.user, g.repository, ID, request)
if err != nil {
return &github.DeploymentStatus{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return status, nil
}
func oauthClient(source Source) (*github.Client, error) {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: source.AccessToken,
})
oauthClient := oauth2.NewClient(oauth2.NoContext, ts)
githubHTTPClient := &http.Client{
Transport: oauthClient.Transport,
}
return github.NewClient(githubHTTPClient), nil
}