secupdater 0.2.0alpha2 → 0.2.0alpha4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/secupdater/ecosystem/rubygems.rb +27 -4
- data/lib/secupdater/publisher/gitlab.rb +88 -27
- data/lib/secupdater/publisher.rb +3 -0
- data/lib/secupdater/update.rb +8 -2
- data/lib/secupdater/version.rb +1 -1
- data/lib/secupdater.rb +3 -0
- data/spec/e2e/secupdater_spec.rb +6 -1
- data/spec/secupdater/ecosystem/rubygems_spec.rb +67 -4
- data/spec/secupdater/publisher/gitlab_spec.rb +170 -30
- data/spec/secupdater/update_spec.rb +55 -4
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 72d1d0d9d634e7b741faf88079cd487319fdd9b9d623b6f21871c57144492849
|
|
4
|
+
data.tar.gz: c08e421af618d820ac2c361844bab4d8d6a96bff6c676f418bce2d979479a09f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b63b7f2d374a1262822179eff166c111f902303defaf335b6d49047f2427d39544c2a02b506b77dc534f990b95971e9391e48cde0bd04a464dab0a7e7a535a7d
|
|
7
|
+
data.tar.gz: ad07d210bd3c346f361440f33bdea10c1b2b925df568550ba46c0946dc1ee80b0827ef7b1850139cec37ef36035275397763903cd2ec6c1c808b38bbeafd3d57
|
|
@@ -6,6 +6,9 @@ module Secupdater
|
|
|
6
6
|
class Ecosystem
|
|
7
7
|
class Rubygems < Secupdater::Ecosystem
|
|
8
8
|
FROZEN_EXIT_CODE = 16
|
|
9
|
+
# Bundler failures that will happen the same way at every update level:
|
|
10
|
+
# GemfileNotFound, GitError, PathError, GemspecError, HTTPError, RubyVersionMismatch
|
|
11
|
+
UNRETRYABLE_EXIT_CODES = [10, 11, 13, 14, 17, 18].freeze
|
|
9
12
|
|
|
10
13
|
def self.ecosystem_name = "RubyGems"
|
|
11
14
|
|
|
@@ -32,15 +35,17 @@ module Secupdater
|
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
def run_update(names, level)
|
|
35
|
-
output, status = Open3.capture2e(
|
|
38
|
+
output, status = Open3.capture2e(bundle_env, "bundle", "lock", "--update", *Array(names), "--conservative", "--#{level}", "--strict")
|
|
36
39
|
|
|
37
40
|
if status.exitstatus == FROZEN_EXIT_CODE
|
|
38
|
-
raise Secupdater::Error, "bundle
|
|
41
|
+
raise Secupdater::Error, "bundle lock can't change Gemfile.lock (is BUNDLE_DEPLOYMENT set?)\n#{output}"
|
|
39
42
|
end
|
|
40
43
|
|
|
41
44
|
unless status.success?
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
error_class = UNRETRYABLE_EXIT_CODES.include?(status.exitstatus) ? Secupdater::UnretryableUpdateFailed : Secupdater::UpdateCommandFailed
|
|
46
|
+
|
|
47
|
+
raise error_class.new(
|
|
48
|
+
"bundle lock command returned #{status.exitstatus}",
|
|
44
49
|
status.exitstatus,
|
|
45
50
|
output
|
|
46
51
|
)
|
|
@@ -71,6 +76,24 @@ module Secupdater
|
|
|
71
76
|
ver = Gem::Version.create(version)
|
|
72
77
|
req.satisfied_by?(ver)
|
|
73
78
|
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# When secupdater itself runs under `bundle exec` (e.g. an image whose
|
|
83
|
+
# entrypoint is `bundle exec`), undo what that set up so bundle lock works on
|
|
84
|
+
# the Gemfile and Gemfile.lock here. PATH is left alone, as callers may have
|
|
85
|
+
# changed it since.
|
|
86
|
+
def bundle_env
|
|
87
|
+
original = Bundler.original_env
|
|
88
|
+
rubyopt = original["RUBYOPT"]&.gsub(%r{-r\S*bundler/setup\S*}, "")&.strip
|
|
89
|
+
|
|
90
|
+
(Bundler::EnvironmentPreserver::BUNDLER_KEYS - %w[PATH MANPATH]).to_h { [it, original[it]] }.merge(
|
|
91
|
+
"BUNDLE_FROZEN" => "false",
|
|
92
|
+
"BUNDLE_GEMFILE" => File.expand_path("Gemfile"),
|
|
93
|
+
"BUNDLE_LOCKFILE" => nil,
|
|
94
|
+
"RUBYOPT" => rubyopt.nil? || rubyopt.empty? ? nil : rubyopt
|
|
95
|
+
)
|
|
96
|
+
end
|
|
74
97
|
end
|
|
75
98
|
end
|
|
76
99
|
end
|
|
@@ -13,43 +13,102 @@ module Secupdater
|
|
|
13
13
|
@start_sha = start_sha
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
# Added to every MR secupdater opens. Closing one declines that version of
|
|
17
|
+
# that dependency; reopening it or removing the label undoes that.
|
|
18
|
+
LABEL = "secupdater"
|
|
19
|
+
|
|
16
20
|
def run
|
|
17
21
|
Net::HTTP.start("gitlab.com", 443, use_ssl: true) do |http|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
22
|
+
merge_requests = find_merge_requests(http)
|
|
23
|
+
open_merge_request = merge_requests.find { it["state"] == "opened" }
|
|
24
|
+
|
|
25
|
+
if !open_merge_request && (previous = merge_requests.find { it["labels"].include?(LABEL) })
|
|
26
|
+
return [:declined, "#{reference} was #{previous["state"]} in !#{previous["iid"]}, reopen it or remove its #{LABEL} label to have it updated again"]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
if branch_exists?(http)
|
|
30
|
+
others = other_authors(http)
|
|
31
|
+
return [:skipped, "#{reference} has commits from #{others.join(", ")}, so it won't be updated"] if others.any?
|
|
32
|
+
|
|
33
|
+
up_to_date = branch_has_changes?(http)
|
|
34
|
+
commit(http, force: true) unless up_to_date
|
|
35
|
+
else
|
|
36
|
+
commit(http)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
return [:up_to_date, nil] if open_merge_request && up_to_date
|
|
40
|
+
|
|
41
|
+
create_merge_request(http) unless open_merge_request
|
|
42
|
+
[:published, nil]
|
|
39
43
|
end
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
private
|
|
43
47
|
|
|
44
|
-
def
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
def commit(http, force: false)
|
|
49
|
+
data = {
|
|
50
|
+
branch: reference,
|
|
51
|
+
start_sha: start_sha,
|
|
52
|
+
commit_message: title,
|
|
53
|
+
actions: changes.map { { action: it.type, file_path: it.filename, content: it.content }.compact }
|
|
54
|
+
}
|
|
55
|
+
data[:force] = true if force
|
|
56
|
+
|
|
57
|
+
request(http, Net::HTTP::Post, "repository/commits", data)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def create_merge_request(http)
|
|
61
|
+
data = {
|
|
62
|
+
source_branch: reference,
|
|
63
|
+
target_branch: target_branch,
|
|
64
|
+
title: title,
|
|
65
|
+
description: description,
|
|
66
|
+
remove_source_branch: true,
|
|
67
|
+
labels: [LABEL]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
request(http, Net::HTTP::Post, "merge_requests", data)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def branch_exists?(http)
|
|
74
|
+
!request(http, Net::HTTP::Get, "repository/branches/#{URI.encode_www_form_component(reference)}", allow_missing: true).nil?
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def find_merge_requests(http)
|
|
78
|
+
query = URI.encode_www_form(source_branch: reference, target_branch: target_branch)
|
|
79
|
+
JSON.parse(request(http, Net::HTTP::Get, "merge_requests?#{query}").body)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Authors of commits on the branch, other than whoever owns the access token
|
|
83
|
+
def other_authors(http)
|
|
84
|
+
query = URI.encode_www_form(from: target_branch, to: reference)
|
|
85
|
+
commits = JSON.parse(request(http, Net::HTTP::Get, "repository/compare?#{query}").body)["commits"]
|
|
86
|
+
|
|
87
|
+
commits.reject { own_emails(http).include?(it["author_email"]) }.map { it["author_name"] }.uniq
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def own_emails(http)
|
|
91
|
+
@own_emails ||= begin
|
|
92
|
+
user = JSON.parse(api_request(http, Net::HTTP::Get, "user").body)
|
|
93
|
+
user.values_at("email", "commit_email", "public_email").reject { it.nil? || it.empty? }
|
|
94
|
+
end
|
|
48
95
|
end
|
|
49
96
|
|
|
50
|
-
def
|
|
97
|
+
def branch_has_changes?(http)
|
|
98
|
+
changes.all? do |change|
|
|
99
|
+
query = URI.encode_www_form(ref: reference)
|
|
100
|
+
res = request(http, Net::HTTP::Get, "repository/files/#{URI.encode_www_form_component(change.filename)}/raw?#{query}", allow_missing: true)
|
|
101
|
+
res&.body == change.content
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def request(http, type, path, data = nil, allow_missing: false)
|
|
106
|
+
api_request(http, type, "projects/#{URI.encode_www_form_component(project_id)}/#{path}", data, allow_missing:)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def api_request(http, type, path, data = nil, allow_missing: false)
|
|
51
110
|
req = type.new(
|
|
52
|
-
"/api/v4
|
|
111
|
+
"/api/v4/#{path}",
|
|
53
112
|
"PRIVATE-TOKEN" => access_token,
|
|
54
113
|
"Content-Type" => "application/json"
|
|
55
114
|
)
|
|
@@ -57,6 +116,8 @@ module Secupdater
|
|
|
57
116
|
|
|
58
117
|
res = http.request(req)
|
|
59
118
|
|
|
119
|
+
return if allow_missing && res.is_a?(Net::HTTPNotFound)
|
|
120
|
+
|
|
60
121
|
unless res.is_a?(Net::HTTPSuccess)
|
|
61
122
|
raise Secupdater::PublishFailed, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
|
|
62
123
|
end
|
data/lib/secupdater/publisher.rb
CHANGED
|
@@ -3,6 +3,9 @@ module Secupdater
|
|
|
3
3
|
attr_reader :ecosystem, :bump, :advisories, :changes
|
|
4
4
|
|
|
5
5
|
def initialize = raise NotImplementedError
|
|
6
|
+
|
|
7
|
+
# Returns [status, message], with a message explaining any status other
|
|
8
|
+
# than :published (e.g. :declined, :skipped or :up_to_date)
|
|
6
9
|
def run = raise NotImplementedError
|
|
7
10
|
|
|
8
11
|
def call(ecosystem, dependency_bump, advisories, changes)
|
data/lib/secupdater/update.rb
CHANGED
|
@@ -42,6 +42,9 @@ module Secupdater
|
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
status
|
|
45
|
+
rescue Secupdater::UnretryableUpdateFailed => e
|
|
46
|
+
logger.error "#{dep.name}: #{e.message}\n#{e.output}"
|
|
47
|
+
:failed
|
|
45
48
|
rescue Secupdater::PublishFailed => e
|
|
46
49
|
logger.error "#{dep.name}: #{e.message}"
|
|
47
50
|
:failed
|
|
@@ -69,8 +72,11 @@ module Secupdater
|
|
|
69
72
|
|
|
70
73
|
return [:resolved, nil] unless publisher
|
|
71
74
|
|
|
72
|
-
publisher.call(ecosystem, bump, advisories, file_changes)
|
|
73
|
-
|
|
75
|
+
status, message = publisher.call(ecosystem, bump, advisories, file_changes)
|
|
76
|
+
logger.warn message if message
|
|
77
|
+
[status, nil]
|
|
78
|
+
rescue Secupdater::UnretryableUpdateFailed
|
|
79
|
+
raise
|
|
74
80
|
rescue Secupdater::UpdateCommandFailed => e
|
|
75
81
|
[:failed, "#{dep.name}: #{e.message}\n#{e.output}"]
|
|
76
82
|
ensure
|
data/lib/secupdater/version.rb
CHANGED
data/lib/secupdater.rb
CHANGED
data/spec/e2e/secupdater_spec.rb
CHANGED
|
@@ -74,6 +74,11 @@ RSpec.describe "secupdater end to end" do
|
|
|
74
74
|
.to_return(status: 200, body: "[]")
|
|
75
75
|
end
|
|
76
76
|
|
|
77
|
+
let!(:find_branch_stub) do
|
|
78
|
+
stub_request(:get, "https://gitlab.com/api/v4/projects/123/repository/branches/security%2Fcss_parser-1.1.0")
|
|
79
|
+
.to_return(status: 404, body: "{}")
|
|
80
|
+
end
|
|
81
|
+
|
|
77
82
|
let!(:create_commit_stub) do
|
|
78
83
|
stub_request(:post, "https://gitlab.com/api/v4/projects/123/repository/commits")
|
|
79
84
|
.with(body: hash_including(
|
|
@@ -109,7 +114,7 @@ RSpec.describe "secupdater end to end" do
|
|
|
109
114
|
end
|
|
110
115
|
end
|
|
111
116
|
|
|
112
|
-
context "real bundle update --local against fixture vendor/cache gems", :integration do
|
|
117
|
+
context "real bundle lock --update --local against fixture vendor/cache gems", :integration do
|
|
113
118
|
it "bumps the gem in Gemfile.lock"
|
|
114
119
|
end
|
|
115
120
|
end
|
|
@@ -128,6 +128,24 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
128
128
|
|
|
129
129
|
describe "#try_update" do
|
|
130
130
|
let(:status) { instance_double(Process::Status, success?: true, exitstatus: 0) }
|
|
131
|
+
let(:original_env) { {} }
|
|
132
|
+
let(:bundle_env) do
|
|
133
|
+
{
|
|
134
|
+
"BUNDLE_BIN_PATH" => nil,
|
|
135
|
+
"BUNDLE_FROZEN" => "false",
|
|
136
|
+
"BUNDLE_GEMFILE" => File.expand_path("Gemfile"),
|
|
137
|
+
"BUNDLE_LOCKFILE" => nil,
|
|
138
|
+
"BUNDLER_SETUP" => nil,
|
|
139
|
+
"BUNDLER_VERSION" => nil,
|
|
140
|
+
"GEM_HOME" => nil,
|
|
141
|
+
"GEM_PATH" => nil,
|
|
142
|
+
"RB_USER_INSTALL" => nil,
|
|
143
|
+
"RUBYLIB" => nil,
|
|
144
|
+
"RUBYOPT" => nil
|
|
145
|
+
}
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
before { allow(Bundler).to receive(:original_env).and_return(original_env) }
|
|
131
149
|
|
|
132
150
|
context "command succeeds" do
|
|
133
151
|
before do
|
|
@@ -137,10 +155,10 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
137
155
|
end
|
|
138
156
|
end
|
|
139
157
|
|
|
140
|
-
it "runs a strict, conservative, unfrozen bundle update at the given level" do
|
|
158
|
+
it "runs a strict, conservative, unfrozen bundle lock --update at the given level" do
|
|
141
159
|
rubygems.try_update("css_parser", level: :minor)
|
|
142
160
|
|
|
143
|
-
expect(Open3).to have_received(:capture2e).with(
|
|
161
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "lock", "--update", "css_parser", "--conservative", "--minor", "--strict")
|
|
144
162
|
end
|
|
145
163
|
|
|
146
164
|
it "returns the lockfile change with its new and original content" do
|
|
@@ -164,7 +182,29 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
164
182
|
it "passes it as a single argument without going through a shell" do
|
|
165
183
|
rubygems.try_update("css_parser; touch pwned", level: :patch)
|
|
166
184
|
|
|
167
|
-
expect(Open3).to have_received(:capture2e).with(
|
|
185
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "lock", "--update", "css_parser; touch pwned", "--conservative", "--patch", "--strict")
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
context "secupdater itself running under bundle exec for another bundle" do
|
|
190
|
+
let(:original_env) do
|
|
191
|
+
{
|
|
192
|
+
"GEM_HOME" => "/usr/local/bundle",
|
|
193
|
+
"BUNDLE_GEMFILE" => "/elsewhere/Gemfile",
|
|
194
|
+
"BUNDLE_LOCKFILE" => "/elsewhere/Gemfile.lock",
|
|
195
|
+
"RUBYOPT" => "-W0 -rbundler/setup"
|
|
196
|
+
}
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
before { allow(Open3).to receive(:capture2e).and_return(["", status]) }
|
|
200
|
+
|
|
201
|
+
it "runs bundle lock with Bundler's settings from before bundle exec, on the Gemfile in the working directory" do
|
|
202
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
203
|
+
|
|
204
|
+
expect(Open3).to have_received(:capture2e).with(
|
|
205
|
+
bundle_env.merge("GEM_HOME" => "/usr/local/bundle", "RUBYOPT" => "-W0"),
|
|
206
|
+
"bundle", "lock", "--update", "css_parser", "--conservative", "--patch", "--strict"
|
|
207
|
+
)
|
|
168
208
|
end
|
|
169
209
|
end
|
|
170
210
|
|
|
@@ -173,14 +213,37 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
173
213
|
|
|
174
214
|
before { allow(Open3).to receive(:capture2e).and_return(["Could not find gem 'css_parser'", status]) }
|
|
175
215
|
|
|
176
|
-
it "raises UpdateCommandFailed with the integer exit code and combined output" do
|
|
216
|
+
it "raises a retryable UpdateCommandFailed with the integer exit code and combined output" do
|
|
177
217
|
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::UpdateCommandFailed) do
|
|
218
|
+
expect(it).not_to be_a(Secupdater::UnretryableUpdateFailed)
|
|
178
219
|
expect(it.exit_code).to eq(1)
|
|
179
220
|
expect(it.output).to eq("Could not find gem 'css_parser'")
|
|
180
221
|
end
|
|
181
222
|
end
|
|
182
223
|
end
|
|
183
224
|
|
|
225
|
+
{
|
|
226
|
+
10 => "Gemfile not found",
|
|
227
|
+
11 => "git source couldn't be fetched",
|
|
228
|
+
13 => "path source not found",
|
|
229
|
+
14 => "gemspec invalid",
|
|
230
|
+
17 => "HTTP error",
|
|
231
|
+
18 => "Ruby version mismatch"
|
|
232
|
+
}.each do |exit_code, reason|
|
|
233
|
+
context "command exits #{exit_code} (#{reason})" do
|
|
234
|
+
let(:status) { instance_double(Process::Status, success?: false, exitstatus: exit_code) }
|
|
235
|
+
|
|
236
|
+
before { allow(Open3).to receive(:capture2e).and_return([reason, status]) }
|
|
237
|
+
|
|
238
|
+
it "raises UnretryableUpdateFailed with the exit code and output" do
|
|
239
|
+
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::UnretryableUpdateFailed) do
|
|
240
|
+
expect(it.exit_code).to eq(exit_code)
|
|
241
|
+
expect(it.output).to eq(reason)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
184
247
|
context "command exits 16 (frozen lockfile)" do
|
|
185
248
|
let(:status) { instance_double(Process::Status, success?: false, exitstatus: 16) }
|
|
186
249
|
|
|
@@ -11,6 +11,10 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
11
11
|
let(:bump) { Secupdater::DependencySpecDiff.new(dep, "1.0.0", "1.1.0") }
|
|
12
12
|
let(:changes) { [Secupdater::FileChange.new("Gemfile.lock", "update", "abc", "xyz")] }
|
|
13
13
|
|
|
14
|
+
let(:api) { "https://gitlab.com/api/v4" }
|
|
15
|
+
let(:project_api) { "#{api}/projects/123" }
|
|
16
|
+
let(:bot_email) { "group_1_bot_abc@noreply.gitlab.com" }
|
|
17
|
+
|
|
14
18
|
let(:headers) do
|
|
15
19
|
{
|
|
16
20
|
"PRIVATE-TOKEN" => "access_token",
|
|
@@ -19,59 +23,95 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
let(:existing_merge_requests) { [] }
|
|
26
|
+
let(:branch_exists) { false }
|
|
27
|
+
let(:branch_commits) { [{ author_name: "release-utilities", author_email: bot_email }] }
|
|
28
|
+
let(:branch_lockfile) { "old" }
|
|
29
|
+
|
|
30
|
+
let(:commit_body) do
|
|
31
|
+
{
|
|
32
|
+
branch: "security/css_parser-1.1.0",
|
|
33
|
+
start_sha: "abc123",
|
|
34
|
+
commit_message: "Upgrade RubyGems dependency css_parser to version 1.1.0",
|
|
35
|
+
actions: [{ action: "update", file_path: "Gemfile.lock", content: "abc" }]
|
|
36
|
+
}
|
|
37
|
+
end
|
|
22
38
|
|
|
23
39
|
let!(:find_mr_stub) do
|
|
24
|
-
stub_request(:get, "
|
|
25
|
-
.with(query: { source_branch: "security/css_parser-1.1.0",
|
|
40
|
+
stub_request(:get, "#{project_api}/merge_requests")
|
|
41
|
+
.with(query: { source_branch: "security/css_parser-1.1.0", target_branch: "main" }, headers: headers)
|
|
26
42
|
.to_return(status: 200, body: existing_merge_requests.to_json)
|
|
27
43
|
end
|
|
28
44
|
|
|
45
|
+
let!(:find_branch_stub) do
|
|
46
|
+
stub_request(:get, "#{project_api}/repository/branches/security%2Fcss_parser-1.1.0")
|
|
47
|
+
.to_return(status: branch_exists ? 200 : 404, body: "{}")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
let!(:compare_stub) do
|
|
51
|
+
stub_request(:get, "#{project_api}/repository/compare")
|
|
52
|
+
.with(query: { from: "main", to: "security/css_parser-1.1.0" })
|
|
53
|
+
.to_return(status: 200, body: { commits: branch_commits }.to_json)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
let!(:user_stub) do
|
|
57
|
+
stub_request(:get, "#{api}/user")
|
|
58
|
+
.to_return(status: 200, body: { email: bot_email, commit_email: bot_email, public_email: "" }.to_json)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
let!(:branch_file_stub) do
|
|
62
|
+
stub_request(:get, "#{project_api}/repository/files/Gemfile.lock/raw")
|
|
63
|
+
.with(query: { ref: "security/css_parser-1.1.0" })
|
|
64
|
+
.to_return(status: 200, body: branch_lockfile)
|
|
65
|
+
end
|
|
66
|
+
|
|
29
67
|
let!(:create_commit_stub) do
|
|
30
|
-
stub_request(:post, "
|
|
31
|
-
.with(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
headers: headers
|
|
39
|
-
)
|
|
68
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
69
|
+
.with(body: commit_body, headers: headers)
|
|
70
|
+
.to_return(status: 201, body: "{}")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
let!(:force_commit_stub) do
|
|
74
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
75
|
+
.with(body: commit_body.merge(force: true), headers: headers)
|
|
40
76
|
.to_return(status: 201, body: "{}")
|
|
41
77
|
end
|
|
42
78
|
|
|
43
79
|
let!(:create_mr_stub) do
|
|
44
|
-
stub_request(:post, "
|
|
80
|
+
stub_request(:post, "#{project_api}/merge_requests")
|
|
45
81
|
.with(body: {
|
|
46
82
|
source_branch: "security/css_parser-1.1.0",
|
|
47
83
|
target_branch: "main",
|
|
48
84
|
remove_source_branch: true,
|
|
49
|
-
labels: [],
|
|
85
|
+
labels: ["secupdater"],
|
|
50
86
|
title: "Upgrade RubyGems dependency css_parser to version 1.1.0",
|
|
51
87
|
description: "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n",
|
|
52
88
|
}, headers: headers)
|
|
53
89
|
.to_return(status: 201, body: "{}")
|
|
54
90
|
end
|
|
55
91
|
|
|
92
|
+
def publish = gitlab.call(ecosystem, bump, advisories, changes)
|
|
93
|
+
|
|
56
94
|
describe "#call(ecosystem, bump, advisories, changes)" do
|
|
57
|
-
|
|
58
|
-
|
|
95
|
+
context "no branch or MR yet" do
|
|
96
|
+
it "creates the branch from start_sha and a labelled merge request" do
|
|
97
|
+
expect(publish).to eq([:published, nil])
|
|
59
98
|
|
|
60
|
-
|
|
61
|
-
|
|
99
|
+
expect(create_commit_stub).to have_been_requested
|
|
100
|
+
expect(create_mr_stub).to have_been_requested
|
|
101
|
+
end
|
|
62
102
|
end
|
|
63
103
|
|
|
64
104
|
context "deleting a file" do
|
|
65
105
|
let(:changes) { [Secupdater::FileChange.new("Gemfile.lock", "delete", nil, "xyz")] }
|
|
66
106
|
|
|
67
107
|
let!(:create_commit_stub) do
|
|
68
|
-
stub_request(:post, "
|
|
108
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
69
109
|
.with(body: hash_including(actions: [{ action: "delete", file_path: "Gemfile.lock" }]))
|
|
70
110
|
.to_return(status: 201, body: "{}")
|
|
71
111
|
end
|
|
72
112
|
|
|
73
113
|
it "doesn't send content for the action" do
|
|
74
|
-
|
|
114
|
+
publish
|
|
75
115
|
|
|
76
116
|
expect(create_commit_stub).to have_been_requested
|
|
77
117
|
end
|
|
@@ -79,27 +119,127 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
79
119
|
|
|
80
120
|
context "API returns 4xx" do
|
|
81
121
|
let!(:create_commit_stub) do
|
|
82
|
-
stub_request(:post, "
|
|
122
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
83
123
|
.to_return(status: 400, body: '{"message":"A branch called security/css_parser-1.1.0 already exists"}')
|
|
84
124
|
end
|
|
85
125
|
|
|
86
126
|
it "raises Secupdater::PublishFailed including the response body" do
|
|
87
|
-
expect {
|
|
88
|
-
.to raise_error(Secupdater::PublishFailed, /400.*already exists/)
|
|
127
|
+
expect { publish }.to raise_error(Secupdater::PublishFailed, /400.*already exists/)
|
|
89
128
|
|
|
90
129
|
expect(create_mr_stub).not_to have_been_requested
|
|
91
130
|
end
|
|
92
131
|
end
|
|
93
132
|
|
|
94
|
-
context "
|
|
95
|
-
let(:existing_merge_requests) { [{ iid:
|
|
133
|
+
context "a closed MR for this version" do
|
|
134
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "closed", labels: ["secupdater"] }] }
|
|
135
|
+
let(:branch_exists) { true }
|
|
96
136
|
|
|
97
|
-
it "
|
|
98
|
-
|
|
137
|
+
it "treats the update as declined, without touching the branch" do
|
|
138
|
+
expect(publish).to eq([:declined, "security/css_parser-1.1.0 was closed in !7, reopen it or remove its secupdater label to have it updated again"])
|
|
99
139
|
|
|
100
|
-
expect(
|
|
101
|
-
|
|
102
|
-
|
|
140
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
context "whose branch has since been deleted" do
|
|
144
|
+
let(:branch_exists) { false }
|
|
145
|
+
|
|
146
|
+
it "still treats the update as declined" do
|
|
147
|
+
expect(publish.first).to eq(:declined)
|
|
148
|
+
|
|
149
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
context "a closed MR without the secupdater label (e.g. from before labels)" do
|
|
155
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "closed", labels: [] }] }
|
|
156
|
+
|
|
157
|
+
it "doesn't count as declined, so opens a new MR" do
|
|
158
|
+
expect(publish).to eq([:published, nil])
|
|
159
|
+
|
|
160
|
+
expect(create_mr_stub).to have_been_requested
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
context "a merged MR for this version" do
|
|
165
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "merged", labels: ["secupdater"] }] }
|
|
166
|
+
|
|
167
|
+
it "doesn't open another" do
|
|
168
|
+
expect(publish).to eq([:declined, "security/css_parser-1.1.0 was merged in !7, reopen it or remove its secupdater label to have it updated again"])
|
|
169
|
+
|
|
170
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
context "an open MR, labelled or not" do
|
|
175
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "opened", labels: [] }, { iid: 3, state: "closed", labels: ["secupdater"] }] }
|
|
176
|
+
let(:branch_exists) { true }
|
|
177
|
+
|
|
178
|
+
context "whose branch already has these changes" do
|
|
179
|
+
let(:branch_lockfile) { "abc" }
|
|
180
|
+
|
|
181
|
+
it "leaves it alone" do
|
|
182
|
+
expect(publish).to eq([:up_to_date, nil])
|
|
183
|
+
|
|
184
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
context "whose branch is out of date" do
|
|
189
|
+
it "replaces the branch's commit, from start_sha, without opening another MR" do
|
|
190
|
+
expect(publish).to eq([:published, nil])
|
|
191
|
+
|
|
192
|
+
expect(force_commit_stub).to have_been_requested
|
|
193
|
+
expect(create_mr_stub).not_to have_been_requested
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
context "whose branch has a commit from someone else" do
|
|
198
|
+
let(:branch_commits) do
|
|
199
|
+
[
|
|
200
|
+
{ author_name: "release-utilities", author_email: bot_email },
|
|
201
|
+
{ author_name: "Jo Bloggs", author_email: "jo@example.com" }
|
|
202
|
+
]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it "leaves the branch alone and says why" do
|
|
206
|
+
expect(publish).to eq([:skipped, "security/css_parser-1.1.0 has commits from Jo Bloggs, so it won't be updated"])
|
|
207
|
+
|
|
208
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
context "whose branch has been deleted" do
|
|
213
|
+
let(:branch_exists) { false }
|
|
214
|
+
|
|
215
|
+
it "recreates the branch without opening another MR" do
|
|
216
|
+
expect(publish).to eq([:published, nil])
|
|
217
|
+
|
|
218
|
+
expect(create_commit_stub).to have_been_requested
|
|
219
|
+
expect(create_mr_stub).not_to have_been_requested
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
context "a branch left without an MR (e.g. MR creation failed, or an MR from before labels)" do
|
|
225
|
+
let(:branch_exists) { true }
|
|
226
|
+
|
|
227
|
+
it "replaces the branch's commit and opens an MR" do
|
|
228
|
+
expect(publish).to eq([:published, nil])
|
|
229
|
+
|
|
230
|
+
expect(force_commit_stub).to have_been_requested
|
|
231
|
+
expect(create_mr_stub).to have_been_requested
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
context "that already has these changes" do
|
|
235
|
+
let(:branch_lockfile) { "abc" }
|
|
236
|
+
|
|
237
|
+
it "opens an MR without committing" do
|
|
238
|
+
expect(publish).to eq([:published, nil])
|
|
239
|
+
|
|
240
|
+
expect(a_request(:post, "#{project_api}/repository/commits")).not_to have_been_made
|
|
241
|
+
expect(create_mr_stub).to have_been_requested
|
|
242
|
+
end
|
|
103
243
|
end
|
|
104
244
|
end
|
|
105
245
|
end
|
|
@@ -5,7 +5,7 @@ require "stringio"
|
|
|
5
5
|
require "tmpdir"
|
|
6
6
|
|
|
7
7
|
module UpdateSpec
|
|
8
|
-
# outcomes: gem name => version try_update moves it to, :unchanged, :fail or :abort, or a hash of those by level
|
|
8
|
+
# outcomes: gem name => version try_update moves it to, :unchanged, :fail, :fail_unretryable or :abort, or a hash of those by level
|
|
9
9
|
class FakeEcosystem
|
|
10
10
|
LOCKFILE = "Gemfile.lock"
|
|
11
11
|
|
|
@@ -39,7 +39,9 @@ module UpdateSpec
|
|
|
39
39
|
|
|
40
40
|
case outcome
|
|
41
41
|
when :fail
|
|
42
|
-
raise Secupdater::UpdateCommandFailed.new("bundle
|
|
42
|
+
raise Secupdater::UpdateCommandFailed.new("bundle lock failed", 1, "could not resolve #{name}")
|
|
43
|
+
when :fail_unretryable
|
|
44
|
+
raise Secupdater::UnretryableUpdateFailed.new("bundle lock failed", 11, "could not fetch git source for #{name}")
|
|
43
45
|
when :abort
|
|
44
46
|
raise Secupdater::Error, "lockfile is frozen"
|
|
45
47
|
when :unchanged
|
|
@@ -82,7 +84,7 @@ RSpec.describe Secupdater::Update do
|
|
|
82
84
|
end
|
|
83
85
|
|
|
84
86
|
before do
|
|
85
|
-
allow(publisher).to receive(:call) { |*args| published << args } if publisher
|
|
87
|
+
allow(publisher).to receive(:call) { |*args| published << args; [:published, nil] } if publisher
|
|
86
88
|
end
|
|
87
89
|
|
|
88
90
|
describe "#call(advisories)" do
|
|
@@ -134,7 +136,7 @@ RSpec.describe Secupdater::Update do
|
|
|
134
136
|
end
|
|
135
137
|
end
|
|
136
138
|
|
|
137
|
-
context "bundle
|
|
139
|
+
context "bundle lock fails for the first gem" do
|
|
138
140
|
let(:outcomes) { { "rack" => :fail, "nokogiri" => "2.0" } }
|
|
139
141
|
|
|
140
142
|
it "records the failure and continues to the next gem" do
|
|
@@ -252,6 +254,26 @@ RSpec.describe Secupdater::Update do
|
|
|
252
254
|
end
|
|
253
255
|
end
|
|
254
256
|
|
|
257
|
+
context "the publisher doesn't publish (e.g. the update was declined)" do
|
|
258
|
+
let(:outcomes) { { "rack" => { patch: "2.0.1", minor: "2.1" } } }
|
|
259
|
+
|
|
260
|
+
before do
|
|
261
|
+
allow(publisher).to receive(:call).and_return([:declined, "security/rack-2.0.1 was closed in !7"])
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
it "returns the publisher's status and warns with its message" do
|
|
265
|
+
expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :declined)
|
|
266
|
+
|
|
267
|
+
expect(log.string).to match(%r{^WARN security/rack-2\.0\.1 was closed in !7})
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
it "doesn't try the other levels" do
|
|
271
|
+
update.call([advisory("rack", "cve/1")])
|
|
272
|
+
|
|
273
|
+
expect(ecosystem.attempts).to eq([["rack", :patch]])
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
255
277
|
context "publishing fails for the first gem" do
|
|
256
278
|
let(:outcomes) { { "rack" => "2.1", "nokogiri" => "2.0" } }
|
|
257
279
|
|
|
@@ -260,6 +282,7 @@ RSpec.describe Secupdater::Update do
|
|
|
260
282
|
raise Secupdater::PublishFailed, "branch already exists" if bump.dependency.name == "rack"
|
|
261
283
|
|
|
262
284
|
published << bump
|
|
285
|
+
[:published, nil]
|
|
263
286
|
end
|
|
264
287
|
end
|
|
265
288
|
|
|
@@ -289,6 +312,34 @@ RSpec.describe Secupdater::Update do
|
|
|
289
312
|
end
|
|
290
313
|
end
|
|
291
314
|
|
|
315
|
+
context "an update failure that another level won't fix, for the first gem" do
|
|
316
|
+
let(:outcomes) { { "rack" => :fail_unretryable, "nokogiri" => "2.0" } }
|
|
317
|
+
|
|
318
|
+
it "returns :failed for that gem without trying the other levels" do
|
|
319
|
+
expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :failed)
|
|
320
|
+
expect(ecosystem.attempts).to eq([["rack", :patch]])
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
it "logs the failure and its output once, as an error" do
|
|
324
|
+
update.call([advisory("rack", "cve/1")])
|
|
325
|
+
|
|
326
|
+
expect(log.string).to match(/^ERROR rack: bundle lock failed\ncould not fetch git source for rack/)
|
|
327
|
+
expect(log.string.scan(/could not fetch git source/).length).to eq(1)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
it "continues to the next gem" do
|
|
331
|
+
update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
|
|
332
|
+
|
|
333
|
+
expect(published.map { it[1].dependency.name }).to eq(["nokogiri"])
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
it "leaves the lockfile as it found it" do
|
|
337
|
+
update.call([advisory("rack", "cve/1")])
|
|
338
|
+
|
|
339
|
+
expect(lockfile_on_disk).to eq(lockfile)
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
|
|
292
343
|
context "an error that isn't an update failure" do
|
|
293
344
|
let(:outcomes) { { "rack" => :abort, "nokogiri" => "2.0" } }
|
|
294
345
|
|