-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgithub_helper_spec.rb
More file actions
811 lines (681 loc) · 32.4 KB
/
Copy pathgithub_helper_spec.rb
File metadata and controls
811 lines (681 loc) · 32.4 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# frozen_string_literal: true
require 'spec_helper'
require 'webmock/rspec'
describe Fastlane::Helper::GithubHelper do
describe '#download_file_from_tag' do
let(:test_repo) { 'repo-test/project-test' }
let(:test_tag) { '1.0' }
let(:test_file) { 'test-folder/test-file.xml' }
let(:content_url) { "https://api.github.com/repos/#{test_repo}/contents/#{test_file}?ref=#{test_tag}" }
let(:client) do
instance_double(
Octokit::Client,
contents: double(download_url: content_url), # rubocop:disable RSpec/VerifiedDoubles
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'fails if it does not find the right release on GitHub' do
stub = stub_request(:get, content_url).to_return(status: [404, 'Not Found'])
downloaded_file = download_file_from_tag(download_folder: './')
expect(downloaded_file).to be_nil
expect(stub).to have_been_made.once
end
it 'writes the raw content to a file' do
stub = stub_request(:get, content_url).to_return(status: 200, body: 'my-test-content')
Dir.mktmpdir('a8c-download-repo-file-') do |tmpdir|
dst_file = File.join(tmpdir, 'test-file.xml')
downloaded_file = download_file_from_tag(download_folder: tmpdir)
expect(downloaded_file).to eq(dst_file)
expect(stub).to have_been_made.once
expect(File.read(dst_file)).to eq('my-test-content')
end
end
def download_file_from_tag(download_folder:)
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
helper.download_file_from_tag(repository: test_repo, tag: test_tag, file_path: test_file, download_folder: download_folder)
end
end
describe '#find_pull_request' do
let(:test_repo) { 'repo-test/project-test' }
let(:found_pr) { double('PullRequest', html_url: 'https://github.com/repo-test/project-test/pull/42') } # rubocop:disable RSpec/VerifiedDoubles
let(:client) do
instance_double(
Octokit::Client,
pull_requests: [found_pr],
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'qualifies an unqualified head with the repository owner and forwards the base' do
expect(client).to receive(:pull_requests).with(test_repo, { state: 'open', head: 'repo-test:my-branch', base: 'trunk' })
find_pull_request(head: 'my-branch', base: 'trunk')
end
it 'uses an already-qualified head as-is and omits the base when not provided' do
expect(client).to receive(:pull_requests).with(test_repo, { state: 'open', head: 'someone:other-branch' })
find_pull_request(head: 'someone:other-branch')
end
it 'returns the first matching pull request' do
expect(find_pull_request(head: 'my-branch')).to eq(found_pr)
end
it 'returns nil when no pull request matches' do
allow(client).to receive(:pull_requests).and_return([])
expect(find_pull_request(head: 'my-branch')).to be_nil
end
def find_pull_request(head:, base: nil)
described_class.new(github_token: 'Fake-GitHubToken-123').find_pull_request(repository: test_repo, head: head, base: base)
end
end
describe '#get_last_milestone' do
let(:test_repo) { 'repo-test/project-test' }
let(:last_stone) { mock_milestone('10.0') }
let(:client) do
instance_double(
Octokit::Client,
list_milestones: ['9.8 ❄️', '9.9'].map { |title| mock_milestone(title) }.append(last_stone),
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'returns correct milestone' do
expect(client).to receive(:list_milestones)
last_milestone = get_last_milestone(repository: test_repo)
expect(last_milestone).to eq(last_stone)
end
def mock_milestone(title)
{ title: title }
end
def get_last_milestone(repository:)
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
helper.get_last_milestone(repository: repository)
end
end
describe '#comment_on_pr' do
let(:client) do
instance_double(
Octokit::Client,
issue_comments: [],
add_comment: nil,
update_comment: nil,
user: instance_double('User', id: 1234, name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'will create a new comment if an existing one is not found' do
expect(client).to receive(:add_comment)
comment_on_pr
end
it 'will update an existing comment if one is found' do
allow(client).to receive(:issue_comments).and_return([mock_comment])
expect(client).to receive(:update_comment)
comment_on_pr
end
it 'will not match text outside the reuseID tag' do
allow(client).to receive(:issue_comments).and_return([mock_comment(body: 'test-id')])
expect(client).to receive(:add_comment)
comment_on_pr
end
it 'will not match comments belonging to other users' do
allow(client).to receive(:issue_comments).and_return([mock_comment(user_id: 0)])
expect(client).to receive(:add_comment)
comment_on_pr
end
it 'will return the reuse identifier' do
expect(comment_on_pr).to eq 'test-id'
end
def comment_on_pr
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
helper.comment_on_pr(
project_slug: 'test/test',
pr_number: 1234,
body: 'Test',
reuse_identifier: 'test-id'
)
end
def mock_comment(body: '<!-- REUSE_ID: test-id -->\n\nTest', user_id: 1234)
instance_double('Comment', id: 1234, body: body, user: instance_double('User', id: user_id))
end
end
describe '#initialize' do
let(:client) do
instance_double(
Octokit::Client,
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
it 'properly passes the token all the way down to the Octokit::Client' do
allow(Octokit::Client).to receive(:new).and_return(client)
expect(Octokit::Client).to receive(:new).with(access_token: 'Fake-GitHubToken-123')
described_class.new(github_token: 'Fake-GitHubToken-123')
end
end
describe '#get_milestone' do
let(:test_repo) { 'repo-test/project-test' }
let(:test_milestones) { [{ title: '9.8' }, { title: '10.1' }, { title: '10.1.3 ❄️' }] }
let(:client) do
instance_double(
Octokit::Client,
list_milestones: [],
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'properly passes the repository all the way down to the Octokit::Client' do
expect(client).to receive(:list_milestones).with(test_repo)
get_milestone(milestone_name: 'test')
end
it 'returns nil when no milestone is returned from the api' do
milestone = get_milestone(milestone_name: '10')
expect(milestone).to be_nil
end
it 'returns nil when no milestone title starts with the searched term' do
allow(client).to receive(:list_milestones).and_return(test_milestones)
milestone = get_milestone(milestone_name: '8.5')
expect(milestone).to be_nil
end
it 'returns a milestone when the milestone title starts with search term' do
allow(client).to receive(:list_milestones).and_return(test_milestones)
milestone = get_milestone(milestone_name: '9')
expect(milestone).to eq({ title: '9.8' })
end
it 'returns the milestone with the latest due date matching the search term when there are more than one' do
allow(client).to receive(:list_milestones).and_return(test_milestones)
milestone = get_milestone(milestone_name: '10.1')
expect(milestone).to eq({ title: '10.1.3 ❄️' })
end
def get_milestone(milestone_name:)
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
helper.get_milestone(test_repo, milestone_name)
end
end
describe '#get_prs_and_issues_for_milestone' do
let(:test_repo) { 'repo-test/project-test' }
let(:client) do
instance_double(
Octokit::Client,
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
let(:helper) do
described_class.new(github_token: 'Fake-GitHubToken-123')
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'returns only opened PRs for a given milestone by default' do
issue_results = [101].map { |num| sawyer_resource_stub(number: num) }
pr_results = [103].map { |num| sawyer_resource_stub(number: num) }
allow(client).to receive(:search_issues)
.with(%(repo:#{test_repo} milestone:"12.3 New Version" is:issue is:open))
.and_return({ items: issue_results })
allow(client).to receive(:search_issues)
.with(%(repo:#{test_repo} milestone:"12.3 New Version" is:pull-request is:open))
.and_return({ items: pr_results })
result = helper.get_prs_and_issues_for_milestone(repository: test_repo, milestone: '12.3 New Version')
expect(result.map(&:number)).to eq([101, 103])
end
it 'returns only opened PRs for a given milestone if include_closed is false' do
issue_results = [101].map { |num| sawyer_resource_stub(number: num) }
pr_results = [103].map { |num| sawyer_resource_stub(number: num) }
allow(client).to receive(:search_issues)
.with(%(repo:#{test_repo} milestone:"12.3 New Version" is:issue is:open))
.and_return({ items: issue_results })
allow(client).to receive(:search_issues)
.with(%(repo:#{test_repo} milestone:"12.3 New Version" is:pull-request is:open))
.and_return({ items: pr_results })
result = helper.get_prs_and_issues_for_milestone(repository: test_repo, milestone: '12.3 New Version', include_closed: false)
expect(result.map(&:number)).to eq([101, 103])
end
it 'returns both opened and closed PRs of a milestone if include_closed is true' do
issue_results = [101, 102].map { |num| sawyer_resource_stub(number: num) }
pr_results = [103, 104].map { |num| sawyer_resource_stub(number: num) }
allow(client).to receive(:search_issues)
.with(%(repo:#{test_repo} milestone:"12.3 New Version" is:issue))
.and_return({ items: issue_results })
allow(client).to receive(:search_issues)
.with(%(repo:#{test_repo} milestone:"12.3 New Version" is:pull-request))
.and_return({ items: pr_results })
result = helper.get_prs_and_issues_for_milestone(repository: test_repo, milestone: '12.3 New Version', include_closed: true)
expect(result.map(&:number)).to eq([101, 102, 103, 104])
end
end
describe '#set_milestone' do
let(:test_repo) { 'repo-test/project-test' }
let(:client) do
instance_double(
Octokit::Client,
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
let(:helper) do
described_class.new(github_token: 'Fake-GitHubToken-123')
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'updates the milestone as expected when everything is OK' do
allow(client).to receive(:update_issue)
.with(test_repo, 1337, { milestone: 42 })
.and_return(sawyer_resource_stub(number: 1337))
result = helper.set_milestone(
repository: test_repo,
number: 1337,
milestone: 42
)
expect(result&.number).to eq(1337)
end
it 'raises a user_error! if PR number does not exist' do
allow(client).to receive(:update_issue)
.with(test_repo, 1337, { milestone: 42 })
.and_raise(Octokit::NotFound)
expect do
helper.set_milestone(
repository: test_repo,
number: 1337,
milestone: 42
)
end.to raise_error(FastlaneCore::Interface::FastlaneError, "Could not find PR or issue #1337 in #{test_repo}")
end
it 'raises a user_error! if the source milestone could not be found' do
allow(client).to receive(:update_issue)
.with(test_repo, 1337, { milestone: 42 })
.and_raise(Octokit::UnprocessableEntity)
expect do
helper.set_milestone(
repository: test_repo,
number: 1337,
milestone: 42
)
end.to raise_error(FastlaneCore::Interface::FastlaneError, 'Invalid milestone 42')
end
end
describe '#create_milestone' do
let(:test_repo) { 'repo-test/project-test' }
let(:test_milestone_number) { '10.0' }
let(:test_milestone_duedate) { '2022-10-22T23:39:01Z' }
let(:client) do
instance_double(
Octokit::Client,
create_milestone: nil,
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'computes the correct dates for standard period' do
due_date = '2022-12-02T08:00:00Z'.to_time.utc
options = {
due_on: '2022-12-02T12:00:00Z',
description: "Code freeze: December 02, 2022\nApp Store submission: December 06, 2022\nRelease: December 09, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 4, days_until_release: 7)
end
it 'computes the correct dates when submission and release dates are in the same day' do
due_date = '2022-12-02T08:00:00Z'.to_time.utc
options = {
due_on: '2022-12-02T12:00:00Z',
description: "Code freeze: December 02, 2022\nApp Store submission: December 03, 2022\nRelease: December 03, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 1, days_until_release: 1)
end
it 'computes the correct dates when the due date is on the verge of a DST day change' do
# Europe DST starts on the last Sunday of March, and ends on the last Sunday of October
Time.use_zone('Europe/London') do
# March 27th, 2022 is the exact day that London switches to the DST (+1h)
# If the due date is too close to the next day, a day change will happen
# So, 2022-03-27 23:00:00Z will be exactly 2022-03-28 00:00:00 +0100 at the DST change
due_date = Time.zone.parse('2022-03-27 23:00:00Z')
options = {
due_on: '2022-03-28T12:00:00Z',
description: "Code freeze: March 28, 2022\nApp Store submission: March 30, 2022\nRelease: March 31, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 2, days_until_release: 3)
end
end
it 'computes the correct dates when the due date is on DST but has no day change' do
# Europe DST starts on the last Sunday of March, and ends on the last Sunday of October
Time.use_zone('Europe/London') do
# March 27th, 2022 is the exact day that London switches to the DST (+1h)
# If the due date is not close enough at the day change, nothing will occur.
# So, 2022-03-27 22:00:00Z will be exactly 2022-03-27 23:00:00 +0100 at the DST change.
due_date = Time.zone.parse('2022-03-27 22:00:00Z')
options = {
due_on: '2022-03-27T12:00:00Z',
description: "Code freeze: March 27, 2022\nApp Store submission: March 29, 2022\nRelease: March 30, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 2, days_until_release: 3)
end
end
it 'computes the correct dates when the due date is one day before a DST change' do
# Europe DST starts on the last Sunday of March, and ends on the last Sunday of October
Time.use_zone('Europe/London') do
# As London changes to DST on March 27th, the date shouldn't be changed
# So, 2022-03-26 23:00:00Z will be exactly 2022-03-26 23:00:00 +0000 at this Timezone.
due_date = Time.zone.parse('2022-03-26 23:00:00Z')
options = {
due_on: '2022-03-26T12:00:00Z',
description: "Code freeze: March 26, 2022\nApp Store submission: March 28, 2022\nRelease: March 29, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 2, days_until_release: 3)
end
end
it 'computes the correct dates when the offset is between DST endings' do
# Europe DST starts on the last Sunday of March, and ends on the last Sunday of October
Time.use_zone('Europe/London') do
due_date = Time.zone.parse('2022-10-30 23:00:00Z')
options = {
due_on: '2022-10-30T12:00:00Z',
description: "Code freeze: October 30, 2022\nApp Store submission: March 19, 2023\nRelease: March 25, 2023\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 140, days_until_release: 146)
end
end
it 'computes the correct dates when the release and submission dates are at the last day of a DST change' do
# Europe DST starts on the last Sunday of March, and ends on the last Sunday of October
Time.use_zone('Europe/London') do
due_date = Time.zone.parse('2022-03-27 23:00:00Z')
options = {
due_on: '2022-03-28T12:00:00Z',
description: "Code freeze: March 28, 2022\nApp Store submission: October 30, 2022\nRelease: October 31, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 216, days_until_release: 217)
end
end
it 'computes the correct dates when the due date is before Europe and USA DST changes and ends inside a DST period on Europe' do
# USA DST starts on the second Sunday in March. and ends on the first Sunday in November
# Europe DST starts on the last Sunday of March, and ends on the last Sunday in October
Time.use_zone('Europe/London') do
due_date = Time.zone.parse('2022-03-05 23:00:00Z')
options = {
due_on: '2022-03-05T12:00:00Z',
description: "Code freeze: March 05, 2022\nApp Store submission: May 04, 2022\nRelease: May 05, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 60, days_until_release: 61)
end
end
it 'computes the correct dates when the due date is before Europe and USA DST changes and ends inside a DST period on USA' do
# USA DST starts on the second Sunday in March. and ends on the first Sunday in November
# Europe DST starts on the last Sunday of March, and ends on the last Sunday in October
Time.use_zone('America/Los_Angeles') do
due_date = Time.zone.parse('2022-03-05 23:00:00Z')
options = {
due_on: '2022-03-05T12:00:00Z',
description: "Code freeze: March 05, 2022\nApp Store submission: May 04, 2022\nRelease: May 05, 2022\n"
}
expect(client).to receive(:create_milestone).with(test_repo, test_milestone_number, options)
create_milestone(due_date: due_date, days_until_submission: 60, days_until_release: 61)
end
end
it 'raises an error if days_until_submission is less than or equal zero' do
due_date = '2022-10-20T08:00:00Z'.to_time.utc
expect { create_milestone(due_date: due_date, days_until_submission: 0, days_until_release: 5) }
.to raise_error(FastlaneCore::Interface::FastlaneError, 'days_until_submission must be greater than zero.')
end
it 'raises an error if days_until_release is less than or equal zero' do
due_date = '2022-10-20T08:00:00Z'.to_time.utc
expect { create_milestone(due_date: due_date, days_until_submission: 12, days_until_release: -8) }
.to raise_error(FastlaneCore::Interface::FastlaneError, 'days_until_release must be greater than zero.')
end
it 'raises an error if days_until_submission is greater than days_until_release' do
due_date = '2022-10-20T08:00:00Z'.to_time.utc
expect { create_milestone(due_date: due_date, days_until_submission: 14, days_until_release: 3) }
.to raise_error(FastlaneCore::Interface::FastlaneError, 'days_until_release must be greater or equal to days_until_submission.')
end
def create_milestone(due_date:, days_until_submission:, days_until_release:)
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
helper.create_milestone(
repository: test_repo,
title: test_milestone_number,
due_date: due_date,
days_until_submission: days_until_submission,
days_until_release: days_until_release
)
end
end
describe '#create_release' do
let(:test_repo) { 'repo-test/project-test' }
let(:test_tag) { '1.0' }
let(:test_target) { 'dummysha123456' }
let(:release_url) { 'https://github.com/org/repo/releases/tag/1.2.3' }
let(:test_description) { 'Hey Im a Test Description' }
let(:client) do
instance_double(
Octokit::Client,
create_release: nil,
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
end
it 'has the correct options' do
options = { body: test_description, draft: true, name: test_tag, prerelease: false, target_commitish: test_target }
expect(client).to receive(:create_release).with(test_repo, test_tag, options)
allow(client).to receive(:create_release).and_return(html_url: release_url)
create_release(is_draft: true)
end
it 'uploads the assets to the correct location' do
test_assets = 'test-file.xml'
test_url = '/test/url'
allow(client).to receive(:create_release).and_return({ url: test_url })
expect(client).to receive(:upload_asset).with(test_url, test_assets, { content_type: 'application/octet-stream' })
create_release(is_draft: true, assets: [test_assets])
end
it 'creates a draft release if is_draft is set to true' do
options_draft_release = { body: test_description, draft: true, name: test_tag, prerelease: false, target_commitish: test_target }
expect(client).to receive(:create_release).with(test_repo, test_tag, options_draft_release)
allow(client).to receive(:create_release).and_return(html_url: release_url)
url = create_release(is_draft: true)
expect(url).to eq(release_url)
end
it 'creates a final (non-draft) release if is_draft is set to false' do
options_final_release = { body: test_description, draft: false, name: test_tag, prerelease: false, target_commitish: test_target }
expect(client).to receive(:create_release).with(test_repo, test_tag, options_final_release)
allow(client).to receive(:create_release).and_return(html_url: release_url)
url = create_release(is_draft: false)
expect(url).to eq(release_url)
end
it 'uses a custom name when provided' do
custom_name = 'Version 1.0'
options = { body: test_description, draft: true, name: custom_name, prerelease: false, target_commitish: test_target }
expect(client).to receive(:create_release).with(test_repo, test_tag, options)
allow(client).to receive(:create_release).and_return(html_url: release_url)
url = create_release(is_draft: true, name: custom_name)
expect(url).to eq(release_url)
end
def create_release(is_draft:, assets: [], name: nil)
helper = described_class.new(github_token: 'Fake-GitHubToken-123')
helper.create_release(
repository: test_repo,
version: test_tag,
name: name,
target: test_target,
description: test_description,
assets: assets,
prerelease: false,
is_draft: is_draft
)
end
end
describe '#upload_release_assets' do
let(:test_repo) { 'repo-test/project-test' }
let(:test_version) { '1.0.0' }
let(:release_url) { 'https://api.github.com/repos/repo-test/project-test/releases/123' }
let(:release_html_url) { 'https://github.com/repo-test/project-test/releases/tag/1.0.0' }
let(:release) { sawyer_resource_stub(url: release_url, html_url: release_html_url) }
let(:existing_assets) { [] }
let(:uploaded_asset) { release_asset(name: 'test-app.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/999') }
let(:client) do
instance_double(
Octokit::Client,
user: instance_double('User', name: 'test'),
'auto_paginate=': nil
)
end
let(:helper) do
described_class.new(github_token: 'Fake-GitHubToken-123')
end
before do
allow(Octokit::Client).to receive(:new).and_return(client)
allow(client).to receive(:release_for_tag).with(test_repo, test_version).and_return(release)
allow(client).to receive(:release_assets).with(release_url).and_return(existing_assets)
allow(client).to receive_messages(upload_asset: uploaded_asset, delete_release_asset: true)
end
it 'fails clearly if the release does not exist' do
allow(client).to receive(:release_for_tag).with(test_repo, test_version).and_raise(Octokit::NotFound)
with_tmp_file(named: 'test-app.zip') do |file_path|
expect do
upload_release_assets(assets: [file_path])
end.to raise_error(FastlaneCore::Interface::FastlaneError, "Could not find GitHub Release for tag #{test_version} in #{test_repo}")
end
end
it 'fails clearly if an asset file does not exist' do
expect(client).not_to receive(:release_for_tag)
expect(client).not_to receive(:release_assets)
expect(client).not_to receive(:upload_asset)
expect do
upload_release_assets(assets: ['missing-file.zip'])
end.to raise_error(FastlaneCore::Interface::FastlaneError, "Can't find file missing-file.zip!")
end
it 'fails clearly if an asset is not a file path' do
expect(client).not_to receive(:release_for_tag)
expect(client).not_to receive(:release_assets)
expect(client).not_to receive(:upload_asset)
expect do
upload_release_assets(assets: [123])
end.to raise_error(FastlaneCore::Interface::FastlaneError, 'release_assets must contain file paths')
end
it 'fails without mutating GitHub when local assets have duplicate filenames' do
in_tmp_dir do |tmpdir|
first_dir = File.join(tmpdir, 'ios')
second_dir = File.join(tmpdir, 'tvos')
Dir.mkdir(first_dir)
Dir.mkdir(second_dir)
first_file_path = File.join(first_dir, 'test-app.zip')
second_file_path = File.join(second_dir, 'test-app.zip')
File.write(first_file_path, 'ios')
File.write(second_file_path, 'tvos')
expect(client).not_to receive(:release_for_tag)
expect(client).not_to receive(:release_assets)
expect(client).not_to receive(:delete_release_asset)
expect(client).not_to receive(:upload_asset)
expect do
upload_release_assets(assets: [first_file_path, second_file_path], replace_existing: false)
end.to raise_error(FastlaneCore::Interface::FastlaneError, 'release_assets must not contain duplicate filenames')
end
end
it 'uploads one asset to the existing release' do
with_tmp_file(named: 'test-app.zip') do |file_path|
expect(client).to receive(:upload_asset).with(release_url, file_path, { content_type: 'application/octet-stream' })
result = upload_release_assets(assets: [file_path])
expect(result).to eq(release_html_url)
end
end
it 'uploads multiple assets to the existing release' do
in_tmp_dir do |tmpdir|
first_file_path = File.join(tmpdir, 'test-ios.zip')
second_file_path = File.join(tmpdir, 'test-tvos.zip')
File.write(first_file_path, 'ios')
File.write(second_file_path, 'tvos')
first_uploaded_asset = release_asset(name: 'test-ios.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/1000')
second_uploaded_asset = release_asset(name: 'test-tvos.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/1001')
expect(client).to receive(:upload_asset).with(release_url, first_file_path, { content_type: 'application/octet-stream' }).ordered.and_return(first_uploaded_asset)
expect(client).to receive(:upload_asset).with(release_url, second_file_path, { content_type: 'application/octet-stream' }).ordered.and_return(second_uploaded_asset)
result = upload_release_assets(assets: [first_file_path, second_file_path])
expect(result).to eq(release_html_url)
end
end
it 'replaces an existing asset with the same filename' do
existing_asset = release_asset(name: 'test-app.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/1234')
allow(client).to receive(:release_assets).with(release_url).and_return([existing_asset])
with_tmp_file(named: 'test-app.zip') do |file_path|
expect(client).to receive(:delete_release_asset).with(existing_asset.url)
expect(client).to receive(:upload_asset).with(release_url, file_path, { content_type: 'application/octet-stream' })
result = upload_release_assets(assets: [file_path])
expect(result).to eq(release_html_url)
end
end
it 'preserves unrelated existing assets' do
matching_asset = release_asset(name: 'test-app.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/1234')
unrelated_asset = release_asset(name: 'other-platform.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/5678')
deleted_asset_urls = []
allow(client).to receive(:release_assets).with(release_url).and_return([matching_asset, unrelated_asset])
allow(client).to receive(:delete_release_asset) do |asset_url|
deleted_asset_urls << asset_url
true
end
with_tmp_file(named: 'test-app.zip') do |file_path|
upload_release_assets(assets: [file_path])
end
expect(deleted_asset_urls).to eq([matching_asset.url])
end
it 'fails without deleting or uploading when replace_existing is false and a matching asset exists' do
existing_asset = release_asset(name: 'test-app.zip', url: 'https://api.github.com/repos/repo-test/project-test/releases/assets/1234')
allow(client).to receive(:release_assets).with(release_url).and_return([existing_asset])
expect(client).not_to receive(:delete_release_asset)
expect(client).not_to receive(:upload_asset)
with_tmp_file(named: 'test-app.zip') do |file_path|
expect do
upload_release_assets(assets: [file_path], replace_existing: false)
end.to raise_error(FastlaneCore::Interface::FastlaneError, "GitHub Release #{test_version} already has an asset named test-app.zip. Set replace_existing: true to replace it.")
end
end
def upload_release_assets(assets:, replace_existing: true)
helper.upload_release_assets(
repository: test_repo,
version: test_version,
assets: assets,
replace_existing: replace_existing
)
end
def release_asset(name:, url:)
sawyer_resource_stub(name: name, url: url)
end
end
describe '#github_token_config_item' do
it 'has the correct key' do
expect(described_class.github_token_config_item.key).to eq(:github_token)
end
it 'has the correct env_name' do
expect(described_class.github_token_config_item.env_name).to eq('GITHUB_TOKEN')
end
it 'has the correct description' do
expect(described_class.github_token_config_item.description).to eq('The GitHub OAuth access token')
end
it 'is not optional' do
expect(described_class.github_token_config_item.optional).to be(false)
end
it 'has String as data_type' do
expect(described_class.github_token_config_item.data_type).to eq(String)
end
end
end