Skip to content

Commit

Permalink
add GitHub action and Gradle task to combine the parts of a component…
Browse files Browse the repository at this point in the history
…'s description
  • Loading branch information
slu-it committed Aug 8, 2024
1 parent c4793d6 commit 32ffd22
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .build/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import documentation.generateDiagramsFromPlantUml
import documentation.generateEndpointOverviewDocumentFromJson
import documentation.generateEventsOverviewDocumentFromJson
import documentation.generateOverviewDiagramsFromJson
import documentation.tasks.generateComponentDescription

tasks.register("generateFiles") {
val srcFolder = File(project.rootDir, "src")
Expand All @@ -15,3 +16,12 @@ tasks.register("generateFiles") {
generateEventsOverviewDocumentFromJson(srcFolder, rootFolder)
}
}

tasks.register("combineParts") {
val sourceFolder = File(project.rootDir, "tmp/parts")
val targetFolder = File(project.rootDir, "src/json/components")
val componentId = project.property("componentId") as String
doLast {
generateComponentDescription(sourceFolder, targetFolder, componentId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package documentation.tasks

import com.fasterxml.jackson.annotation.JsonInclude.Include
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import documentation.model.Application
import documentation.model.Dependency
import documentation.model.Dependent
import documentation.model.Event
import documentation.model.HttpEndpoint
import java.io.File
import kotlin.reflect.KClass

private val objectMapper = jacksonObjectMapper()
.setSerializationInclusion(Include.NON_EMPTY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.enable(SerializationFeature.INDENT_OUTPUT)

fun generateComponentDescription(sourceFolder: File, targetFolder: File, applicationId: String) {
val baseApplicationDescription = loadBaseApplicationDescription(sourceFolder, applicationId)
val dependents = loadDependents(sourceFolder)
val dependencies = loadDependencies(sourceFolder)
val events = loadEvents(sourceFolder)

val applicationDescription = baseApplicationDescription
.copy(dependents = dependents, dependencies = dependencies, events = events)

val file = File(targetFolder, applicationDescription.id + ".json")
objectMapper.writeValue(file, applicationDescription)
}

private fun loadBaseApplicationDescription(sourceFolder: File, applicationId: String): Application {
val file = File(sourceFolder, "$applicationId.json")
check(file.isFile) { "File not found: $file" }
return objectMapper.readValue<Application>(file)
}

private fun loadDependents(sourceFolder: File): List<Dependent> =
listJsonFilesInFolder(File(sourceFolder, "dependents"))
.map { file -> loadDependent(file) }

private fun loadDependent(file: File): Dependent {
return objectMapper.readValue<Dependent>(file)
}

private fun loadDependencies(sourceFolder: File): List<Dependency> =
listJsonFilesInFolder(File(sourceFolder, "dependencies"))
.map { file -> loadDependency(file) }

private fun loadDependency(file: File): Dependency {
val dependency = objectMapper.readValue<Dependency>(file)

var httpEndpoints = emptyList<HttpEndpoint>()

val httpEndpointsFile = File(file.parentFile, "http-endpoints/${dependency.id}.jsonl")
if (httpEndpointsFile.isFile) {
httpEndpoints = loadFromJsonListFile(httpEndpointsFile, HttpEndpoint::class)
.sortedWith(compareBy(HttpEndpoint::path, HttpEndpoint::method))
.distinct()
}

return dependency.copy(httpEndpoints = httpEndpoints)
}

private fun loadEvents(sourceFolder: File): List<Event> =
listJsonFilesInFolder(File(sourceFolder, "events"))
.map { file -> loadEvent(file) }

private fun loadEvent(file: File): Event {
return objectMapper.readValue<Event>(file)
}

private fun listJsonFilesInFolder(folder: File): List<File> =
if (folder.isDirectory) {
folder.listFiles()!!
.filter { it.isFile }
.filter { it.extension == "json" }
} else {
emptyList()
}

private fun <T : Any> loadFromJsonListFile(file: File, clazz: KClass<T>): List<T> =
file.readLines()
.filter(String::isNotBlank)
.map(String::trim)
.map { objectMapper.readValue(it, clazz.java) }
51 changes: 51 additions & 0 deletions .github/actions/update-component/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: "Update Component"
description: "Downloads the parts of a component's architecture documentation, combines them and updates the existing file."

inputs:
component-id:
description: "The ID of the component."
required: true
github-token:
description: "A GitHub token that can be used to write to the repositories content."
required: true

runs:
using: composite
steps:
- name: "Checkout"
uses: actions/checkout@v4
with:
repository: automatic-architecture-documentation/documentation
token: ${{ inputs.github-token }}
- name: "Setup Java"
uses: actions/setup-java@v4
with:
java-version: "17"
distribution: "temurin"
- name: "Setup Gradle"
uses: gradle/gradle-build-action@v3
with:
gradle-version: wrapper
- name: "Download Architecture Documentation Parts"
uses: actions/download-artifact@v4
with:
name: architecture-documentation-parts
path: .build/tmp/parts
- name: "Setup Gradle Wrapper"
working-directory: .build
shell: bash
run: chmod +x ./gradlew
- name: "Combine Parts"
working-directory: .build
shell: bash
run: './gradlew --no-daemon combineParts -PcomponentId=${{ inputs.component-id }}'
- name: "Commit & Push Changes"
shell: bash
run: |
git config --global user.name "Botty"
git config --global user.email "bot@example.com"
git add .build/src/json/components/${{ inputs.component-id }}.json
git diff --quiet --exit-code --cached || {
git commit -m "updated ${{ inputs.component-id }}.json"
git push origin HEAD:master
}

0 comments on commit 32ffd22

Please sign in to comment.