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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::GitalyClient::OperationService do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository) }
let(:repository) { project.repository.raw }
let(:client) { described_class.new(repository) }
let(:gitaly_user) { Gitlab::Git::User.from_gitlab(user).to_gitaly }
describe '#user_create_branch' do
let(:branch_name) { 'new' }
let(:start_point) { 'master' }
let(:request) do
Gitaly::UserCreateBranchRequest.new(
repository: repository.gitaly_repository,
branch_name: branch_name,
start_point: start_point,
user: gitaly_user
)
end
let(:gitaly_commit) { build(:gitaly_commit) }
let(:commit_id) { gitaly_commit.id }
let(:gitaly_branch) do
Gitaly::Branch.new(name: branch_name, target_commit: gitaly_commit)
end
let(:response) { Gitaly::UserCreateBranchResponse.new(branch: gitaly_branch) }
let(:commit) { Gitlab::Git::Commit.new(repository, gitaly_commit) }
subject { client.user_create_branch(branch_name, user, start_point) }
it 'sends a user_create_branch message and returns a Gitlab::git::Branch' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_create_branch).with(request, kind_of(Hash))
.and_return(response)
expect(subject.name).to eq(branch_name)
expect(subject.dereferenced_target).to eq(commit)
end
context "when pre_receive_error is present" do
let(:response) do
Gitaly::UserCreateBranchResponse.new(pre_receive_error: "GitLab: something failed")
end
it "throws a PreReceive exception" do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_create_branch).with(request, kind_of(Hash))
.and_return(response)
expect { subject }.to raise_error(
Gitlab::Git::PreReceiveError, "something failed")
end
end
end
describe '#user_update_branch' do
let(:branch_name) { 'my-branch' }
let(:newrev) { '01e' }
let(:oldrev) { '01d' }
let(:request) do
Gitaly::UserUpdateBranchRequest.new(
repository: repository.gitaly_repository,
branch_name: branch_name,
newrev: newrev,
oldrev: oldrev,
user: gitaly_user
)
end
let(:response) { Gitaly::UserUpdateBranchResponse.new }
subject { client.user_update_branch(branch_name, user, newrev, oldrev) }
it 'sends a user_update_branch message' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_update_branch).with(request, kind_of(Hash))
.and_return(response)
subject
end
context "when pre_receive_error is present" do
let(:response) do
Gitaly::UserUpdateBranchResponse.new(pre_receive_error: "GitLab: something failed")
end
it "throws a PreReceive exception" do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_update_branch).with(request, kind_of(Hash))
.and_return(response)
expect { subject }.to raise_error(
Gitlab::Git::PreReceiveError, "something failed")
end
end
end
describe '#user_merge_to_ref' do
let(:first_parent_ref) { 'refs/heads/my-branch' }
let(:source_sha) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }
let(:ref) { 'refs/merge-requests/x/merge' }
let(:message) { 'validaciĆ³n' }
let(:response) { Gitaly::UserMergeToRefResponse.new(commit_id: 'new-commit-id') }
let(:payload) do
{ source_sha: source_sha, branch: 'branch', target_ref: ref,
message: message, first_parent_ref: first_parent_ref, allow_conflicts: true }
end
it 'sends a user_merge_to_ref message' do
freeze_time do
expect_any_instance_of(Gitaly::OperationService::Stub).to receive(:user_merge_to_ref) do |_, request, options|
expect(options).to be_kind_of(Hash)
expect(request.to_h).to eq(
payload.merge({
repository: repository.gitaly_repository.to_h,
message: message.dup.force_encoding(Encoding::ASCII_8BIT),
user: Gitlab::Git::User.from_gitlab(user).to_gitaly.to_h,
timestamp: { nanos: 0, seconds: Time.current.to_i }
})
)
end.and_return(response)
client.user_merge_to_ref(user, **payload)
end
end
end
describe '#user_delete_branch' do
let(:branch_name) { 'my-branch' }
let(:request) do
Gitaly::UserDeleteBranchRequest.new(
repository: repository.gitaly_repository,
branch_name: branch_name,
user: gitaly_user
)
end
let(:response) { Gitaly::UserDeleteBranchResponse.new }
subject { client.user_delete_branch(branch_name, user) }
it 'sends a user_delete_branch message' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_delete_branch).with(request, kind_of(Hash))
.and_return(response)
subject
end
context "when pre_receive_error is present" do
let(:response) do
Gitaly::UserDeleteBranchResponse.new(pre_receive_error: "GitLab: something failed")
end
it "throws a PreReceive exception" do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_delete_branch).with(request, kind_of(Hash))
.and_return(response)
expect { subject }.to raise_error(
Gitlab::Git::PreReceiveError, "something failed")
end
end
context 'with a custom hook error' do
let(:stdout) { nil }
let(:stderr) { nil }
let(:error_message) { "error_message" }
let(:custom_hook_error) do
new_detailed_error(
GRPC::Core::StatusCodes::PERMISSION_DENIED,
error_message,
Gitaly::UserDeleteBranchError.new(
custom_hook: Gitaly::CustomHookError.new(
stdout: stdout,
stderr: stderr,
hook_type: Gitaly::CustomHookError::HookType::HOOK_TYPE_PRERECEIVE
)))
end
shared_examples 'a failed branch deletion' do
it 'raises a PreReceiveError' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_delete_branch).with(request, kind_of(Hash))
.and_raise(custom_hook_error)
expect { subject }.to raise_error do |error|
expect(error).to be_a(Gitlab::Git::PreReceiveError)
expect(error.message).to eq(expected_message)
expect(error.raw_message).to eq(expected_raw_message)
end
end
end
context 'when details contain stderr' do
let(:stderr) { "something" }
let(:stdout) { "GL-HOOK-ERR: stdout is overridden by stderr" }
let(:expected_message) { error_message }
let(:expected_raw_message) { stderr }
it_behaves_like 'a failed branch deletion'
end
context 'when details contain stdout' do
let(:stderr) { " \n" }
let(:stdout) { "something" }
let(:expected_message) { error_message }
let(:expected_raw_message) { stdout }
it_behaves_like 'a failed branch deletion'
end
end
context 'with a non-detailed error' do
it 'raises a GRPC error' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_delete_branch).with(request, kind_of(Hash))
.and_raise(GRPC::Internal.new('non-detailed error'))
expect { subject }.to raise_error(GRPC::Internal)
end
end
end
describe '#user_merge_branch' do
let(:target_branch) { 'master' }
let(:source_sha) { '5937ac0a7beb003549fc5fd26fc247adbce4a52e' }
let(:message) { 'Merge a branch' }
subject { client.user_merge_branch(user, source_sha, target_branch, message) {} }
it 'sends a user_merge_branch message' do
expect(subject).to be_a(Gitlab::Git::OperationService::BranchUpdate)
expect(subject.newrev).to be_present
expect(subject.repo_created).to be(false)
expect(subject.branch_created).to be(false)
end
context 'with an exception with the UserMergeBranchError' do
let(:permission_error) do
new_detailed_error(
GRPC::Core::StatusCodes::PERMISSION_DENIED,
"GitLab: You are not allowed to push code to this project.",
Gitaly::UserMergeBranchError.new(
access_check: Gitaly::AccessCheckError.new(
error_message: "You are not allowed to push code to this project.",
protocol: "web",
user_id: "user-15",
changes: "df15b32277d2c55c6c595845a87109b09c913c556 5d6e0f935ad9240655f64e883cd98fad6f9a17ee refs/heads/master\n"
)))
end
it 'raises PreRecieveError with the error message' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_merge_branch).with(kind_of(Enumerator), kind_of(Hash))
.and_raise(permission_error)
expect { subject }.to raise_error do |error|
expect(error).to be_a(Gitlab::Git::PreReceiveError)
expect(error.message).to eq("You are not allowed to push code to this project.")
end
end
end
context 'with a custom hook error' do
let(:stdout) { nil }
let(:stderr) { nil }
let(:error_message) { "error_message" }
let(:custom_hook_error) do
new_detailed_error(
GRPC::Core::StatusCodes::PERMISSION_DENIED,
error_message,
Gitaly::UserMergeBranchError.new(
custom_hook: Gitaly::CustomHookError.new(
stdout: stdout,
stderr: stderr,
hook_type: Gitaly::CustomHookError::HookType::HOOK_TYPE_PRERECEIVE
)))
end
shared_examples 'a failed merge' do
it 'raises a PreReceiveError' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_merge_branch).with(kind_of(Enumerator), kind_of(Hash))
.and_raise(custom_hook_error)
expect { subject }.to raise_error do |error|
expect(error).to be_a(Gitlab::Git::PreReceiveError)
expect(error.message).to eq(expected_message)
expect(error.raw_message).to eq(expected_raw_message)
end
end
end
context 'when details contain stderr without prefix' do
let(:stderr) { "something" }
let(:stdout) { "GL-HOOK-ERR: stdout is overridden by stderr" }
let(:expected_message) { error_message }
let(:expected_raw_message) { stderr }
it_behaves_like 'a failed merge'
end
context 'when details contain stderr with prefix' do
let(:stderr) { "GL-HOOK-ERR: something" }
let(:stdout) { "GL-HOOK-ERR: stdout is overridden by stderr" }
let(:expected_message) { "something" }
let(:expected_raw_message) { stderr }
it_behaves_like 'a failed merge'
end
context 'when details contain stdout without prefix' do
let(:stderr) { " \n" }
let(:stdout) { "something" }
let(:expected_message) { error_message }
let(:expected_raw_message) { stdout }
it_behaves_like 'a failed merge'
end
context 'when details contain stdout with prefix' do
let(:stderr) { " \n" }
let(:stdout) { "GL-HOOK-ERR: something" }
let(:expected_message) { "something" }
let(:expected_raw_message) { stdout }
it_behaves_like 'a failed merge'
end
context 'when details contain no stderr or stdout' do
let(:stderr) { " \n" }
let(:stdout) { "\n \n" }
let(:expected_message) { error_message }
let(:expected_raw_message) { "\n \n" }
it_behaves_like 'a failed merge'
end
end
context 'with an exception without the detailed error' do
let(:permission_error) do
GRPC::PermissionDenied.new
end
it 'raises PermissionDenied' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_merge_branch).with(kind_of(Enumerator), kind_of(Hash))
.and_raise(permission_error)
expect { subject }.to raise_error(GRPC::PermissionDenied)
end
end
context 'with ReferenceUpdateError' do
let(:reference_update_error) do
new_detailed_error(GRPC::Core::StatusCodes::FAILED_PRECONDITION,
"some ignored error message",
Gitaly::UserMergeBranchError.new(
reference_update: Gitaly::ReferenceUpdateError.new(
reference_name: "refs/heads/something",
old_oid: "1234",
new_oid: "6789"
)))
end
it 'returns nil' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_merge_branch).with(kind_of(Enumerator), kind_of(Hash))
.and_raise(reference_update_error)
expect(subject).to be_nil
end
end
end
describe '#user_ff_branch' do
let(:target_branch) { 'my-branch' }
let(:source_sha) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }
let(:request) do
Gitaly::UserFFBranchRequest.new(
repository: repository.gitaly_repository,
branch: target_branch,
commit_id: source_sha,
user: gitaly_user
)
end
let(:branch_update) do
Gitaly::OperationBranchUpdate.new(
commit_id: source_sha,
repo_created: false,
branch_created: false
)
end
let(:response) { Gitaly::UserFFBranchResponse.new(branch_update: branch_update) }
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_ff_branch).with(request, kind_of(Hash))
.and_return(response)
end
subject { client.user_ff_branch(user, source_sha, target_branch) }
it 'sends a user_ff_branch message and returns a BranchUpdate object' do
expect(subject).to be_a(Gitlab::Git::OperationService::BranchUpdate)
expect(subject.newrev).to eq(source_sha)
expect(subject.repo_created).to be(false)
expect(subject.branch_created).to be(false)
end
context 'when the response has no branch_update' do
let(:response) { Gitaly::UserFFBranchResponse.new }
it { expect(subject).to be_nil }
end
context "when the pre-receive hook fails" do
let(:response) do
Gitaly::UserFFBranchResponse.new(
branch_update: nil,
pre_receive_error: "pre-receive hook error message\n"
)
end
it "raises the error" do
# the PreReceiveError class strips the GL-HOOK-ERR prefix from this error
expect { subject }.to raise_error(Gitlab::Git::PreReceiveError, "pre-receive hook failed.")
end
end
end
shared_examples 'cherry pick and revert errors' do
context 'when a pre_receive_error is present' do
let(:response) { response_class.new(pre_receive_error: "GitLab: something failed") }
it 'raises a PreReceiveError' do
expect { subject }.to raise_error(Gitlab::Git::PreReceiveError, "something failed")
end
end
context 'when a commit_error is present' do
let(:response) { response_class.new(commit_error: "something failed") }
it 'raises a CommitError' do
expect { subject }.to raise_error(Gitlab::Git::CommitError, "something failed")
end
end
context 'when a create_tree_error is present' do
let(:response) { response_class.new(create_tree_error: "something failed", create_tree_error_code: 'EMPTY') }
it 'raises a CreateTreeError' do
expect { subject }.to raise_error(Gitlab::Git::Repository::CreateTreeError) do |error|
expect(error.error_code).to eq(:empty)
end
end
end
context 'when branch_update is nil' do
let(:response) { response_class.new }
it { expect(subject).to be_nil }
end
end
shared_examples '#user_cherry_pick with a gRPC error' do
it 'raises an exception' do
expect_any_instance_of(Gitaly::OperationService::Stub).to receive(:user_cherry_pick)
.and_raise(raised_error)
expect { subject }.to raise_error(expected_error, expected_error_message)
end
end
describe '#user_cherry_pick' do
let(:response_class) { Gitaly::UserCherryPickResponse }
subject do
client.user_cherry_pick(
user: user,
commit: repository.commit,
branch_name: 'master',
message: 'Cherry-pick message',
start_branch_name: 'master',
start_repository: repository
)
end
context 'when errors are not raised but returned in the response' do
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_cherry_pick).with(kind_of(Gitaly::UserCherryPickRequest), kind_of(Hash))
.and_return(response)
end
it_behaves_like 'cherry pick and revert errors'
end
context 'when AccessCheckError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::INTERNAL,
'something failed',
Gitaly::UserCherryPickError.new(
access_check: Gitaly::AccessCheckError.new(
error_message: 'something went wrong'
)))
end
let(:expected_error) { Gitlab::Git::PreReceiveError }
let(:expected_error_message) { "something went wrong" }
it_behaves_like '#user_cherry_pick with a gRPC error'
end
context 'when NotAncestorError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::FAILED_PRECONDITION,
'Branch diverged',
Gitaly::UserCherryPickError.new(
target_branch_diverged: Gitaly::NotAncestorError.new
)
)
end
let(:expected_error) { Gitlab::Git::CommitError }
let(:expected_error_message) { 'branch diverged' }
it_behaves_like '#user_cherry_pick with a gRPC error'
end
context 'when MergeConflictError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::FAILED_PRECONDITION,
'Conflict',
Gitaly::UserCherryPickError.new(
cherry_pick_conflict: Gitaly::MergeConflictError.new
)
)
end
let(:expected_error) { Gitlab::Git::Repository::CreateTreeError }
let(:expected_error_message) {}
it_behaves_like '#user_cherry_pick with a gRPC error'
end
context 'when a non-detailed gRPC error is raised' do
let(:raised_error) { GRPC::Internal.new('non-detailed error') }
let(:expected_error) { GRPC::Internal }
let(:expected_error_message) {}
it_behaves_like '#user_cherry_pick with a gRPC error'
end
end
describe '#user_revert' do
let(:response_class) { Gitaly::UserRevertResponse }
subject do
client.user_revert(
user: user,
commit: repository.commit,
branch_name: 'master',
message: 'Revert message',
start_branch_name: 'master',
start_repository: repository
)
end
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_revert).with(kind_of(Gitaly::UserRevertRequest), kind_of(Hash))
.and_return(response)
end
it_behaves_like 'cherry pick and revert errors'
end
describe '#rebase' do
let(:response) { Gitaly::UserRebaseConfirmableResponse.new }
subject do
client.rebase(
user,
'',
branch: 'master',
branch_sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0',
remote_repository: repository,
remote_branch: 'master'
)
end
shared_examples '#rebase with an error' do
it 'raises a GitError exception' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_rebase_confirmable)
.and_raise(raised_error)
expect { subject }.to raise_error(expected_error)
end
end
context 'when AccessError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::INTERNAL,
'something failed',
Gitaly::UserRebaseConfirmableError.new(
access_check: Gitaly::AccessCheckError.new(
error_message: 'something went wrong'
)))
end
let(:expected_error) { Gitlab::Git::PreReceiveError }
it_behaves_like '#rebase with an error'
end
context 'when RebaseConflictError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::INTERNAL,
'something failed',
Gitaly::UserSquashError.new(
rebase_conflict: Gitaly::MergeConflictError.new(
conflicting_files: ['conflicting-file']
)))
end
let(:expected_error) { Gitlab::Git::Repository::GitError }
it_behaves_like '#rebase with an error'
end
context 'when non-detailed gRPC error is raised' do
let(:raised_error) do
GRPC::Internal.new('non-detailed error')
end
let(:expected_error) { GRPC::Internal }
it_behaves_like '#rebase with an error'
end
end
describe '#user_squash' do
let(:start_sha) { 'b83d6e391c22777fca1ed3012fce84f633d7fed0' }
let(:end_sha) { '54cec5282aa9f21856362fe321c800c236a61615' }
let(:commit_message) { 'Squash message' }
let(:time) do
Time.now.utc
end
let(:request) do
Gitaly::UserSquashRequest.new(
repository: repository.gitaly_repository,
user: gitaly_user,
start_sha: start_sha,
end_sha: end_sha,
author: gitaly_user,
commit_message: commit_message,
timestamp: Google::Protobuf::Timestamp.new(seconds: time.to_i)
)
end
let(:squash_sha) { 'f00' }
let(:response) { Gitaly::UserSquashResponse.new(squash_sha: squash_sha) }
subject do
client.user_squash(user, start_sha, end_sha, user, commit_message, time)
end
it 'sends a user_squash message and returns the squash sha' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_squash).with(request, kind_of(Hash))
.and_return(response)
expect(subject).to eq(squash_sha)
end
shared_examples '#user_squash with an error' do
it 'raises a GitError exception' do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_squash).with(request, kind_of(Hash))
.and_raise(raised_error)
expect { subject }.to raise_error(expected_error)
end
end
context 'when ResolveRevisionError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::INVALID_ARGUMENT,
'something failed',
Gitaly::UserSquashError.new(
resolve_revision: Gitaly::ResolveRevisionError.new(
revision: start_sha
)))
end
let(:expected_error) { Gitlab::Git::Repository::GitError }
it_behaves_like '#user_squash with an error'
end
context 'when RebaseConflictError is raised' do
let(:raised_error) do
new_detailed_error(
GRPC::Core::StatusCodes::INTERNAL,
'something failed',
Gitaly::UserSquashError.new(
rebase_conflict: Gitaly::MergeConflictError.new(
conflicting_files: ['conflicting-file']
)))
end
let(:expected_error) { Gitlab::Git::Repository::GitError }
it_behaves_like '#user_squash with an error'
end
context 'when non-detailed gRPC error is raised' do
let(:raised_error) do
GRPC::Internal.new('non-detailed error')
end
let(:expected_error) { GRPC::Internal }
it_behaves_like '#user_squash with an error'
end
end
describe '#user_commit_files' do
subject do
client.user_commit_files(
gitaly_user, 'my-branch', 'Commit files message', [], 'janedoe@example.com', 'Jane Doe',
'master', repository)
end
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_commit_files).with(kind_of(Enumerator), kind_of(Hash))
.and_return(response)
end
context 'when a pre_receive_error is present' do
let(:response) { Gitaly::UserCommitFilesResponse.new(pre_receive_error: "GitLab: something failed") }
it 'raises a PreReceiveError' do
expect { subject }.to raise_error(Gitlab::Git::PreReceiveError, "something failed")
end
end
context 'when an index_error is present' do
let(:response) { Gitaly::UserCommitFilesResponse.new(index_error: "something failed") }
it 'raises a PreReceiveError' do
expect { subject }.to raise_error(Gitlab::Git::Index::IndexError, "something failed")
end
end
context 'when branch_update is nil' do
let(:response) { Gitaly::UserCommitFilesResponse.new }
it { expect(subject).to be_nil }
end
end
describe '#user_commit_patches' do
let(:patches_folder) { Rails.root.join('spec/fixtures/patchfiles') }
let(:patch_content) do
patch_names.map { |name| File.read(File.join(patches_folder, name)) }.join("\n")
end
let(:patch_names) { %w(0001-This-does-not-apply-to-the-feature-branch.patch) }
let(:branch_name) { 'branch-with-patches' }
subject(:commit_patches) do
client.user_commit_patches(user, branch_name, patch_content)
end
it 'applies the patch correctly' do
branch_update = commit_patches
expect(branch_update).to be_branch_created
commit = repository.commit(branch_update.newrev)
expect(commit.author_email).to eq('patchuser@gitlab.org')
expect(commit.committer_email).to eq(user.email)
expect(commit.message.chomp).to eq('This does not apply to the `feature` branch')
end
context 'when the patch could not be applied' do
let(:patch_names) { %w(0001-This-does-not-apply-to-the-feature-branch.patch) }
let(:branch_name) { 'feature' }
it 'raises the correct error' do
expect { commit_patches }.to raise_error(GRPC::FailedPrecondition)
end
end
end
describe '#add_tag' do
let(:tag_name) { 'some-tag' }
let(:tag_message) { nil }
let(:target) { 'master' }
subject(:add_tag) do
client.add_tag(tag_name, user, target, tag_message)
end
context 'without tag message' do
let(:tag_name) { 'lightweight-tag' }
it 'creates a lightweight tag' do
tag = add_tag
expect(tag.name).to eq(tag_name)
expect(tag.message).to eq('')
end
end
context 'with tag message' do
let(:tag_name) { 'annotated-tag' }
let(:tag_message) { "tag message" }
it 'creates an annotated tag' do
tag = add_tag
expect(tag.name).to eq(tag_name)
expect(tag.message).to eq(tag_message)
end
end
context 'with preexisting tag' do
let(:tag_name) { 'v1.0.0' }
it 'raises a TagExistsError' do
expect { add_tag }.to raise_error(Gitlab::Git::Repository::TagExistsError)
end
end
context 'with invalid target' do
let(:target) { 'refs/heads/does-not-exist' }
it 'raises an InvalidRef error' do
expect { add_tag }.to raise_error(Gitlab::Git::Repository::InvalidRef)
end
end
context 'with pre-receive error' do
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_create_tag)
.and_return(Gitaly::UserCreateTagResponse.new(pre_receive_error: "GitLab: something failed"))
end
it 'raises a PreReceiveError' do
expect { add_tag }.to raise_error(Gitlab::Git::PreReceiveError, "something failed")
end
end
context 'with internal error' do
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_create_tag)
.and_raise(GRPC::Internal.new('undetailed internal error'))
end
it 'raises an Internal error' do
expect { add_tag }.to raise_error do |error|
expect(error).to be_a(GRPC::Internal)
expect(error.details).to eq('undetailed internal error')
end
end
end
context 'with structured errors' do
before do
expect_any_instance_of(Gitaly::OperationService::Stub)
.to receive(:user_create_tag)
.and_raise(structured_error)
end
context 'with ReferenceExistsError' do
let(:structured_error) do
new_detailed_error(
GRPC::Core::StatusCodes::ALREADY_EXISTS,
'tag exists already',
Gitaly::UserCreateTagError.new(
reference_exists: Gitaly::ReferenceExistsError.new(
reference_name: tag_name,
oid: 'something'
)))
end
it 'raises a TagExistsError' do
expect { add_tag }.to raise_error(Gitlab::Git::Repository::TagExistsError)
end
end
context 'with AccessCheckError' do
let(:structured_error) do
new_detailed_error(
GRPC::Core::StatusCodes::PERMISSION_DENIED,
"error creating tag",
Gitaly::UserCreateTagError.new(
access_check: Gitaly::AccessCheckError.new(
error_message: "You are not allowed to create this tag.",
protocol: "web",
user_id: "user-15",
changes: "df15b32277d2c55c6c595845a87109b09c913c556 5d6e0f935ad9240655f64e883cd98fad6f9a17ee refs/tags/v1.0.0\n"
)))
end
it 'raises a PreReceiveError' do
expect { add_tag }.to raise_error do |error|
expect(error).to be_a(Gitlab::Git::PreReceiveError)
expect(error.message).to eq("You are not allowed to create this tag.")
end
end
end
context 'with CustomHookError' do
let(:structured_error) do
new_detailed_error(
GRPC::Core::StatusCodes::PERMISSION_DENIED,
"custom hook error",
Gitaly::UserCreateTagError.new(
custom_hook: Gitaly::CustomHookError.new(
stdout: "some stdout",
stderr: "GitLab: some custom hook error message",
hook_type: Gitaly::CustomHookError::HookType::HOOK_TYPE_PRERECEIVE
)))
end
it 'raises a PreReceiveError' do
expect { add_tag }.to raise_error do |error|
expect(error).to be_a(Gitlab::Git::PreReceiveError)
expect(error.message).to eq("some custom hook error message")
end
end
end
end
end
end
|