Skip to content

Commit 8c964e1

Browse files
committed
feat(build): add get build api
1 parent 72a03dc commit 8c964e1

2 files changed

Lines changed: 44 additions & 77 deletions

File tree

builds/builds.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,31 @@ import (
99
"github.com/drycc/controller-sdk-go/api"
1010
)
1111

12-
// List lists an app's builds.
13-
func List(c *drycc.Client, appID string, results int) ([]api.Build, int, error) {
14-
u := fmt.Sprintf("/v2/apps/%s/builds/", appID)
15-
body, count, reqErr := c.LimitedRequest(u, results)
12+
// Get a build of an app.
13+
func Get(c *drycc.Client, appID string, version int) (api.Build, error) {
14+
u := fmt.Sprintf("/v2/apps/%s/build/", appID)
15+
if version > 0 {
16+
u = fmt.Sprintf("%s?version=v%d", u, version)
17+
}
1618

19+
res, reqErr := c.Request("GET", u, nil)
1720
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
18-
return []api.Build{}, -1, reqErr
21+
return api.Build{}, reqErr
1922
}
23+
defer res.Body.Close()
2024

21-
var builds []api.Build
22-
if err := json.Unmarshal([]byte(body), &builds); err != nil {
23-
return []api.Build{}, -1, err
25+
var build api.Build
26+
if err := json.NewDecoder(res.Body).Decode(&build); err != nil {
27+
return api.Build{}, err
2428
}
25-
26-
return builds, count, reqErr
29+
return build, reqErr
2730
}
2831

29-
// New creates a build for an app from an container image.
30-
// By default this will create a cmd process that runs the CMD command from the Dockerfile.
31-
// If you want to define more process types, you can pass a Procfile map,
32-
// where the key is the process name and the value is the command for that process.
33-
// To pull from a private container registry, a custom username and password must be set in the app's
34-
// configuration object. This can be done with `drycc registry:set` or by using this SDK.
35-
//
36-
// This example adds custom registry credentials to an app:
37-
//
38-
// import (
39-
// "github.com/drycc/controller-sdk-go/api"
40-
// "github.com/drycc/controller-sdk-go/config"
41-
// )
42-
//
43-
// // Create username/password map
44-
// registryMap := map[string]string{
45-
// "username": "password"
46-
// }
47-
//
48-
// // Create a new configuration, assign the credentials, and set it.
49-
// // Note that config setting is a patching operation, it doesn't overwrite or unset
50-
// // unrelated configuration.
51-
// newConfig := api.Config{}
52-
// newConfig.Registry = registryMap
53-
// _, err := config.Set(<client>, "appname", newConfig)
54-
// if err != nil {
55-
// log.Fatal(err)
56-
// }
32+
// New a build of an app.
5733
func New(c *drycc.Client, appID string, image string, stack string,
5834
procfile map[string]string, dryccfile map[string]interface{}) (api.Build, error) {
5935

60-
u := fmt.Sprintf("/v2/apps/%s/builds/", appID)
36+
u := fmt.Sprintf("/v2/apps/%s/build/", appID)
6137

6238
req := api.CreateBuildRequest{
6339
Image: image,

builds/builds_test.go

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@ import (
1414

1515
const buildsFixture string = `
1616
{
17-
"count": 1,
18-
"next": null,
19-
"previous": null,
20-
"results": [
21-
{
22-
"app": "example-go",
23-
"created": "2014-01-01T00:00:00UTC",
24-
"dockerfile": "FROM drycc/slugrunner RUN mkdir -p /app WORKDIR /app ENTRYPOINT [\"/runner/init\"] ADD slug.tgz /app ENV GIT_SHA 060da68f654e75fac06dbedd1995d5f8ad9084db",
25-
"image": "example-go",
26-
"stack": "container",
27-
"owner": "test",
28-
"procfile": {
29-
"web": "example-go"
30-
},
31-
"dryccfile": {},
32-
"sha": "060da68f",
33-
"updated": "2014-01-01T00:00:00UTC",
34-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
35-
}
36-
]
17+
"app": "example-go",
18+
"created": "2014-01-01T00:00:00UTC",
19+
"dockerfile": "FROM drycc/slugrunner RUN mkdir -p /app WORKDIR /app ENTRYPOINT [\"/runner/init\"] ADD slug.tgz /app ENV GIT_SHA 060da68f654e75fac06dbedd1995d5f8ad9084db",
20+
"image": "example-go",
21+
"stack": "container",
22+
"owner": "test",
23+
"procfile": {
24+
"web": "example-go"
25+
},
26+
"dryccfile": {},
27+
"sha": "060da68f",
28+
"updated": "2014-01-01T00:00:00UTC",
29+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
3730
}`
3831

3932
const buildFixture string = `
@@ -60,12 +53,12 @@ type fakeHTTPServer struct{}
6053
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
6154
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
6255

63-
if req.URL.Path == "/v2/apps/example-go/builds/" && req.Method == "GET" {
56+
if req.URL.Path == "/v2/apps/example-go/build/" && req.Method == "GET" {
6457
res.Write([]byte(buildsFixture))
6558
return
6659
}
6760

68-
if req.URL.Path == "/v2/apps/example-go/builds/" && req.Method == "POST" {
61+
if req.URL.Path == "/v2/apps/example-go/build/" && req.Method == "POST" {
6962
body, err := io.ReadAll(req.Body)
7063

7164
if err != nil {
@@ -91,23 +84,21 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
9184
res.Write(nil)
9285
}
9386

94-
func TestBuildsList(t *testing.T) {
87+
func TestBuildsGet(t *testing.T) {
9588
t.Parallel()
9689

97-
expected := []api.Build{
98-
{
99-
App: "example-go",
100-
Created: "2014-01-01T00:00:00UTC",
101-
Dockerfile: "FROM drycc/slugrunner RUN mkdir -p /app WORKDIR /app ENTRYPOINT [\"/runner/init\"] ADD slug.tgz /app ENV GIT_SHA 060da68f654e75fac06dbedd1995d5f8ad9084db",
102-
Image: "example-go",
103-
Stack: "container",
104-
Owner: "test",
105-
Procfile: map[string]string{"web": "example-go"},
106-
Dryccfile: map[string]interface{}{},
107-
Sha: "060da68f",
108-
Updated: "2014-01-01T00:00:00UTC",
109-
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
110-
},
90+
expected := api.Build{
91+
App: "example-go",
92+
Created: "2014-01-01T00:00:00UTC",
93+
Dockerfile: "FROM drycc/slugrunner RUN mkdir -p /app WORKDIR /app ENTRYPOINT [\"/runner/init\"] ADD slug.tgz /app ENV GIT_SHA 060da68f654e75fac06dbedd1995d5f8ad9084db",
94+
Image: "example-go",
95+
Stack: "container",
96+
Owner: "test",
97+
Procfile: map[string]string{"web": "example-go"},
98+
Dryccfile: map[string]interface{}{},
99+
Sha: "060da68f",
100+
Updated: "2014-01-01T00:00:00UTC",
101+
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
111102
}
112103

113104
handler := fakeHTTPServer{}
@@ -119,7 +110,7 @@ func TestBuildsList(t *testing.T) {
119110
t.Fatal(err)
120111
}
121112

122-
actual, _, err := List(drycc, "example-go", 100)
113+
actual, err := Get(drycc, "example-go", -1)
123114

124115
if err != nil {
125116
t.Fatal(err)

0 commit comments

Comments
 (0)