|
1 | 1 | package api |
2 | 2 |
|
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 | | - |
13 | 3 | 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 |
28 | 5 | ) |
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