Merge pull request #19995 from Homebrew/fix_truncate

dev-cmd/bump-formula-pr: use `Formatter.truncate`.
This commit is contained in:
Mike McQuaid 2025-05-21 16:16:56 +00:00 committed by GitHub
commit 4887d58826
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 2 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

View File

@ -34,8 +34,9 @@ task test: :build do
], ],
ignore_urls: [ ignore_urls: [
"/", "/",
%r{https://formulae.brew.sh"}, %r{https://formulae.brew.sh},
%r{https://github.com/}, %r{https://github.com/},
%r{https://homebrew.1password.com/},
"https://legacy.python.org/dev/peps/pep-0453/#recommendations-for-downstream-distributors", "https://legacy.python.org/dev/peps/pep-0453/#recommendations-for-downstream-distributors",
], ],
cache: { cache: {