Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Add more endpoints and refactor server to make it testable
Browse files Browse the repository at this point in the history
Changed the test/demo.go data to return data values rather than call ent directly.
This enables using the data in multiple places, such as server/server_test.go which
now uses the data to test the api... pretty neat
  • Loading branch information
jlarfors committed Sep 4, 2021
1 parent 2711d76 commit dde06a3
Show file tree
Hide file tree
Showing 96 changed files with 7,882 additions and 5,167 deletions.
8 changes: 4 additions & 4 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/valocode/bubbly/ent"
entadapter "github.com/valocode/bubbly/ent/adapter"
"github.com/valocode/bubbly/ent/model"
)

const DefaultTag = "default"
Expand Down Expand Up @@ -46,7 +46,7 @@ type (
}
)

func FromModel(model *model.AdapterModel) (*Adapter, error) {
func FromModel(model *ent.AdapterModelRead) (*Adapter, error) {
a := &Adapter{
Name: *model.Name,
Tag: model.Tag,
Expand Down Expand Up @@ -106,7 +106,7 @@ func (a *Adapter) Run(args RunArgs) (*Output, error) {
return a.Results.Spec.Output(a.Name)
}

func (a *Adapter) Model() (*model.AdapterModel, error) {
func (a *Adapter) Model() (*ent.AdapterModelCreate, error) {
op, err := json.Marshal(a.Operation)
if err != nil {
return nil, fmt.Errorf("json marshalling adapter operation: %w", err)
Expand All @@ -115,7 +115,7 @@ func (a *Adapter) Model() (*model.AdapterModel, error) {
if err != nil {
return nil, fmt.Errorf("converting results body to bytes: %w", err)
}
return model.NewAdapterModel().
return ent.NewAdapterModelCreate().
SetName(a.Name).
SetTag(a.TagOrDefault()).
SetType(entadapter.Type(a.Operation.Type)).
Expand Down
8 changes: 4 additions & 4 deletions adapter/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/valocode/bubbly/ent/model"
"github.com/valocode/bubbly/ent"
"github.com/valocode/bubbly/store/api"
)

Expand Down Expand Up @@ -106,9 +106,9 @@ func (r *CodeScanResults) Output(name string) (*Output, error) {
}
return &Output{
CodeScan: &api.CodeScan{
CodeScanModel: *model.NewCodeScanModel().SetTool(tool),
Issues: r.CodeIssues,
Components: r.Components,
CodeScanModelCreate: ent.NewCodeScanModelCreate().SetTool(tool),
Issues: r.CodeIssues,
Components: r.Components,
},
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion adapter/testdata/adapters/gosec.adapt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
adapter {
name = "gosec"
// optional: the tag which uniquely identifies this adapter (default: default)
tag = ""
// tag = ""

operation "json" {}
// preprocess = "[${join(",", split("\n", trimspace(raw_data)))}]"
Expand Down
33 changes: 29 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ import (
"github.com/valocode/bubbly/store/api"
)

// type RequestBuilder interface {
// NewRequest(method, url string, body io.Reader) (*http.Request, error)
// }

// var (
// Request RequestBuilder
// )

// type HTTPRequestBuilder struct{}

// func (h HTTPRequestBuilder) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
// return http.NewRequest(method, url, body)
// }

// func init() {
// Request = &HTTPRequestBuilder{}
// }

func CreateRelease(bCtx *env.BubblyContext, req *api.ReleaseCreateRequest) error {
return handlePushRequest(bCtx, req, "releases")
}
Expand All @@ -21,7 +39,7 @@ func SaveCodeScan(bCtx *env.BubblyContext, req *api.CodeScanRequest) error {
}

func GetAdapter(bCtx *env.BubblyContext, req *api.AdapterGetRequest) (*adapter.Adapter, error) {
url := "http://localhost:8111/api/v1/adapters/" + *req.Name
url := bCtx.ClientConfig.V1() + "/adapters/" + *req.Name
httpReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
Expand All @@ -45,7 +63,7 @@ func GetAdapter(bCtx *env.BubblyContext, req *api.AdapterGetRequest) (*adapter.A
if err := json.NewDecoder(resp.Body).Decode(&a); err != nil {
return nil, fmt.Errorf("decoding adapter response: %w", err)
}
return adapter.FromModel(a.AdapterModel)
return adapter.FromModel(&a.AdapterModelRead)
}

func SaveAdapter(bCtx *env.BubblyContext, req *api.AdapterSaveRequest) error {
Expand All @@ -57,8 +75,15 @@ func handlePushRequest(bCtx *env.BubblyContext, req interface{}, urlsuffix strin
if err := json.NewEncoder(b).Encode(req); err != nil {
return err
}
url := "http://localhost:8111/api/v1/" + urlsuffix
resp, err := http.Post(url, echo.MIMEApplicationJSON, b)

fmt.Printf("%s\n", b)
url := bCtx.ClientConfig.V1() + "/" + urlsuffix
httpReq, err := http.NewRequest(http.MethodPost, url, b)
if err != nil {
return err
}
httpReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return err
}
Expand Down
20 changes: 0 additions & 20 deletions client/client_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/adapter/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func New(bCtx *env.BubblyContext) *cobra.Command {
}
fmt.Println("results_type: ", *model.ResultsType)
if err := client.SaveAdapter(bCtx, &api.AdapterSaveRequest{
AdapterModel: model,
Adapter: model,
}); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/adapter/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package run

import (
"errors"
"fmt"
"path"

"github.com/valocode/bubbly/adapter"
Expand Down Expand Up @@ -111,7 +110,7 @@ func New(bCtx *env.BubblyContext) *cobra.Command {
CodeScan: output.CodeScan,
})
if err != nil {
return fmt.Errorf("error saving code scan: %w", err)
return err
}
}
return nil
Expand Down
6 changes: 0 additions & 6 deletions cmd/adapter/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ func New(bCtx *env.BubblyContext) *cobra.Command {
if err != nil {
return err
}
results, err := a.Results.SpecBytes()
if err != nil {
return err
}
fmt.Println("Name: " + a.Name)
fmt.Println("Tag: " + a.TagOrDefault())
fmt.Println("Type: " + a.Operation.Type)
fmt.Println("Results:")
fmt.Printf("%s\n", results)
return nil
},
}
Expand Down
41 changes: 19 additions & 22 deletions cmd/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,25 @@ func New(bCtx *env.BubblyContext) *cobra.Command {
fmt.Println("")
fmt.Println("Populating the store with dummy data...")

// if !skipAll {
// if !skipCVE {
// fmt.Println("Fetching CVEs from NVD... This will take a few seconds...")
// if err := test.SaveCVEData(client); err != nil {
// log.Fatal("loading CVEs: ", err)
// }
// fmt.Println("Done!")
// fmt.Println("")
// }
// if !skipSPDX {
// fmt.Println("Fetching SPDX licenses from GitHub...")
// if err := test.SaveSPDXData(client); err != nil {
// log.Fatal("loading SPDX: ", err)
// }
// fmt.Println("Done!")
// fmt.Println("")

// }

fmt.Println("Creating dummy releases...")
if err := test.CreateDummyData(store); err != nil {
log.Fatal("creating dummy data: ", err)
data := test.CreateDummyData()

for _, repos := range data {
for _, release := range repos.Releases {
if _, err := store.CreateRelease(release.Release); err != nil {
log.Fatalf("creating release %s: %s", *release.Release.Release.Name, err.Error())
}
for _, scan := range release.CodeScans {
if _, err := store.SaveCodeScan(scan); err != nil {
log.Fatalf("saving code scan: %s", err.Error())
}
}
for _, run := range release.TestRuns {
if _, err := store.SaveTestRun(run); err != nil {
log.Fatalf("saving test run: %s", err.Error())
}
}
}
}
fmt.Println("Done!")
fmt.Println("")
Expand All @@ -89,7 +86,7 @@ func New(bCtx *env.BubblyContext) *cobra.Command {
fmt.Println("")

fmt.Printf("Starting HTTP server on %s:%s\n", bCtx.ServerConfig.Host, bCtx.ServerConfig.Port)
if err := server.ListenAndServe(bCtx, store); err != nil {
if err := server.NewWithStore(bCtx, store).Start(); err != nil {
return err
}
return nil
Expand Down
112 changes: 0 additions & 112 deletions cmd/demoold/main.go

This file was deleted.

Loading

0 comments on commit dde06a3

Please sign in to comment.