From c57d3e59c081dbdc40e7dc6ff74bd66b5b4d0378 Mon Sep 17 00:00:00 2001 From: Audun Solemdal Date: Wed, 29 Nov 2023 20:19:03 +0100 Subject: [PATCH] Wiki: add outline (#5) --- .github/workflows/update-wiki.yaml | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 .github/workflows/update-wiki.yaml diff --git a/.github/workflows/update-wiki.yaml b/.github/workflows/update-wiki.yaml new file mode 100644 index 0000000..da0fcd2 --- /dev/null +++ b/.github/workflows/update-wiki.yaml @@ -0,0 +1,89 @@ +name: "Update Wiki" +on: + workflow_call: {} + +jobs: + update-wiki: + runs-on: ubuntu-latest + name: "Update Wiki" + permissions: + contents: write + steps: + - uses: dawidd6/action-download-artifact@v2 + with: + workflow: unused + run_id: ${{ github.event.client_payload.run_id }} + path: outputs + + - name: Checkout wiki + uses: actions/checkout@v4 + with: + repository: ${{ github.repository }}.wiki + token: ${{ secrets.GITHUB_TOKEN }} + path: wiki + fetch-depth: 0 + - name: Ensure wiki folder + run: "[ -d wiki/generated ] || mkdir wiki/generated" + - name: Display structure of retrieved files + run: ls -R + + - uses: actions/github-script@v7 + id: write-changes + with: + script: | + const { owner, repo } = context.repo; + const { readdir, readFile, writeFile } = require('fs').promises; + const utf8 = { encoding: 'utf-8' }; + const msgParts = []; + + const newPage = ` + + + `; + + for (const folder of await readdir('outputs', { withFileTypes: true })) { + if (!folder.isDirectory()) continue; + const readText = (name) => readFile(name, utf8).then(x => x.trim()); + + const wikiPath = `wiki/generated/${folder.name}.md`; + const prevWiki = await readText(wikiPath) + .catch(err => err.code === 'ENOENT' ? newPage : Promise.reject(err)); + if (!prevWiki) console.warn('Starting new wiki page for', folder.name); + + const newVersion = await readText(`outputs/${folder.name}/new-version.txt`); + const docs = await readText(`outputs/${folder.name}/documentation.md`); + const changelog = await readText(`outputs/${folder.name}/changelog.md`); + + let newWiki = prevWiki; + if (!newWiki.match(/^$/m)) { + newWiki += `\n\n\n`; + } + newWiki = newWiki.replace(/^$[^]+^$/m, + `\n${docs}\n`); + + if (!newWiki.match(/^$/m)) { + newWiki += `\n\n# Changelog\n\n`; + } + const changelogHeader = `## ${newVersion} (${new Date().toISOString().split('T')[0]})` + newWiki = newWiki.replace(/^$/m, + `\n${changelogHeader}\n${changelog}\n`); + + await writeFile(wikiPath, newWiki, utf8); + msgParts.push(`${folder.name} @ ${newVersion}`); + console.log(`Updated ${folder.name} @ ${newVersion}`); + } + return msgParts.join(', '); + result-encoding: string + + - name: Push changes to wiki + working-directory: wiki + run: | + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git add -A + git commit -m "${{ steps.write-changes.outputs.result }}" + git push