Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Preserve release notes when adding mobile build information (#4737)

Sure Admin (bot)c032bf4d1b50
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 7246305737..6a0c682069 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -675,6 +675,75 @@ jobs:
echo "Release assets:"
ls -la "${{ runner.temp }}/release-assets/"
+ - name: Prepare release notes
+ uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
+ env:
+ RELEASE_TAG: ${{ github.ref_name }}
+ ALIAS_NOTE: ${{ needs.prepare.outputs.alias_note }}
+ with:
+ script: |
+ const fs = require('node:fs');
+ const path = require('node:path');
+ const tag = process.env.RELEASE_TAG;
+ let body;
+ try {
+ const release = await github.rest.repos.getReleaseByTag({ ...context.repo, tag });
+ body = release.data.body ?? '';
+ } catch (error) {
+ // Never replace existing notes after an authentication or server error.
+ if (error.status !== 404) throw error;
+ const notes = await github.rest.repos.generateReleaseNotes({
+ ...context.repo, tag_name: tag
+ });
+ body = notes.data.body;
+ }
+
+ const mobile = [
+ '## Mobile Debug Builds',
+ '',
+ 'This release includes debug builds of the mobile applications. Download from the `Assets` area below.',
+ '',
+ '- **Android APK**: Debug build for testing on Android devices',
+ '- **iOS Build**: Unsigned iOS build (requires code signing for installation)',
+ '',
+ '> **Note**: These are debug builds intended for testing purposes. For production use, please build from source with proper signing credentials.'
+ ].join('\n');
+
+ // Move only the exact workflow-owned notice, never an entire Markdown section.
+ // Removing its separator too makes reruns idempotent without trimming user text.
+ for (const notice of [mobile, mobile.replace('applications. Download', 'applications. Download')]) {
+ for (const lineEnding of ['\n', '\r\n']) {
+ const matchingNotice = notice.replace(/\n/g, lineEnding);
+ body = body.split(matchingNotice + lineEnding + lineEnding).join('').split(matchingNotice).join('');
+ }
+ }
+ const alias = process.env.ALIAS_NOTE || '';
+ const additions = alias && !body.split(/\r?\n/).includes(alias)
+ ? alias + '\n\n' + mobile : mobile;
+ // Track fenced examples without rewriting any authored line endings.
+ let fence = null;
+ let index = body.length;
+ for (const line of body.matchAll(/[^\n]*(?:\n|$)/g)) {
+ const text = line[0].replace(/\r?\n$/, '');
+ const marker = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(text);
+ if (fence) {
+ if (marker && marker[1][0] === fence[0] && marker[1].length >= fence.length && /^[ \t]*$/.test(marker[2])) {
+ fence = null;
+ }
+ continue;
+ }
+ if (marker && (marker[1][0] !== '`' || !marker[2].includes('`'))) {
+ fence = marker[1];
+ } else if (/^#{1,6}[ \t]+What['’]s Changed[ \t]*#*[ \t]*$/.test(text)) {
+ index = line.index;
+ break;
+ }
+ }
+ const before = body.slice(0, index);
+ const separator = !before || /(?:\r?\n){2}$/.test(before) ? '' : before.endsWith('\n') ? '\n' : '\n\n';
+ body = before + separator + additions + '\n\n' + body.slice(index);
+ fs.writeFileSync(path.join(process.env.RUNNER_TEMP, 'release-notes.md'), body);
+
- name: Create GitHub Release
uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0
with:
@@ -682,17 +751,7 @@ jobs:
name: ${{ needs.prepare.outputs.release_name }}
draft: false
prerelease: ${{ needs.prepare.outputs.stable != 'true' }}
- generate_release_notes: true
+ generate_release_notes: false
+ body_path: ${{ runner.temp }}/release-notes.md
files: |
${{ runner.temp }}/release-assets/*
- body: |
- ${{ needs.prepare.outputs.alias_note }}
-
- ## Mobile Debug Builds
-
- This release includes debug builds of the mobile applications. Download from the `Assets` area below.
-
- - **Android APK**: Debug build for testing on Android devices
- - **iOS Build**: Unsigned iOS build (requires code signing for installation)
-
- > **Note**: These are debug builds intended for testing purposes. For production use, please build from source with proper signing credentials.
diff --git a/test/workflows/release_notes_workflow_test.rb b/test/workflows/release_notes_workflow_test.rb
new file mode 100644
index 0000000000..7417b424c2
--- /dev/null
+++ b/test/workflows/release_notes_workflow_test.rb
@@ -0,0 +1,140 @@
+require "test_helper"
+require "open3"
+require "tmpdir"
+require "yaml"
+
+class ReleaseNotesWorkflowTest < ActiveSupport::TestCase
+ test "preserves authored text and changelog around the mobile notice" do
+ intro = "# Highlights\n\nKeep `code`, $variables, and [links](https://example.com). \n\n"
+ changes = "## What's Changed\n* Original entry\n\n**Full Changelog**: original\n"
+ result = compose(intro + changes)
+ assert result.start_with?(intro)
+ assert result.end_with?(changes)
+ assert_includes result, "credentials.\n\n#{changes}"
+ assert_equal result, compose(result)
+ end
+
+ test "moves the legacy notice without removing surrounding custom content" do
+ mobile = compose("").delete_suffix("\n\n")
+ body = "#{mobile}\n\n# Maintainer highlights\n\nCustom mobile advice\n\n## What's Changed\n* Original\n"
+ result = compose(body)
+ assert result.start_with?("# Maintainer highlights\n\nCustom mobile advice\n\n")
+ assert_equal 1, result.scan("## Mobile Debug Builds").size
+ assert_equal result, compose(result)
+ assert_equal result, compose(body.sub("applications. Download", "applications. Download"))
+ end
+
+ test "appends without dropping text when no changelog heading exists" do
+ [ "", "Custom notes without a trailing newline", "Custom\n", "Custom\n\n" ].each do |body|
+ result = compose(body)
+ assert result.start_with?(body)
+ assert_includes result, "## Mobile Debug Builds"
+ assert_equal result, compose(result)
+ end
+ end
+
+ test "supports curly apostrophes and preserves CRLF changelog text" do
+ changes = "## What’s Changed\r\n* Keep me\r\n"
+ result = compose("Highlights\r\n\r\n#{changes}")
+ assert result.start_with?("Highlights\r\n\r\n")
+ assert result.end_with?(changes)
+ assert_equal result, compose(result)
+ end
+
+ test "relocates CRLF notices without changing authored line endings" do
+ mobile = compose("").delete_suffix("\n\n").gsub("\n", "\r\n")
+ intro = "# Highlights\r\n\r\nKeep this text. \r\n\r\n"
+ changes = "## What's Changed\r\n* Original\r\n"
+ [ mobile, mobile.sub("applications. Download", "applications. Download") ].each do |notice|
+ result = compose("#{notice}\r\n\r\n#{intro}#{changes}")
+ assert result.start_with?(intro)
+ assert result.end_with?(changes)
+ assert_equal 1, result.scan("## Mobile Debug Builds").size
+ assert_equal result, compose(result)
+ assert_equal result, compose(result.gsub(/\r?\n/, "\r\n"))
+ end
+ end
+
+ test "ignores changelog headings inside backtick and tilde fences" do
+ [ "```markdown", "~~~~markdown", " ````markdown" ].each do |opening|
+ fence = opening.strip.delete_suffix("markdown")
+ example = "#{opening}\n## What's Changed\n```\n~~~\n## What's Changed\n#{fence}\n\n"
+ # A shorter or different-character fence must not end the outer fence.
+ example = "#{opening}\n## What's Changed\n#{fence}\n\n" if fence == "```"
+ changes = "## What’s Changed\n* Actual change\n"
+ result = compose(example + changes)
+ assert result.start_with?(example)
+ assert result.end_with?(changes)
+ assert_includes result, "credentials.\n\n#{changes}"
+ assert_equal result, compose(result)
+ assert compose(example).start_with?(example)
+ end
+ end
+
+ test "preserves and deduplicates compatibility aliases" do
+ alias_note = "Compatibility alias: v0.7.7 (same commit and image)."
+ result = compose("Intro\n\n## What's Changed\n* Change", alias_note: alias_note)
+ assert_equal 1, result.scan(alias_note).size
+ assert_equal result, compose(result, alias_note: alias_note)
+ existing = "#{alias_note}\n\nIntro\n\n## What's Changed\n"
+ assert compose(existing, alias_note: alias_note).start_with?("#{alias_note}\n\nIntro\n\n")
+ end
+
+ test "generates notes only for missing releases and fails closed on API errors" do
+ result = compose(nil, status: 404)
+ assert result.end_with?("## What's Changed\n* Generated\n")
+ [ 401, 403, 500 ].each do |status|
+ assert_nil compose("Do not replace", status: status)
+ end
+ assert_includes compose(nil), "## Mobile Debug Builds"
+ end
+
+ test "publisher uses the prepared body without regenerating or appending notes" do
+ release = steps.find { |step| step["name"] == "Create GitHub Release" }.fetch("with")
+ assert_equal false, release.fetch("generate_release_notes")
+ assert_equal "${{ runner.temp }}/release-notes.md", release.fetch("body_path")
+ assert_not release.key?("body")
+ assert_not release["append_body"]
+ end
+
+ private
+
+ def steps
+ YAML.load_file(Rails.root.join(".github/workflows/publish.yml")).fetch("jobs").fetch("release").fetch("steps")
+ end
+
+ def compose(body, status: nil, alias_note: "")
+ script = steps.find { |step| step["name"] == "Prepare release notes" }.fetch("with").fetch("script")
+ Dir.mktmpdir do |dir|
+ harness = <<~JS
+ const assert = require('node:assert/strict');
+ const context = { repo: { owner: 'example', repo: 'sure' } };
+ let generated = 0;
+ const github = { rest: { repos: {
+ getReleaseByTag: async (args) => {
+ assert.equal(args.tag, 'v2026.11.4');
+ if (#{status.to_json}) throw Object.assign(new Error('API failure'), { status: #{status.to_json} });
+ return { data: { body: #{body.to_json} } };
+ },
+ generateReleaseNotes: async (args) => {
+ generated++;
+ assert.equal(args.tag_name, 'v2026.11.4');
+ return { data: { body: "## What's Changed\\n* Generated\\n" } };
+ }
+ } } };
+ (async () => {
+ #{script}
+ assert.equal(generated, #{status == 404 ? 1 : 0});
+ })().catch(error => { console.error(error); process.exitCode = 1; });
+ JS
+ output, result = Open3.capture2e({ "RUNNER_TEMP" => dir, "RELEASE_TAG" => "v2026.11.4", "ALIAS_NOTE" => alias_note }, "node", "-e", harness)
+ if status && status != 404
+ assert_not result.success?
+ assert_not File.exist?(File.join(dir, "release-notes.md"))
+ return nil
+ end
+ assert result.success?, output
+ File.read(File.join(dir, "release-notes.md"))
+ end
+ end
+end