Skip to content

Commit

Permalink
feat(test): Implement fossa test
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Jul 12, 2018
1 parent b0a8b72 commit bb8f1a5
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 197 deletions.
30 changes: 30 additions & 0 deletions api/fossa/builds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fossa

import (
"fmt"
"net/url"

"github.com/pkg/errors"
)

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

// A Build holds the FOSSA API response for the builds API.
type Build struct {
ID int
Error string
Task struct {
Status string
}
}

// GetBuild loads the build for a project.
func GetBuild(locator Locator) (Build, error) {
var build 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
}
32 changes: 32 additions & 0 deletions api/fossa/issues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fossa

import (
"fmt"
"net/url"

"github.com/pkg/errors"
)

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

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

// 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, url.PathEscape(locator.String())+"?"+q.Encode()), &issues)
if err != nil {
return nil, errors.Wrap(err, "could not get Issues from API")
}

return issues, nil
}
34 changes: 13 additions & 21 deletions api/fossa/revisions.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package fossa

import (
"fmt"
"net/url"
"path"

"github.com/fossas/fossa-cli/log"
"github.com/fossas/fossa-cli/pkg"
)

// A License holds the FOSSA API response for the license API.
Expand Down Expand Up @@ -35,12 +34,6 @@ type RevisionMeta struct {
LastScan string `json:"last_scan"`
}

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

// A Project holds the FOSSA API response for the project API.
type Project struct {
Title string
Expand All @@ -50,13 +43,12 @@ type Project struct {
}

// RevisionsAPI is the API endpoint for revisions.
const RevisionsAPI = "/api/revisions"
const RevisionsAPI = "/api/revisions/%s"

// GetRevision loads a single revision.
func GetRevision(id pkg.ID) (Revision, error) {
locator := LocatorOf(id)
func GetRevision(locator Locator) (Revision, error) {
var revision Revision
_, err := GetJSON(path.Join(RevisionsAPI, url.PathEscape(locator.String())), &revision)
_, err := GetJSON(fmt.Sprintf(RevisionsAPI, url.PathEscape(locator.String())), &revision)
if err != nil {
return Revision{}, err
}
Expand All @@ -80,23 +72,23 @@ func GetRevision(id pkg.ID) (Revision, error) {
}

// GetRevisions loads many revisions in batched requests.
func GetRevisions(ids []pkg.ID) (revs []Revision, err error) {
var locators []string
for _, id := range ids {
locators = append(locators, LocatorOf(id).String())
func GetRevisions(locators []Locator) (revs []Revision, err error) {
var locs []string
for _, loc := range locators {
locs = append(locs, loc.String())
}

// Split locators into chunks of 20 (this is an API limitation).
chunks := make([][]string, 0)
chunkSize := 20
for i := 0; i < len(locators); i += chunkSize {
for i := 0; i < len(locs); i += chunkSize {
end := i + chunkSize

if end > len(locators) {
end = len(locators)
if end > len(locs) {
end = len(locs)
}

chunks = append(chunks, locators[i:end])
chunks = append(chunks, locs[i:end])
}

// Make chunked API calls in parallel.
Expand All @@ -116,7 +108,7 @@ func GetRevisions(ids []pkg.ID) (revs []Revision, err error) {
} else {
responses <- revisions
}
}(RevisionsAPI + "?" + qs.Encode())
}(fmt.Sprintf(RevisionsAPI, "?"+qs.Encode()))
}

var revisions []Revision
Expand Down
17 changes: 0 additions & 17 deletions api/fossa/test.go

This file was deleted.

25 changes: 12 additions & 13 deletions cmd/fossa/cmd/report/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"text/template"

"github.com/fossas/fossa-cli/cmd/fossa/cmdutil"
"github.com/fossas/fossa-cli/pkg"
"github.com/urfave/cli"

"github.com/fossas/fossa-cli/api/fossa"
"github.com/fossas/fossa-cli/cmd/fossa/flags"
"github.com/fossas/fossa-cli/log"
)

const defaultLicenceReportTemplate = `# 3rd-Party Software License Notice
const defaultLicenseReportTemplate = `# 3rd-Party Software License Notice
Generated by fossa-cli (https://github.com/fossas/fossa-cli).
This software includes the following software and licenses:
{{range $license, $deps := .}}
Expand Down Expand Up @@ -47,37 +46,37 @@ func generateLicenses(ctx *cli.Context) (err error) {
for _, dep := range module.Deps {
i++
log.ShowSpinner(fmt.Sprintf("Fetching License Info (%d/%d): %s", i+1, totalDeps, dep.ID.Name))
rev, err := fossa.GetRevision(dep.ID)
rev, err := fossa.GetRevision(fossa.LocatorOf(dep.ID))
if err != nil {
log.Logger.Warning(err.Error())
continue
}
revs = append(revs, rev)
}
} else {
log.ShowSpinner("Fetching Licence Info")
var ids []pkg.ID
log.ShowSpinner("Fetching License Info")
var locators []fossa.Locator
for _, dep := range module.Deps {
ids = append(ids, dep.ID)
locators = append(locators, fossa.LocatorOf(dep.ID))
}
revs, err = fossa.GetRevisions(ids)
revs, err = fossa.GetRevisions(locators)
if err != nil {
log.Logger.Fatalf("Could not fetch revisions: %s", err.Error())
}
}
}

depsByLicence := make(map[string]map[string]fossa.Revision, 0)
depsByLicense := make(map[string]map[string]fossa.Revision, 0)
for _, rev := range revs {
for _, license := range rev.Licenses {
if _, ok := depsByLicence[license.LicenseID]; !ok {
depsByLicence[license.LicenseID] = make(map[string]fossa.Revision, 0)
if _, ok := depsByLicense[license.LicenseID]; !ok {
depsByLicense[license.LicenseID] = make(map[string]fossa.Revision, 0)
}
depsByLicence[license.LicenseID][rev.Locator.String()] = rev
depsByLicense[license.LicenseID][rev.Locator.String()] = rev
}
}

tmpl, err := template.New("base").Parse(defaultLicenceReportTemplate)
tmpl, err := template.New("base").Parse(defaultLicenseReportTemplate)
if err != nil {
log.Logger.Fatalf("Could not parse template data: %s", err.Error())
}
Expand All @@ -90,5 +89,5 @@ func generateLicenses(ctx *cli.Context) (err error) {
}
log.StopSpinner()

return cmdutil.OutputData(ctx.String(flags.ShowOutput), tmpl, depsByLicence)
return cmdutil.OutputData(ctx.String(flags.ShowOutput), tmpl, depsByLicense)
}

0 comments on commit bb8f1a5

Please sign in to comment.