Skip to content

Commit

Permalink
Merge branch 'master' into ci/coveralls
Browse files Browse the repository at this point in the history
  • Loading branch information
zlav committed Sep 13, 2018
2 parents 7654166 + 58e029f commit c75503c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 59 deletions.
26 changes: 5 additions & 21 deletions api/fossa/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
)

const BuildsAPI = "/api/builds/%s"
const BuildsAPI = "/api/cli/%s/latest_build"

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

func QueueBuild(locator Locator) (Build, error) {
q := url.Values{}
q.Add("locator", locator.String())

// GetLatestBuild loads the most recent build for a revision
// or returns an error if the revision does not exist, or the revision has no builds.
func GetLatestBuild(locator Locator) (Build, error) {
var build Build
_, err := GetJSON(fmt.Sprintf(BuildsAPI, "?"+q.Encode()), &build)
_, err := GetJSON(fmt.Sprintf(BuildsAPI, url.PathEscape(locator.String())), &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
}
21 changes: 13 additions & 8 deletions api/fossa/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ import (
"github.com/pkg/errors"
)

const IssuesAPI = "/api/issues/%s"
const IssuesAPI = "/api/cli/%s/issues"

// An Issue holds the FOSSA API response for the issue API.
type Issue struct {
ID int
PriorityString string
Resolved bool
Revision Revision
Type string
}

// A wrapped list of issues returned by the FOSSA CLI issues endpoint
// If a push-only API key is used, then only the count is returned
type Issues struct {
Count int
Issues []Issue
}

// GetIssues loads the issues for a project.
func GetIssues(locator Locator) ([]Issue, error) {
q := url.Values{}
q.Add("fromRevision", locator.String())
q.Add("count", "1000")
var issues []Issue
_, err := GetJSON(fmt.Sprintf(IssuesAPI, "?"+q.Encode()), &issues)
func GetIssues(locator Locator) (Issues, error) {
var issues Issues
_, err := GetJSON(fmt.Sprintf(IssuesAPI, url.PathEscape(locator.String())), &issues)
if err != nil {
return nil, errors.Wrap(err, "could not get Issues from API")
return Issues{}, errors.Wrap(err, "could not get Issues from API")
}

return issues, nil
Expand Down
19 changes: 9 additions & 10 deletions api/fossa/users.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package fossa

import (
"fmt"
"net/url"
"errors"
"strconv"
)

var UsersAPI = "/api/users/%s"
const OrganizationAPI = "/api/cli/organization"

var CachedOrganizationID = -1

type User struct {
type Organization struct {
OrganizationID int
}

Expand All @@ -19,15 +18,15 @@ func GetOrganizationID() (string, error) {
return strconv.Itoa(CachedOrganizationID), nil
}

q := url.Values{}
q.Add("count", "1")

var users []User
_, err := GetJSON(fmt.Sprintf(UsersAPI, "?"+q.Encode()), &users)
var organization Organization
_, err := GetJSON(OrganizationAPI, &organization)
if err != nil {
return "", err
}
if organization.OrganizationID == 0 {
return "", errors.New("could not get organization for api key")
}

CachedOrganizationID = users[0].OrganizationID
CachedOrganizationID = organization.OrganizationID
return strconv.Itoa(CachedOrganizationID), nil
}
36 changes: 16 additions & 20 deletions cmd/fossa/cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ func Run(ctx *cli.Context) error {
log.Fatalf("Could not test revision: %s", err.Error())
}

log.Debugf("Test succeeded: %#v", issues)
if len(issues) == 0 {
if issues.Count == 0 {
fmt.Fprintln(os.Stderr, "Test passed! 0 issues found")
return nil
}

pluralizedIssues := "issues"
if len(issues) == 1 {
if issues.Count == 1 {
pluralizedIssues = "issue"
}
fmt.Fprintf(os.Stderr, "Test failed! %d %s found\n", len(issues), pluralizedIssues)
fmt.Fprintf(os.Stderr, "Test failed! %d %s found\n", issues.Count, pluralizedIssues)

marshalled, err := json.Marshal(issues)
if err != nil {
Expand All @@ -66,13 +65,13 @@ func Run(ctx *cli.Context) error {
return nil
}

func Do(stop <-chan time.Time) ([]fossa.Issue, error) {
func Do(stop <-chan time.Time) (fossa.Issues, error) {
defer display.ClearProgress()
display.InProgress("Waiting for analysis to complete...")

revision := config.Revision()
if revision == "" {
return nil, errors.New("could not detect current revision (please set with --revision)")
return fossa.Issues{}, errors.New("could not detect current revision (please set with --revision)")
}

project := fossa.Locator{
Expand All @@ -85,7 +84,7 @@ func Do(stop <-chan time.Time) ([]fossa.Issue, error) {
if project.Fetcher == "custom" {
orgID, err := fossa.GetOrganizationID()
if err != nil {
return nil, err
return fossa.Issues{}, err
}
project.Project = orgID + "/" + fossa.NormalizeGitURL(project.Project)
}
Expand All @@ -112,44 +111,41 @@ 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:
builds, err := fossa.GetBuilds(locator)
build, err := fossa.GetLatestBuild(locator)
if _, ok := err.(api.TimeoutError); ok {
time.Sleep(pollRequestDelay)
continue
}
if err != nil {
return fossa.Build{}, errors.Wrap(err, "error while loading build")
}
if len(builds) == 0 {
time.Sleep(pollRequestDelay)
continue
}
latestBuild := builds[0]
switch latestBuild.Task.Status {
switch build.Task.Status {
case "SUCCEEDED":
return latestBuild, nil
return build, nil
case "FAILED":
return latestBuild, fmt.Errorf("failed to analyze build #%d: %s (visit FOSSA or contact support@fossa.io)", latestBuild.ID, latestBuild.Error)
default:
return build, fmt.Errorf("failed to analyze build #%d: %s (visit FOSSA or contact support@fossa.io)", build.ID, build.Error)
case "CREATED", "ASSIGNED", "RUNNING":
time.Sleep(pollRequestDelay)
default:
return fossa.Build{}, fmt.Errorf("unknown task status: %s", build.Task.Status)
}
}
}
}

func CheckIssues(locator fossa.Locator, stop <-chan time.Time) ([]fossa.Issue, error) {
func CheckIssues(locator fossa.Locator, stop <-chan time.Time) (fossa.Issues, error) {
for {
select {
case <-stop:
return nil, errors.New("timed out while waiting for scan")
return fossa.Issues{}, errors.New("timed out while waiting for scan")
default:
issues, err := fossa.GetIssues(locator)
if _, ok := err.(api.TimeoutError); ok {
time.Sleep(pollRequestDelay)
continue
}
if err != nil {
return nil, errors.Wrap(err, "error while loading issues")
return fossa.Issues{}, errors.Wrap(err, "error while loading issues")
}
log.Debugf("Got issues: %#v", issues)
return issues, nil
Expand Down

0 comments on commit c75503c

Please sign in to comment.