diff --git a/common/lib/dependabot/pull_request_updater/azure.rb b/common/lib/dependabot/pull_request_updater/azure.rb index e6111d1ea9a..2e5d086f044 100644 --- a/common/lib/dependabot/pull_request_updater/azure.rb +++ b/common/lib/dependabot/pull_request_updater/azure.rb @@ -11,16 +11,17 @@ class PullRequestUpdateFailed < Dependabot::DependabotError; end OBJECT_ID_FOR_BRANCH_DELETE = "0000000000000000000000000000000000000000" attr_reader :source, :files, :base_commit, :old_commit, :credentials, - :pull_request_number + :pull_request_number, :author_details def initialize(source:, files:, base_commit:, old_commit:, - credentials:, pull_request_number:) + credentials:, pull_request_number:, author_details: nil) @source = source @files = files @base_commit = base_commit @old_commit = old_commit @credentials = credentials @pull_request_number = pull_request_number + @author_details = author_details end def update @@ -74,12 +75,15 @@ def source_branch_name end def create_temp_branch + author = author_details&.slice(:name, :email, :date) + author = nil unless author&.any? + response = azure_client_for_source.create_commit( temp_branch_name, base_commit, commit_message, files, - nil + author ) JSON.parse(response.body).fetch("refUpdates").first.fetch("newObjectId") diff --git a/common/spec/dependabot/pull_request_updater/azure_spec.rb b/common/spec/dependabot/pull_request_updater/azure_spec.rb index 311c91bba90..c8f9c35ce87 100644 --- a/common/spec/dependabot/pull_request_updater/azure_spec.rb +++ b/common/spec/dependabot/pull_request_updater/azure_spec.rb @@ -12,7 +12,8 @@ old_commit: old_commit, files: files, credentials: credentials, - pull_request_number: pull_request_number + pull_request_number: pull_request_number, + author_details: author_details ) end @@ -29,6 +30,7 @@ let(:source_branch_new_commit) { "newcommitsha" } let(:tree_object_id) { "treeobjectid" } let(:pull_request_number) { 1 } + let(:author_details) { nil } let(:source_branch) { "dependabot/npm_and_yarn/business-1.5.0" } let(:temp_branch) { source_branch + "-temp" } let(:path) { "files/are/here" } @@ -79,9 +81,25 @@ to_return(status: 200, body: fixture("azure", "pull_request_source_branch_details.json"), headers: json_header) - stub_request(:post, "#{repo_url}/pushes?api-version=5.0"). + stub_request(:post, create_commit_url). to_return(status: 201, + body: fixture("azure", "create_new_branch.json"), headers: json_header) + stub_request(:get, source_branch_commits_url). + to_return(status: 200, + body: fixture("azure", "commits.json"), + headers: json_header) + stub_request(:get, repo_contents_tree_url). + to_return(status: 200, + body: fixture("azure", "repo_contents_treeroot.json"), + headers: json_header) + stub_request(:get, repo_contents_url). + to_return(status: 200, + body: fixture("azure", "repo_contents.json"), + headers: json_header) + stub_request(:post, branch_update_url). + to_return(status: 201, + body: fixture("azure", "update_ref.json")) end describe "#update" do @@ -113,52 +131,41 @@ end end - context "tries updating source branch head commit in AzureDevOps" do + context "when updating source branch head commit in AzureDevOps" do before do - stub_request(:get, source_branch_commits_url). - to_return(status: 200, - body: fixture("azure", "commits.json"), - headers: json_header) - stub_request(:get, repo_contents_tree_url). - to_return(status: 200, - body: fixture("azure", "repo_contents_treeroot.json"), - headers: json_header) - stub_request(:get, repo_contents_url). - to_return(status: 200, - body: fixture("azure", "repo_contents.json"), - headers: json_header) - stub_request(:post, create_commit_url). - with(body: { - refUpdates: [ - { name: "refs/heads/#{temp_branch}", oldObjectId: base_commit } - ], - commits: [ - { - comment: commit_message, - changes: files.map do |file| - { - changeType: "edit", - item: { path: file.path }, - newContent: { - content: Base64.encode64(file.content), - contentType: "base64encoded" - } - } - end - } - ] - }). - to_return(status: 201, - body: fixture("azure", "create_new_branch.json"), - headers: json_header) - allow(updater).to receive(:temp_branch_name).and_return(temp_branch) end - it "sends request to AzureDevOps to update source branch head commit" do - stub_request(:post, branch_update_url). - to_return(status: 201, body: fixture("azure", "update_ref.json")) + it "commits on the temp branch" do + updater.update + expect(WebMock). + to( + have_requested(:post, create_commit_url). + with(body: { + refUpdates: [ + { name: "refs/heads/#{temp_branch}", oldObjectId: base_commit } + ], + commits: [ + { + comment: commit_message, + changes: files.map do |file| + { + changeType: "edit", + item: { path: file.path }, + newContent: { + content: Base64.encode64(file.content), + contentType: "base64encoded" + } + } + end + } + ] + }) + ) + end + + it "sends request to AzureDevOps to update source branch head commit" do allow(updater).to receive(:temp_branch_name).and_return(temp_branch) updater.update @@ -179,5 +186,26 @@ expect { updater.update }.to raise_error(Dependabot::PullRequestUpdater::Azure::PullRequestUpdateFailed) end end + + context "with author details provided" do + let(:author_details) do + { email: "support@dependabot.com", name: "dependabot" } + end + + it "includes the author details when commiting on the temp branch" do + updater.update + + expect(WebMock). + to( + have_requested(:post, create_commit_url). + with do |req| + json_body = JSON.parse(req.body) + expect(json_body.fetch("commits").count).to eq(1) + expect(json_body.fetch("commits").first.fetch("author")). + to eq(author_details.transform_keys(&:to_s)) + end + ) + end + end end end