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

Commit

Permalink
feat: add intergration for frontent api analyser
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 22, 2022
1 parent f84b730 commit 8301f81
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
Expand Up @@ -504,6 +504,7 @@ class ClassRepository(systemId: String, language: String, workspace: String) {
private fun flush() {
batch.execute()
}

fun close() {
batch.execute()
batch.close()
Expand Down
@@ -0,0 +1,63 @@
package com.thoughtworks.archguard.scanner.sourcecode

import com.thoughtworks.archguard.scanner.sourcecode.frontend.ContainerDemand
import com.thoughtworks.archguard.scanner.sourcecode.frontend.ContainerService
import infrastructure.SourceBatch
import java.util.HashMap

class ContainerRepository(systemId: String, language: String, workspace: String) {
private val batch: SourceBatch = SourceBatch()
private val batchStep = 100
private val systemId: String
private val language: String
private val workspace: String

init {
this.systemId = systemId
this.language = language
this.workspace = workspace
}

fun saveContainerServices(services: Array<ContainerService>) {
val serviceId = saveMainServices()
services.forEach { service ->
service.demands.map { saveDemand(it, serviceId) }.toTypedArray()
}
}

private fun saveMainServices(): String {
val time: String = ClassRepository.currentTime
val serviceId = ClassRepository.generateId()
val values: MutableMap<String, String> = HashMap()

values["id"] = serviceId
values["updated_at"] = time
values["created_at"] = time

batch.add("container_service", values)
return serviceId
}

private fun saveDemand(demand: ContainerDemand, serviceId: String): String {
val time: String = ClassRepository.currentTime
val demandId = ClassRepository.generateId()
val values: MutableMap<String, String> = HashMap()

values["id"] = demandId

values["target_http_method"] = demand.target_http_method
values["target_url"] = demand.target_url
values["source_method"] = demand.source_caller
values["service_id"] = serviceId
values["updated_at"] = time
values["created_at"] = time

batch.add("container_demand", values)
return demandId
}

fun close() {
batch.execute()
batch.close()
}
}
Expand Up @@ -7,6 +7,7 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.thoughtworks.archguard.scanner.sourcecode.frontend.FrontendApiAnalyser
import infrastructure.DBIStore
import infrastructure.task.SqlExecuteThreadPool
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -42,7 +43,12 @@ class Runner : CliktCommand(help = "scan git to sql") {
"code_ref_method_callees",
"code_ref_class_dependencies",
"code_annotation",
"code_annotation_value"
"code_annotation_value",

// for c4 level in api call
"container_demand",
"container_resource",
"container_service"
)

override fun run() {
Expand Down Expand Up @@ -86,23 +92,32 @@ class Runner : CliktCommand(help = "scan git to sql") {
val repo = ClassRepository(systemId, language, path)

if (withApi) {
when(language.lowercase()) {
when (language.lowercase()) {
"typescript", "javascript" -> {

}
}
}


val feApiAnalysis = FrontendApiAnalyser()
// save class first, and can query dependencies for later
dataStructs.forEach { data ->
repo.saveClassItem(data)
feApiAnalysis.analysisByNode(data, path)
}

// save class imports, callees and parent
dataStructs.forEach { data ->
repo.saveClassBody(data)
}

val containerRepository = ContainerRepository(systemId, language, path)
val apiCalls = feApiAnalysis.toContainerServices()
containerRepository.saveContainerServices(apiCalls)

containerRepository.close()

repo.close()
}

Expand Down
Expand Up @@ -46,7 +46,7 @@ class FrontendApiAnalyser {
return toContainerServices()
}

private fun analysisByNode(node: CodeDataStruct, workspace: String) {
fun analysisByNode(node: CodeDataStruct, workspace: String) {
var isComponent: Boolean
val isComponentExt = node.fileExt() == "tsx" || node.fileExt() == "jsx"
val isNotInterface = node.Type != DataStructType.INTERFACE
Expand Down Expand Up @@ -94,7 +94,7 @@ class FrontendApiAnalyser {
}
}

private fun toContainerServices(): Array<ContainerService> {
public fun toContainerServices(): Array<ContainerService> {
var componentCalls: Array<ContainerService> = arrayOf()
componentInbounds.forEach { map ->
val componentRef = ContainerService(name = map.key)
Expand Down

0 comments on commit 8301f81

Please sign in to comment.