-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GitHub action and Gradle task to combine the parts of a component…
…'s description
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.build/buildSrc/src/main/kotlin/documentation/tasks/combine-parts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |