This repository has been archived by the owner on May 13, 2024. It is now read-only.
forked from ahume/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
166 lines (130 loc) · 3.79 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
156
157
158
159
160
161
162
163
164
165
166
package resource
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"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
timeout time.Duration
}
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
}
}
tokens := strings.SplitN(source.Repository, "/", 2)
if len(tokens) < 2 {
return nil, fmt.Errorf("%s is not a full repository name", source.Repository)
}
user := tokens[0]
repo := tokens[1]
return &GitHubClient{
client: client,
user: user,
repository: repo,
timeout: 20*time.Second,
}, nil
}
func (g *GitHubClient) ListDeployments() ([]*github.Deployment, error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, g.timeout)
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, g.timeout)
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, g.timeout)
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, g.timeout)
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, g.timeout)
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
}