/
dependency.go
113 lines (92 loc) · 3.46 KB
/
dependency.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
package teamcity
import (
"errors"
"fmt"
"net/http"
"github.com/dghubble/sling"
)
//DependencyService provides operations for managing dependencies for a buildType
type DependencyService struct {
BuildTypeID string
httpClient *http.Client
artifactSling *sling.Sling
snapshotSling *sling.Sling
artifactHelper *restHelper
snapshotHelper *restHelper
}
//NewDependencyService constructs and instance of DependencyService scoped to a given buildTypeId
func NewDependencyService(buildTypeID string, c *http.Client, base *sling.Sling) *DependencyService {
artifactSling := base.New().Path(fmt.Sprintf("buildTypes/%s/artifact-dependencies/", buildTypeID))
snapshotSling := base.New().Path(fmt.Sprintf("buildTypes/%s/snapshot-dependencies/", buildTypeID))
return &DependencyService{
BuildTypeID: buildTypeID,
httpClient: c,
artifactSling: artifactSling,
snapshotSling: snapshotSling,
artifactHelper: newRestHelperWithSling(c, artifactSling),
snapshotHelper: newRestHelperWithSling(c, snapshotSling),
}
}
//AddSnapshotDependency adds a new snapshot dependency to build type
func (s *DependencyService) AddSnapshotDependency(dep *SnapshotDependency) (*SnapshotDependency, error) {
var out SnapshotDependency
if dep == nil {
return nil, errors.New("dep can't be nil")
}
resp, err := s.snapshotSling.New().Post("").BodyJSON(dep).ReceiveSuccess(&out)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Unknown error when adding snapshot dependency, statusCode: %d", resp.StatusCode)
}
out.BuildTypeID = s.BuildTypeID
return &out, nil
}
//AddArtifactDependency adds a new artifact dependency to build type
func (s *DependencyService) AddArtifactDependency(dep *ArtifactDependency) (*ArtifactDependency, error) {
var out ArtifactDependency
if dep == nil {
return nil, errors.New("dep can't be nil")
}
resp, err := s.artifactSling.New().Post("").BodyJSON(dep).ReceiveSuccess(&out)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Unknown error when adding artifact dependency, statusCode: %d", resp.StatusCode)
}
out.SetBuildTypeID(s.BuildTypeID)
return &out, nil
}
//GetSnapshotByID returns a snapshot dependency by its id
func (s *DependencyService) GetSnapshotByID(depID string) (*SnapshotDependency, error) {
var out SnapshotDependency
resp, err := s.snapshotSling.New().Get(depID).ReceiveSuccess(&out)
if resp.StatusCode == 404 {
return nil, fmt.Errorf("404 Not Found - Snapshot dependency (id: %s) for buildTypeId (id: %s) was not found", depID, s.BuildTypeID)
}
if err != nil {
return nil, err
}
out.BuildTypeID = s.BuildTypeID
return &out, nil
}
//GetArtifactByID returns an artifact dependency by its id
func (s *DependencyService) GetArtifactByID(depID string) (*ArtifactDependency, error) {
var out ArtifactDependency
err := s.artifactHelper.get(depID, &out, "artifact dependency")
if err != nil {
return nil, err
}
out.SetBuildTypeID(s.BuildTypeID)
return &out, nil
}
//DeleteSnapshot removes a snapshot dependency from the build configuration by its id
func (s *DependencyService) DeleteSnapshot(depID string) error {
return s.snapshotHelper.deleteByIDWithSling(s.snapshotSling, depID, "snapshot dependency")
}
//DeleteArtifact removes an artifact dependency from the build configuration by its id
func (s *DependencyService) DeleteArtifact(depID string) error {
return s.artifactHelper.deleteByIDWithSling(s.artifactSling, depID, "artifact dependency")
}