Skip to content

Commit

Permalink
Merge pull request dependabot#4450 from dependabot/lsep/max-pr-length
Browse files Browse the repository at this point in the history
Limit the GitHub PR description message to API max
  • Loading branch information
lseppala authored Nov 30, 2021
2 parents 6c545bd + 17a2858 commit 5456c0d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions common/lib/dependabot/pull_request_creator/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Dependabot
class PullRequestCreator
# rubocop:disable Metrics/ClassLength
class Github
MAX_PR_DESCRIPTION_LENGTH = 65_536 # characters (see #create_pull_request)

attr_reader :source, :branch_name, :base_commit, :credentials,
:files, :pr_description, :pr_name, :commit_message,
:author_details, :signature_key, :custom_headers,
Expand Down Expand Up @@ -347,6 +349,18 @@ def add_milestone_to_pull_request(pull_request)
end

def create_pull_request
# Limit PR description to MAX_PR_DESCRIPTION_LENGTH (65,536) characters
# and truncate with message if over. The API limit is 262,144 bytes
# (https://github.community/t/maximum-length-for-the-comment-body-in-issues-and-pr/148867/2).
# As Ruby strings are UTF-8 encoded, this is a pessimistic limit: it
# presumes the case where all characters are 4 bytes.
pr_description = @pr_description.dup
if pr_description && pr_description.length > MAX_PR_DESCRIPTION_LENGTH
truncated_msg = "...\n\n_Description has been truncated_"
truncate_length = MAX_PR_DESCRIPTION_LENGTH - truncated_msg.length
pr_description = (pr_description[0, truncate_length] + truncated_msg)
end

github_client_for_source.create_pull_request(
source.repo,
target_branch,
Expand Down
19 changes: 19 additions & 0 deletions common/spec/dependabot/pull_request_creator/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,25 @@
)
end
end

context "the PR description is too long" do
let(:pr_description) { "a" * (described_class::MAX_PR_DESCRIPTION_LENGTH + 1) }

it "truncates the description" do
creator.create

expect(WebMock).
to have_requested(:post, "#{repo_api_url}/pulls").
with(
body: {
base: "master",
head: "dependabot/bundler/business-1.5.0",
title: "PR name",
body: ->(body) { expect(body.length).to be <= described_class::MAX_PR_DESCRIPTION_LENGTH }
}
)
end
end
end
end
end

0 comments on commit 5456c0d

Please sign in to comment.