Skip to content

Instantly share code, notes, and snippets.

@ogerardin
Last active November 27, 2023 16:20
Show Gist options
  • Save ogerardin/7e7789fbcae341cbd6cffa8f292157c6 to your computer and use it in GitHub Desktop.
Save ogerardin/7e7789fbcae341cbd6cffa8f292157c6 to your computer and use it in GitHub Desktop.
Export Archi diagrams to Confluence
#! /bin/bash
# This script will export the model's views and upload them to a Confluence page.
# Tested on Windows with bash 5.2.21 (git-bash), Archi 5.2.0, Confluence server 7.19.16
# Requirements: curl, jq, xmllint
#
# Ref:
# Confluence API doc: https://docs.atlassian.com/ConfluenceServer/rest/7.19.16/
# Archi CLI doc: https://github.com/archimatetool/archi/wiki/Archi-Command-Line-Interface
# Confluence base URI
BASEURL=https://......
# ID of target Confluence page (must exist beforehand)
PAGEID=......
# Personal access token for Confluence API authentication
TOKEN=.......
uploadAttachment() {
local -r file=$1
local -r basename=$(basename "$file")
# query attachment
result=$(curl --silent --location "$BASEURL/rest/api/content/$PAGEID/child/attachment?filename=$basename&expand=version" \
--header "Authorization: Bearer $TOKEN") || return 1
size=$(echo "$result" | jq -r ".size")
if [ $size -ne 0 ]; then
attachmentId=$(echo "$result" | jq -r ".results[0].id")
version=$(echo "$result" | jq ".results[0].version.number")
echo " attachment exists (id=$attachmentId, version=$version) - updating"
result=$(curl --silent --location "$BASEURL/rest/api/content/$PAGEID/child/attachment/$attachmentId/data" \
--header "X-Atlassian-Token: nocheck" \
--header "Authorization: Bearer $TOKEN" \
--form "file=@$file") || return 1
version=$(echo "$result" | jq ".version.number")
echo " new version: $version"
else
echo " new attachment - creating"
result=$(curl --silent --location "$BASEURL/rest/api/content/$PAGEID/child/attachment" \
--header "X-Atlassian-Token: nocheck" \
--header "Authorization: Bearer $TOKEN" \
--form "file=@$file") || return 1
attachmentId=$(echo "$result" | jq -r ".results[0].id")
version=$(echo "$result" | jq ".results[0].version.number")
echo " attachment created (id=$attachmentId, version=$version)"
fi
}
updateBody() {
echo " Querying page $PAGEID"
result=$(curl --silent --location "$BASEURL/rest/api/content/$PAGEID?expand=body.view,version" --header "Authorization: Bearer $TOKEN") || return 1
version=$(echo "$result" | jq ".version.number")
title=$(echo "$result" | jq -r ".title")
# body=$(echo "$result" | jq -r ".body.view.value")
echo " version=$version, title=$title"
echo " Updating body"
((version++))
result=$(curl --silent --location --request PUT "$BASEURL/rest/api/content/$PAGEID" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
--data "{
\"version\": {
\"number\": $version
},
\"type\": \"page\",
\"title\": \"$title\",
\"body\": {
\"storage\": {
\"value\": $(echo "$BODY" | jq -Rsa .),
\"representation\": \"storage\"
}
}
}") || return 1
echo " new version: $version"
}
#
# main
#
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR"/.. || exit 1
echo "Generating Archi HTML interface..."
Archi -application com.archimatetool.commandline.app -consoleLog -nosplash --modelrepository.loadModel . \
--html.createReport output/html || exit 1
echo "Generating Archi PDF report..."
Archi -application com.archimatetool.commandline.app -consoleLog -nosplash --modelrepository.loadModel . \
--jasper.filename report -jasper.format PDF --jasper.title "ArchiMate report" --jasper.createReport output/report || exit 1
modelId=$(xmllint --xpath 'string(//*/@id)' model/folder.xml) || exit 1
echo "Found model: $modelId"
BODY="<ac:structured-macro ac:name=\"info\">
<ac:rich-text-body>
<p><b>This page was generated automatically from Archi model: $(git remote get-url origin) on $(date)</b></p>
<p><b>Do not edit manually!</b></p>
</ac:rich-text-body>
</ac:structured-macro>"
echo "Attaching PDF report to page $PAGEID"
pdf=output/report/report.pdf
uploadAttachment "$pdf"
BODY="$BODY<p>Full report: <ac:link><ri:attachment ri:filename=\"$(basename "$pdf")\"/></ac:link></p>"
shopt -s globstar nullglob
for f in model/diagrams/**/ArchimateDiagramModel_id-*.xml; do
echo "Found diagram $f"
viewName=$(xmllint --xpath 'string(//@name)' $f)
echo " name: $viewName"
id=$(basename "$f" .xml | cut -d- -f2)
img=output/html/$modelId/images/id-$id.png
echo "Found image: $(file "$img")"
echo "Attaching image to page $PAGEID"
uploadAttachment "$img"
BODY="$BODY<h1>$viewName</h1><ac:image ac:border='true'><ri:attachment ri:filename=\"$(basename "$img")\"/></ac:image><br/>"
# break
done
echo "Udpating page $PAGEID"
updateBody
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment