From b792a5fd2066d5952aeed4ea4024997e0af21282 Mon Sep 17 00:00:00 2001 From: Nanda H Krishna Date: Sat, 18 Mar 2023 10:44:35 -0400 Subject: [PATCH] utils/github/actions: add `format_multiline_string` method --- Library/Homebrew/utils/github/actions.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Library/Homebrew/utils/github/actions.rb b/Library/Homebrew/utils/github/actions.rb index c255dec4a0..84e447cd0c 100644 --- a/Library/Homebrew/utils/github/actions.rb +++ b/Library/Homebrew/utils/github/actions.rb @@ -1,6 +1,7 @@ # typed: true # frozen_string_literal: true +require "securerandom" require "utils/tty" module GitHub @@ -18,6 +19,24 @@ module GitHub .gsub("\r", "%0D") end + sig { params(name: String, value: String).returns(String) } + def self.format_multiline_string(name, value) + # Format multiline strings for environment files + # See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings + + delimiter = "ghadelimiter_#{SecureRandom.uuid}" + + if name.include?(delimiter) || value.include?(delimiter) + raise Error, "`name` and `value` must not contain the delimiter" + end + + <<~EOS + #{name}<<#{delimiter} + #{value} + #{delimiter} + EOS + end + # Helper class for formatting annotations on GitHub Actions. class Annotation extend T::Sig @@ -85,5 +104,9 @@ module GitHub @file.descend.next.to_s != ".." end end + + # Generic GitHub Actions error. + class Error < RuntimeError + end end end