2015-05-18 17:05:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-05-18 22:47:13 +00:00
|
|
|
"io/ioutil"
|
2015-05-18 17:05:58 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-05-18 22:47:13 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2015-05-18 17:05:58 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
logs "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
2015-05-18 17:05:58 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
|
|
|
)
|
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
type updater struct{}
|
2015-05-18 17:05:58 +00:00
|
|
|
|
|
|
|
func (u *updater) SetCommit(user *common.User, r *common.Repo, c *common.Commit) error {
|
2015-05-19 03:44:44 +00:00
|
|
|
path := fmt.Sprintf("/api/queue/push/%s", r.FullName)
|
2015-05-18 22:47:13 +00:00
|
|
|
return sendBackoff("POST", path, c, nil)
|
2015-05-18 17:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *updater) SetBuild(r *common.Repo, c *common.Commit, b *common.Build) error {
|
2015-05-19 03:44:44 +00:00
|
|
|
path := fmt.Sprintf("/api/queue/push/%s/%v", r.FullName, c.Sequence)
|
|
|
|
return sendBackoff("POST", path, b, nil)
|
2015-05-18 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *updater) SetLogs(r *common.Repo, c *common.Commit, b *common.Build, rc io.ReadCloser) error {
|
|
|
|
path := fmt.Sprintf("/api/queue/push/%s/%v/%v", r.FullName, c.Sequence, b.Sequence)
|
|
|
|
return sendBackoff("POST", path, rc, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendBackoff(method, path string, in, out interface{}) error {
|
|
|
|
var err error
|
|
|
|
var attempts int
|
|
|
|
for {
|
|
|
|
err = send(method, path, in, out)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2015-05-19 03:44:44 +00:00
|
|
|
if attempts > 99 {
|
2015-05-18 22:47:13 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
attempts++
|
|
|
|
time.Sleep(time.Second * 30)
|
2015-05-18 17:05:58 +00:00
|
|
|
}
|
2015-05-18 22:47:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// do makes an http.Request and returns the response
|
|
|
|
func send(method, path string, in, out interface{}) error {
|
2015-05-18 17:05:58 +00:00
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
// create the URI
|
|
|
|
uri, err := url.Parse(addr + path)
|
2015-05-18 17:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
if len(uri.Scheme) == 0 {
|
|
|
|
uri.Scheme = "http"
|
2015-05-18 17:05:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
params := uri.Query()
|
|
|
|
params.Add("token", token)
|
|
|
|
uri.RawQuery = params.Encode()
|
|
|
|
|
|
|
|
// create the request
|
|
|
|
req, err := http.NewRequest(method, uri.String(), nil)
|
2015-05-18 17:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-18 22:47:13 +00:00
|
|
|
req.ProtoAtLeast(1, 1)
|
|
|
|
req.Close = true
|
|
|
|
req.ContentLength = 0
|
|
|
|
|
|
|
|
// If the data is a readCloser we can attach directly
|
|
|
|
// to the request body.
|
|
|
|
//
|
|
|
|
// Else we serialize the data input as JSON.
|
|
|
|
if rc, ok := in.(io.ReadCloser); ok {
|
|
|
|
req.Body = rc
|
2015-05-18 17:05:58 +00:00
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
} else if in != nil {
|
|
|
|
inJson, err := json.Marshal(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(inJson)
|
|
|
|
req.Body = ioutil.NopCloser(buf)
|
|
|
|
|
|
|
|
req.ContentLength = int64(len(inJson))
|
|
|
|
req.Header.Set("Content-Length", strconv.Itoa(len(inJson)))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make the request using the default http client
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2015-05-18 17:05:58 +00:00
|
|
|
if err != nil {
|
2015-05-19 03:44:44 +00:00
|
|
|
logs.Errorf("Error posting request. %s", err)
|
2015-05-18 17:05:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
// Check for an http error status (ie not 200 StatusOK)
|
|
|
|
if resp.StatusCode > 300 {
|
2015-05-19 03:44:44 +00:00
|
|
|
logs.Errorf("Error status code %d", resp.StatusCode)
|
2015-05-18 22:47:13 +00:00
|
|
|
return fmt.Errorf(resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the JSON response
|
|
|
|
if out != nil {
|
2015-05-19 03:44:44 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(out)
|
2015-05-18 17:05:58 +00:00
|
|
|
}
|
2015-05-18 22:47:13 +00:00
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
return err
|
2015-05-18 17:05:58 +00:00
|
|
|
}
|