mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Issue #600 points out Jenkins module panics occasionally I noticed that a number of places panic because of JSON parsing Centralize JSON parsing, have it return an error, and have widgets report the error
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package travisci
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/wtfutil/wtf/utils"
|
|
)
|
|
|
|
var TRAVIS_HOSTS = map[bool]string{
|
|
false: "travis-ci.org",
|
|
true: "travis-ci.com",
|
|
}
|
|
|
|
func BuildsFor(settings *Settings) (*Builds, error) {
|
|
builds := &Builds{}
|
|
|
|
travisAPIURL.Host = "api." + TRAVIS_HOSTS[settings.pro]
|
|
|
|
resp, err := travisBuildRequest(settings)
|
|
if err != nil {
|
|
return builds, err
|
|
}
|
|
|
|
err = utils.ParseJson(&builds, resp.Body)
|
|
if err != nil {
|
|
return builds, err
|
|
}
|
|
|
|
return builds, nil
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
var (
|
|
travisAPIURL = &url.URL{Scheme: "https", Path: "/"}
|
|
)
|
|
|
|
func travisBuildRequest(settings *Settings) (*http.Response, error) {
|
|
var path string = "builds"
|
|
params := url.Values{}
|
|
params.Add("limit", settings.limit)
|
|
params.Add("sort_by", settings.sort_by)
|
|
|
|
requestUrl := travisAPIURL.ResolveReference(&url.URL{Path: path, RawQuery: params.Encode()})
|
|
|
|
req, err := http.NewRequest("GET", requestUrl.String(), nil)
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Travis-API-Version", "3")
|
|
|
|
bearer := fmt.Sprintf("token %s", settings.apiKey)
|
|
req.Header.Add("Authorization", bearer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpClient := &http.Client{}
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return nil, fmt.Errorf(resp.Status)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|