From 8749e830fbba86345b24e2cb2827c5bc23d31200 Mon Sep 17 00:00:00 2001 From: Justin D'Errico Date: Fri, 1 Apr 2022 17:48:27 -0400 Subject: [PATCH] Do some YAML magic extraordinaire to rewrite the changelog (#108) --- .github/workflows/release.yml | 37 +++++++++++++++++++++-------------- scripts/rewrite-changelog.js | 27 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 scripts/rewrite-changelog.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4184a1e46..5918d54cc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,8 +12,8 @@ jobs: name: Release runs-on: ubuntu-latest outputs: - release_id: ${{ steps.persist_release.outputs.release_id }} released: ${{ steps.persist_release.outputs.released }} + changelog: ${{ steps.persist_release.outputs.changelog }} steps: - name: Checkout repository uses: actions/checkout@v2 @@ -48,10 +48,28 @@ jobs: env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - - id: persist_release + - name: Fetch Release + if: steps.release.outputs.released == 'true' + uses: cardinalby/git-get-release-action@v1 + id: fetch_release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + releaseId: ${{ steps.release.outputs.release_id }} + + - name: Output Release Changelog + if: steps.release.outputs.released == 'true' + run: echo "${{ steps.fetch_release.outputs.body }}" + + - name: Persist Release + if: steps.release.outputs.released == 'true' + id: persist_release + env: + RAW_CHANGELOG: ${{ steps.fetch_release.outputs.body }} + REPO_URL: ${{ github.repositoryUrl }} run: | - echo "::set-output name=release_id::${{ steps.release.outputs.release_id }}" echo "::set-output name=released::${{ steps.release.outputs.released }}" + echo "::set-output name=changelog::$(node ./scripts/rewrite-changelog.js RAW_CHANGELOG $REPO_URL)" bump_zapper_studio: name: Bump on Zapper API @@ -70,17 +88,6 @@ jobs: token: ${{ secrets.GH_ZAPPER_BOT }} ref: master - - name: Output Release ID - run: echo "Release ID ${{ needs.release.outputs.release_id }}" - - - name: Fetch Release - uses: cardinalby/git-get-release-action@v1 - id: fetch_release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - releaseId: ${{ needs.release.outputs.release_id }} - - name: Setup Node uses: actions/setup-node@v2 with: @@ -112,6 +119,6 @@ jobs: body: | ⚙️ _This pull request was opened automatically from Zapper-fi/studio._ - ${{ steps.fetch_release.outputs.body }} + ${{ needs.release.outputs.changelog }} base: master delete-branch: true diff --git a/scripts/rewrite-changelog.js b/scripts/rewrite-changelog.js new file mode 100644 index 000000000..c8d83b2c0 --- /dev/null +++ b/scripts/rewrite-changelog.js @@ -0,0 +1,27 @@ +const [envVarTarget, repoUrl] = process.argv.slice(2); + +function run() { + const rawChangelog = process.env[envVarTarget]; + const prSectionExpr = /.*(\(#(\d.*)\))$/; + + const lines = rawChangelog.split('\n'); + const rewrittenLines = lines + .map(line => { + try { + // In commit `whatever (#234)`, extract `(#234)` and `234` + const [, prSection, prNumber] = prSectionExpr.exec(line); + // Rewrite line using a URL of the pull request rather than just `(#234)` + const [lineStart] = line.split(prSection); + const rewrittenPrSection = `(${repoUrl}/pull/${prNumber})`; + return [lineStart, rewrittenPrSection].join(''); + } catch (e) { + return line; + } + }) + .join('\n'); + + /* eslint-disable-next-line no-console */ + console.log(rewrittenLines); +} + +run();