forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipelines.go
73 lines (59 loc) · 1.89 KB
/
pipelines.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
package api
import (
"bytes"
"fmt"
"mime/multipart"
"path/filepath"
"github.com/buildkite/agent/mime"
)
// PipelinesService handles communication with the pipeline related methods of the
// Buildkite Agent API.
type PipelinesService struct {
client *Client
}
// Pipeline represents a Buildkite Agent API Pipeline
type Pipeline struct {
UUID string
Data []byte
FileName string
Replace bool
}
// Uploads the pipeline to the Buildkite Agent API. This request doesn't use JSON,
// but a multi-part HTTP form upload
func (cs *PipelinesService) Upload(jobId string, pipeline *Pipeline) (*Response, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Default the filename
fileName := pipeline.FileName
if fileName == "" {
fileName = "pipeline"
}
// Calculate the mime type based on the filename
extension := filepath.Ext(fileName)
contentType := mime.TypeByExtension(extension)
if contentType == "" {
contentType = "binary/octet-stream"
}
// Write the pipeline to the form
part, _ := createFormFileWithContentType(writer, "pipeline", fileName, contentType)
part.Write([]byte(pipeline.Data))
// Add the replace option
writer.WriteField("replace", fmt.Sprintf("%t", pipeline.Replace))
// The pipeline upload endpoint requires a way for it to uniquely
// identify this upload (because it's an idempotent endpoint). If a job
// tries to upload a pipeline that matches a previously uploaded one
// with a matching uuid, then it'll just return and not do anything.
writer.WriteField("uuid", pipeline.UUID)
// Close the writer because we don't need to add any more values to it
err := writer.Close()
if err != nil {
return nil, err
}
u := fmt.Sprintf("jobs/%s/pipelines", jobId)
req, err := cs.client.NewFormRequest("POST", u, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
return cs.client.Do(req, nil)
}