Skip to content

Commit

Permalink
fix(upload): Add more debugging stuff to upload and use standard API …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
elldritch committed Mar 6, 2018
1 parent 83b0d07 commit 835c014
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .fossa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: 1
cli:
server: https://app.fossa.io
project: git@github.com:fossas/fossa-cli.git
project: github.com/fossas/fossa-cli
fetcher: git
analyze:
modules:
Expand Down
12 changes: 7 additions & 5 deletions cmd/fossa/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ func makeAPIRequest(method, endpoint, apiKey string, payload []byte) ([]byte, er
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read API HTTP response: %s", err.Error())
}

if resp.StatusCode == http.StatusForbidden {
commonLogger.Debugf("Response body: %s", string(body))
return nil, fmt.Errorf("invalid API key %#v (try setting $FOSSA_API_KEY); get one at https://fossa.io", apiKey)
} else if resp.StatusCode != http.StatusOK {
commonLogger.Debugf("Response body: %s", string(body))
return nil, fmt.Errorf("bad server response: %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read API HTTP response: %s", err.Error())
}
commonLogger.Debugf("Got API response: %#v", string(body))

return body, nil
}
32 changes: 9 additions & 23 deletions cmd/fossa/upload.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -170,33 +168,21 @@ func doUpload(conf config.CLIConfig, results []normalizedModule) (string, error)

analysisLogger.Debugf("Sending build data to <%#v>", postURL)

req, _ := http.NewRequest("POST", postURL, bytes.NewReader(buildData))
req.Close = true
req.Header.Set("Authorization", "token "+conf.APIKey)
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
res, err := makeAPIRequest(http.MethodPost, postURL, conf.APIKey, buildData)
if err != nil {
return "", fmt.Errorf("could not begin upload: %s", err.Error())
}
defer resp.Body.Close()
responseBytes, _ := ioutil.ReadAll(resp.Body)
responseStr := string(responseBytes)

if resp.StatusCode == http.StatusForbidden {
return "", fmt.Errorf("invalid API key (check the $FOSSA_API_KEY environment variable); get one at https://fossa.io")
} else if resp.StatusCode == http.StatusPreconditionRequired {
// TODO: handle "Managed Project" workflow
return "", fmt.Errorf("invalid project or revision; make sure this version is published and FOSSA has access to your repo. To submit a custom project, set Fetcher to `custom` in `.fossa.yml`")
} else if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("bad server response (%#v)", responseStr)
// HACK: really, we should be exporting this error and comparing against it
if err.Error() == "bad server response: 428" {
// TODO: handle "Managed Project" workflow
return "", fmt.Errorf("invalid project or revision; make sure this version is published and FOSSA has access to your repo (to submit a custom project, set Fetcher to `custom` in `.fossa.yml`)")
}
return "", fmt.Errorf("could not upload: %s", err.Error())
}

analysisLogger.Debugf("Upload succeeded")
analysisLogger.Debugf("Response: %#v", responseStr)
analysisLogger.Debugf("Response: %#v", string(res))

var jsonResponse map[string]interface{}
if err := json.Unmarshal(responseBytes, &jsonResponse); err != nil {
if err := json.Unmarshal(res, &jsonResponse); err != nil {
return "", fmt.Errorf("invalid response, but build was uploaded")
}
locParts := strings.Split(jsonResponse["locator"].(string), "$")
Expand Down

0 comments on commit 835c014

Please sign in to comment.