/
artifact_service.go
114 lines (92 loc) · 2.87 KB
/
artifact_service.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
package octopusdeploy
import (
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
)
// actionTemplateService handles communication for any operations in the
// Octopus API that pertain to artifacts.
type artifactService struct {
canDeleteService
}
// newArtifactService returns an artifactService with a preconfigured client.
func newArtifactService(sling *sling.Sling, uriTemplate string) *artifactService {
artifactService := &artifactService{}
artifactService.service = newService(ServiceArtifactService, sling, uriTemplate)
return artifactService
}
func (s artifactService) getPagedResponse(path string) ([]*Artifact, error) {
resources := []*Artifact{}
loadNextPage := true
for loadNextPage {
resp, err := apiGet(s.getClient(), new(Artifacts), path)
if err != nil {
return resources, err
}
responseList := resp.(*Artifacts)
resources = append(resources, responseList.Items...)
path, loadNextPage = LoadNextPage(responseList.PagedResults)
}
return resources, nil
}
// Add creates a new artifact.
func (s artifactService) Add(resource *Artifact) (*Artifact, error) {
path, err := getAddPath(s, resource)
if err != nil {
return nil, err
}
resp, err := apiAdd(s.getClient(), resource, new(Artifact), path)
if err != nil {
return nil, err
}
return resp.(*Artifact), nil
}
// Get returns a collection of artifacts based on the criteria defined by its
// input query parameter. If an error occurs, an empty collection is returned
// along with the associated error.
func (s artifactService) Get(artifactsQuery ArtifactsQuery) (*Artifacts, error) {
v, _ := query.Values(artifactsQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}
resp, err := apiGet(s.getClient(), new(Artifacts), path)
if err != nil {
return &Artifacts{}, err
}
return resp.(*Artifacts), nil
}
// GetAll returns all artifacts. If none can be found or an error occurs, it
// returns an empty collection.
func (s artifactService) GetAll() ([]*Artifact, error) {
path, err := getPath(s)
if err != nil {
return []*Artifact{}, err
}
return s.getPagedResponse(path)
}
// GetByID returns the artifact that matches the input ID. If one cannot be
// found, it returns nil and an error.
func (s artifactService) GetByID(id string) (*Artifact, error) {
path, err := getByIDPath(s, id)
if err != nil {
return nil, err
}
resp, err := apiGet(s.getClient(), new(Artifact), path)
if err != nil {
return nil, err
}
return resp.(*Artifact), nil
}
// Update modifies an Artifact based on the one provided as input.
func (s artifactService) Update(artifact Artifact) (*Artifact, error) {
path, err := getUpdatePath(s, &artifact)
if err != nil {
return nil, err
}
resp, err := apiUpdate(s.getClient(), artifact, new(Artifact), path)
if err != nil {
return nil, err
}
return resp.(*Artifact), nil
}