Skip to content

Commit

Permalink
Merge pull request #134 from atomist-skills/fixed-data-source
Browse files Browse the repository at this point in the history
fixed data source should be in here for testing
  • Loading branch information
chrispatrick authored Sep 5, 2024
2 parents 6da3eb6 + fd5b6b4 commit f12f479
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
42 changes: 42 additions & 0 deletions policy/data/fixed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package data

import (
"context"

"github.com/atomist-skills/go-skill/policy/data/proxy"
"github.com/atomist-skills/go-skill/policy/data/query"
"github.com/atomist-skills/go-skill/policy/goals"
"github.com/atomist-skills/go-skill/policy/types"
)

type vulnerabilityFetcher func(ctx context.Context, evalCtx goals.GoalEvaluationContext, imageSbom types.SBOM) (*query.QueryResponse, []types.Package, map[string][]types.Vulnerability, error)

type fixedDataSource struct {
jynxGQLClient query.QueryClient
proxyClient *proxy.ProxyClient
vulnerabilities vulnerabilityFetcher
}

func NewFixedDataSource(jynxGQLClient query.QueryClient, proxyClient *proxy.ProxyClient, vulnerabilities vulnerabilityFetcher) DataSource {
return &fixedDataSource{
jynxGQLClient: jynxGQLClient,
proxyClient: proxyClient,
vulnerabilities: vulnerabilities,
}
}

func (ds *fixedDataSource) GetQueryClient() query.QueryClient {
return ds.jynxGQLClient
}

func (ds *fixedDataSource) GetProxyClient() (*proxy.ProxyClient, error) {
return ds.proxyClient, nil
}

func (ds *fixedDataSource) GetImageVulnerabilities(ctx context.Context, evalCtx goals.GoalEvaluationContext, imageSbom types.SBOM) (*query.QueryResponse, []types.Package, map[string][]types.Vulnerability, error) {
if ds.vulnerabilities != nil {
return ds.vulnerabilities(ctx, evalCtx, imageSbom)
}

return &query.QueryResponse{}, []types.Package{}, map[string][]types.Vulnerability{}, nil
}
34 changes: 34 additions & 0 deletions policy/data/query/fixed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package query

import (
"context"
)

type FixedQueryClientUnmarshaler func(data []byte, output interface{}) error

// FixedQueryClient returns static data from responses passed in at construction time
type FixedQueryClient struct {
unmarshaler FixedQueryClientUnmarshaler
data map[string][]byte
}

func NewFixedQueryClient(unmarshaler FixedQueryClientUnmarshaler, data map[string][]byte) FixedQueryClient {
return FixedQueryClient{
unmarshaler: unmarshaler,
data: data,
}
}

func (ds FixedQueryClient) Query(ctx context.Context, queryName string, queryBody string, variables map[string]interface{}, output interface{}) (*QueryResponse, error) {
res, ok := ds.data[queryName]
if !ok {
return nil, nil
}

err := ds.unmarshaler(res, output)
if err != nil {
return nil, err
}

return &QueryResponse{}, nil
}
2 changes: 1 addition & 1 deletion policy/data/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type DataSource interface {
GetQueryClient() query.QueryClient
GetProxyClient() proxy.ProxyClient
GetProxyClient() (*proxy.ProxyClient, error)

GetImageVulnerabilities(ctx context.Context, evalCtx goals.GoalEvaluationContext, imageSbom types.SBOM) (*query.QueryResponse, []types.Package, map[string][]types.Vulnerability, error)
}

0 comments on commit f12f479

Please sign in to comment.