diff --git a/common/lib/dependabot/clients/azure.rb b/common/lib/dependabot/clients/azure.rb index b6904bab2f6..0e512a99851 100644 --- a/common/lib/dependabot/clients/azure.rb +++ b/common/lib/dependabot/clients/azure.rb @@ -172,7 +172,8 @@ def create_commit(branch_name, base_commit, commit_message, files, # rubocop:disable Metrics/ParameterLists def create_pull_request(pr_name, source_branch, target_branch, - pr_description, labels, work_item = nil) + pr_description, labels, + reviewers = nil, assignees = nil, work_item = nil) pr_description = truncate_pr_description(pr_description) content = { @@ -181,6 +182,7 @@ def create_pull_request(pr_name, source_branch, target_branch, title: pr_name, description: pr_description, labels: labels.map { |label| { name: label } }, + reviewers: pr_reviewers(reviewers, assignees), workItemRefs: [{ id: work_item }] } @@ -324,6 +326,13 @@ def tags_creation_forbidden?(response) message&.include?("TF401289") end + def pr_reviewers(reviewers, assignees) + return [] unless reviewers || assignees + + pr_reviewers = reviewers&.map { |r_id| { id: r_id, isRequired: true } } || [] + pr_reviewers + (assignees&.map { |r_id| { id: r_id, isRequired: false } } || []) + end + attr_reader :auth_header attr_reader :credentials attr_reader :source diff --git a/common/lib/dependabot/pull_request_creator.rb b/common/lib/dependabot/pull_request_creator.rb index 895745e6c4e..a264b8b23dd 100644 --- a/common/lib/dependabot/pull_request_creator.rb +++ b/common/lib/dependabot/pull_request_creator.rb @@ -176,6 +176,8 @@ def azure_creator pr_name: message.pr_name, author_details: author_details, labeler: labeler, + reviewers: reviewers, + assignees: assignees, work_item: provider_metadata&.fetch(:work_item, nil) ) end diff --git a/common/lib/dependabot/pull_request_creator/azure.rb b/common/lib/dependabot/pull_request_creator/azure.rb index 841166fbce3..1a139bf3631 100644 --- a/common/lib/dependabot/pull_request_creator/azure.rb +++ b/common/lib/dependabot/pull_request_creator/azure.rb @@ -8,11 +8,11 @@ class PullRequestCreator class Azure attr_reader :source, :branch_name, :base_commit, :credentials, :files, :commit_message, :pr_description, :pr_name, - :author_details, :labeler, :work_item + :author_details, :labeler, :reviewers, :assignees, :work_item def initialize(source:, branch_name:, base_commit:, credentials:, files:, commit_message:, pr_description:, pr_name:, - author_details:, labeler:, work_item: nil) + author_details:, labeler:, reviewers: nil, assignees: nil, work_item: nil) @source = source @branch_name = branch_name @base_commit = base_commit @@ -23,6 +23,8 @@ def initialize(source:, branch_name:, base_commit:, credentials:, @pr_name = pr_name @author_details = author_details @labeler = labeler + @reviewers = reviewers + @assignees = assignees @work_item = work_item end @@ -79,6 +81,8 @@ def create_pull_request source.branch || default_branch, pr_description, labeler.labels_for_pr, + reviewers, + assignees, work_item ) end diff --git a/common/spec/dependabot/pull_request_creator/azure_spec.rb b/common/spec/dependabot/pull_request_creator/azure_spec.rb index b0f1caf4a61..1996ad7a38d 100644 --- a/common/spec/dependabot/pull_request_creator/azure_spec.rb +++ b/common/spec/dependabot/pull_request_creator/azure_spec.rb @@ -18,6 +18,8 @@ pr_name: pr_name, author_details: author_details, labeler: labeler, + reviewers: reviewers, + assignees: assignees, work_item: work_item ) end @@ -40,8 +42,8 @@ let(:pr_description) { "PR msg" } let(:pr_name) { "PR name" } let(:author_details) { nil } - let(:approvers) { nil } - let(:assignee) { nil } + let(:reviewers) { nil } + let(:assignees) { nil } let(:milestone) { nil } let(:labeler) do Dependabot::PullRequestCreator::Labeler.new( @@ -124,6 +126,48 @@ to have_requested(:post, "#{repo_api_url}/pullrequests?api-version=5.0") end + context "with reviewers" do + let(:reviewers) { ["0013-0006-1980"] } + it "pushes a commit to Azure and creates a pull request with assigned reviewers" do + creator.create + + expect(WebMock). + to( + have_requested(:post, "#{repo_api_url}/pullrequests?api-version=5.0"). + with do |req| + reviewers = JSON.parse(req.body).fetch("reviewers") + expect(reviewers.count).to eq(1) + first_participant = reviewers.first + expect(first_participant.fetch("id")). + to eq("0013-0006-1980") + expect(first_participant.fetch("isRequired")). + to eq(true) + end + ) + end + end + + context "with assignees" do + let(:assignees) { ["0013-0006-1980"] } + it "pushes a commit to Azure and creates a pull request with assigned optional reviewers" do + creator.create + + expect(WebMock). + to( + have_requested(:post, "#{repo_api_url}/pullrequests?api-version=5.0"). + with do |req| + reviewers = JSON.parse(req.body).fetch("reviewers") + expect(reviewers.count).to eq(1) + first_participant = reviewers.first + expect(first_participant.fetch("id")). + to eq("0013-0006-1980") + expect(first_participant.fetch("isRequired")). + to eq(false) + end + ) + end + end + context "with e very long pr description" do let(:pr_description) { ("a" * 3997) + "💣 kaboom" } it "truncates the description respecting azures encoding" do diff --git a/common/spec/dependabot/pull_request_creator_spec.rb b/common/spec/dependabot/pull_request_creator_spec.rb index 7e7ba8f49f6..bcffc7905ae 100644 --- a/common/spec/dependabot/pull_request_creator_spec.rb +++ b/common/spec/dependabot/pull_request_creator_spec.rb @@ -282,6 +282,8 @@ pr_name: "PR name", author_details: author_details, labeler: instance_of(described_class::Labeler), + reviewers: reviewers, + assignees: assignees, work_item: 123 ).and_return(dummy_creator) expect(dummy_creator).to receive(:create)