forked from Scalingo/go-scalingo
/
scm_integrations.go
127 lines (104 loc) · 3.55 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
package scalingo
import (
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/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() ([]SCMIntegration, error)
SCMIntegrationsShow(id string) (*SCMIntegration, error)
SCMIntegrationsCreate(scmType SCMType, url string, accessToken string) (*SCMIntegration, error)
SCMIntegrationsDelete(id string) error
SCMIntegrationsImportKeys(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"`
}
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() ([]SCMIntegration, error) {
var res SCMIntegrationsRes
err := c.AuthAPI().ResourceList("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(id string) (*SCMIntegration, error) {
var res SCMIntegrationRes
err := c.AuthAPI().ResourceGet("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(scmType SCMType, url string, accessToken string) (*SCMIntegration, error) {
payload := SCMIntegrationParamsReq{SCMIntegrationParams{
SCMType: scmType,
URL: url,
AccessToken: accessToken,
}}
var res SCMIntegrationRes
err := c.AuthAPI().ResourceAdd("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(id string) error {
err := c.AuthAPI().ResourceDelete("scm_integrations", id)
if err != nil {
return errgo.Notef(err, "fail to delete this SCM integration")
}
return nil
}
func (c *Client) SCMIntegrationsImportKeys(id string) ([]Key, error) {
var res KeysRes
var err = c.AuthAPI().DoRequest(&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
}