Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fossas/fossa-cli into feat/rubyFa…
Browse files Browse the repository at this point in the history
…llback
  • Loading branch information
microsoftly committed Sep 6, 2018
2 parents eb8b518 + d9afc8f commit b0a8ab3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
12 changes: 6 additions & 6 deletions api/fossa/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ type UploadOptions struct {

// Upload uploads a project's analysis.
func Upload(title string, locator Locator, options UploadOptions, data []SourceUnit) (Locator, error) {
// Check preconditions
if locator.Fetcher != "custom" && locator.Revision == "" {
log.Fatal("Could not infer revision name from `git` remote named `origin`. To submit a custom project, set Fetcher to `custom` in `.fossa.yml`")
// Check preconditions.
if locator.Fetcher == "git" && locator.Revision == "" {
return Locator{}, errors.New("Could not infer revision name from `git` remote named `origin`. To submit a custom project, set Fetcher to `custom` in `.fossa.yml`")
}
if locator.Project == "" {
log.Fatal("Could not infer project name from either `.fossa.yml` or `git` remote named `origin`")
return Locator{}, errors.New("Could not infer project name from either `.fossa.yml` or `git` remote named `origin`")
}
if len(data) == 0 {
log.Fatal("No data to upload")
return Locator{}, errors.New("No data to upload")
}

payload, err := json.Marshal(data)
Expand Down Expand Up @@ -72,7 +72,7 @@ func Upload(title string, locator Locator, options UploadOptions, data []SourceU

endpoint, err := url.Parse("/api/builds/custom?" + q.Encode())
if err != nil {
log.Fatal("Failed to generate upload URL")
return Locator{}, errors.New("Failed to generate upload URL")
}
log.WithField("endpoint", endpoint.String()).Debug("uploading build")

Expand Down
49 changes: 28 additions & 21 deletions cmd/fossa/cmd/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
Locators = "locators"
)

// Cmd is the CLI command struct for uploading.
var Cmd = cli.Command{
Name: "upload",
Usage: "Uploads user-provided test results to FOSSA",
Expand All @@ -52,18 +53,31 @@ var Cmd = cli.Command{
})),
}

// ParseLocators parses newline-delimited string locators into SourceUnits.
func ParseLocators(locators string) (fossa.SourceUnit, error) {
// Handle empty input.
if locators == "" {
return fossa.SourceUnit{}, errors.New("upload did not receive any input")
}

// Handle bad input with empty lines.
var deps []fossa.SourceUnitDependency
lines := strings.Split(locators, "\n")
for _, line := range lines {
deps = append(deps, fossa.SourceUnitDependency{Locator: line})
if strings.TrimSpace(line) == "" {
continue
}
deps = append(deps, fossa.SourceUnitDependency{Locator: line, Imports: make([]string, 0)})
}

// TODO: validate the locators.

return fossa.SourceUnit{
Build: fossa.SourceUnitBuild{
Succeeded: true,
Dependencies: deps,
},
}, nil // TODO: validate the locators
}, nil
}

func hasPipeInput() bool {
Expand All @@ -76,7 +90,7 @@ func hasPipeInput() bool {
}

func getInput(ctx *cli.Context, usingLocators bool) ([]fossa.SourceUnit, error) {
// Read input
// Read input.
var raw string
if hasPipeInput() {
stdin, err := ioutil.ReadAll(os.Stdin)
Expand All @@ -92,23 +106,24 @@ func getInput(ctx *cli.Context, usingLocators bool) ([]fossa.SourceUnit, error)
raw = args.First()
}

// Parse input
// Parse input.
if usingLocators {
sourceUnit, err := ParseLocators(raw)
if err != nil {
return nil, errors.Wrap(err, "could not parse build data")
}
return []fossa.SourceUnit{sourceUnit}, nil
} else {
var out []fossa.SourceUnit
err := json.Unmarshal([]byte(raw), &out)
if err != nil {
return nil, errors.Wrap(err, "could not parse build data")
}
return out, nil
}

var out []fossa.SourceUnit
err := json.Unmarshal([]byte(raw), &out)
if err != nil {
return nil, errors.Wrap(err, "could not parse build data")
}
return out, nil
}

// Run executes the upload command.
func Run(ctx *cli.Context) {
err := setup.SetContext(ctx)
if err != nil {
Expand All @@ -129,17 +144,9 @@ func Run(ctx *cli.Context) {
fmt.Printf(locator.ReportURL())
}

// Do performs a SourceUnit upload of the current project without other side
// effects.
func Do(data []fossa.SourceUnit) (fossa.Locator, error) {
if config.Project() == "" {
log.Fatalf("Could not infer project name from either `.fossa.yml` or `git` remote named `origin`")
}
if config.Fetcher() != "custom" && config.Revision() == "" {
log.Fatalf("Could not infer revision name from `git` remote named `origin`. To submit a custom project, set Fetcher to `custom` in `.fossa.yml`")
}
if len(data) == 0 {
log.Fatalf("No data to upload")
}

return fossa.Upload(
config.Title(),
fossa.Locator{
Expand Down
22 changes: 21 additions & 1 deletion cmd/fossa/cmd/upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ import (
"github.com/fossas/fossa-cli/cmd/fossa/flags"
)

func TestUploadHasEndpointFlag(t *testing.T) {
func TestUploadCmdHasEndpointFlag(t *testing.T) {
assert.Subset(t, upload.Cmd.Flags, flags.API)
}

func TestParseLocatorsEmptyString(t *testing.T) {
_, err := upload.ParseLocators("")
assert.Error(t, err)
}

func TestParseLocatorsEmptyLine(t *testing.T) {
locators := `fetcher+project$revision
fetcher+otherproject$revision
fetcher+lastproject$revision
`
src, err := upload.ParseLocators(locators)
assert.NoError(t, err)

for _, dep := range src.Build.Dependencies {
assert.NotNil(t, dep.Imports)
assert.NotEmpty(t, dep.Locator)
}
}

0 comments on commit b0a8ab3

Please sign in to comment.