Skip to content

Commit

Permalink
Merge pull request #3191 from dependabot/base-updater-message-builder
Browse files Browse the repository at this point in the history
PullRequestCreator::Message
  • Loading branch information
thepwagner authored Mar 1, 2021
2 parents 351cf5f + ca0e30f commit 120a413
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 39 deletions.
54 changes: 34 additions & 20 deletions bin/dry-run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
commit: nil,
updater_options: {},
security_advisories: [],
security_updates_only: false
security_updates_only: false,
pull_request: false
}

unless ENV["LOCAL_GITHUB_ACCESS_TOKEN"].to_s.strip.empty?
Expand Down Expand Up @@ -207,6 +208,11 @@
"Profile using Stackprof. Output in `tmp/stackprof-<datetime>.dump`") do
$options[:profile] = true
end

opts.on("--pull-request",
"Output pull request information: title, description") do
$options[:pull_request] = true
end
end

option_parse.parse!
Expand Down Expand Up @@ -430,7 +436,7 @@ def handle_dependabot_error(error:, dependency:)

StackProf.start(raw: true) if $options[:profile]

source = Dependabot::Source.new(
$source = Dependabot::Source.new(
provider: $options[:provider],
repo: $repo_name,
directory: $options[:directory],
Expand All @@ -446,7 +452,7 @@ def handle_dependabot_error(error:, dependency:)
end

fetcher = Dependabot::FileFetchers.for_package_manager($package_manager).
new(source: source, credentials: $options[:credentials],
new(source: $source, credentials: $options[:credentials],
repo_contents_path: $repo_contents_path)
$files = if $options[:clone] || always_clone
fetcher.clone_repo_contents
Expand All @@ -462,7 +468,7 @@ def handle_dependabot_error(error:, dependency:)
parser = Dependabot::FileParsers.for_package_manager($package_manager).new(
dependency_files: $files,
repo_contents_path: $repo_contents_path,
source: source,
source: $source,
credentials: $options[:credentials],
reject_external_code: $options[:reject_external_code],
)
Expand Down Expand Up @@ -528,29 +534,24 @@ def peer_dependencies_can_update?(checker, reqs_to_unlock)
end

def file_updater_for(dependencies)
Dependabot::FileUpdaters.for_package_manager($package_manager).new(
dependencies: dependencies,
dependency_files: $files,
repo_contents_path: $repo_contents_path,
credentials: $options[:credentials],
options: $options[:updater_options]
)
end

def generate_dependency_files_for(updated_dependencies)
if updated_dependencies.count == 1
updated_dependency = updated_dependencies.first
if dependencies.count == 1
updated_dependency = dependencies.first
prev_v = updated_dependency.previous_version
prev_v_msg = prev_v ? "from #{prev_v} " : ""
puts " => updating #{updated_dependency.name} #{prev_v_msg}to " \
"#{updated_dependency.version}"
else
dependency_names = updated_dependencies.map(&:name)
dependency_names = dependencies.map(&:name)
puts " => updating #{dependency_names.join(', ')}"
end

updater = file_updater_for(updated_dependencies)
updater.updated_dependency_files
Dependabot::FileUpdaters.for_package_manager($package_manager).new(
dependencies: dependencies,
dependency_files: $files,
repo_contents_path: $repo_contents_path,
credentials: $options[:credentials],
options: $options[:updater_options],
)
end

def security_fix?(dependency)
Expand Down Expand Up @@ -650,7 +651,8 @@ def security_fix?(dependency)
next
end

updated_files = generate_dependency_files_for(updated_deps)
updater = file_updater_for(updated_deps)
updated_files = updater.updated_dependency_files

# Currently unused but used to create pull requests (from the updater)
updated_deps.reject do |d|
Expand Down Expand Up @@ -691,6 +693,18 @@ def security_fix?(dependency)
end
end
end

if $options[:pull_request]
msg = Dependabot::PullRequestCreator::MessageBuilder.new(
dependencies: updated_deps,
files: updated_files,
credentials: $options[:credentials],
source: $source,
).message
puts "Pull Request Title: #{msg.pr_name}"
puts "--description--\n#{msg.pr_message}\n--/description--"
puts "--commit--\n#{msg.commit_message}\n--/commit--"
end
rescue StandardError => e
handle_dependabot_error(error: e, dependency: dep)
end
Expand Down
37 changes: 19 additions & 18 deletions common/lib/dependabot/pull_request_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def initialize(source:, base_commit:, dependencies:, files:, credentials:,
label_language: false, automerge_candidate: false,
github_redirection_service: "github-redirect.dependabot.com",
custom_headers: nil, require_up_to_date_base: false,
provider_metadata: {})
provider_metadata: {}, message: nil)
@dependencies = dependencies
@source = source
@base_commit = base_commit
Expand All @@ -72,6 +72,7 @@ def initialize(source:, base_commit:, dependencies:, files:, credentials:,
@custom_headers = custom_headers
@require_up_to_date_base = require_up_to_date_base
@provider_metadata = provider_metadata
@message = message

check_dependencies_have_previous_version
end
Expand Down Expand Up @@ -116,9 +117,9 @@ def github_creator
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
commit_message: message.commit_message,
pr_description: message.pr_message,
pr_name: message.pr_name,
author_details: author_details,
signature_key: signature_key,
labeler: labeler,
Expand All @@ -137,9 +138,9 @@ def gitlab_creator
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
commit_message: message.commit_message,
pr_description: message.pr_message,
pr_name: message.pr_name,
author_details: author_details,
labeler: labeler,
approvers: reviewers,
Expand All @@ -155,9 +156,9 @@ def azure_creator
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
commit_message: message.commit_message,
pr_description: message.pr_message,
pr_name: message.pr_name,
author_details: author_details,
labeler: labeler,
work_item: provider_metadata&.fetch(:work_item, nil)
Expand All @@ -171,9 +172,9 @@ def bitbucket_creator
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
commit_message: message.commit_message,
pr_description: message.pr_message,
pr_name: message.pr_name,
author_details: author_details,
labeler: labeler,
work_item: provider_metadata&.fetch(:work_item, nil)
Expand All @@ -187,17 +188,17 @@ def codecommit_creator
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message_builder.commit_message,
pr_description: message_builder.pr_message,
pr_name: message_builder.pr_name,
commit_message: message.commit_message,
pr_description: message.pr_message,
pr_name: message.pr_name,
author_details: author_details,
labeler: labeler,
require_up_to_date_base: require_up_to_date_base?
)
end

def message_builder
@message_builder ||
def message
@message ||=
MessageBuilder.new(
source: source,
dependencies: dependencies,
Expand Down
16 changes: 16 additions & 0 deletions common/lib/dependabot/pull_request_creator/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Dependabot
class PullRequestCreator
# Message is a static alternative to MessageBuilder
class Message
attr_reader :commit_message, :pr_name, :pr_message

def initialize(commit_message: nil, pr_name: nil, pr_message: nil)
@commit_message = commit_message
@pr_name = pr_name
@pr_message = pr_message
end
end
end
end
10 changes: 10 additions & 0 deletions common/lib/dependabot/pull_request_creator/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
require "dependabot/clients/gitlab_with_retries"
require "dependabot/metadata_finders"
require "dependabot/pull_request_creator"
require "dependabot/pull_request_creator/message"

# rubocop:disable Metrics/ClassLength
module Dependabot
class PullRequestCreator
# MessageBuilder builds PR message for a dependency update
class MessageBuilder
require_relative "message_builder/metadata_presenter"
require_relative "message_builder/issue_linker"
Expand Down Expand Up @@ -56,6 +58,14 @@ def commit_message
message
end

def message
Dependabot::PullRequestCreator::Message.new(
pr_name: pr_name,
pr_message: pr_message,
commit_message: commit_message
)
end

private

def library_pr_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1812,4 +1812,24 @@ def commits_details(base:, head:)
end
end
end

describe "#message" do
subject(:message) { builder.message }

pr_name = "PR title"
pr_message = "PR message"
commit_message = "Commit message"
before do
allow(builder).to receive(:pr_name).and_return(pr_name)
allow(builder).to receive(:pr_message).and_return(pr_message)
allow(builder).to receive(:commit_message).and_return(commit_message)
end

it "returns a Message" do
expect(message).to be_a(Dependabot::PullRequestCreator::Message)
end
its(:pr_name) { should eq(pr_name) }
its(:pr_message) { should eq(pr_message) }
its(:commit_message) { should eq(commit_message) }
end
end
67 changes: 66 additions & 1 deletion common/spec/dependabot/pull_request_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "dependabot/dependency"
require "dependabot/dependency_file"
require "dependabot/pull_request_creator"
require "dependabot/pull_request_creator/message"

RSpec.describe Dependabot::PullRequestCreator do
subject(:creator) do
Expand Down Expand Up @@ -68,7 +69,7 @@
end
before do
allow(described_class::MessageBuilder).
to receive(:new).and_return(dummy_message_builder)
to receive(:new).once.and_return(dummy_message_builder)
allow(dummy_message_builder).
to receive(:commit_message).
and_return("Commit msg")
Expand Down Expand Up @@ -259,5 +260,69 @@
creator.create
end
end

context "with prebuilt message" do
subject(:creator) do
described_class.new(
source: source,
base_commit: base_commit,
dependencies: dependencies,
files: files,
credentials: credentials,
custom_labels: custom_labels,
reviewers: reviewers,
assignees: assignees,
milestone: milestone,
author_details: author_details,
signature_key: signature_key,
provider_metadata: provider_metadata,
message: message
)
end

let(:pr_name) { "my awesome PR" }
let(:pr_message) { "update dependency for more awesome" }
let(:commit_message) { "awesome" }
let(:message) do
Dependabot::PullRequestCreator::Message.new(
pr_name: pr_name,
pr_message: pr_message,
commit_message: commit_message
)
end
let(:source) { Dependabot::Source.new(provider: "github", repo: "gc/bp") }
let(:dummy_creator) { instance_double(described_class::Github) }

%i(pr_name pr_message commit_message).each do |field|
it "does not build a '#{field}' message" do
expect(dummy_message_builder).not_to receive(field)
end
end

it "delegates to PullRequestCreator::Github with correct params" do
expect(described_class::Github).
to receive(:new).
with(
source: source,
branch_name: "dependabot/bundler/business-1.5.0",
base_commit: base_commit,
credentials: credentials,
files: files,
author_details: author_details,
signature_key: signature_key,
custom_headers: nil,
labeler: instance_of(described_class::Labeler),
reviewers: reviewers,
assignees: assignees,
milestone: milestone,
require_up_to_date_base: false,
pr_name: pr_name,
pr_description: pr_message,
commit_message: commit_message
).and_return(dummy_creator)
expect(dummy_creator).to receive(:create)
creator.create
end
end
end
end

0 comments on commit 120a413

Please sign in to comment.