forked from bndr/gojenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
170 lines (148 loc) · 4.17 KB
/
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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)
}
}
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(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
}
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
}
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
}
func (pr *PipelineRun) GetArtifacts(ctx context.Context) (artifacts []PipelineArtifact, err error) {
artifacts = make([]PipelineArtifact, 0, 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
}
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
}
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
}