-
Notifications
You must be signed in to change notification settings - Fork 8
/
scm_repo_link.go
253 lines (219 loc) · 9.61 KB
/
scm_repo_link.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package scalingo
import (
"context"
"fmt"
"time"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
type SCMRepoLinkService interface {
SCMRepoLinkList(ctx context.Context, opts PaginationOpts) ([]*SCMRepoLink, PaginationMeta, error)
SCMRepoLinkShow(ctx context.Context, app string) (*SCMRepoLink, error)
SCMRepoLinkCreate(ctx context.Context, app string, params SCMRepoLinkCreateParams) (*SCMRepoLink, error)
SCMRepoLinkUpdate(ctx context.Context, app string, params SCMRepoLinkUpdateParams) (*SCMRepoLink, error)
SCMRepoLinkDelete(ctx context.Context, app string) error
SCMRepoLinkPullRequest(ctx context.Context, app string, number int) (*RepoLinkPullRequest, error)
SCMRepoLinkManualDeploy(ctx context.Context, app, branch string) (*Deployment, error)
SCMRepoLinkManualReviewApp(ctx context.Context, app, pullRequestID string) error
SCMRepoLinkDeployments(ctx context.Context, app string) ([]*Deployment, error)
SCMRepoLinkReviewApps(ctx context.Context, app string) ([]*ReviewApp, error)
}
type SCMRepoLinkCreateParams struct {
Source *string `json:"source,omitempty"`
Branch *string `json:"branch,omitempty"`
AuthIntegrationUUID *string `json:"auth_integration_uuid,omitempty"`
AutoDeployEnabled *bool `json:"auto_deploy_enabled,omitempty"`
DeployReviewAppsEnabled *bool `json:"deploy_review_apps_enabled,omitempty"`
DestroyOnCloseEnabled *bool `json:"delete_on_close_enabled,omitempty"`
HoursBeforeDeleteOnClose *uint `json:"hours_before_delete_on_close,omitempty"`
DestroyStaleEnabled *bool `json:"delete_stale_enabled,omitempty"`
HoursBeforeDeleteStale *uint `json:"hours_before_delete_stale,omitempty"`
AutomaticCreationFromForksAllowed *bool `json:"automatic_creation_from_forks_allowed,omitempty"`
}
type SCMRepoLinkUpdateParams struct {
Branch *string `json:"branch,omitempty"`
AutoDeployEnabled *bool `json:"auto_deploy_enabled,omitempty"`
DeployReviewAppsEnabled *bool `json:"deploy_review_apps_enabled,omitempty"`
DestroyOnCloseEnabled *bool `json:"delete_on_close_enabled,omitempty"`
HoursBeforeDeleteOnClose *uint `json:"hours_before_delete_on_close,omitempty"`
DestroyStaleEnabled *bool `json:"delete_stale_enabled,omitempty"`
HoursBeforeDeleteStale *uint `json:"hours_before_delete_stale,omitempty"`
AutomaticCreationFromForksAllowed *bool `json:"automatic_creation_from_forks_allowed,omitempty"`
}
type SCMRepoLink struct {
ID string `json:"id"`
AppID string `json:"app_id"`
Linker SCMRepoLinkLinker `json:"linker"`
URL string `json:"url"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Branch string `json:"branch"`
SCMType SCMType `json:"scm_type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
AutoDeployEnabled bool `json:"auto_deploy_enabled"`
AuthIntegrationUUID string `json:"auth_integration_uuid"`
DeployReviewAppsEnabled bool `json:"deploy_review_apps_enabled"`
DeleteOnCloseEnabled bool `json:"delete_on_close_enabled"`
DeleteStaleEnabled bool `json:"delete_stale_enabled"`
HoursBeforeDeleteOnClose uint `json:"hours_before_delete_on_close"`
HoursBeforeDeleteStale uint `json:"hours_before_delete_stale"`
LastAutoDeployAt time.Time `json:"last_auto_deploy_at"`
AutomaticCreationFromForksAllowed bool `json:"automatic_creation_from_forks_allowed"`
}
type SCMRepoLinkLinker struct {
Username string `json:"username"`
Email string `json:"email"`
ID string `json:"id"`
}
type SCMRepoLinksResponse struct {
SCMRepoLinks []*SCMRepoLink `json:"scm_repo_links"`
Meta struct {
PaginationMeta PaginationMeta `json:"pagination"`
}
}
type ScmRepoLinkResponse struct {
SCMRepoLink *SCMRepoLink `json:"scm_repo_link"`
}
type SCMRepoLinkDeploymentsResponse struct {
Deployments []*Deployment `json:"deployments"`
}
type SCMRepoLinkManualDeployResponse struct {
Deployment *Deployment `json:"deployment"`
}
type SCMRepoLinkReviewAppsResponse struct {
ReviewApps []*ReviewApp `json:"review_apps"`
}
type SCMRepoLinkPullRequestResponse struct {
Pull RepoLinkPullRequest `json:"pull"`
}
type RepoLinkPullRequest struct {
ID int `json:"id"`
Number int `json:"number"`
Title string `json:"title"`
HTMLURL string `json:"html_url"`
SourceRepoName string `json:"source_repo_name"`
SourceRepoHTMLURL string `json:"source_repo_html_url"`
OpenedFromAForkedRepo bool `json:"opened_from_a_forked_repo"`
}
var _ SCMRepoLinkService = (*Client)(nil)
func (c *Client) SCMRepoLinkList(ctx context.Context, opts PaginationOpts) ([]*SCMRepoLink, PaginationMeta, error) {
var res SCMRepoLinksResponse
err := c.ScalingoAPI().ResourceList(ctx, "scm_repo_links", opts.ToMap(), &res)
if err != nil {
return nil, PaginationMeta{}, errgo.Notef(err, "fail to list SCM repo links")
}
return res.SCMRepoLinks, res.Meta.PaginationMeta, nil
}
func (c *Client) SCMRepoLinkShow(ctx context.Context, app string) (*SCMRepoLink, error) {
var res ScmRepoLinkResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "GET",
Endpoint: "/apps/" + app + "/scm_repo_link",
Expected: http.Statuses{200},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to get this SCM repo link")
}
return res.SCMRepoLink, nil
}
func (c *Client) SCMRepoLinkCreate(ctx context.Context, app string, params SCMRepoLinkCreateParams) (*SCMRepoLink, error) {
var res ScmRepoLinkResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/scm_repo_link",
Expected: http.Statuses{201},
Params: map[string]SCMRepoLinkCreateParams{"scm_repo_link": params},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to create the SCM repo link")
}
return res.SCMRepoLink, nil
}
func (c *Client) SCMRepoLinkUpdate(ctx context.Context, app string, params SCMRepoLinkUpdateParams) (*SCMRepoLink, error) {
var res ScmRepoLinkResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "PATCH",
Endpoint: "/apps/" + app + "/scm_repo_link",
Expected: http.Statuses{200},
Params: map[string]SCMRepoLinkUpdateParams{"scm_repo_link": params},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to update this SCM repo link")
}
return res.SCMRepoLink, nil
}
func (c *Client) SCMRepoLinkDelete(ctx context.Context, app string) error {
res, err := c.ScalingoAPI().Do(ctx, &http.APIRequest{
Method: "DELETE",
Endpoint: "/apps/" + app + "/scm_repo_link",
Expected: http.Statuses{204},
})
if err != nil {
return errgo.Notef(err, "fail to delete this SCM repo link")
}
defer res.Body.Close()
return nil
}
func (c *Client) SCMRepoLinkPullRequest(ctx context.Context, app string, number int) (*RepoLinkPullRequest, error) {
var res SCMRepoLinkPullRequestResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "GET",
Endpoint: fmt.Sprintf("/apps/%s/scm_repo_link/pulls/%d", app, number),
Expected: http.Statuses{200},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to get this SCM repo link")
}
return &res.Pull, nil
}
func (c *Client) SCMRepoLinkManualDeploy(ctx context.Context, app, branch string) (*Deployment, error) {
var res SCMRepoLinkManualDeployResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/scm_repo_link/manual_deploy",
Expected: http.Statuses{200},
Params: map[string]string{"branch": branch},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to trigger manual app deployment")
}
return res.Deployment, nil
}
func (c *Client) SCMRepoLinkManualReviewApp(ctx context.Context, app, pullRequestID string) error {
res, err := c.ScalingoAPI().Do(ctx, &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/scm_repo_link/manual_review_app",
Expected: http.Statuses{200},
Params: map[string]string{"pull_request_id": pullRequestID},
})
if err != nil {
return errgo.Notef(err, "fail to trigger manual review app deployment")
}
defer res.Body.Close()
return nil
}
func (c *Client) SCMRepoLinkDeployments(ctx context.Context, app string) ([]*Deployment, error) {
var res SCMRepoLinkDeploymentsResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "GET",
Endpoint: "/apps/" + app + "/scm_repo_link/deployments",
Expected: http.Statuses{200},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to list deployments of this SCM repo link")
}
return res.Deployments, nil
}
func (c *Client) SCMRepoLinkReviewApps(ctx context.Context, app string) ([]*ReviewApp, error) {
var res SCMRepoLinkReviewAppsResponse
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "GET",
Endpoint: "/apps/" + app + "/scm_repo_link/review_apps",
Expected: http.Statuses{200},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to list review apps of this SCM repo link")
}
return res.ReviewApps, nil
}