Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated integrations/scm_repo_link structs and urls #126

Merged
merged 3 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 0 additions & 104 deletions integrations.go

This file was deleted.

120 changes: 120 additions & 0 deletions scm_integrations.go
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"`
brandon-welsch marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading