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

Bitrise to Mermaid converter #148

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ I have a complex environment setup on Windows, this repository shall contain thi
* [svn2git-migration](scripts/special/svn2git-migration) A script to migrate from an SVN monorepo into git repository(s).
* [local-intellij](scripts/special/local-intellij) How to start Android Studio (IntelliJ IDEA) without a trace in a specific directory.
* [pst-maildir-imap](scripts/special/pst-maildir-imap) A process to convert a .pst file to Maildir format and then sync with an IMAP server.
* [bitrise-mermaid](scripts/special/bitrise-mermaid) Convert Bitrise configuration to Mermaid diagram.
* [Git](scripts/git)
* [github-away](scripts/git/github-away) How to set up GitHub and git on a foreign machine.
* [merged-branches](scripts/git/merged-branches) Find squashed branches from `git log`.
Expand Down
2 changes: 2 additions & 0 deletions scripts/special/bitrise-mermaid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bitrise-*.yml
/bitrise-*.mermaid
8 changes: 8 additions & 0 deletions scripts/special/bitrise-mermaid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Bitrise to Mermaid

This script converts a Bitrise configuration file to a Mermaid diagram.
It shows the dependencies between workflows and triggers.

```shell
kotlin bitrise-mermaid.main.kts bitrise.yml > bitrise.mermaid
```
114 changes: 114 additions & 0 deletions scripts/special/bitrise-mermaid/bitrise-mermaid.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@file:DependsOn("org.snakeyaml:snakeyaml-engine:2.7")

import org.snakeyaml.engine.v2.api.Load
import org.snakeyaml.engine.v2.api.LoadSettings
import java.io.File

fun main(vararg args: String) {
val bitriseFile = File(args[0])
val load = Load(LoadSettings.builder().build())
val bitriseYaml = load.loadFromInputStream(bitriseFile.inputStream())
val bitrise = parse(bitriseYaml)
val mermaid = render(bitrise)
println(mermaid)
}

data class Workflow(
val id: String,
val title: String,
val beforeRun: List<String>,
val afterRun: List<String>,
val buildRouterStartSteps: List<List<String>>
)

data class BitriseYaml(
val triggers: Map<String, String>,
val workflows: Map<String, Workflow>,
)

@Suppress("UNCHECKED_CAST")
fun parse(data: Any): BitriseYaml {
val bitrise = data as Map<String, Any>

val triggerMap = bitrise["trigger_map"] as List<Map<String, Any>>
val triggers = triggerMap.associate {
val trigger = it.filterNot { it.key == "workflow" }
val triggeredWorkflow = it["workflow"] as String
trigger.toString() to triggeredWorkflow
}

val workflows = bitrise["workflows"] as Map<String, Map<String, Any>>
val bitriseWorkflows = workflows.mapValues { (id, workflow) ->
val title = workflow["title"] as String? ?: ""
val beforeRun = workflow["before_run"] as List<String>? ?: emptyList()
val afterRun = workflow["after_run"] as List<String>? ?: emptyList()
val steps = workflow["steps"] as List<Map<String, Map<String, Any>>>? ?: emptyList()
val stepsMap = steps.map { it.entries.single() }.associate { it.key to it.value }
val buildRouterStartSteps = stepsMap
.filterKeys { name -> name.startsWith("build-router-start@") }
.mapValues { (_, step) ->
val inputs = step["inputs"] as List<Map<String, Any>>
val inputsMap = inputs.reduce(Map<String, Any>::plus)
(inputsMap["workflows"] as String).lines()
}
.map { it.value }
Workflow(id, title, beforeRun, afterRun, buildRouterStartSteps)
}
return BitriseYaml(triggers, bitriseWorkflows)
}

@Suppress("detekt.CyclomaticComplexMethod")
fun render(bitrise: BitriseYaml): String = buildString {
appendLine("%%{init: {'flowchart': {'curve': 'bumpX'}}}%%")
appendLine("graph LR") // https://mermaid.js.org/syntax/flowchart.html
appendLine(
"""
style trigger_map fill:#FFD700,stroke:#000000,stroke-width:4px,color:black;
classDef workflow fill:#46B8FF,stroke:#FFFFFF,stroke-width:2px,color:black;
classDef complexStep fill:#D9D9D9,stroke:#FFFFFF,stroke-width:2px,color:black;
""".trimIndent()
)
appendLine("trigger_map((trigger_map))")

fun workflow(workflowId: String) {
if (workflowId.startsWith("_")) {
appendLine("${workflowId}[${workflowId}]:::complexStep")
} else {
appendLine("${workflowId}{{${workflowId}}}:::workflow")
}
}

fun triggers(trigger: String, workflow: String) {
appendLine("trigger_map -- ${trigger} --> ${workflow}")
}

fun calls(caller: String, callee: String) {
appendLine("${caller} --> ${callee}")
}

fun dispatch(caller: String, callee: String) {
appendLine("${caller} -.-> ${callee}")
}


bitrise.triggers.forEach { (trigger, workflow) ->
triggers(trigger, workflow)
}
bitrise.workflows.forEach { (workflowName, workflow) ->
workflow(workflowName)
workflow.beforeRun.forEach { beforeRun ->
calls(workflowName, beforeRun)
}
workflow.afterRun.forEach { afterRun ->
calls(workflowName, afterRun)
}
workflow.buildRouterStartSteps.forEach { workflows ->
workflows.forEach { parallelWorkflow ->
dispatch(workflowName, parallelWorkflow)
}
}
}
}

@Suppress("detekt.SpreadOperator")
main(*args)