-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhooks.go
More file actions
86 lines (71 loc) · 2.42 KB
/
hooks.go
File metadata and controls
86 lines (71 loc) · 2.42 KB
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
// Package hooks implements the controller's builder hooks api.
//
// This is primarily intended to be consumed by the builder to communicate with the controller.
package hooks
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// UserFromKey retrives a user from their SSH key fingerprint.
func UserFromKey(c *drycc.Client, fingerprint string) (api.UserApps, error) {
res, reqErr := c.Request("GET", fmt.Sprintf("/v2/hooks/key/%s", fingerprint), nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.UserApps{}, reqErr
}
defer res.Body.Close()
resUser := api.UserApps{}
if err := json.NewDecoder(res.Body).Decode(&resUser); err != nil {
return api.UserApps{}, err
}
return resUser, reqErr
}
// GetAppConfig retrives an app's configuration from the controller.
func GetAppConfig(c *drycc.Client, username, app string) (api.Config, error) {
req := api.ConfigHookRequest{User: username, App: app}
b, err := json.Marshal(req)
if err != nil {
return api.Config{}, err
}
res, reqErr := c.Request("POST", "/v2/hooks/config/", b)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Config{}, reqErr
}
defer res.Body.Close()
config := api.Config{}
if err := json.NewDecoder(res.Body).Decode(&config); err != nil {
return api.Config{}, err
}
return config, reqErr
}
// CreateBuild creates a new release of an application. It returns the version of the new release.
// gitSha should be the first 8 characters of the git commit sha. Image is either the container image
// location for the dockerfile app the absolute url to the tar file for a buldpack app.
func CreateBuild(c *drycc.Client, username, app, image, stack, gitSha string,
procfile api.ProcessType, dryccfile map[string]interface{}, dockerfile string) (int, error) {
req := api.BuildHookRequest{
Sha: gitSha,
User: username,
App: app,
Image: image,
Stack: stack,
Procfile: procfile,
Dockerfile: dockerfile,
Dryccfile: dryccfile,
}
b, err := json.Marshal(req)
if err != nil {
return -1, err
}
res, reqErr := c.Request("POST", "/v2/hooks/build/", b)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return -1, reqErr
}
defer res.Body.Close()
resMap := make(map[string]map[string]int)
if err := json.NewDecoder(res.Body).Decode(&resMap); err != nil {
return -1, err
}
return resMap["release"]["version"], reqErr
}