-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from Scalingo/fix/125/update_structs_and_urls
Updated integrations/scm_repo_link structs and urls
- Loading branch information
Showing
3 changed files
with
175 additions
and
148 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
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) | ||
) | ||
|
||
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 | ||
} |
Oops, something went wrong.