-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathpipeline.go
More file actions
181 lines (159 loc) · 4.92 KB
/
pipeline.go
File metadata and controls
181 lines (159 loc) · 4.92 KB
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
// Copyright 2017 - Tessa Nordgren
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// this file implements the pipeline-stage-view API:
// https://github.com/jenkinsci/pipeline-stage-view-plugin/tree/master/rest-api
package gojenkins
import (
"context"
"fmt"
"regexp"
)
var baseURLRegex *regexp.Regexp
func init() {
var err error
baseURLRegex, err = regexp.Compile("(.+)/wfapi/.*$")
if err != nil {
panic(err)
}
}
// PipelineRun represents a single run of a Jenkins pipeline job.
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
}
// PipelineNode represents a stage or step within a pipeline run.
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
}
// PipelineInputAction represents a pending input action that requires user interaction.
type PipelineInputAction struct {
ID string
Message string
ProceedURL string
AbortURL string
}
// PipelineArtifact represents an artifact produced by a pipeline run.
type PipelineArtifact struct {
ID string
Name string
Path string
URL string
Size int `json:"size"`
}
// PipelineNodeLog represents the console log output for a pipeline node.
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]
}
}
}
// GetPipelineRuns returns all pipeline runs for a pipeline job.
func (job *Job) GetPipelineRuns(ctx context.Context) (pr []PipelineRun, err error) {
_, err = job.Jenkins.Requester.GetJSON(ctx, job.Base+"/wfapi/runs", &pr, nil)
if err != nil {
return nil, err
}
for i := range pr {
pr[i].update()
pr[i].Job = job
}
return pr, nil
}
// GetPipelineRun returns a specific pipeline run by its ID.
func (job *Job) GetPipelineRun(ctx context.Context, id string) (pr *PipelineRun, err error) {
pr = new(PipelineRun)
href := job.Base + "/" + id + "/wfapi/describe"
_, err = job.Jenkins.Requester.GetJSON(ctx, href, pr, nil)
if err != nil {
return nil, err
}
pr.update()
pr.Job = job
return pr, nil
}
// GetPendingInputActions returns all pending input actions for a pipeline run.
func (pr *PipelineRun) GetPendingInputActions(ctx context.Context) (PIAs []PipelineInputAction, err error) {
PIAs = make([]PipelineInputAction, 0, 1)
href := pr.Base + "/wfapi/pendingInputActions"
_, err = pr.Job.Jenkins.Requester.GetJSON(ctx, href, &PIAs, nil)
if err != nil {
return nil, err
}
return PIAs, nil
}
// GetArtifacts returns all artifacts produced by a pipeline run.
func (pr *PipelineRun) GetArtifacts(ctx context.Context) (artifacts []PipelineArtifact, err error) {
artifacts = make([]PipelineArtifact, 0)
href := pr.Base + "/wfapi/artifacts"
_, err = pr.Job.Jenkins.Requester.GetJSON(ctx, href, artifacts, nil)
if err != nil {
return nil, err
}
return artifacts, nil
}
// GetNode returns a specific pipeline node by its ID.
func (pr *PipelineRun) GetNode(ctx context.Context, id string) (node *PipelineNode, err error) {
node = new(PipelineNode)
href := pr.Base + "/execution/node/" + id + "/wfapi/describe"
_, err = pr.Job.Jenkins.Requester.GetJSON(ctx, href, node, nil)
if err != nil {
return nil, err
}
return node, nil
}
// GetLog returns the console log output for a pipeline node.
func (node *PipelineNode) GetLog(ctx context.Context) (log *PipelineNodeLog, err error) {
log = new(PipelineNodeLog)
href := node.Base + "/wfapi/log"
fmt.Println(href)
_, err = node.Run.Job.Jenkins.Requester.GetJSON(ctx, href, log, nil)
if err != nil {
return nil, err
}
return log, nil
}