/
stage.go
186 lines (155 loc) · 4.4 KB
/
stage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package sdk
import (
"encoding/json"
"fmt"
"net/http"
)
// Stage Pipeline step that parallelize actions by order
type Stage struct {
ID int64 `json:"id" yaml:"pipeline_stage_id"`
Name string `json:"name"`
PipelineID int64 `json:"-" yaml:"-"`
BuildOrder int `json:"build_order"`
Enabled bool `json:"enabled"`
Actions []Action `json:"actions"` // WIP: refacto to delete Actions and use Jobs
ActionBuilds []ActionBuild `json:"builds"`
Prerequisites []Prerequisite `json:"prerequisites"`
LastModified int64 `json:"last_modified"`
Jobs []Job `json:"jobs"`
}
// NewStage instanciate a new Stage
func NewStage(name string) *Stage {
s := &Stage{
Name: name,
}
return s
}
// JSON return the marshalled string of Stage object
func (s *Stage) JSON() string {
data, err := json.Marshal(s)
if err != nil {
fmt.Printf("Action.JSON: cannot marshal: %s\n", err)
return ""
}
return string(data)
}
// FromJSON unmarshal given json data into Stage object
func (s *Stage) FromJSON(data []byte) (*Stage, error) {
return s, json.Unmarshal(data, &s)
}
// AddStage creates a new stage
func AddStage(projectKey, pipelineName, name string) error {
s := NewStage(name)
data, err := json.Marshal(s)
if err != nil {
return err
}
url := fmt.Sprintf("/project/%s/pipeline/%s/stage", projectKey, pipelineName)
data, code, err := Request("POST", url, data)
if err != nil {
return err
}
if code != http.StatusCreated && code != http.StatusOK {
return fmt.Errorf("Error [%d]: %s", code, data)
}
e := DecodeError(data)
if e != nil {
return e
}
return nil
}
// GetStage Get stage by ID
func GetStage(projectKey, pipelineName, pipelineStageID string) (*Stage, error) {
s := &Stage{}
url := fmt.Sprintf("/project/%s/pipeline/%s/stage/%s", projectKey, pipelineName, pipelineStageID)
data, code, err := Request("GET", url, nil)
if err != nil {
return s, err
}
if code != http.StatusCreated && code != http.StatusOK {
return s, fmt.Errorf("Error [%d]: %s", code, data)
}
e := DecodeError(data)
if e != nil {
return s, e
}
err = json.Unmarshal(data, s)
return s, err
}
func updateStage(projectKey, pipelineName, pipelineStageID string, stageData *Stage) error {
data, err := json.Marshal(stageData)
if err != nil {
return err
}
url := fmt.Sprintf("/project/%s/pipeline/%s/stage/%s", projectKey, pipelineName, pipelineStageID)
data, code, err := Request("PUT", url, data)
if err != nil {
return err
}
if code != http.StatusCreated && code != http.StatusOK {
return fmt.Errorf("Error [%d]: %s", code, data)
}
e := DecodeError(data)
if e != nil {
return e
}
return nil
}
// RenameStage Rename a stage
func RenameStage(projectKey, pipelineName, pipelineStageID, newName string) error {
s, err := GetStage(projectKey, pipelineName, pipelineStageID)
if err != nil {
return err
}
s.Name = newName
return updateStage(projectKey, pipelineName, pipelineStageID, s)
}
// ChangeStageState Enabled/Disabled a stage
func ChangeStageState(projectKey, pipelineName, pipelineStageID string, enabled bool) error {
s, err := GetStage(projectKey, pipelineName, pipelineStageID)
if err != nil {
return err
}
s.Enabled = enabled
return updateStage(projectKey, pipelineName, pipelineStageID, s)
}
// MoveStage Change stage buildOrder
func MoveStage(projectKey, pipelineName string, pipelineStageID int64, buildOrder int) error {
s := &Stage{
ID: pipelineStageID,
BuildOrder: buildOrder,
}
data, err := json.Marshal(s)
if err != nil {
return err
}
url := fmt.Sprintf("/project/%s/pipeline/%s/stage/move", projectKey, pipelineName)
data, code, err := Request("POST", url, data)
if err != nil {
return err
}
if code != http.StatusCreated && code != http.StatusOK {
return fmt.Errorf("Error [%d]: %s", code, data)
}
e := DecodeError(data)
if e != nil {
return e
}
return nil
}
// DeleteStage Call API to delete the given stage from the given pipeline
func DeleteStage(projectKey, pipelineName, pipelineStageID string) error {
url := fmt.Sprintf("/project/%s/pipeline/%s/stage/%s", projectKey, pipelineName, pipelineStageID)
data, code, err := Request("DELETE", url, nil)
if err != nil {
return err
}
if code != http.StatusCreated && code != http.StatusOK {
return fmt.Errorf("Error [%d]: %s", code, data)
}
e := DecodeError(data)
if e != nil {
return e
}
return nil
}