/
cloudsmith.go
277 lines (214 loc) · 6.42 KB
/
cloudsmith.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package cloudsmith
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/cloudsmith-io/cloudsmith-api/bindings/go/src"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
)
type Error struct {
Detail string `json:"detail"`
}
type Client struct {
Files cloudsmith_api.FilesApi
Packages cloudsmith_api.PackagesApi
KnownVersions []string
}
func NewClient(apiKey string) *Client {
configuration := cloudsmith_api.NewConfiguration()
configuration.AddDefaultHeader("X-Api-Key", apiKey)
return &Client{
Files: cloudsmith_api.FilesApi{
Configuration: configuration,
},
Packages: cloudsmith_api.PackagesApi{
Configuration: configuration,
},
}
}
func (c *Client) UploadComposerPackage(owner, repo, artifactPath string) (csPkg *cloudsmith_api.ModelPackage, error error) {
fileName := filepath.Base(artifactPath)
// Get upload details from Cloudsmith (which is a pre-signed s3 upload)
upload, rawUpload, err := c.Files.FilesCreate(owner, repo, cloudsmith_api.FilesCreate{
Filename: fileName,
Md5Checksum: calculateMd5Checksum(artifactPath),
})
if err := checkForCloudsmithRequestError(rawUpload, err); err != nil {
return csPkg, err
}
// Convert the upload interface{} to map[string]string
params := getParams(upload.UploadFields)
// Prepare request to upload to S3 based on data given from Cloudsmith
req, err := newS3UploadRequest(upload.UploadUrl, params, "file", artifactPath)
if err != nil {
return csPkg, err
}
// Perform the upload
resp, err := http.DefaultClient.Do(req)
if err != nil {
return csPkg, err
}
if resp.StatusCode >= 300 {
return csPkg, errors.New("s3 file upload failed")
}
// Alright, the file uploaded, now to create a package on Cloudsmith and
// link it to the file
pkg, rawPkg, err := c.Packages.PackagesUploadComposer(owner, repo, cloudsmith_api.PackagesUploadComposer{
PackageFile: upload.Identifier,
})
if err := checkForCloudsmithRequestError(rawPkg, err); err != nil {
return csPkg, err
}
return pkg, nil
}
func (c *Client) LoadPackages(owner, repo string) error {
pageSize := 100
page := 1
for {
pkgs, rawList, err := c.Packages.PackagesList(owner, repo, int32(page), int32(pageSize), "status:completed format:composer")
if err := checkForCloudsmithRequestError(rawList, err); err != nil {
// If the error is because of a 404, we've reached the end of the list!
if rawList.StatusCode == 404 {
break
}
return err
}
for _, pkg := range pkgs {
c.KnownVersions = append(c.KnownVersions, pkg.Name+":"+pkg.Version)
}
if len(pkgs) < pageSize {
break
}
page++
}
return nil
}
func (c *Client) RemoteCheckPackageExists(owner, repo, name, version string) (bool, error) {
searchTerm := fmt.Sprintf("name:%s version:%s format:composer", name, version)
pkgs, rawList, err := c.Packages.PackagesList(owner, repo, 1, 1, searchTerm)
if err := checkForCloudsmithRequestError(rawList, err); err != nil {
// If the error is because of a 404, we've reached the end of the list! or there is nothing to deal with
if rawList.StatusCode == 404 {
return false, nil
}
return false, err
}
return len(pkgs) != 0, nil
}
func (c *Client) DeletePackageIfExists(owner, repo, name, version string) error {
searchTerm := fmt.Sprintf("name:%s version:%s status:completed format:composer", name, version)
pkgs, rawList, err := c.Packages.PackagesList(owner, repo, 1, 1, searchTerm)
if err := checkForCloudsmithRequestError(rawList, err); err != nil {
// If the error is because of a 404, we've reached the end of the list! or there is nothing to deal with
if rawList.StatusCode == 404 {
return nil
}
return err
}
if len(pkgs) == 0 {
return nil
}
// Delete the first matching version
pkg := pkgs[0]
c.Packages.PackagesDelete(owner, repo, strconv.Itoa(int(pkg.Identifier)))
return nil
}
func (c *Client) RetryFailed(owner, repo string) error {
pkgs, rawList, err := c.Packages.PackagesList(owner, repo, 1, 100, "status:failed format:composer")
if err := checkForCloudsmithRequestError(rawList, err); err != nil {
// If the error is because of a 404, we've reached the end of the list! or there is nothing to deal with
if rawList.StatusCode != 404 {
return nil
}
}
if len(pkgs) == 0 {
return nil
}
for _, pkg := range pkgs {
c.Packages.PackagesResync(owner, repo, strconv.Itoa(int(pkg.Identifier)))
}
return nil
}
func (c *Client) IsAwareOfPackage(name string, version string) bool {
for _, knownVersion := range c.KnownVersions {
if knownVersion == name+":"+version {
return true
}
}
return false
}
func checkForCloudsmithRequestError(response *cloudsmith_api.APIResponse, err error) error {
// just straight up return err if it isn't nil
if err != nil {
return err
}
// Check for 4xx 5xx responses as those *should* hopefully be in the error
// format described in their documentation :)
if response.StatusCode >= 400 {
var cmError Error
json.Unmarshal(response.Payload, cmError)
return errors.New(cmError.Detail)
}
if response.StatusCode >= 300 && response.StatusCode < 400 {
return errors.New("api request got a redirect back for some reason")
}
if response.StatusCode < 200 {
return errors.New("request got a status code the 100 range")
}
// All fine :D
return nil
}
func calculateMd5Checksum(filePath string) string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(h.Sum(nil))
}
func getParams(fields interface{}) map[string]string {
params := make(map[string]string)
for key, value := range fields.(map[string]interface{}) {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value)
params[strKey] = strValue
}
return params
}
func newS3UploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for key, val := range params {
_ = writer.WriteField(key, val)
}
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}