forked from ashwanthkumar/go-gocd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
132 lines (114 loc) · 4.29 KB
/
jobs.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
package gocd
import (
"encoding/json"
"encoding/xml"
"fmt"
multierror "github.com/hashicorp/go-multierror"
)
// Job definition used also by other elements like the pipeline and stages
type Job struct {
Name string `json:"name"`
Result string `json:"result"`
State string `json:"state"`
ScheduledDate int64 `json:"scheduled_date"`
}
// ScheduledJobResource wrapper for resources > resource
type ScheduledJobResource struct {
Name string `xml:",chardata"`
}
// ScheduledJob instance
type ScheduledJob struct {
Name string `xml:"name,attr"`
JobID string `xml:"id,attr"`
BuildLocator string `xml:"buildLocator"`
Link LinkInXML `xml:"link"`
Environment string `xml:"environment,omitempty"`
RawResources []ScheduledJobResource `xml:"resources>resource,omitempty"`
}
// Resources - return resources as []string
func (sj *ScheduledJob) Resources() []string {
stringSlice := make([]string, len(sj.RawResources))
for index, resource := range sj.RawResources {
stringSlice[index] = resource.Name
}
return stringSlice
}
// JobURL - Full URL location of the scheduled job
func (sj *ScheduledJob) JobURL() string {
return sj.Link.Href
}
// LinkInXML - <link rel="..." href="..."> tag
type LinkInXML struct {
Rel string `xml:"rel,attr"`
Href string `xml:"href,attr"`
}
// GetScheduledJobs - Lists all the current job instances which are scheduled but not yet assigned to any agent.
func (c *DefaultClient) GetScheduledJobs() ([]*ScheduledJob, error) {
var errors *multierror.Error
type ScheduledJobsResponse struct {
XMLName xml.Name `xml:"scheduledJobs"`
Jobs []*ScheduledJob `xml:"job"`
}
var jobs ScheduledJobsResponse
_, body, errs := c.Request.
Get(c.resolve("/go/api/jobs/scheduled.xml")).
End()
if errs != nil {
errors = multierror.Append(errors, errs...)
return []*ScheduledJob{}, errors.ErrorOrNil()
}
xmlErr := xml.Unmarshal([]byte(body), &jobs)
if xmlErr != nil {
errors = multierror.Append(errors, xmlErr)
}
return jobs.Jobs, errors.ErrorOrNil()
}
// JobHistory - Represents an instance of a job from the past
type JobHistory struct {
AgentUUID string `json:"agent_uuid"`
Name string `json:"name"`
JobStateTransitions []JobStateTransition `json:"job_state_transitions"`
ScheduledDate int `json:"scheduled_date"`
OriginalJobID string `json:"original_job_id"`
PipelineCounter int `json:"pipeline_counter"`
PipelineName string `json:"pipeline_name"`
Result string `json:"result"`
State string `json:"state"`
ID int `json:"id"`
StageCounter string `json:"stage_counter"`
StageName string `json:"stage_name"`
ReRun bool `json:"rerun"`
}
// JobStateTransition - Represents an instance of StateTransition the job went through
type JobStateTransition struct {
StateChangeTime int `json:"state_change_time,omitempty"`
ID int `json:"id,omitempty"`
State string `json:"state,omitempty"`
}
type JobRunHistory struct {
Jobs []*JobHistory `json:"jobs"`
Pagination Pagination `json:"pagination"`
}
// GetJobHistory - The job history allows users to list job instances of specified job. Supports pagination using offset which tells the API how many instances to skip.
func (c *DefaultClient) GetJobHistory(pipeline, stage, job string, offset int) ([]*JobHistory, error) {
var errors *multierror.Error
_, body, errs := c.Request.
Get(c.resolve(fmt.Sprintf("/go/api/jobs/%s/%s/%s/history/%d", pipeline, stage, job, offset))).
// 18.6.0: providing API version here results in "resource not found"
Set("Accept", "application/json").
End()
if errs != nil {
errors = multierror.Append(errors, errs...)
return []*JobHistory{}, errors.ErrorOrNil()
}
type JobHistoryResponse struct {
Jobs []*JobHistory `json:"jobs"`
}
jobs := JobHistoryResponse{}
jsonErr := json.Unmarshal([]byte(body), &jobs)
if jsonErr != nil {
errors = multierror.Append(errors, jsonErr)
return []*JobHistory{}, errors.ErrorOrNil()
}
return jobs.Jobs, nil
}