11package gitreceive
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "errors"
57 "fmt"
8+ "net/http"
69 "strings"
10+
11+ "github.com/deis/builder/pkg"
712)
813
914var (
@@ -28,3 +33,116 @@ func (u unexpectedControllerStatusCode) Error() string {
2833func controllerURLStr (conf * Config , additionalPath ... string ) string {
2934 return fmt .Sprintf ("http://%s:%s/%s" , conf .WorkflowHost , conf .WorkflowPort , strings .Join (additionalPath , "/" ))
3035}
36+
37+ func setReqHeaders (builderKey string , req * http.Request ) {
38+ req .Header .Add ("Content-Type" , contentType )
39+ req .Header .Add ("Accept" , contentType )
40+ req .Header .Add ("User-Agent" , userAgent )
41+ req .Header .Add ("X-Deis-Builder-Auth" , builderKey )
42+ }
43+
44+ func getAppConfig (conf * Config , builderKey , userName , appName string ) (* pkg.Config , error ) {
45+ data , err := json .Marshal (& pkg.ConfigHook {
46+ ReceiveUser : userName ,
47+ ReceiveRepo : appName ,
48+ })
49+
50+ if err != nil {
51+ return nil , err
52+ }
53+
54+ b := bytes .NewReader (data )
55+ url := controllerURLStr (conf , "v2" , "hooks" , "config" )
56+ req , err := http .NewRequest ("POST" , url , b )
57+
58+ if err != nil {
59+ return nil , err
60+ }
61+
62+ setReqHeaders (builderKey , req )
63+
64+ res , err := http .DefaultClient .Do (req )
65+ if err != nil {
66+ return nil , err
67+ }
68+ defer res .Body .Close ()
69+
70+ if res .StatusCode == 404 {
71+ return nil , errControllerNotFound
72+ } else if res .StatusCode != 200 {
73+ return nil , newUnexpectedControllerStatusCode (url , 200 , res .StatusCode )
74+ }
75+
76+ ret := & pkg.Config {}
77+ if err := json .NewDecoder (res .Body ).Decode (ret ); err != nil {
78+ return nil , err
79+ }
80+ return ret , nil
81+ }
82+
83+ func publishRelease (conf * Config , builderKey string , buildHook * pkg.BuildHook ) (* pkg.BuildHookResponse , error ) {
84+
85+ var b bytes.Buffer
86+ if err := json .NewEncoder (& b ).Encode (buildHook ); err != nil {
87+ return nil , err
88+ }
89+ url := controllerURLStr (conf , "v2" , "hooks" , "build" )
90+ req , err := http .NewRequest ("POST" , url , & b )
91+ if err != nil {
92+ return nil , err
93+ }
94+ setReqHeaders (builderKey , req )
95+
96+ res , err := http .DefaultClient .Do (req )
97+ if err != nil {
98+ return nil , err
99+ }
100+
101+ defer res .Body .Close ()
102+
103+ if res .StatusCode == http .StatusNotFound {
104+ return nil , errControllerNotFound
105+ } else if res .StatusCode == http .StatusServiceUnavailable {
106+ return nil , errControllerServiceUnavailable
107+ } else if res .StatusCode != 200 {
108+ return nil , newUnexpectedControllerStatusCode (url , 200 , res .StatusCode )
109+ }
110+
111+ ret := new (pkg.BuildHookResponse )
112+ if err := json .NewDecoder (res .Body ).Decode (ret ); err != nil {
113+ return nil , err
114+ }
115+
116+ return ret , nil
117+ }
118+
119+ func receive (conf * Config , builderKey , gitSha string ) error {
120+ urlStr := controllerURLStr (conf , "v2" , "hooks" , "push" )
121+ bodyMap := map [string ]string {
122+ "receive_user" : conf .Username ,
123+ "receive_repo" : conf .App (),
124+ "sha" : gitSha ,
125+ "fingerprint" : conf .Fingerprint ,
126+ "ssh_connection" : conf .SSHConnection ,
127+ "ssh_original_command" : conf .SSHOriginalCommand ,
128+ }
129+ var body bytes.Buffer
130+ if err := json .NewEncoder (& body ).Encode (bodyMap ); err != nil {
131+ return err
132+ }
133+ req , err := http .NewRequest ("POST" , urlStr , & body )
134+ if err != nil {
135+ return err
136+ }
137+ setReqHeaders (builderKey , req )
138+
139+ // TODO: use ctxhttp here (https://godoc.org/golang.org/x/net/context/ctxhttp)
140+ resp , err := http .DefaultClient .Do (req )
141+ if err != nil {
142+ return err
143+ }
144+ if resp .StatusCode != 201 {
145+ return newUnexpectedControllerStatusCode (urlStr , 201 , resp .StatusCode )
146+ }
147+ return nil
148+ }
0 commit comments