-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_pipeline.go
151 lines (129 loc) · 3.42 KB
/
service_pipeline.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
// this file implements the pipeline-stage-view API:
// https://github.com/jenkinsci/pipeline-stage-view-plugin/tree/master/rest-api
package ejenkins
import (
"fmt"
"regexp"
)
var baseURLRegex *regexp.Regexp
func init() {
baseURLRegex = regexp.MustCompile("(.+)/wfapi/.*$")
}
type PipelineRun struct {
Job *Job
Base string
URLs map[string]map[string]string `json:"_links"`
ID string
Name string
Status string
StartTime int64 `json:"startTimeMillis"`
EndTime int64 `json:"endTimeMillis"`
Duration int64 `json:"durationMillis"`
Stages []PipelineNode
}
type PipelineNode struct {
Run *PipelineRun
Base string
URLs map[string]map[string]string `json:"_links"`
ID string
Name string
Status string
StartTime int64 `json:"startTimeMillis"`
Duration int64 `json:"durationMillis"`
StageFlowNodes []PipelineNode
ParentNodes []int64
}
type PipelineInputAction struct {
ID string
Message string
ProceedURL string
AbortURL string
}
type PipelineArtifact struct {
ID string
Name string
Path string
URL string
size int
}
type PipelineNodeLog struct {
NodeID string
NodeStatus string
Length int64
HasMore bool
Text string
ConsoleURL string
}
// utility function to fill in the Base fields under PipelineRun
func (run *PipelineRun) update() {
href := run.URLs["self"]["href"]
if matches := baseURLRegex.FindStringSubmatch(href); len(matches) > 1 {
run.Base = matches[1]
}
for i := range run.Stages {
run.Stages[i].Run = run
href := run.Stages[i].URLs["self"]["href"]
if matches := baseURLRegex.FindStringSubmatch(href); len(matches) > 1 {
run.Stages[i].Base = matches[1]
}
}
}
func (job *Job) GetPipelineRuns() (pr []PipelineRun, err error) {
_, err = job.Jenkins.Requester.GetJSON(job.Base+"/wfapi/runs", nil, &pr, nil)
if err != nil {
return nil, err
}
for i := range pr {
pr[i].update()
pr[i].Job = job
}
return pr, nil
}
func (job *Job) GetPipelineRun(id string) (pr *PipelineRun, err error) {
pr = new(PipelineRun)
href := job.Base + "/" + id + "/wfapi/describe"
_, err = job.Jenkins.Requester.GetJSON(href, nil, pr, nil)
if err != nil {
return nil, err
}
pr.update()
pr.Job = job
return pr, nil
}
func (pr *PipelineRun) GetPendingInputActions() (PIAs []PipelineInputAction, err error) {
PIAs = make([]PipelineInputAction, 0, 1)
href := pr.Base + "/wfapi/pendingInputActions"
_, err = pr.Job.Jenkins.Requester.GetJSON(href, nil, &PIAs, nil)
if err != nil {
return nil, err
}
return PIAs, nil
}
func (pr *PipelineRun) GetArtifacts() (artifacts []PipelineArtifact, err error) {
artifacts = make([]PipelineArtifact, 0, 0)
href := pr.Base + "/wfapi/artifacts"
_, err = pr.Job.Jenkins.Requester.GetJSON(href, nil, artifacts, nil)
if err != nil {
return nil, err
}
return artifacts, nil
}
func (pr *PipelineRun) GetNode(id string) (node *PipelineNode, err error) {
node = new(PipelineNode)
href := pr.Base + "/execution/node/" + id + "/wfapi/describe"
_, err = pr.Job.Jenkins.Requester.GetJSON(href, nil, node, nil)
if err != nil {
return nil, err
}
return node, nil
}
func (node *PipelineNode) GetLog() (log *PipelineNodeLog, err error) {
log = new(PipelineNodeLog)
href := node.Base + "/wfapi/log"
fmt.Println(href)
_, err = node.Run.Job.Jenkins.Requester.GetJSON(href, nil, log, nil)
if err != nil {
return nil, err
}
return log, nil
}