Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 2175558

Browse files
author
Noah Lee
authored
Refactorying the api package (#336)
* Fix the names of the services * Split files
1 parent 90a720a commit 2175558

26 files changed

+543
-457
lines changed

cmd/cli/deployment_deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var deploymentDeployCommand = &cli.Command{
3434
}
3535

3636
c := buildClient(cli)
37-
d, err := c.Deployment.Create(cli.Context, ns, n, api.DeploymentCreateRequest{
37+
d, err := c.Deployments.Create(cli.Context, ns, n, api.DeploymentCreateRequest{
3838
Type: cli.String("type"),
3939
Ref: cli.String("ref"),
4040
Env: cli.String("env"),

cmd/cli/deployment_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var deploymentGetCommand = &cli.Command{
2323
}
2424

2525
c := buildClient(cli)
26-
d, err := c.Deployment.Get(cli.Context, ns, n, number)
26+
d, err := c.Deployments.Get(cli.Context, ns, n, number)
2727
if err != nil {
2828
return err
2929
}

cmd/cli/deployment_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var deploymentListCommand = &cli.Command{
4040
return err
4141
}
4242

43-
ds, err := c.Deployment.List(cli.Context, ns, n, api.DeploymentListOptions{
43+
ds, err := c.Deployments.List(cli.Context, ns, n, api.DeploymentListOptions{
4444
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
4545
Env: cli.String("env"),
4646
Status: deployment.Status(cli.String("status")),

cmd/cli/deployment_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var deploymentUpdateCommand = &cli.Command{
2222
}
2323

2424
c := buildClient(cli)
25-
d, err := c.Deployment.Update(cli.Context, ns, n, number)
25+
d, err := c.Deployments.Update(cli.Context, ns, n, number)
2626
if err != nil {
2727
return err
2828
}

cmd/cli/repo_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var repoGetCommand = &cli.Command{
1919
}
2020

2121
c := buildClient(cli)
22-
repo, err := c.Repo.Get(cli.Context, ns, n)
22+
repo, err := c.Repos.Get(cli.Context, ns, n)
2323
if err != nil {
2424
return err
2525
}

cmd/cli/repo_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ var repoListCommand = &cli.Command{
4040
)
4141

4242
if cli.Bool("all") {
43-
if repos, err = c.Repo.ListAll(cli.Context); err != nil {
43+
if repos, err = c.Repos.ListAll(cli.Context); err != nil {
4444
return err
4545
}
4646
} else {
47-
if repos, err = c.Repo.List(cli.Context, api.RepoListOptions{
47+
if repos, err = c.Repos.List(cli.Context, api.RepoListOptions{
4848
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
4949
}); err != nil {
5050
return err

cmd/cli/repo_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var repoUpdateCommand = &cli.Command{
4949
}
5050

5151
c := buildClient(cli)
52-
repo, err := c.Repo.Update(cli.Context, ns, n, req)
52+
repo, err := c.Repos.Update(cli.Context, ns, n, req)
5353
if err != nil {
5454
return err
5555
}

internal/server/api/v1/repos/repo_update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"go.uber.org/zap"
2020
)
2121

22-
func TestRepoService_UpdateRepo(t *testing.T) {
22+
func TestReposAPI_UpdateRepo(t *testing.T) {
2323
t.Run("Patch config_path field.", func(t *testing.T) {
2424
input := struct {
2525
payload *RepoPatchPayload

pkg/api/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type (
2020
common *client
2121

2222
// Services used for talking to different parts of the Gitploy API.
23-
Repo *RepoService
24-
Deployment *DeploymentService
25-
Config *ConfigService
23+
Repos *ReposService
24+
Deployments *DeploymentsService
25+
Config *ConfigService
2626
}
2727

2828
client struct {
@@ -55,8 +55,8 @@ func NewClient(host string, httpClient *http.Client) *Client {
5555
common: &client{httpClient: httpClient, BaseURL: baseURL},
5656
}
5757

58-
c.Repo = &RepoService{client: c.common}
59-
c.Deployment = &DeploymentService{client: c.common}
58+
c.Repos = &ReposService{client: c.common}
59+
c.Deployments = &DeploymentsService{client: c.common}
6060
c.Config = &ConfigService{client: c.common}
6161

6262
return c

pkg/api/deployment.go

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,5 @@
11
package api
22

3-
import (
4-
"context"
5-
"fmt"
6-
"net/url"
7-
"strconv"
8-
9-
"github.com/gitploy-io/gitploy/model/ent"
10-
"github.com/gitploy-io/gitploy/model/ent/deployment"
11-
)
12-
133
type (
14-
DeploymentService service
15-
16-
DeploymentListOptions struct {
17-
ListOptions
18-
19-
Env string
20-
Status deployment.Status
21-
}
22-
23-
DeploymentCreateRequest struct {
24-
Type string `json:"type"`
25-
Ref string `json:"ref"`
26-
Env string `json:"env"`
27-
}
4+
DeploymentsService service
285
)
29-
30-
// List returns the deployment list.
31-
// It returns an error for a bad request.
32-
func (s *DeploymentService) List(ctx context.Context, namespace, name string, options DeploymentListOptions) ([]*ent.Deployment, error) {
33-
// Build the query.
34-
vals := url.Values{}
35-
36-
vals.Add("page", strconv.Itoa(options.ListOptions.Page))
37-
vals.Add("per_page", strconv.Itoa(options.PerPage))
38-
39-
if options.Env != "" {
40-
vals.Add("env", options.Env)
41-
}
42-
43-
if options.Status != "" {
44-
if err := deployment.StatusValidator(options.Status); err != nil {
45-
return nil, err
46-
}
47-
48-
vals.Add("status", string(options.Status))
49-
}
50-
51-
// Request a server.
52-
req, err := s.client.NewRequest(
53-
"GET",
54-
fmt.Sprintf("api/v1/repos/%s/%s/deployments?%s", namespace, name, vals.Encode()),
55-
nil,
56-
)
57-
if err != nil {
58-
return nil, err
59-
}
60-
61-
var ds []*ent.Deployment
62-
err = s.client.Do(ctx, req, &ds)
63-
if err != nil {
64-
return nil, err
65-
}
66-
67-
return ds, nil
68-
}
69-
70-
// Get returns the deployment.
71-
func (s *DeploymentService) Get(ctx context.Context, namespace, name string, number int) (*ent.Deployment, error) {
72-
req, err := s.client.NewRequest(
73-
"GET",
74-
fmt.Sprintf("api/v1/repos/%s/%s/deployments/%d", namespace, name, number),
75-
nil,
76-
)
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
var d *ent.Deployment
82-
err = s.client.Do(ctx, req, &d)
83-
if err != nil {
84-
return nil, err
85-
}
86-
87-
return d, nil
88-
}
89-
90-
// Create requests a server to deploy a specific ref(branch, SHA, tag).
91-
func (s *DeploymentService) Create(ctx context.Context, namespace, name string, body DeploymentCreateRequest) (*ent.Deployment, error) {
92-
req, err := s.client.NewRequest(
93-
"POST",
94-
fmt.Sprintf("api/v1/repos/%s/%s/deployments", namespace, name),
95-
body,
96-
)
97-
if err != nil {
98-
return nil, err
99-
}
100-
101-
var d *ent.Deployment
102-
err = s.client.Do(ctx, req, &d)
103-
if err != nil {
104-
return nil, err
105-
}
106-
107-
return d, nil
108-
}
109-
110-
// Update requests to trigger the 'waiting' deployment.
111-
func (s *DeploymentService) Update(ctx context.Context, namespace, name string, number int) (*ent.Deployment, error) {
112-
req, err := s.client.NewRequest(
113-
"PUT",
114-
fmt.Sprintf("api/v1/repos/%s/%s/deployments/%d", namespace, name, number),
115-
nil,
116-
)
117-
if err != nil {
118-
return nil, err
119-
}
120-
121-
var d *ent.Deployment
122-
err = s.client.Do(ctx, req, &d)
123-
if err != nil {
124-
return nil, err
125-
}
126-
127-
return d, nil
128-
}

0 commit comments

Comments
 (0)