From 5db5798198f581c66ca701219721f5553ab1fab8 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Fri, 16 Jul 2021 15:17:28 +0200 Subject: [PATCH] Add support for Azure.create_pull_request(reviewers) --- common/spec/dependabot/clients/azure_spec.rb | 128 ++++++++++++++++-- .../pull_request_creator/azure_spec.rb | 2 + .../azure/create_pull_request_details.json | 84 ++++++++++++ 3 files changed, 199 insertions(+), 15 deletions(-) create mode 100644 common/spec/fixtures/azure/create_pull_request_details.json diff --git a/common/spec/dependabot/clients/azure_spec.rb b/common/spec/dependabot/clients/azure_spec.rb index caf6ad9c424..0848194b9b3 100644 --- a/common/spec/dependabot/clients/azure_spec.rb +++ b/common/spec/dependabot/clients/azure_spec.rb @@ -41,7 +41,7 @@ end describe "#fetch_commit" do - subject { client.fetch_commit(nil, branch) } + subject(:fetch_commit) { client.fetch_commit(nil, branch) } context "when response is 200" do before do @@ -50,7 +50,7 @@ to_return(status: 200, body: fixture("azure", "master_branch.json")) end - specify { expect { subject }.to_not raise_error } + specify { expect { fetch_commit }.to_not raise_error } it { is_expected.to eq("9c8376e9b2e943c2c72fac4b239876f377f0305a") } end @@ -63,7 +63,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::NotFound) + expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::NotFound) end end @@ -75,7 +75,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::Forbidden) + expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::Forbidden) end end @@ -87,7 +87,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::Unauthorized) + expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::Unauthorized) end end @@ -99,7 +99,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::NotFound) + expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::NotFound) end end end @@ -129,7 +129,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::Forbidden) + expect { create_commit }.to raise_error(Dependabot::Clients::Azure::Forbidden) end end @@ -182,12 +182,98 @@ end describe "#create_pull_request" do - subject do - client.create_pull_request("pr_name", "source_branch", "target_branch", - "", [], nil) + subject(:create_pull_request) do + client.create_pull_request( + pr_name, source_branch, target_branch, + pr_description, labels, reviewers, work_item + ) end - let(:pull_request_url) { repo_url + "/pullrequests?api-version=5.0" } + let(:pr_name) { "test-create-pr" } + let(:source_branch) { "feature/test-create-pr" } + let(:target_branch) { "main" } + let(:pr_description) { "PR description" } + let(:labels) { [] } + let(:reviewers) { [] } + let(:work_item) { nil } + let(:create_pull_request_url) { repo_url + "/pullrequests?api-version=5.0" } + + context "when response is 200" do + response_body = fixture("azure", "create_pull_request_details.json") + + before do + request_body = { + sourceRefName: "refs/heads/#{source_branch}", + targetRefName: "refs/heads/#{target_branch}", title: pr_name, + description: pr_description, labels: [], workItemRefs: [], reviewers: [] + } + + stub_request(:post, create_pull_request_url). + with(basic_auth: [username, password], body: JSON.dump(request_body)). + to_return(status: 200, body: response_body) + end + + it "returns the parsed response" do + expect(create_pull_request).to eq(JSON.parse(response_body)) + end + end + + context "when response is 200 - with workItem" do + let(:work_item) { 42 } + response_body = fixture("azure", "create_pull_request_details.json") + + before do + request_body = { + sourceRefName: "refs/heads/#{source_branch}", + targetRefName: "refs/heads/#{target_branch}", title: pr_name, + description: pr_description, labels: [], workItemRefs: [{ id: 42 }], reviewers: [] + } + + stub_request(:post, create_pull_request_url). + with(basic_auth: [username, password], body: JSON.dump(request_body)). + to_return(status: 200, body: response_body) + end + + it "returns the parsed response" do + expect(create_pull_request).to eq(JSON.parse(response_body)) + end + end + + context "when response is 200 - with reviewer" do + let(:reviewers) { ["d6245f20-2af8-44f4-9451-8107cb2767db"] } + response_body = fixture("azure", "create_pull_request_details.json") + + before do + request_body = { + sourceRefName: "refs/heads/#{source_branch}", + targetRefName: "refs/heads/#{target_branch}", title: pr_name, + description: pr_description, labels: [], workItemRefs: [], + reviewers: [{ id: "d6245f20-2af8-44f4-9451-8107cb2767db" }] + } + + stub_request(:post, create_pull_request_url). + with(basic_auth: [username, password], body: JSON.dump(request_body)). + to_return(status: 200, body: response_body) + end + + specify { expect { create_pull_request }.to_not raise_error } + + it "returns the parsed response" do + expect(create_pull_request).to eq(JSON.parse(response_body)) + end + end + + context "when response is 401" do + before do + stub_request(:post, create_pull_request_url). + with(basic_auth: [username, password]). + to_return(status: 401) + end + + it "raises a helpful error" do + expect { create_pull_request }.to raise_error(Dependabot::Clients::Azure::Unauthorized) + end + end context "when response is 403 & tags creation is forbidden" do before do @@ -215,6 +301,18 @@ expect { subject }.to raise_error(Dependabot::Clients::Azure::Forbidden) end end + + context "when response is 404" do + before do + stub_request(:post, create_pull_request_url). + with(basic_auth: [username, password]). + to_return(status: 404) + end + + it "raises a helpful error" do + expect { create_pull_request }.to raise_error(Dependabot::Clients::Azure::NotFound) + end + end end describe "#autocomplete_pull_request" do @@ -286,7 +384,7 @@ end describe "#pull_request" do - subject { client.pull_request(pull_request_id) } + subject(:pull_request) { client.pull_request(pull_request_id) } let(:pull_request_id) { "1" } let(:pull_request_url) { base_url + "/_apis/git/pullrequests/#{pull_request_id}" } @@ -300,7 +398,7 @@ to_return(status: 200, body: response_body) end - specify { expect { subject }.to_not raise_error } + specify { expect { pull_request }.to_not raise_error } it { is_expected.to eq(JSON.parse(response_body)) } end @@ -313,7 +411,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::Unauthorized) + expect { pull_request }.to raise_error(Dependabot::Clients::Azure::Unauthorized) end end @@ -325,7 +423,7 @@ end it "raises a helpful error" do - expect { subject }.to raise_error(Dependabot::Clients::Azure::NotFound) + expect { pull_request }.to raise_error(Dependabot::Clients::Azure::NotFound) end end end diff --git a/common/spec/dependabot/pull_request_creator/azure_spec.rb b/common/spec/dependabot/pull_request_creator/azure_spec.rb index 1996ad7a38d..0506766f0b2 100644 --- a/common/spec/dependabot/pull_request_creator/azure_spec.rb +++ b/common/spec/dependabot/pull_request_creator/azure_spec.rb @@ -56,6 +56,7 @@ automerge_candidate: false ) end + let(:reviewers) { [] } let(:work_item) { 123 } let(:custom_labels) { nil } let(:dependency) do @@ -105,6 +106,7 @@ headers: json_header) stub_request(:post, "#{repo_api_url}/pullrequests?api-version=5.0"). to_return(status: 200, + body: fixture("azure", "create_pull_request_details.json"), headers: json_header) end diff --git a/common/spec/fixtures/azure/create_pull_request_details.json b/common/spec/fixtures/azure/create_pull_request_details.json new file mode 100644 index 00000000000..198e2659e30 --- /dev/null +++ b/common/spec/fixtures/azure/create_pull_request_details.json @@ -0,0 +1,84 @@ +{ + "repository": { + "id": "3411ebc1-d5aa-464f-9615-0b527bc66719", + "name": "2016_10_31", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719", + "project": { + "id": "a7573007-bbb3-4341-b726-0c4148a07853", + "name": "2016_10_31", + "description": "test project created on Halloween 2016", + "url": "https://dev.azure.com/fabrikam/_apis/projects/a7573007-bbb3-4341-b726-0c4148a07853", + "state": "wellFormed", + "revision": 7 + }, + "remoteUrl": "https://dev.azure.com/fabrikam/_git/2016_10_31" + }, + "pullRequestId": 22, + "codeReviewId": 22, + "status": "active", + "createdBy": { + "id": "d6245f20-2af8-44f4-9451-8107cb2767db", + "displayName": "Normal Paulk", + "uniqueName": "fabrikamfiber16@hotmail.com", + "url": "https://dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db", + "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db" + }, + "creationDate": "2016-11-01T16:30:31.6655471Z", + "title": "A new feature", + "description": "Adding a new feature", + "sourceRefName": "refs/heads/npaulk/my_work", + "targetRefName": "refs/heads/new_feature", + "mergeStatus": "queued", + "mergeId": "f5fc8381-3fb2-49fe-8a0d-27dcc2d6ef82", + "lastMergeSourceCommit": { + "commitId": "b60280bc6e62e2f880f1b63c1e24987664d3bda3", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/b60280bc6e62e2f880f1b63c1e24987664d3bda3" + }, + "lastMergeTargetCommit": { + "commitId": "f47bbc106853afe3c1b07a81754bce5f4b8dbf62", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/f47bbc106853afe3c1b07a81754bce5f4b8dbf62" + }, + "reviewers": [ + { + "reviewerUrl": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22/reviewers/d6245f20-2af8-44f4-9451-8107cb2767db", + "vote": 0, + "id": "d6245f20-2af8-44f4-9451-8107cb2767db", + "displayName": "Normal Paulk", + "uniqueName": "fabrikamfiber16@hotmail.com", + "url": "https://dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db", + "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db" + } + ], + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22", + "_links": { + "self": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22" + }, + "repository": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719" + }, + "workItems": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22/workitems" + }, + "sourceBranch": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/refs" + }, + "targetBranch": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/refs" + }, + "sourceCommit": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/b60280bc6e62e2f880f1b63c1e24987664d3bda3" + }, + "targetCommit": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/f47bbc106853afe3c1b07a81754bce5f4b8dbf62" + }, + "createdBy": { + "href": "https://dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db" + }, + "iterations": { + "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22/iterations" + } + }, + "supportsIterations": true, + "artifactId": "vstfs:///Git/PullRequestId/a7573007-bbb3-4341-b726-0c4148a07853%2f3411ebc1-d5aa-464f-9615-0b527bc66719%2f22" +}