Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion cmd/cli/deployment_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var deploymentDeployCommand = &cli.Command{
}

c := buildClient(cli)
d, err := c.Deployment.Create(cli.Context, ns, n, api.DeploymentCreateRequest{
d, err := c.Deployments.Create(cli.Context, ns, n, api.DeploymentCreateRequest{
Type: cli.String("type"),
Ref: cli.String("ref"),
Env: cli.String("env"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/deployment_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var deploymentGetCommand = &cli.Command{
}

c := buildClient(cli)
d, err := c.Deployment.Get(cli.Context, ns, n, number)
d, err := c.Deployments.Get(cli.Context, ns, n, number)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/deployment_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var deploymentListCommand = &cli.Command{
return err
}

ds, err := c.Deployment.List(cli.Context, ns, n, api.DeploymentListOptions{
ds, err := c.Deployments.List(cli.Context, ns, n, api.DeploymentListOptions{
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
Env: cli.String("env"),
Status: deployment.Status(cli.String("status")),
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/deployment_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var deploymentUpdateCommand = &cli.Command{
}

c := buildClient(cli)
d, err := c.Deployment.Update(cli.Context, ns, n, number)
d, err := c.Deployments.Update(cli.Context, ns, n, number)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/repo_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var repoGetCommand = &cli.Command{
}

c := buildClient(cli)
repo, err := c.Repo.Get(cli.Context, ns, n)
repo, err := c.Repos.Get(cli.Context, ns, n)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ var repoListCommand = &cli.Command{
)

if cli.Bool("all") {
if repos, err = c.Repo.ListAll(cli.Context); err != nil {
if repos, err = c.Repos.ListAll(cli.Context); err != nil {
return err
}
} else {
if repos, err = c.Repo.List(cli.Context, api.RepoListOptions{
if repos, err = c.Repos.List(cli.Context, api.RepoListOptions{
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
}); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/repo_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var repoUpdateCommand = &cli.Command{
}

c := buildClient(cli)
repo, err := c.Repo.Update(cli.Context, ns, n, req)
repo, err := c.Repos.Update(cli.Context, ns, n, req)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/api/v1/repos/repo_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go.uber.org/zap"
)

func TestRepoService_UpdateRepo(t *testing.T) {
func TestReposAPI_UpdateRepo(t *testing.T) {
t.Run("Patch config_path field.", func(t *testing.T) {
input := struct {
payload *RepoPatchPayload
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type (
common *client

// Services used for talking to different parts of the Gitploy API.
Repo *RepoService
Deployment *DeploymentService
Config *ConfigService
Repos *ReposService
Deployments *DeploymentsService
Config *ConfigService
}

client struct {
Expand Down Expand Up @@ -55,8 +55,8 @@ func NewClient(host string, httpClient *http.Client) *Client {
common: &client{httpClient: httpClient, BaseURL: baseURL},
}

c.Repo = &RepoService{client: c.common}
c.Deployment = &DeploymentService{client: c.common}
c.Repos = &ReposService{client: c.common}
c.Deployments = &DeploymentsService{client: c.common}
c.Config = &ConfigService{client: c.common}

return c
Expand Down
125 changes: 1 addition & 124 deletions pkg/api/deployment.go
Original file line number Diff line number Diff line change
@@ -1,128 +1,5 @@
package api

import (
"context"
"fmt"
"net/url"
"strconv"

"github.com/gitploy-io/gitploy/model/ent"
"github.com/gitploy-io/gitploy/model/ent/deployment"
)

type (
DeploymentService service

DeploymentListOptions struct {
ListOptions

Env string
Status deployment.Status
}

DeploymentCreateRequest struct {
Type string `json:"type"`
Ref string `json:"ref"`
Env string `json:"env"`
}
DeploymentsService service
)

// List returns the deployment list.
// It returns an error for a bad request.
func (s *DeploymentService) List(ctx context.Context, namespace, name string, options DeploymentListOptions) ([]*ent.Deployment, error) {
// Build the query.
vals := url.Values{}

vals.Add("page", strconv.Itoa(options.ListOptions.Page))
vals.Add("per_page", strconv.Itoa(options.PerPage))

if options.Env != "" {
vals.Add("env", options.Env)
}

if options.Status != "" {
if err := deployment.StatusValidator(options.Status); err != nil {
return nil, err
}

vals.Add("status", string(options.Status))
}

// Request a server.
req, err := s.client.NewRequest(
"GET",
fmt.Sprintf("api/v1/repos/%s/%s/deployments?%s", namespace, name, vals.Encode()),
nil,
)
if err != nil {
return nil, err
}

var ds []*ent.Deployment
err = s.client.Do(ctx, req, &ds)
if err != nil {
return nil, err
}

return ds, nil
}

// Get returns the deployment.
func (s *DeploymentService) Get(ctx context.Context, namespace, name string, number int) (*ent.Deployment, error) {
req, err := s.client.NewRequest(
"GET",
fmt.Sprintf("api/v1/repos/%s/%s/deployments/%d", namespace, name, number),
nil,
)
if err != nil {
return nil, err
}

var d *ent.Deployment
err = s.client.Do(ctx, req, &d)
if err != nil {
return nil, err
}

return d, nil
}

// Create requests a server to deploy a specific ref(branch, SHA, tag).
func (s *DeploymentService) Create(ctx context.Context, namespace, name string, body DeploymentCreateRequest) (*ent.Deployment, error) {
req, err := s.client.NewRequest(
"POST",
fmt.Sprintf("api/v1/repos/%s/%s/deployments", namespace, name),
body,
)
if err != nil {
return nil, err
}

var d *ent.Deployment
err = s.client.Do(ctx, req, &d)
if err != nil {
return nil, err
}

return d, nil
}

// Update requests to trigger the 'waiting' deployment.
func (s *DeploymentService) Update(ctx context.Context, namespace, name string, number int) (*ent.Deployment, error) {
req, err := s.client.NewRequest(
"PUT",
fmt.Sprintf("api/v1/repos/%s/%s/deployments/%d", namespace, name, number),
nil,
)
if err != nil {
return nil, err
}

var d *ent.Deployment
err = s.client.Do(ctx, req, &d)
if err != nil {
return nil, err
}

return d, nil
}
34 changes: 34 additions & 0 deletions pkg/api/deployment_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import (
"context"
"fmt"

"github.com/gitploy-io/gitploy/model/ent"
)

type DeploymentCreateRequest struct {
Type string `json:"type"`
Ref string `json:"ref"`
Env string `json:"env"`
}

// Create requests a server to deploy a specific ref(branch, SHA, tag).
func (s *DeploymentsService) Create(ctx context.Context, namespace, name string, body DeploymentCreateRequest) (*ent.Deployment, error) {
req, err := s.client.NewRequest(
"POST",
fmt.Sprintf("api/v1/repos/%s/%s/deployments", namespace, name),
body,
)
if err != nil {
return nil, err
}

var d *ent.Deployment
err = s.client.Do(ctx, req, &d)
if err != nil {
return nil, err
}

return d, nil
}
48 changes: 48 additions & 0 deletions pkg/api/deployment_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package api

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"testing"

"github.com/gitploy-io/gitploy/model/ent"
"github.com/gitploy-io/gitploy/model/ent/deployment"
"gopkg.in/h2non/gock.v1"
)

func TestDeploymentsService_Create(t *testing.T) {
t.Run("Verify the body of a request.", func(t *testing.T) {
d := &ent.Deployment{
ID: 2, Number: 1, Type: deployment.TypeBranch, Env: "production", Ref: "main",
}
gock.New("https://cloud.gitploy.io").
Post("/api/v1/repos/gitploy-io/gitploy/deployments").
AddMatcher(func(req *http.Request, ereq *gock.Request) (bool, error) {
defer req.Body.Close()
output, _ := ioutil.ReadAll(req.Body)

b := DeploymentCreateRequest{}
if err := json.Unmarshal(output, &b); err != nil {
return false, err
}

// Verify the fields of the body.
return b.Type == "branch" && b.Env == "production" && b.Ref == "main", nil
}).
Reply(201).
JSON(d)

c := NewClient("https://cloud.gitploy.io/", http.DefaultClient)

_, err := c.Deployments.Create(context.Background(), "gitploy-io", "gitploy", DeploymentCreateRequest{
Type: "branch",
Env: "production",
Ref: "main",
})
if err != nil {
t.Fatalf("Create returns an error: %s", err)
}
})
}
28 changes: 28 additions & 0 deletions pkg/api/deployment_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"context"
"fmt"

"github.com/gitploy-io/gitploy/model/ent"
)

// Get returns the deployment.
func (s *DeploymentsService) Get(ctx context.Context, namespace, name string, number int) (*ent.Deployment, error) {
req, err := s.client.NewRequest(
"GET",
fmt.Sprintf("api/v1/repos/%s/%s/deployments/%d", namespace, name, number),
nil,
)
if err != nil {
return nil, err
}

var d *ent.Deployment
err = s.client.Do(ctx, req, &d)
if err != nil {
return nil, err
}

return d, nil
}
Loading