-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// Common utilities among commands | ||
func makeAPIRequest(requestType string, url string, payload []byte, apiKey string) ([]byte, error) { | ||
req, _ := http.NewRequest(requestType, url, bytes.NewReader(payload)) | ||
req.Header.Set("Authorization", "token "+apiKey) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode == http.StatusForbidden { | ||
return nil, errors.New("invalid API key, check the $FOSSA_API_KEY environment variable") | ||
} else if resp.StatusCode != http.StatusOK { | ||
return nil, errors.New("bad server response (" + string(resp.StatusCode) + ")") | ||
} | ||
responseBytes, _ := ioutil.ReadAll(resp.Body) | ||
return responseBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
logging "github.com/op/go-logging" | ||
"github.com/urfave/cli" | ||
emoji "gopkg.in/kyokomi/emoji.v1" | ||
|
||
"github.com/tidwall/gjson" | ||
) | ||
|
||
var testLogger = logging.MustGetLogger("test") | ||
|
||
const PollRequestDelay = 8000 | ||
|
||
func confirmBuild(config cliConfig, timing int) error { | ||
fossaBaseURL, err := url.Parse(config.endpoint) | ||
if err != nil { | ||
return errors.New("invalid FOSSA endpoint") | ||
} | ||
|
||
reqRef, _ := url.Parse("/api/revisions/" + url.PathEscape(config.getVcsLocator()) + "/build") | ||
reqURL := fossaBaseURL.ResolveReference(reqRef).String() | ||
|
||
testLogger.Debugf("Querying <%#v>", reqURL) | ||
resp, err := makeAPIRequest("PUT", reqURL, nil, config.apiKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buildData := string(resp) | ||
buildStatus := gjson.Get(buildData, "task.status").String() | ||
|
||
testLogger.Debugf("Build status returned: %s", buildStatus) | ||
|
||
switch buildStatus { | ||
case "": | ||
return errors.New("unable to parse build results") | ||
case "FAILED": | ||
return errors.New("failed to analyze build #" + gjson.Get(buildData, "id").String() + " <" + gjson.Get(buildData, "error").String() + ">; visit FOSSA or contact support@fossa.io") | ||
case "SUCCEEDED": | ||
return nil | ||
default: | ||
} | ||
|
||
if timing >= config.timeout { | ||
return errors.New("request series timed out") | ||
} | ||
|
||
time.Sleep(time.Duration(PollRequestDelay) * time.Millisecond) | ||
|
||
return confirmBuild(config, timing+PollRequestDelay) | ||
} | ||
|
||
func confirmScan(config cliConfig, timing int) error { | ||
fossaBaseURL, err := url.Parse(config.endpoint) | ||
if err != nil { | ||
return errors.New("invalid FOSSA endpoint") | ||
} | ||
|
||
reqRef, _ := url.Parse("/api/revisions/" + url.PathEscape(config.getVcsLocator())) | ||
reqURL := fossaBaseURL.ResolveReference(reqRef).String() | ||
|
||
testLogger.Debugf("Querying <%#v>", reqURL) | ||
resp, err := makeAPIRequest("GET", reqURL, nil, config.apiKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
revisionData := string(resp) | ||
if gjson.Get(revisionData, "meta.0.last_scan").String() == "" { | ||
// not scanned yet | ||
if timing >= config.timeout { | ||
return errors.New("request series timed out") | ||
} | ||
time.Sleep(time.Duration(PollRequestDelay) * time.Millisecond) | ||
|
||
return confirmScan(config, timing+PollRequestDelay) | ||
} | ||
|
||
issueCount := 0 | ||
gjson.Get(revisionData, "issues.#.resolved").ForEach(func(key, value gjson.Result) bool { | ||
if value.Bool() == false { | ||
issueCount++ | ||
} | ||
return true // keep iterating | ||
}) | ||
if issueCount > 0 { | ||
return errors.New(strconv.Itoa(issueCount) + " issues found") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCmd(c *cli.Context) { | ||
config, err := initialize(c) | ||
if err != nil { | ||
testLogger.Fatalf("Could not load configuration: %s", err.Error()) | ||
} | ||
|
||
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) | ||
s.Writer = os.Stderr | ||
s.Suffix = " Waiting for analysis job to succeed..." | ||
s.Start() | ||
|
||
if err := confirmBuild(config, 0); err != nil { | ||
s.Stop() | ||
testLogger.Fatalf("Error executing test: %#v", err) | ||
} | ||
|
||
s.Suffix = " Waiting for FOSSA scan results..." | ||
s.Restart() | ||
|
||
if err := confirmScan(config, 0); err != nil { | ||
s.Stop() | ||
testLogger.Fatalf(err.Error()) | ||
} | ||
// TODO: pipe issue data into a report function | ||
s.Stop() | ||
emoji.Println("Success; No issues found! :tada:") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters