Skip to content

Commit

Permalink
fix(test): Fix test bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Jul 12, 2018
1 parent bb8f1a5 commit 7e8b560
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
7 changes: 6 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func PostJSON(endpoint *url.URL, apiKey string, body []byte, v interface{}) (sta
return jsonAPIRequest(http.MethodPost, endpoint, apiKey, body, v)
}

// PutJSON is a convenience method for MakeAPIRequest.
func PutJSON(endpoint *url.URL, apiKey string, body []byte, v interface{}) (statusCode int, err error) {
return jsonAPIRequest(http.MethodPut, endpoint, apiKey, body, v)
}

func stringAPIRequest(method string, endpoint *url.URL, APIKey string, body []byte) (string, int, error) {
res, code, err := MakeAPIRequest(method, endpoint, APIKey, body)
if err != nil {
Expand Down Expand Up @@ -78,7 +83,7 @@ type TimeoutError error

// MakeAPIRequest runs and logs a request backed by a default `http.Client`.
func MakeAPIRequest(method string, endpoint *url.URL, APIKey string, body []byte) (res []byte, statusCode int, err error) {
log.Logger.Debug(log.Entry{
log.Logger.Debugf("%s", log.Entry{
Message: "making API request",
Fields: log.Fields{
"endpoint": *endpoint,
Expand Down
26 changes: 22 additions & 4 deletions api/fossa/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/pkg/errors"
)

const BuildsAPI = "/api/revisions/%s/build"
const BuildRevisionAPI = "/api/revisions/%s/build"
const BuildsAPI = "/api/builds/%s"

// A Build holds the FOSSA API response for the builds API.
type Build struct {
Expand All @@ -18,13 +19,30 @@ type Build struct {
}
}

// GetBuild loads the build for a project.
func GetBuild(locator Locator) (Build, error) {
func QueueBuild(locator Locator) (Build, error) {
q := url.Values{}
q.Add("locator", locator.String())

var build Build
_, err := GetJSON(fmt.Sprintf(BuildsAPI, url.PathEscape(locator.String())), &build)
_, err := GetJSON(fmt.Sprintf(BuildsAPI, "?"+q.Encode()), &build)
if err != nil {
return Build{}, errors.Wrap(err, "could not get Build from API")
}

return build, nil
}

// GetBuilds loads the build for a revision.
func GetBuilds(locator Locator) ([]Build, error) {
q := url.Values{}
q.Add("locator", locator.String())
q.Add("sort", "-createdAt")

var builds []Build
_, err := GetJSON(fmt.Sprintf(BuildsAPI, "?"+q.Encode()), &builds)
if err != nil {
return nil, errors.Wrap(err, "could not get Build from API")
}

return builds, nil
}
6 changes: 6 additions & 0 deletions api/fossa/fossa.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ func Post(endpoint string, body []byte) (res string, statusCode int, err error)
u := mustParse(endpoint)
return api.Post(u, apiKey, body)
}

// PutJSON makes a JSON PUT request to a FOSSA API endpoint.
func PutJSON(endpoint string, body []byte, v interface{}) (statusCode int, err error) {
u := mustParse(endpoint)
return api.PutJSON(u, apiKey, body, v)
}
2 changes: 1 addition & 1 deletion api/fossa/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetIssues(locator Locator) ([]Issue, error) {
q.Add("fromRevision", locator.String())
q.Add("count", "1000")
var issues []Issue
_, err := GetJSON(fmt.Sprintf(IssuesAPI, url.PathEscape(locator.String())+"?"+q.Encode()), &issues)
_, err := GetJSON(fmt.Sprintf(IssuesAPI, "?"+q.Encode()), &issues)
if err != nil {
return nil, errors.Wrap(err, "could not get Issues from API")
}
Expand Down
2 changes: 0 additions & 2 deletions api/fossa/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func LocatorOf(id pkg.ID) Locator {
fetcher = "mvn"
case pkg.Ant:
fetcher = "mvn"
case pkg.Go:
fetcher = "git"
}

return Locator{
Expand Down
15 changes: 11 additions & 4 deletions cmd/fossa/cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func Run(ctx *cli.Context) error {
}

issues, err := Do(time.After(time.Duration(ctx.Int(Timeout)) * time.Second))
log.ShowSpinner("")
log.StopSpinner()
if err != nil {
log.Logger.Fatalf("Could not test revision: %s", err.Error())
}
Expand Down Expand Up @@ -98,19 +100,24 @@ func CheckBuild(locator fossa.Locator, stop <-chan time.Time) (fossa.Build, erro
case <-stop:
return fossa.Build{}, errors.New("timed out while waiting for build")
default:
build, err := fossa.GetBuild(locator)
builds, err := fossa.GetBuilds(locator)
if _, ok := err.(api.TimeoutError); ok {
time.Sleep(pollRequestDelay)
continue
}
if err != nil {
return fossa.Build{}, errors.Wrap(err, "error while loading build")
}
switch build.Task.Status {
if len(builds) == 0 {
time.Sleep(pollRequestDelay)
continue
}
latestBuild := builds[0]
switch latestBuild.Task.Status {
case "SUCCEEDED":
return build, nil
return latestBuild, nil
case "FAILED":
return fossa.Build{}, fmt.Errorf("failed to analyze build #%d: %s (visit FOSSA or contact support@fossa.io)", build.ID, build.Error)
return latestBuild, fmt.Errorf("failed to analyze build #%d: %s (visit FOSSA or contact support@fossa.io)", latestBuild.ID, latestBuild.Error)
default:
time.Sleep(pollRequestDelay)
}
Expand Down

0 comments on commit 7e8b560

Please sign in to comment.