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

Commit adc523a

Browse files
author
Noah Lee
authored
Add the deployment-status command (#352)
* Add the 'deployment-status' command * Build the description automatically. * Fix the type of parameters
1 parent 36495f7 commit adc523a

16 files changed

+273
-17
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.Deployment.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_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.Deployment.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/deploymentstatus.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "github.com/urfave/cli/v2"
4+
5+
var deploymentStatusCommand = &cli.Command{
6+
Name: "deployment-status",
7+
Aliases: []string{"ds"},
8+
Usage: "Manage deployment statuses.",
9+
Subcommands: []*cli.Command{
10+
deploymentStatusListCommand,
11+
deploymentStatusCreateCommand,
12+
},
13+
}

cmd/cli/deploymentstatus_create.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/urfave/cli/v2"
8+
9+
"github.com/gitploy-io/gitploy/pkg/api"
10+
)
11+
12+
var deploymentStatusCreateCommand = &cli.Command{
13+
Name: "create",
14+
Usage: "Create the remote deployment status under the deployment.",
15+
ArgsUsage: "<owner>/<repo> <number>",
16+
Flags: []cli.Flag{
17+
&cli.StringFlag{
18+
Name: "status",
19+
Usage: "The remote deployment status. For GitHub, Can be one of error, failure, in_progress, queued, or success.",
20+
Required: true,
21+
},
22+
&cli.StringFlag{
23+
Name: "description",
24+
Usage: "A short description of the status.",
25+
DefaultText: "USER_LOGIN updated the status manually.",
26+
},
27+
},
28+
Action: func(cli *cli.Context) error {
29+
ns, n, err := splitFullName(cli.Args().First())
30+
if err != nil {
31+
return err
32+
}
33+
34+
number, err := strconv.Atoi(cli.Args().Get(1))
35+
if err != nil {
36+
return err
37+
}
38+
39+
// Build the request.
40+
c := buildClient(cli)
41+
req := &api.DeploymentStatusCreateRemoteRequest{
42+
Status: cli.String("status"),
43+
Description: cli.String("description"),
44+
}
45+
if cli.String("description") == "" {
46+
req.Description = buildDescription(cli)
47+
}
48+
49+
dss, err := c.DeploymentStatus.CreateRemote(cli.Context, ns, n, number, req)
50+
if err != nil {
51+
return err
52+
}
53+
54+
return printJson(cli, dss)
55+
},
56+
}
57+
58+
func buildDescription(cli *cli.Context) string {
59+
c := buildClient(cli)
60+
61+
me, err := c.User.GetMe(cli.Context)
62+
if err != nil {
63+
return "Updated the status manually."
64+
}
65+
66+
return fmt.Sprintf("%s updated the status manually.", me.Login)
67+
}

cmd/cli/deploymentstatus_list.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/urfave/cli/v2"
7+
8+
"github.com/gitploy-io/gitploy/pkg/api"
9+
)
10+
11+
var deploymentStatusListCommand = &cli.Command{
12+
Name: "list",
13+
Aliases: []string{"ls"},
14+
Usage: "Show the deployment status under the deployment.",
15+
ArgsUsage: "<owner>/<repo> <number>",
16+
Action: func(cli *cli.Context) error {
17+
ns, n, err := splitFullName(cli.Args().First())
18+
if err != nil {
19+
return err
20+
}
21+
22+
number, err := strconv.Atoi(cli.Args().Get(1))
23+
if err != nil {
24+
return err
25+
}
26+
27+
c := buildClient(cli)
28+
dss, err := c.DeploymentStatus.List(cli.Context, ns, n, number, &api.ListOptions{})
29+
if err != nil {
30+
return err
31+
}
32+
33+
return printJson(cli, dss)
34+
},
35+
}

cmd/cli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func main() {
3737
Commands: []*cli.Command{
3838
repoCommand,
3939
deploymentCommand,
40+
deploymentStatusCommand,
4041
configCommand,
4142
},
4243
}

cmd/cli/repo_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var repoListCommand = &cli.Command{
4444
return err
4545
}
4646
} else {
47-
if repos, err = c.Repo.List(cli.Context, api.RepoListOptions{
47+
if repos, err = c.Repo.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
@@ -34,7 +34,7 @@ var repoUpdateCommand = &cli.Command{
3434
}
3535

3636
// Build the request body.
37-
req := api.RepoUpdateRequest{}
37+
req := &api.RepoUpdateRequest{}
3838
if config := cli.String("config"); config != "" {
3939
req.ConfigPath = pointer.ToString(config)
4040
}

pkg/api/client.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ type (
1919
// Reuse a single struct instead of allocating one for each service on the heap.
2020
common *client
2121

22-
// Services used for talking to different parts of the Gitploy API.
23-
Repo *RepoService
24-
Deployment *DeploymentService
25-
Config *ConfigService
22+
// Services is used for talking to different parts of the Gitploy API.
23+
Repo *RepoService
24+
Deployment *DeploymentService
25+
DeploymentStatus *DeploymentStatusService
26+
Config *ConfigService
27+
User *UserService
2628
}
2729

2830
client struct {
@@ -57,7 +59,9 @@ func NewClient(host string, httpClient *http.Client) *Client {
5759

5860
c.Repo = &RepoService{client: c.common}
5961
c.Deployment = &DeploymentService{client: c.common}
62+
c.DeploymentStatus = &DeploymentStatusService{client: c.common}
6063
c.Config = &ConfigService{client: c.common}
64+
c.User = &UserService{client: c.common}
6165

6266
return c
6367
}

pkg/api/deployment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type (
2929

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

@@ -88,7 +88,7 @@ func (s *DeploymentService) Get(ctx context.Context, namespace, name string, num
8888
}
8989

9090
// 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) {
91+
func (s *DeploymentService) Create(ctx context.Context, namespace, name string, body *DeploymentCreateRequest) (*ent.Deployment, error) {
9292
req, err := s.client.NewRequest(
9393
"POST",
9494
fmt.Sprintf("api/v1/repos/%s/%s/deployments", namespace, name),

0 commit comments

Comments
 (0)