github.com/bndr/gojenkins@v1.1.0/pipeline.go (about) 1 // Copyright 2017 - Tessa Nordgren 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 // 15 // this file implements the pipeline-stage-view API: 16 // https://github.com/jenkinsci/pipeline-stage-view-plugin/tree/master/rest-api 17 18 package gojenkins 19 20 import ( 21 "context" 22 "fmt" 23 "regexp" 24 ) 25 26 var baseURLRegex *regexp.Regexp 27 28 func init() { 29 var err error 30 baseURLRegex, err = regexp.Compile("(.+)/wfapi/.*$") 31 if err != nil { 32 panic(err) 33 } 34 } 35 36 type PipelineRun struct { 37 Job *Job 38 Base string 39 URLs map[string]map[string]string `json:"_links"` 40 ID string 41 Name string 42 Status string 43 StartTime int64 `json:"startTimeMillis"` 44 EndTime int64 `json:"endTimeMillis"` 45 Duration int64 `json:"durationMillis"` 46 Stages []PipelineNode 47 } 48 49 type PipelineNode struct { 50 Run *PipelineRun 51 Base string 52 URLs map[string]map[string]string `json:"_links"` 53 ID string 54 Name string 55 Status string 56 StartTime int64 `json:"startTimeMillis"` 57 Duration int64 `json:"durationMillis"` 58 StageFlowNodes []PipelineNode 59 ParentNodes []int64 60 } 61 62 type PipelineInputAction struct { 63 ID string 64 Message string 65 ProceedURL string 66 AbortURL string 67 } 68 69 type PipelineArtifact struct { 70 ID string 71 Name string 72 Path string 73 URL string 74 size int 75 } 76 77 type PipelineNodeLog struct { 78 NodeID string 79 NodeStatus string 80 Length int64 81 HasMore bool 82 Text string 83 ConsoleURL string 84 } 85 86 // utility function to fill in the Base fields under PipelineRun 87 func (run *PipelineRun) update() { 88 href := run.URLs["self"]["href"] 89 if matches := baseURLRegex.FindStringSubmatch(href); len(matches) > 1 { 90 run.Base = matches[1] 91 } 92 for i := range run.Stages { 93 run.Stages[i].Run = run 94 href := run.Stages[i].URLs["self"]["href"] 95 if matches := baseURLRegex.FindStringSubmatch(href); len(matches) > 1 { 96 run.Stages[i].Base = matches[1] 97 } 98 } 99 } 100 101 func (job *Job) GetPipelineRuns(ctx context.Context) (pr []PipelineRun, err error) { 102 _, err = job.Jenkins.Requester.GetJSON(ctx, job.Base+"/wfapi/runs", &pr, nil) 103 if err != nil { 104 return nil, err 105 } 106 for i := range pr { 107 pr[i].update() 108 pr[i].Job = job 109 } 110 111 return pr, nil 112 } 113 114 func (job *Job) GetPipelineRun(ctx context.Context, id string) (pr *PipelineRun, err error) { 115 pr = new(PipelineRun) 116 href := job.Base + "/" + id + "/wfapi/describe" 117 _, err = job.Jenkins.Requester.GetJSON(ctx, href, pr, nil) 118 if err != nil { 119 return nil, err 120 } 121 pr.update() 122 pr.Job = job 123 124 return pr, nil 125 } 126 127 func (pr *PipelineRun) GetPendingInputActions(ctx context.Context) (PIAs []PipelineInputAction, err error) { 128 PIAs = make([]PipelineInputAction, 0, 1) 129 href := pr.Base + "/wfapi/pendingInputActions" 130 _, err = pr.Job.Jenkins.Requester.GetJSON(ctx, href, &PIAs, nil) 131 if err != nil { 132 return nil, err 133 } 134 135 return PIAs, nil 136 } 137 138 func (pr *PipelineRun) GetArtifacts(ctx context.Context) (artifacts []PipelineArtifact, err error) { 139 artifacts = make([]PipelineArtifact, 0, 0) 140 href := pr.Base + "/wfapi/artifacts" 141 _, err = pr.Job.Jenkins.Requester.GetJSON(ctx, href, artifacts, nil) 142 if err != nil { 143 return nil, err 144 } 145 146 return artifacts, nil 147 } 148 149 func (pr *PipelineRun) GetNode(ctx context.Context, id string) (node *PipelineNode, err error) { 150 node = new(PipelineNode) 151 href := pr.Base + "/execution/node/" + id + "/wfapi/describe" 152 _, err = pr.Job.Jenkins.Requester.GetJSON(ctx, href, node, nil) 153 if err != nil { 154 return nil, err 155 } 156 157 return node, nil 158 } 159 160 func (node *PipelineNode) GetLog(ctx context.Context) (log *PipelineNodeLog, err error) { 161 log = new(PipelineNodeLog) 162 href := node.Base + "/wfapi/log" 163 fmt.Println(href) 164 _, err = node.Run.Job.Jenkins.Requester.GetJSON(ctx, href, log, nil) 165 if err != nil { 166 return nil, err 167 } 168 169 return log, nil 170 }