/
azure_spec.rb
183 lines (164 loc) 路 6.41 KB
/
azure_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# frozen_string_literal: true
require "spec_helper"
require "dependabot/dependency_file"
require "dependabot/pull_request_updater/azure"
RSpec.describe Dependabot::PullRequestUpdater::Azure do
subject(:updater) do
described_class.new(
source: source,
base_commit: base_commit,
old_commit: old_commit,
files: files,
credentials: credentials,
pull_request_number: pull_request_number
)
end
let(:org) { "org" }
let(:project) { "project" }
let(:repo_name) { "repo" }
let(:source) do
Dependabot::Source.new(provider: "azure", repo: "#{org}/#{project}/_git/#{repo_name}")
end
let(:files) { [gemfile, gemspec] }
let(:base_commit) { "basecommitsha" }
let(:old_commit) { "oldcommitsha" }
let(:source_branch_old_commit) { "9c8376e9b2e943c2c72fac4b239876f377f0305a" }
let(:source_branch_new_commit) { "newcommitsha" }
let(:tree_object_id) { "treeobjectid" }
let(:pull_request_number) { 1 }
let(:source_branch) { "dependabot/npm_and_yarn/business-1.5.0" }
let(:temp_branch) { source_branch + "-temp" }
let(:path) { "files/are/here" }
let(:credentials) do
[{
"type" => "git_source",
"host" => "dev.azure.com",
"username" => "x-access-token",
"password" => "token"
}]
end
let(:gemfile) do
Dependabot::DependencyFile.new(
name: "Gemfile",
content: fixture("ruby", "gemfiles", "Gemfile"),
directory: path
)
end
let(:gemspec) do
Dependabot::DependencyFile.new(
name: "test.gemspec",
content: fixture("ruby", "gemspecs", "example"),
directory: path
)
end
let(:commit_message) { "Added example file" }
let(:json_header) { { "Content-Type" => "application/json" } }
let(:source_api_endpoint) { "https://dev.azure.com" }
let(:repo_url) { "#{source_api_endpoint}/#{org}/#{project}/_apis/git/repositories/#{repo_name}" }
let(:pull_request_url) { "#{source_api_endpoint}/#{org}/#{project}/_apis/git/pullrequests/#{pull_request_number}" }
let(:source_branch_url) { repo_url + "/refs?filter=heads/#{source_branch}" }
let(:source_branch_commits_url) { repo_url + "/commits?searchCriteria.itemVersion.version=#{source_branch}" }
let(:repo_contents_tree_url) do
repo_url + "/items?path=/#{path}&versionDescriptor.version=#{base_commit}&versionDescriptor.versionType=commit"
end
let(:repo_contents_url) { repo_url + "/trees/#{tree_object_id}?recursive=false" }
let(:create_commit_url) { repo_url + "/pushes?api-version=5.0" }
let(:branch_update_url) { repo_url + "/refs?api-version=5.0" }
before do
stub_request(:get, pull_request_url).
to_return(status: 200,
body: fixture("azure", "pull_request_details.json"),
headers: json_header)
stub_request(:get, source_branch_url).
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").
to_return(status: 201,
headers: json_header)
end
describe "#update" do
context "when the PR doesn't exist" do
before { stub_request(:get, pull_request_url).to_return(status: 404) }
it "doesn't update source branch head commit in AzureDevOps" do
updater.update
expect(WebMock).
to_not have_requested(:post, branch_update_url)
end
it "returns nil" do
expect(updater.update).to be_nil
end
end
context "when the branch doesn't exist" do
before { stub_request(:get, source_branch_url).to_return(status: 404) }
it "doesn't update source branch head commit in AzureDevOps" do
updater.update
expect(WebMock).
to_not have_requested(:post, branch_update_url)
end
it "returns nil" do
expect(updater.update).to be_nil
end
end
context "tries 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"))
allow(updater).to receive(:temp_branch_name).and_return(temp_branch)
updater.update
expect(WebMock).
to(
have_requested(:post, branch_update_url).
with(body: [
{ name: "refs/heads/#{source_branch}", oldObjectId: source_branch_old_commit,
newObjectId: source_branch_new_commit }
].to_json)
)
end
it "raises a helpful error when source branch update is unsuccessful" do
stub_request(:post, branch_update_url).
to_return(status: 200, body: fixture("azure", "update_ref_failed.json"))
expect { updater.update }.to raise_error(Dependabot::PullRequestUpdater::Azure::PullRequestUpdateFailed)
end
end
end
end