Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/automated graph doc #1171

Merged
merged 27 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
716c830
Add automated Doc generation for Nodes and Relationships
konradweiss May 12, 2023
d181483
Add command line option to print Schema to a specified file
konradweiss May 12, 2023
4cb2ed2
Use css classes instead of repeating style
KuechA May 23, 2023
b7d26ee
Small renaming
konradweiss Jun 26, 2023
bb457b5
Merge main into feature/automated-graph-doc
KuechA Aug 1, 2023
89e8134
Automatically build schema on main
KuechA Aug 1, 2023
dbaf65f
Delete duplicate file
KuechA Aug 1, 2023
a0f2f8e
Also list properties
KuechA Aug 2, 2023
08ee758
Ban inherited fields and properties to detail boxes
KuechA Aug 2, 2023
7c3dbd0
Merge branch 'main' into feature/automated-graph-doc
konradweiss Sep 12, 2023
835e58a
Merge branch 'feature/automated-graph-doc' of github.com:Fraunhofer-A…
konradweiss Sep 12, 2023
57177eb
Removing newlines in output to have all relationships share lines
konradweiss Sep 12, 2023
38bbdeb
Merge branch 'main' into feature/automated-graph-doc
konradweiss Jan 23, 2024
2ef4a63
Merge branch 'main' into feature/automated-graph-doc
konradweiss Jan 26, 2024
007800f
Remove github ci part
KuechA Jan 26, 2024
db6924c
Updated generated documentation
KuechA Jan 30, 2024
07a2e2e
sonar
KuechA Jan 30, 2024
3f61e22
Merge branch 'main' into feature/automated-graph-doc
KuechA Jan 30, 2024
138abd5
Formatting
KuechA Jan 31, 2024
e08c4bb
Merge branch 'main' into feature/automated-graph-doc
KuechA Jan 31, 2024
d149c51
Merge branch 'main' into feature/automated-graph-doc
konradweiss Feb 6, 2024
32b8e9a
Merge branch 'main' into feature/automated-graph-doc
konradweiss Feb 6, 2024
8ffb9d9
Add Option to print the schema into a different format
konradweiss Feb 8, 2024
85c99c7
Finish Json output and add more documentation to finalize the PR
konradweiss Feb 12, 2024
314bcc4
Merge branch 'main' into feature/automated-graph-doc
konradweiss Feb 12, 2024
252604a
Remove old serialization of ast children edges
konradweiss Feb 12, 2024
640bda0
Test that files are correctly written and in case of Json also parseable
konradweiss Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ import de.fraunhofer.aisec.cpg.sarif.Region
import java.net.URI
import org.neo4j.ogm.typeconversion.CompositeAttributeConverter

interface CpgCompositeConverter<A> : CompositeAttributeConverter<A> {
/**
* Determines to which properties and their types the received value will be split in the neo4j
* representation. The type is the first element in the pair and the property name is the second
* one.
*/
val graphSchema: List<Pair<String, String>>
}

/**
* This class converts a [PhysicalLocation] into the the necessary composite attributes when
* persisting a node into a Neo4J graph database.
* This class converts a [PhysicalLocation] into the necessary composite attributes when persisting
* a node into a Neo4J graph database.
*/
class LocationConverter : CompositeAttributeConverter<PhysicalLocation?> {
class LocationConverter : CpgCompositeConverter<PhysicalLocation?> {
override fun toGraphProperties(value: PhysicalLocation?): Map<String, *> {
val properties: MutableMap<String, Any> = HashMap()
if (value != null) {
Expand All @@ -47,6 +56,16 @@ class LocationConverter : CompositeAttributeConverter<PhysicalLocation?> {
return properties
}

override val graphSchema: List<Pair<String, String>>
get() =
listOf(
Pair("String", ARTIFACT),
Pair("int", START_LINE),
Pair("int", END_LINE),
Pair("int", START_COLUMN),
Pair("int", END_COLUMN)
)

override fun toEntityAttribute(value: Map<String, *>?): PhysicalLocation? {
return try {
val startLine = toInt(value?.get(START_LINE)) ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package de.fraunhofer.aisec.cpg.helpers.neo4j

import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.graph.parseName
import org.neo4j.ogm.typeconversion.CompositeAttributeConverter

/**
* This converter can be used in a Neo4J session to persist the [Name] class into its components:
Expand All @@ -37,7 +36,7 @@ import org.neo4j.ogm.typeconversion.CompositeAttributeConverter
*
* Additionally, it converts the aforementioned Neo4J attributes in a node back into a [Name].
*/
class NameConverter : CompositeAttributeConverter<Name?> {
class NameConverter : CpgCompositeConverter<Name?> {

companion object {
const val FIELD_FULL_NAME = "fullName"
Expand All @@ -61,14 +60,22 @@ class NameConverter : CompositeAttributeConverter<Name?> {

// For reasons such as backwards compatibility and the fact that Neo4J likes to display
// nodes in the UI with a "name" field as default, we also persist the full name (aka
// the
// toString() representation) as "name"
// the toString() representation) as "name"
map[FIELD_NAME] = value.toString()
}

return map
}

override val graphSchema: List<Pair<String, String>>
get() =
listOf(
Pair("String", FIELD_FULL_NAME),
Pair("String", FIELD_LOCAL_NAME),
Pair("String", FIELD_NAME),
Pair("String", FIELD_NAME_DELIMITER)
)

override fun toEntityAttribute(value: MutableMap<String, *>): Name {
return parseName(value[FIELD_FULL_NAME].toString(), value[FIELD_NAME_DELIMITER].toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ class Application : Callable<Int> {
)
private var inferNodes: Boolean = false

@CommandLine.Option(
names = ["--schema-markdown"],
description = ["Print the CPGs nodes and edges that they can have."]
)
private var schemaMarkdown: Boolean = false

@CommandLine.Option(
names = ["--schema-json"],
description = ["Print the CPGs nodes and edges that they can have."]
)
private var schemaJson: Boolean = false

@CommandLine.Option(
names = ["--top-level"],
description =
Expand Down Expand Up @@ -534,6 +546,12 @@ class Application : Callable<Int> {
return translationConfiguration.build()
}

public fun printSchema(filenames: Collection<String>, format: Schema.Format) {
val schema = Schema()
schema.extractSchema()
filenames.forEach { schema.printToFile(it, format) }
}

/**
* The entrypoint of the cpg-vis-neo4j.
*
Expand All @@ -546,6 +564,17 @@ class Application : Callable<Int> {
*/
@Throws(Exception::class, ConnectException::class, IllegalArgumentException::class)
override fun call(): Int {

if (schemaMarkdown || schemaJson) {
if (schemaMarkdown) {
printSchema(mutuallyExclusiveParameters.files, Schema.Format.MARKDOWN)
}
if (schemaJson) {
printSchema(mutuallyExclusiveParameters.files, Schema.Format.JSON)
}
return EXIT_SUCCESS
}

if (mutuallyExclusiveParameters.listPasses) {
log.info("List of passes:")
passList.iterator().forEach { log.info("- $it") }
Expand Down
Loading
Loading