-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.go
121 lines (108 loc) · 3.75 KB
/
workflow.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
package circleci
import "time"
const workflowBasePath = "/workflow"
// WorkflowService is an interface for Workflow API.
type WorkflowService interface {
Get(id string) (*Workflow, error)
Approve(id, approvalReqID string) (*Message, error)
Cancel(id string) (*Message, error)
GetJobs(id string) (*WorkflowJobs, error)
Rerun(id string, jobIDs []string, fromFailed bool) (*Message, error)
}
// WorkflowOp handles communication with the project related methods in the CircleCI API v2.
type WorkflowOp struct {
client *Client
}
var _ WorkflowService = (*WorkflowOp)(nil)
// Workflow represents information workflow.
type Workflow struct {
PipelineID string `json:"pipeline_id,omitempty"`
CanceledBy string `json:"canceled_by,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ProjectSlug string `json:"project_slug,omitempty"`
ErroredBy string `json:"errored_by,omitempty"`
Tag string `json:"tag,omitempty"`
Status string `json:"status,omitempty"`
StartedBy string `json:"started_by,omitempty"`
PipelineNumber int `json:"pipeline_number,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
StoppedAt time.Time `json:"stopped_at,omitempty"`
}
// WorkflowJobs is jobs belongs to a Workflow.
type WorkflowJobs struct {
Items []struct {
CanceledBy string `json:"canceled_by,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
JobNumber int `json:"job_number,omitempty"`
ID string `json:"id,omitempty"`
StartedAt time.Time `json:"started_at,omitempty"`
Name string `json:"name,omitempty"`
ApprovedBy string `json:"approved_by,omitempty"`
ProjectSlug string `json:"project_slug,omitempty"`
Status interface{} `json:"status,omitempty"`
Type string `json:"type,omitempty"`
StoppedAt time.Time `json:"stopped_at,omitempty"`
ApprovalRequestID string `json:"approval_request_id,omitempty"`
} `json:"items,omitempty"`
NextPageToken string `json:"next_page_token,omitempty"`
}
// RerunJob is a payload to send when rerunning jobs in Workflow.
// Multiple job ids can be given in Jobs.
type RerunJob struct {
Jobs []string `json:"jobs,omitempty"`
FromFailed bool `json:"from_failed,omitempty"`
}
// Get gets detail of workflow.
func (ps *WorkflowOp) Get(id string) (*Workflow, error) {
w := &Workflow{}
path := workflowBasePath + "/" + id
err := ps.client.Get(path, w, nil)
if err != nil {
return nil, err
}
return w, nil
}
// Approve approves pending workflow job.
func (ps *WorkflowOp) Approve(id, approvalReqID string) (*Message, error) {
m := &Message{}
path := workflowBasePath + "/" + id + "/approve/" + approvalReqID
err := ps.client.Post(path, nil, m)
if err != nil {
return nil, err
}
return m, nil
}
// Cancel cancels given workflow.
func (ps *WorkflowOp) Cancel(id string) (*Message, error) {
m := &Message{}
path := workflowBasePath + "/" + id + "/cancel"
err := ps.client.Post(path, nil, m)
if err != nil {
return nil, err
}
return m, nil
}
// GetJobs get jobs in the workflow.
func (ps *WorkflowOp) GetJobs(id string) (*WorkflowJobs, error) {
wj := &WorkflowJobs{}
path := workflowBasePath + "/" + id + "/job"
err := ps.client.Post(path, nil, wj)
if err != nil {
return nil, err
}
return wj, nil
}
// Rerun cancels given workflow
func (ps *WorkflowOp) Rerun(id string, jobIDs []string, fromFailed bool) (*Message, error) {
m := &Message{}
path := workflowBasePath + "/" + id + "/rerun"
err := ps.client.Post(path, &RerunJob{
Jobs: jobIDs,
FromFailed: fromFailed,
}, m)
if err != nil {
return nil, err
}
return m, nil
}