-
Notifications
You must be signed in to change notification settings - Fork 13
/
pipeline_group.go
75 lines (64 loc) · 2.42 KB
/
pipeline_group.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
package gocd
import (
"encoding/json"
multierror "github.com/hashicorp/go-multierror"
)
// Pipeline Object
type Pipeline struct {
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Materials []Material `json:"materials,omitempty"`
Stages []string `json:"stages,omitempty"`
}
// PipelineGroup Object
type PipelineGroup struct {
Name string `json:"name,omitempty"`
Pipelines []Pipeline `json:"pipelines,omitempty"`
}
// GetPipelineGroups List pipeline groups along with the pipelines, stages and materials for each pipeline.
func (c *DefaultClient) GetPipelineGroups() ([]*PipelineGroup, error) {
var errors *multierror.Error
// Somehow GoCD will return "The resource you requested was not found!" if you specify an Accept header
_, body, errs := c.Request.
Get(c.resolve("/go/api/config/pipeline_groups")).
//Set("Accept", "application/vnd.go.cd.v2+json").
End()
if errs != nil {
errors = multierror.Append(errors, errs...)
return []*PipelineGroup{}, errors.ErrorOrNil()
}
// first parse the json into temporary structure, so we parse stages object
// with a single name string attribute as simple string
type tmpStage struct {
Name string `json:"name,omitempty"`
}
type tmpPipeline struct {
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Materials []Material `json:"materials,omitempty"`
Stages []tmpStage `json:"stages,omitempty"`
}
type tmpPipelineGroup struct {
Name string `json:"name,omitempty"`
Pipelines []tmpPipeline `json:"pipelines,omitempty"`
}
var tmpPipelineGroups []tmpPipelineGroup
jsonErr := json.Unmarshal([]byte(body), &tmpPipelineGroups)
if jsonErr != nil {
errors = multierror.Append(errors, jsonErr)
return []*PipelineGroup{}, errors.ErrorOrNil()
}
// create the good pipeline groups structures and copy data from the temporary structs
pipelineGroups := make([]*PipelineGroup, len(tmpPipelineGroups))
for i, pg := range tmpPipelineGroups {
pipelineGroups[i] = &PipelineGroup{Name: pg.Name}
pipelineGroups[i].Pipelines = make([]Pipeline, len(pg.Pipelines))
for j, p := range pg.Pipelines {
pipelineGroups[i].Pipelines[j] = Pipeline{Name: p.Name, Label: p.Label, Materials: p.Materials, Stages: make([]string, len(p.Stages))}
for k, s := range p.Stages {
pipelineGroups[i].Pipelines[j].Stages[k] = s.Name
}
}
}
return pipelineGroups, errors.ErrorOrNil()
}