-
Notifications
You must be signed in to change notification settings - Fork 8
/
scm_integrations.go
132 lines (108 loc) · 3.89 KB
/
scm_integrations.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
package scalingo
import (
"context"
"time"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
type SCMType string
// Type of SCM integrations
const (
SCMGithubType SCMType = "github" // GitHub
SCMGithubEnterpriseType SCMType = "github-enterprise" // GitHub Enterprise (private instance)
SCMGitlabType SCMType = "gitlab" // GitLab.com
SCMGitlabSelfHostedType SCMType = "gitlab-self-hosted" // GitLab self-hosted (private instance)
)
var SCMTypeDisplay = map[SCMType]string{
SCMGithubType: "GitHub",
SCMGitlabType: "GitLab",
SCMGithubEnterpriseType: "GitHub Enterprise",
SCMGitlabSelfHostedType: "GitLab self-hosted",
}
func (t SCMType) Str() string {
return string(t)
}
type SCMIntegrationsService interface {
SCMIntegrationsList(context.Context) ([]SCMIntegration, error)
SCMIntegrationsShow(ctx context.Context, id string) (*SCMIntegration, error)
SCMIntegrationsCreate(ctx context.Context, scmType SCMType, url string, accessToken string) (*SCMIntegration, error)
SCMIntegrationsDelete(ctx context.Context, id string) error
SCMIntegrationsImportKeys(ctx context.Context, id string) ([]Key, error)
}
var _ SCMIntegrationsService = (*Client)(nil)
type SCMIntegration struct {
ID string `json:"id"`
SCMType SCMType `json:"scm_type"`
URL string `json:"url,omitempty"`
AccessToken string `json:"access_token"`
UID string `json:"uid"`
Username string `json:"username"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
ProfileURL string `json:"profile_url"`
CreatedAt time.Time `json:"created_at"`
Owner Owner `json:"owner"`
}
type SCMIntegrationParams struct {
SCMType SCMType `json:"scm_type"`
URL string `json:"url,omitempty"`
AccessToken string `json:"access_token"`
}
type SCMIntegrationRes struct {
SCMIntegration SCMIntegration `json:"scm_integration"`
}
type SCMIntegrationsRes struct {
SCMIntegrations []SCMIntegration `json:"scm_integrations"`
}
type SCMIntegrationParamsReq struct {
SCMIntegrationParams SCMIntegrationParams `json:"scm_integration"`
}
func (c *Client) SCMIntegrationsList(ctx context.Context) ([]SCMIntegration, error) {
var res SCMIntegrationsRes
err := c.AuthAPI().ResourceList(ctx, "scm_integrations", nil, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to list SCM integration")
}
return res.SCMIntegrations, nil
}
func (c *Client) SCMIntegrationsShow(ctx context.Context, id string) (*SCMIntegration, error) {
var res SCMIntegrationRes
err := c.AuthAPI().ResourceGet(ctx, "scm_integrations", id, nil, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to get this SCM integration")
}
return &res.SCMIntegration, nil
}
func (c *Client) SCMIntegrationsCreate(ctx context.Context, scmType SCMType, url string, accessToken string) (*SCMIntegration, error) {
payload := SCMIntegrationParamsReq{SCMIntegrationParams{
SCMType: scmType,
URL: url,
AccessToken: accessToken,
}}
var res SCMIntegrationRes
err := c.AuthAPI().ResourceAdd(ctx, "scm_integrations", payload, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to create the SCM integration")
}
return &res.SCMIntegration, nil
}
func (c *Client) SCMIntegrationsDelete(ctx context.Context, id string) error {
err := c.AuthAPI().ResourceDelete(ctx, "scm_integrations", id)
if err != nil {
return errgo.Notef(err, "fail to delete this SCM integration")
}
return nil
}
func (c *Client) SCMIntegrationsImportKeys(ctx context.Context, id string) ([]Key, error) {
var res KeysRes
var err = c.AuthAPI().DoRequest(ctx, &http.APIRequest{
Method: "POST",
Endpoint: "/scm_integrations/" + id + "/import_keys",
Params: nil,
Expected: http.Statuses{201},
}, &res)
if err != nil {
return nil, errgo.Notef(err, "fail to import ssh keys from this SCM integration")
}
return res.Keys, nil
}