dev-cmd/bump-formula-pr: use Formatter.truncate.

Add new `Formatter.truncate` method, tests and use it.
This commit is contained in:
Mike McQuaid 2025-05-21 17:02:18 +01:00
parent daeb5b1dfe
commit 4aa7f83954
No known key found for this signature in database
3 changed files with 28 additions and 1 deletions

View File

@ -400,7 +400,7 @@ module Homebrew
if github_release_data.present? if github_release_data.present?
pre = "pre" if github_release_data["prerelease"].present? pre = "pre" if github_release_data["prerelease"].present?
# maximum length of PR body is 65,536 characters so let's truncate release notes to half of that. # maximum length of PR body is 65,536 characters so let's truncate release notes to half of that.
body = github_release_data["body"].truncate(32_768) body = Formatter.truncate(github_release_data["body"], max: 32_768)
formula_pr_message += <<~XML formula_pr_message += <<~XML
<details> <details>

View File

@ -110,4 +110,18 @@ RSpec.describe Formatter do
expect(described_class.format_help_text(text, width: 80)).to eq expected expect(described_class.format_help_text(text, width: 80)).to eq expected
end end
end end
describe "::truncate" do
it "returns the original string if it's shorter than max length" do
expect(described_class.truncate("short", max: 10)).to eq("short")
end
it "truncates strings longer than max length" do
expect(described_class.truncate("this is a long string", max: 10)).to eq("this is...")
end
it "uses custom omission string" do
expect(described_class.truncate("this is a long string", max: 10, omission: " [...]")).to eq("this [...]")
end
end
end end

View File

@ -54,6 +54,19 @@ module Formatter
label(label, string, :red) label(label, string, :red)
end end
# Truncate a string to a specific length.
#
# @api internal
sig { params(string: String, max: Integer, omission: String).returns(String) }
def self.truncate(string, max: 30, omission: "...")
return string if string.length <= max
length_with_room_for_omission = max - omission.length
truncated = string[0, length_with_room_for_omission]
"#{truncated}#{omission}"
end
# Wraps text to fit within a given number of columns using regular expressions that: # Wraps text to fit within a given number of columns using regular expressions that:
# #
# 1. convert hard-wrapped paragraphs to a single line # 1. convert hard-wrapped paragraphs to a single line