forked from ahume/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
125 lines (96 loc) · 2.87 KB
/
github.go
File metadata and controls
125 lines (96 loc) · 2.87 KB
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
package resource
import (
"net/http"
"golang.org/x/oauth2"
"github.com/ahume/go-github/github"
)
//go:generate counterfeiter -o fakes/fake_git_hub.go . GitHub
type GitHub interface {
ListDeployments() ([]*github.Deployment, error)
ListDeploymentStatuses(ID int) ([]*github.DeploymentStatus, error)
GetDeployment(ID int) (*github.Deployment, error)
CreateDeployment(request *github.DeploymentRequest) (*github.Deployment, error)
CreateDeploymentStatus(ID int, 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
}
return &GitHubClient{
client: client,
user: source.User,
repository: source.Repository,
}, nil
}
func (g *GitHubClient) ListDeployments() ([]*github.Deployment, error) {
deployments, res, err := g.client.Repositories.ListDeployments(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 int) (*github.Deployment, error) {
deployment, res, err := g.client.Repositories.GetDeployment(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) {
deployment, res, err := g.client.Repositories.CreateDeployment(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 int) ([]*github.DeploymentStatus, error) {
statuses, res, err := g.client.Repositories.ListDeploymentStatuses(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 int, request *github.DeploymentStatusRequest) (*github.DeploymentStatus, error) {
status, res, err := g.client.Repositories.CreateDeploymentStatus(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
}