Skip to content

Commit

Permalink
Refactor MATCH values such that they can be used by other verbs (guac…
Browse files Browse the repository at this point in the history
…sec#492)

* refactor MATCH values such that they can be used by other verbs

Signed-off-by: pxp928 <[email protected]>

* fix pass bool as reference

Signed-off-by: pxp928 <[email protected]>

---------

Signed-off-by: pxp928 <[email protected]>
  • Loading branch information
pxp928 authored Feb 27, 2023
1 parent caa8efd commit 027a150
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 341 deletions.
40 changes: 29 additions & 11 deletions pkg/assembler/backends/neo4j/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,11 @@ func (c *neo4jClient) Artifacts(ctx context.Context, artifactSpec *model.Artifac
var firstMatch bool = true
queryValues := map[string]any{}

sb.WriteString("MATCH (n:Artifact)")
sb.WriteString("MATCH (a:Artifact)")

if artifactSpec.Algorithm != nil {
matchProperties(&sb, firstMatch, "n", "algorithm", "$artifactAlgo")
firstMatch = false
queryValues["artifactAlgo"] = strings.ToLower(*artifactSpec.Algorithm)
}
if artifactSpec.Digest != nil {
matchProperties(&sb, firstMatch, "n", "digest", "$artifactDigest")
queryValues["artifactDigest"] = strings.ToLower(*artifactSpec.Digest)
}
setArtifactMatchValues(&sb, artifactSpec, false, &firstMatch, queryValues)

sb.WriteString(" RETURN n.algorithm, n.digest")
sb.WriteString(" RETURN a.algorithm, a.digest")

result, err := session.ReadTransaction(
func(tx neo4j.Transaction) (interface{}, error) {
Expand Down Expand Up @@ -139,3 +131,29 @@ RETURN a.algorithm, a.digest`

return result.(*model.Artifact), nil
}

func setArtifactMatchValues(sb *strings.Builder, art *model.ArtifactSpec, objectArt bool, firstMatch *bool, queryValues map[string]any) {
if art != nil {
if art.Algorithm != nil {
if !objectArt {
matchProperties(sb, *firstMatch, "a", "algorithm", "$algorithm")
queryValues["algorithm"] = strings.ToLower(*art.Algorithm)
} else {
matchProperties(sb, *firstMatch, "objArt", "algorithm", "$objAlgorithm")
queryValues["objAlgorithm"] = strings.ToLower(*art.Algorithm)
}
*firstMatch = false
}

if art.Digest != nil {
if !objectArt {
matchProperties(sb, *firstMatch, "a", "digest", "$digest")
queryValues["digest"] = strings.ToLower(*art.Digest)
} else {
matchProperties(sb, *firstMatch, "objArt", "digest", "$objDigest")
queryValues["objDigest"] = strings.ToLower(*art.Digest)
}
*firstMatch = false
}
}
}
72 changes: 38 additions & 34 deletions pkg/assembler/backends/neo4j/certifyPkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ func (c *neo4jClient) CertifyPkg(ctx context.Context, certifyPkgSpec *model.Cert
}

returnValue := " RETURN type.type, namespace.namespace, name.name, version.version, version.subpath, " +
"version.qualifier_list, certifyPkg, depType.type, depNamespace.namespace, depName.name, " +
"depVersion.version, depVersion.subpath, depVersion.qualifier_list"
"version.qualifier_list, certifyPkg, objPkgType.type, objPkgNamespace.namespace, objPkgName.name, " +
"objPkgVersion.version, objPkgVersion.subpath, objPkgVersion.qualifier_list"

queryValues := map[string]any{}

// query with pkgVersion
query := "MATCH (n:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
query := "MATCH (root:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
"-[:PkgHasName]->(name:PkgName)-[:PkgHasVersion]->(version:PkgVersion)" +
"-[certifyPkg:CertifyPkg]-(depVersion:PkgVersion)<-[:PkgHasVersion]-(depName:PkgName)<-[:PkgHasName]" +
"-(depNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(depType:PkgType)<-[:PkgHasType]-(depPkg:Pkg)"
"-[certifyPkg:CertifyPkg]-(objPkgVersion:PkgVersion)<-[:PkgHasVersion]-(objPkgName:PkgName)<-[:PkgHasName]" +
"-(objPkgNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(objPkgType:PkgType)<-[:PkgHasType]-(objPkgRoot:Pkg)"
sb.WriteString(query)

firstMatch = true
setMatchValues(&sb, selectedPkg, dependentPkg, firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, firstMatch, queryValues)
setPkgMatchValues(&sb, selectedPkg, false, &firstMatch, queryValues)
setPkgMatchValues(&sb, dependentPkg, true, &firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, &firstMatch, queryValues)

sb.WriteString(returnValue)

Expand All @@ -75,15 +75,17 @@ func (c *neo4jClient) CertifyPkg(ctx context.Context, certifyPkgSpec *model.Cert

sb.WriteString("\nUNION")
// query with pkgVersion
query = "\nMATCH (n:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
query = "\nMATCH (root:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
"-[:PkgHasName]->(name:PkgName)-[:PkgHasVersion]->(version:PkgVersion)" +
"-[certifyPkg:CertifyPkg]-(depName:PkgName)<-[:PkgHasName]-(depNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(depType:PkgType)<-[:PkgHasType]-(depPkg:Pkg)" +
"\nWITH *, null AS depVersion"
"-[certifyPkg:CertifyPkg]-(objPkgName:PkgName)<-[:PkgHasName]-(objPkgNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(objPkgType:PkgType)<-[:PkgHasType]-(objPkgRoot:Pkg)" +
"\nWITH *, null AS objPkgVersion"
sb.WriteString(query)

setMatchValues(&sb, selectedPkg, dependentPkg, firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, firstMatch, queryValues)
firstMatch = true
setPkgMatchValues(&sb, selectedPkg, false, &firstMatch, queryValues)
setPkgMatchValues(&sb, dependentPkg, true, &firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, &firstMatch, queryValues)

sb.WriteString(returnValue)

Expand All @@ -96,16 +98,17 @@ func (c *neo4jClient) CertifyPkg(ctx context.Context, certifyPkgSpec *model.Cert

sb.WriteString("\nUNION")
// query without pkgVersion
query = "\nMATCH (n:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
query = "\nMATCH (root:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
"-[:PkgHasName]->(name:PkgName)" +
"-[certifyPkg:CertifyPkg]-(depName:PkgName)<-[:PkgHasName]-(depNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(depType:PkgType)<-[:PkgHasType]-(depPkg:Pkg)" +
"\nWITH *, null AS version, null AS depVersion"
"-[certifyPkg:CertifyPkg]-(objPkgName:PkgName)<-[:PkgHasName]-(objPkgNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(objPkgType:PkgType)<-[:PkgHasType]-(objPkgRoot:Pkg)" +
"\nWITH *, null AS version, null AS objPkgVersion"
sb.WriteString(query)

firstMatch = true
setMatchValues(&sb, selectedPkg, dependentPkg, firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, firstMatch, queryValues)
setPkgMatchValues(&sb, selectedPkg, false, &firstMatch, queryValues)
setPkgMatchValues(&sb, dependentPkg, true, &firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, &firstMatch, queryValues)

sb.WriteString(returnValue)
}
Expand All @@ -115,17 +118,18 @@ func (c *neo4jClient) CertifyPkg(ctx context.Context, certifyPkgSpec *model.Cert

sb.WriteString("\nUNION")
// query without pkgVersion
query = "\nMATCH (n:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
query = "\nMATCH (root:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
"-[:PkgHasName]->(name:PkgName)" +
"-[certifyPkg:CertifyPkg]-(depVersion:PkgVersion)<-[:PkgHasVersion]-(depName:PkgName)<-[:PkgHasName]" +
"-(depNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(depType:PkgType)<-[:PkgHasType]-(depPkg:Pkg)" +
"-[certifyPkg:CertifyPkg]-(objPkgVersion:PkgVersion)<-[:PkgHasVersion]-(objPkgName:PkgName)<-[:PkgHasName]" +
"-(objPkgNamespace:PkgNamespace)<-[:PkgHasNamespace]" +
"-(objPkgType:PkgType)<-[:PkgHasType]-(objPkgRoot:Pkg)" +
"\nWITH *, null AS version"
sb.WriteString(query)

firstMatch = true
setMatchValues(&sb, selectedPkg, dependentPkg, firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, firstMatch, queryValues)
setPkgMatchValues(&sb, selectedPkg, false, &firstMatch, queryValues)
setPkgMatchValues(&sb, dependentPkg, true, &firstMatch, queryValues)
setCertifyPkgValues(&sb, certifyPkgSpec, &firstMatch, queryValues)

sb.WriteString(returnValue)
}
Expand Down Expand Up @@ -221,23 +225,23 @@ func (c *neo4jClient) CertifyPkg(ctx context.Context, certifyPkgSpec *model.Cert
return result.([]*model.CertifyPkg), nil
}

func setCertifyPkgValues(sb *strings.Builder, certifyPkgSpec *model.CertifyPkgSpec, firstMatch bool, queryValues map[string]any) {
func setCertifyPkgValues(sb *strings.Builder, certifyPkgSpec *model.CertifyPkgSpec, firstMatch *bool, queryValues map[string]any) {
if certifyPkgSpec.Justification != nil {

matchProperties(sb, firstMatch, "certifyPkg", "justification", "$justification")
firstMatch = false
matchProperties(sb, *firstMatch, "certifyPkg", "justification", "$justification")
*firstMatch = false
queryValues["justification"] = certifyPkgSpec.Justification
}
if certifyPkgSpec.Origin != nil {

matchProperties(sb, firstMatch, "certifyPkg", "origin", "$origin")
firstMatch = false
matchProperties(sb, *firstMatch, "certifyPkg", "origin", "$origin")
*firstMatch = false
queryValues["origin"] = certifyPkgSpec.Origin
}
if certifyPkgSpec.Collector != nil {

matchProperties(sb, firstMatch, "certifyPkg", "collector", "$collector")
firstMatch = false
matchProperties(sb, *firstMatch, "certifyPkg", "collector", "$collector")
*firstMatch = false
queryValues["collector"] = certifyPkgSpec.Collector
}
}
125 changes: 22 additions & 103 deletions pkg/assembler/backends/neo4j/hasSourceAt.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,37 @@ func (c *neo4jClient) HasSourceAt(ctx context.Context, hasSourceAtSpec *model.Ha
var firstMatch bool = true

returnValue := " RETURN type.type, namespace.namespace, name.name, version.version, version.subpath, " +
"version.qualifier_list, hasSourceAt, srcType.type, srcNamespace.namespace, srcName.name, srcName.tag, srcName.commit"
"version.qualifier_list, hasSourceAt, objSrcType.type, objSrcNamespace.namespace, objSrcName.name, objSrcName.tag, objSrcName.commit"

queryValues := map[string]any{}
// query with pkgVersion
query := "MATCH (n:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
query := "MATCH (root:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
"-[:PkgHasName]->(name:PkgName)-[:PkgHasVersion]->(version:PkgVersion)" +
"-[hasSourceAt:HasSourceAt]-(srcName:SrcName)<-[:SrcHasName]-(srcNamespace:SrcNamespace)<-[:SrcHasNamespace]" +
"-(srcType:SrcType)<-[:SrcHasType]-(src:Src)"
"-[hasSourceAt:HasSourceAt]-(objSrcName:SrcName)<-[:SrcHasName]-(objSrcNamespace:SrcNamespace)<-[:SrcHasNamespace]" +
"-(objSrcType:SrcType)<-[:SrcHasType]-(objSrcRoot:Src)"
sb.WriteString(query)

setPkgSrcMatchValues(&sb, hasSourceAtSpec.Package, hasSourceAtSpec.Source, firstMatch, queryValues)
setHasSourceAtValues(&sb, hasSourceAtSpec, firstMatch, queryValues)
setPkgMatchValues(&sb, hasSourceAtSpec.Package, false, &firstMatch, queryValues)
setSrcMatchValues(&sb, hasSourceAtSpec.Source, true, &firstMatch, queryValues)
setHasSourceAtValues(&sb, hasSourceAtSpec, &firstMatch, queryValues)
sb.WriteString(returnValue)

if hasSourceAtSpec.Package == nil || hasSourceAtSpec.Package != nil && hasSourceAtSpec.Package.Version == nil && hasSourceAtSpec.Package.Subpath == nil &&
len(hasSourceAtSpec.Package.Qualifiers) == 0 && !*hasSourceAtSpec.Package.MatchOnlyEmptyQualifiers {

sb.WriteString("\nUNION")
// query without pkgVersion
query = "\nMATCH (n:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
query = "\nMATCH (root:Pkg)-[:PkgHasType]->(type:PkgType)-[:PkgHasNamespace]->(namespace:PkgNamespace)" +
"-[:PkgHasName]->(name:PkgName)" +
"-[hasSourceAt:HasSourceAt]-(srcName:SrcName)<-[:SrcHasName]-(srcNamespace:SrcNamespace)<-[:SrcHasNamespace]" +
"-(srcType:SrcType)<-[:SrcHasType]-(src:Src)" +
"-[hasSourceAt:HasSourceAt]-(objSrcName:SrcName)<-[:SrcHasName]-(objSrcNamespace:SrcNamespace)<-[:SrcHasNamespace]" +
"-(objSrcType:SrcType)<-[:SrcHasType]-(objSrcRoot:Src)" +
"\nWITH *, null AS version"
sb.WriteString(query)

firstMatch = true
setPkgSrcMatchValues(&sb, hasSourceAtSpec.Package, hasSourceAtSpec.Source, firstMatch, queryValues)
setHasSourceAtValues(&sb, hasSourceAtSpec, firstMatch, queryValues)
setPkgMatchValues(&sb, hasSourceAtSpec.Package, false, &firstMatch, queryValues)
setSrcMatchValues(&sb, hasSourceAtSpec.Source, true, &firstMatch, queryValues)
setHasSourceAtValues(&sb, hasSourceAtSpec, &firstMatch, queryValues)
sb.WriteString(returnValue)
}

Expand Down Expand Up @@ -165,112 +167,29 @@ func (c *neo4jClient) HasSourceAt(ctx context.Context, hasSourceAtSpec *model.Ha
return result.([]*model.HasSourceAt), nil
}

func setHasSourceAtValues(sb *strings.Builder, hasSourceAtSpec *model.HasSourceAtSpec, firstMatch bool, queryValues map[string]any) {
func setHasSourceAtValues(sb *strings.Builder, hasSourceAtSpec *model.HasSourceAtSpec, firstMatch *bool, queryValues map[string]any) {
if hasSourceAtSpec.KnownSince != nil {

matchProperties(sb, firstMatch, "hasSourceAt", "knownSince", "$knownSince")
firstMatch = false
matchProperties(sb, *firstMatch, "hasSourceAt", "knownSince", "$knownSince")
*firstMatch = false
queryValues["knownSince"] = hasSourceAtSpec.KnownSince
}
if hasSourceAtSpec.Justification != nil {

matchProperties(sb, firstMatch, "hasSourceAt", "justification", "$justification")
firstMatch = false
matchProperties(sb, *firstMatch, "hasSourceAt", "justification", "$justification")
*firstMatch = false
queryValues["justification"] = hasSourceAtSpec.Justification
}
if hasSourceAtSpec.Origin != nil {

matchProperties(sb, firstMatch, "hasSourceAt", "origin", "$origin")
firstMatch = false
matchProperties(sb, *firstMatch, "hasSourceAt", "origin", "$origin")
*firstMatch = false
queryValues["origin"] = hasSourceAtSpec.Origin
}
if hasSourceAtSpec.Collector != nil {

matchProperties(sb, firstMatch, "hasSourceAt", "collector", "$collector")
firstMatch = false
matchProperties(sb, *firstMatch, "hasSourceAt", "collector", "$collector")
*firstMatch = false
queryValues["collector"] = hasSourceAtSpec.Collector
}
}

// TODO (parth): Refactor to remove reused code by multiple verbs
func setPkgSrcMatchValues(sb *strings.Builder, pkg *model.PkgSpec, src *model.SourceSpec, firstMatch bool, queryValues map[string]any) {
if pkg != nil {
if pkg.Type != nil {

matchProperties(sb, firstMatch, "type", "type", "$pkgType")
firstMatch = false
queryValues["pkgType"] = pkg.Type
}
if pkg.Namespace != nil {

matchProperties(sb, firstMatch, "namespace", "namespace", "$pkgNamespace")
firstMatch = false
queryValues["pkgNamespace"] = pkg.Namespace
}
if pkg.Name != nil {

matchProperties(sb, firstMatch, "name", "name", "$pkgName")
firstMatch = false
queryValues["pkgName"] = pkg.Name
}
if pkg.Version != nil {

matchProperties(sb, firstMatch, "version", "version", "$pkgVersion")
firstMatch = false
queryValues["pkgVersion"] = pkg.Version
}

if pkg.Subpath != nil {

matchProperties(sb, firstMatch, "version", "subpath", "$pkgSubpath")
firstMatch = false
queryValues["pkgSubpath"] = pkg.Subpath
}

if !*pkg.MatchOnlyEmptyQualifiers {

if len(pkg.Qualifiers) > 0 {
qualifiers := getQualifiers(pkg.Qualifiers)
matchProperties(sb, firstMatch, "version", "qualifier_list", "$pkgQualifierList")
firstMatch = false
queryValues["pkgQualifierList"] = qualifiers
}

} else {
matchProperties(sb, firstMatch, "version", "qualifier_list", "$pkgQualifierList")
firstMatch = false
queryValues["pkgQualifierList"] = []string{}
}
}
if src != nil {
if src.Type != nil {

matchProperties(sb, firstMatch, "srcType", "type", "$srcType")
firstMatch = false
queryValues["srcType"] = src.Type
}
if src.Namespace != nil {

matchProperties(sb, firstMatch, "srcNamespace", "namespace", "$srcNamespace")
firstMatch = false
queryValues["srcNamespace"] = src.Namespace
}
if src.Name != nil {

matchProperties(sb, firstMatch, "srcName", "name", "$srcName")
firstMatch = false
queryValues["srcName"] = src.Name
}

if src.Tag != nil {
matchProperties(sb, firstMatch, "srcName", "tag", "$srcTag")
firstMatch = false
queryValues["srcTag"] = src.Tag
}

if src.Commit != nil {
matchProperties(sb, firstMatch, "srcName", "commit", "$srcCommit")
queryValues["srcCommit"] = src.Commit
}
}
}
Loading

0 comments on commit 027a150

Please sign in to comment.