diff --git a/common/lib/dependabot/pull_request_creator/message_builder.rb b/common/lib/dependabot/pull_request_creator/message_builder.rb index ef3177bfde3..0dd7d7d8ecb 100644 --- a/common/lib/dependabot/pull_request_creator/message_builder.rb +++ b/common/lib/dependabot/pull_request_creator/message_builder.rb @@ -85,7 +85,7 @@ def truncate_pr_message(msg) msg = (msg[0..trunc_length] + tr_msg) end # if we used a custom encoding for calculating length, then we need to force back to UTF-8 - msg.force_encoding(Encoding::UTF_8) unless pr_message_encoding.nil? + msg = msg.encode("utf-8", "binary", invalid: :replace, undef: :replace) unless pr_message_encoding.nil? msg end diff --git a/common/spec/dependabot/pull_request_creator/message_builder_spec.rb b/common/spec/dependabot/pull_request_creator/message_builder_spec.rb index bd7da9401e0..b7e884ba10e 100644 --- a/common/spec/dependabot/pull_request_creator/message_builder_spec.rb +++ b/common/spec/dependabot/pull_request_creator/message_builder_spec.rb @@ -2791,11 +2791,17 @@ def commits_details(base:, head:) trunc_msg = (+"...\n\n_Description has been truncated_").force_encoding(encode_utf16) trunc_length = pr_message_max_length - trunc_msg.length msg = "#{msg[0..trunc_length]}#{trunc_msg}" - msg = msg.force_encoding(Encoding::UTF_8) + msg = msg.encode("utf-8", "binary", invalid: :replace, undef: :replace) message_builder.pr_message_max_length = pr_message_max_length message_builder.pr_message_encoding = encode_utf16 expect(message_builder.truncate_pr_message(message)).to eq(msg) + + # ensure we can work convert to JSON (uses UTF-8 by default) + # this matches what happens in the azure client when creating a pull request + expect({ description: msg }.to_json) + .to start_with("{\"description\":\"") + .and end_with("\\n\\n_Description has been truncated_\"}") end end