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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Release, feature_category: :release_orchestration do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :public, :repository) }
let(:release) { create(:release, project: project, author: user) }
it { expect(release).to be_valid }
describe 'associations' do
it { is_expected.to belong_to(:project).touch(true) }
it { is_expected.to belong_to(:author).class_name('User') }
it { is_expected.to have_many(:links).class_name('Releases::Link') }
it { is_expected.to have_many(:milestones) }
it { is_expected.to have_many(:milestone_releases) }
it { is_expected.to have_many(:evidences).class_name('Releases::Evidence') }
it { is_expected.to have_one(:catalog_resource_version).class_name('Ci::Catalog::Resources::Version') }
end
describe 'validation' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:tag) }
context 'when a release exists in the database without a name' do
it 'does not require name' do
existing_release_without_name = build(:release, project: project, author: user, name: nil)
existing_release_without_name.save!(validate: false)
existing_release_without_name.description = "change"
existing_release_without_name.save!
existing_release_without_name.reload
expect(existing_release_without_name).to be_valid
expect(existing_release_without_name.description).to eq("change")
expect(existing_release_without_name.name).not_to be_nil
end
end
describe 'scopes' do
let_it_be(:another_project) { create(:project) }
let_it_be(:release) { create(:release, project: project, author: user, tag: 'v1') }
let_it_be(:another_release) { create(:release, project: another_project, tag: 'v2') }
describe '.for_projects' do
it 'returns releases for the given projects' do
expect(described_class.for_projects([project])).to eq([release])
end
end
describe '.by_tag' do
it 'returns releases with the given tag' do
expect(described_class.by_tag(release.tag)).to eq([release])
end
end
end
context 'when description of a release is longer than the limit' do
let(:description) { 'a' * (Gitlab::Database::MAX_TEXT_SIZE_LIMIT + 1) }
let(:release) { build(:release, project: project, description: description) }
it 'creates a validation error' do
release.validate
expect(release.errors.full_messages)
.to include("Description is too long (maximum is #{Gitlab::Database::MAX_TEXT_SIZE_LIMIT} characters)")
end
end
context 'when a release is tied to a milestone for another project' do
it 'creates a validation error' do
milestone = build(:milestone, project: create(:project))
expect { release.milestones << milestone }.to raise_error(
ActiveRecord::RecordInvalid, 'Validation failed: Release does not have the same project as the milestone'
)
end
end
context 'when a release is tied to a milestone linked to the same project' do
it 'successfully links this release to this milestone' do
milestone = build(:milestone, project: project)
expect { release.milestones << milestone }.to change { MilestoneRelease.count }.by(1)
end
end
context 'when creating new release' do
subject { build(:release, project: project, name: 'Release 1.0') }
it { is_expected.to validate_presence_of(:author_id) }
end
# Deleting user along with their contributions, nullifies releases author_id.
context 'when updating existing release without author' do
let(:release) { create(:release, :legacy) }
it 'updates successfully' do
release.description += 'Update'
expect { release.save! }.not_to raise_error
end
end
end
describe 'tagged' do
# We only test for empty string since there's a not null constraint at the database level
it 'does not return the tagless release' do
empty_string_tag = create(:release, tag: 'v99.0.0')
empty_string_tag.update_column(:tag, '')
expect(described_class.tagged).not_to include(empty_string_tag)
end
it 'does return the tagged releases' do
expect(described_class.tagged).to include(release)
end
end
describe 'latest releases' do
let_it_be(:yesterday) { Time.zone.now - 1.day }
let_it_be(:today) { Time.zone.now }
let_it_be(:tomorrow) { Time.zone.now + 1.day }
let_it_be(:project2) { create(:project) }
let_it_be(:project_release1) do
create(:release, project: project, released_at: yesterday, created_at: tomorrow)
end
let_it_be(:project_release2) do
create(:release, project: project, released_at: tomorrow, created_at: yesterday)
end
let_it_be(:project2_release1) do
create(:release, project: project2, released_at: yesterday, created_at: tomorrow)
end
let_it_be(:project2_release2) do
create(:release, project: project2, released_at: tomorrow, created_at: yesterday)
end
let_it_be(:project2_release3) do
create(:release, project: project2, released_at: today, created_at: yesterday)
end
let_it_be(:project2_release4) do
create(:release, project: project2, released_at: today, created_at: yesterday, release_published_at: today)
end
let(:args) { {} }
describe '.latest' do
subject(:latest) { project.releases.latest(**args) }
context 'without order_by' do
it 'returns the latest release by released date' do
expect(latest).to eq(project_release2)
end
end
context 'with order_by: created_at' do
let(:args) { { order_by: 'created_at' } }
it 'returns the latest release by created date' do
expect(latest).to eq(project_release1)
end
end
context 'when there are no releases' do
it 'returns nil' do
project.releases.delete_all
expect(latest).to eq(nil)
end
end
end
describe '.latest_for_projects' do
let(:projects) { [project, project2] }
subject(:latest_for_projects) { described_class.latest_for_projects(projects, **args) }
context 'without order_by' do
it 'returns the latest release by released date for each project' do
expect(latest_for_projects).to match_array([project_release2, project2_release2])
end
end
context 'with order_by: created_at' do
let(:args) { { order_by: 'created_at' } }
it 'returns the latest release by created date for each project' do
expect(latest_for_projects).to match_array([project_release1, project2_release1])
end
end
context 'when no projects are provided' do
let(:projects) { [] }
it 'returns empty response' do
expect(latest_for_projects).to be_empty
end
end
context 'when there are no releases' do
it 'returns empty response' do
project.releases.delete_all
project2.releases.delete_all
expect(latest_for_projects).to be_empty
end
end
end
describe '.waiting_for_publish_event' do
let(:releases) { [project2_release3] }
subject(:waiting) { described_class.waiting_for_publish_event }
it "find today's releases not yet published" do
expect(waiting).to match_array(releases)
end
end
end
describe '#assets_count' do
subject { described_class.find(release.id).assets_count }
it 'returns the number of sources' do
is_expected.to eq(Gitlab::Workhorse::ARCHIVE_FORMATS.count)
end
context 'when a links exists' do
let!(:link) { create(:release_link, release: release) }
it 'counts the link as an asset' do
is_expected.to eq(1 + Gitlab::Workhorse::ARCHIVE_FORMATS.count)
end
it "excludes sources count when asked" do
assets_count = described_class.find(release.id).assets_count(except: [:sources])
expect(assets_count).to eq(1)
end
end
end
describe '.create' do
it "fills released_at using created_at if it's not set" do
release = create(:release, project: project, author: user, released_at: nil)
expect(release.released_at).to eq(release.created_at)
end
it "does not change released_at if it's set explicitly" do
released_at = Time.zone.parse('2018-10-20T18:00:00Z')
release = create(:release, project: project, author: user, released_at: released_at)
expect(release.released_at).to eq(released_at)
end
end
describe '#update' do
subject { release.update!(params) }
context 'when links do not exist' do
context 'when params are specified for creation' do
let(:params) do
{ links_attributes: [{ name: 'test', url: 'https://www.google.com/' }] }
end
it 'creates a link successfuly' do
is_expected.to eq(true)
expect(release.links.count).to eq(1)
expect(release.links.first.name).to eq('test')
expect(release.links.first.url).to eq('https://www.google.com/')
end
end
end
context 'when a link exists' do
let!(:link1) { create(:release_link, release: release, name: 'test1', url: 'https://www.google1.com/') }
let!(:link2) { create(:release_link, release: release, name: 'test2', url: 'https://www.google2.com/') }
before do
release.reload
end
context 'when params are specified for update' do
let(:params) do
{ links_attributes: [{ id: link1.id, name: 'new' }] }
end
it 'updates the link successfully' do
is_expected.to eq(true)
expect(release.links.count).to eq(2)
expect(release.links.first.name).to eq('new')
end
end
context 'when params are specified for deletion' do
let(:params) do
{ links_attributes: [{ id: link1.id, _destroy: true }] }
end
it 'removes the link successfuly' do
is_expected.to eq(true)
expect(release.links.count).to eq(1)
expect(release.links.first.name).to eq(link2.name)
end
end
end
end
describe '#sources' do
subject { release.sources }
it 'returns sources' do
is_expected.to all(be_a(Releases::Source))
end
end
describe '#upcoming_release?' do
context 'during the backfill migration when released_at could be nil' do
it 'handles a nil released_at value and returns false' do
allow(release).to receive(:released_at).and_return nil
expect(release.upcoming_release?).to eq(false)
end
end
end
describe 'evidence' do
let(:release_with_evidence) { create(:release, :with_evidence, project: project) }
context 'when a release is deleted' do
it 'also deletes the associated evidence' do
release_with_evidence
expect { release_with_evidence.destroy! }.to change(Releases::Evidence, :count).by(-1)
end
end
end
describe '#name' do
context 'name is nil' do
before do
release.update!(name: nil)
end
it 'returns tag' do
expect(release.name).to eq(release.tag)
end
end
end
describe '#milestone_titles' do
let_it_be(:milestone_1) { create(:milestone, project: project, title: 'Milestone 1') }
let_it_be(:milestone_2) { create(:milestone, project: project, title: 'Milestone 2') }
let_it_be(:release) { create(:release, project: project, milestones: [milestone_1, milestone_2]) }
it { expect(release.milestone_titles).to eq("#{milestone_1.title}, #{milestone_2.title}") }
end
describe 'updating catalog resource version' do
let_it_be(:project) { create(:project) }
let_it_be(:resource) { create(:ci_catalog_resource, project: project) }
let_it_be_with_reload(:release) do
create(:release, :with_catalog_resource_version, project: project, tag: 'v1', released_at: '2023-01-01T00:00:00Z')
end
let(:version) { release.catalog_resource_version }
context 'when released_at has changed' do
it 'calls sync_with_release! on the catalog resource version' do
expect(version).to receive(:sync_with_release!).once
release.update!(released_at: '2023-02-02T00:00:00Z')
end
end
context 'when released_at has not changed' do
it 'does not call sync_with_release! on the catalog resource version' do
expect(version).not_to receive(:sync_with_release!)
release.update!(released_at: '2023-01-01T00:00:00Z')
end
end
end
end
|