forked from gitlabhq/gitlabhq
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpret_service_spec.rb
4181 lines (3186 loc) · 140 KB
/
interpret_service_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe QuickActions::InterpretService, feature_category: :text_editors do
include AfterNextHelpers
let_it_be(:support_bot) { Users::Internal.support_bot }
let_it_be(:group) { create(:group) }
let_it_be(:public_project) { create(:project, :public, group: group) }
let_it_be(:repository_project) { create(:project, :repository) }
let_it_be(:project) { public_project }
let_it_be(:developer) { create(:user, developer_of: [public_project, repository_project]) }
let_it_be(:developer2) { create(:user) }
let_it_be(:developer3) { create(:user) }
let_it_be_with_reload(:issue) { create(:issue, project: project) }
let_it_be(:inprogress) { create(:label, project: project, title: 'In Progress') }
let_it_be(:helmchart) { create(:label, project: project, title: 'Helm Chart Registry') }
let_it_be(:bug) { create(:label, project: project, title: 'Bug') }
let(:milestone) { create(:milestone, project: project, title: '9.10') }
let(:commit) { create(:commit, project: project) }
let(:current_user) { developer }
let(:container) { project }
subject(:service) { described_class.new(container: container, current_user: current_user) }
before do
stub_licensed_features(
multiple_issue_assignees: false,
multiple_merge_request_reviewers: false,
multiple_merge_request_assignees: false
)
project.add_developer(current_user)
end
before_all { Users::Internal.support_bot_id }
describe '#execute' do
let_it_be(:work_item) { create(:work_item, :task, project: project) }
let(:merge_request) { create(:merge_request, source_project: project) }
shared_examples 'reopen command' do
it 'returns state_event: "reopen" if content contains /reopen' do
issuable.close!
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(state_event: 'reopen')
end
it 'returns the reopen message' do
issuable.close!
_, _, message = service.execute(content, issuable)
translated_string = _("Reopened this %{issuable_to_ability_name_humanize}.")
formatted_message = format(translated_string, issuable_to_ability_name_humanize: issuable.to_ability_name.humanize(capitalize: false).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'close command' do
it 'returns state_event: "close" if content contains /close' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(state_event: 'close')
end
it 'returns the close message' do
_, _, message = service.execute(content, issuable)
translated_string = _("Closed this %{issuable_to_ability_name_humanize}.")
formatted_message = format(translated_string, issuable_to_ability_name_humanize: issuable.to_ability_name.humanize(capitalize: false).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'title command' do
it 'populates title: "A brand new title" if content contains /title A brand new title' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(title: 'A brand new title')
end
it 'returns the title message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_(%(Changed the title to "A brand new title".)))
end
end
shared_examples 'milestone command' do
it 'fetches milestone and populates milestone_id if content contains /milestone' do
milestone # populate the milestone
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(milestone_id: milestone.id)
end
it 'returns the milestone message' do
milestone # populate the milestone
_, _, message = service.execute(content, issuable)
translated_string = _("Set the milestone to %{milestone_to_reference}.")
formatted_message = format(translated_string, milestone_to_reference: milestone.to_reference.to_s)
expect(message).to eq(formatted_message)
end
it 'returns empty milestone message when milestone is wrong' do
_, _, message = service.execute('/milestone %wrong-milestone', issuable)
expect(message).to be_empty
end
end
shared_examples 'remove_milestone command' do
it 'populates milestone_id: nil if content contains /remove_milestone' do
issuable.update!(milestone_id: milestone.id)
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(milestone_id: nil)
end
it 'returns removed milestone message' do
issuable.update!(milestone_id: milestone.id)
_, _, message = service.execute(content, issuable)
translated_string = _("Removed %{milestone_to_reference} milestone.")
formatted_message = format(translated_string, milestone_to_reference: milestone.to_reference.to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'label command' do
it 'fetches label ids and populates add_label_ids if content contains /label' do
bug # populate the label
inprogress # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to match(add_label_ids: contain_exactly(bug.id, inprogress.id))
end
it 'returns the label message' do
bug # populate the label
inprogress # populate the label
_, _, message = service.execute(content, issuable)
# Compare message without making assumptions about ordering.
expect(message).to match %r{Added ~".*" ~".*" labels.}
expect(message).to include(bug.to_reference(format: :name))
expect(message).to include(inprogress.to_reference(format: :name))
end
end
shared_examples 'multiple label command' do
it 'fetches label ids and populates add_label_ids if content contains multiple /label' do
bug # populate the label
inprogress # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(add_label_ids: [inprogress.id, bug.id])
end
end
shared_examples 'multiple label with same argument' do
it 'prevents duplicate label ids and populates add_label_ids if content contains multiple /label' do
inprogress # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(add_label_ids: [inprogress.id])
end
end
shared_examples 'multiword label name starting without ~' do
it 'fetches label ids and populates add_label_ids if content contains /label' do
_, updates = service.execute(content, issuable)
expect(updates).to eq(add_label_ids: [helmchart.id])
end
end
shared_examples 'label name is included in the middle of another label name' do
it 'ignores the sublabel when the content contains the includer label name' do
create(:label, project: project, title: 'Chart')
_, updates = service.execute(content, issuable)
expect(updates).to eq(add_label_ids: [helmchart.id])
end
end
shared_examples 'unlabel command' do
it 'fetches label ids and populates remove_label_ids if content contains /unlabel' do
issuable.update!(label_ids: [inprogress.id]) # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(remove_label_ids: [inprogress.id])
end
it 'returns the unlabel message' do
issuable.update!(label_ids: [inprogress.id]) # populate the label
_, _, message = service.execute(content, issuable)
translated_string = _("Removed %{inprogress_to_reference} label.")
formatted_message = format(translated_string, inprogress_to_reference: inprogress.to_reference(format: :name).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'multiple unlabel command' do
it 'fetches label ids and populates remove_label_ids if content contains mutiple /unlabel' do
issuable.update!(label_ids: [inprogress.id, bug.id]) # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(remove_label_ids: [inprogress.id, bug.id])
end
end
shared_examples 'unlabel command with no argument' do
it 'populates label_ids: [] if content contains /unlabel with no arguments' do
issuable.update!(label_ids: [inprogress.id]) # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(label_ids: [])
end
end
shared_examples 'relabel command' do
it 'populates label_ids: [] if content contains /relabel' do
issuable.update!(label_ids: [bug.id]) # populate the label
inprogress # populate the label
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(label_ids: [inprogress.id])
end
it 'returns the relabel message' do
issuable.update!(label_ids: [bug.id]) # populate the label
inprogress # populate the label
_, _, message = service.execute(content, issuable)
translated_string = _("Replaced all labels with %{inprogress_to_reference} label.")
formatted_message = format(translated_string, inprogress_to_reference: inprogress.to_reference(format: :name).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'todo command' do
it 'populates todo_event: "add" if content contains /todo' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(todo_event: 'add')
end
it 'returns the todo message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Added a to-do item.'))
end
end
shared_examples 'done command' do
it 'populates todo_event: "done" if content contains /done' do
TodoService.new.mark_todo(issuable, developer)
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(todo_event: 'done')
end
it 'returns the done message' do
TodoService.new.mark_todo(issuable, developer)
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Marked to-do item as done.'))
end
end
shared_examples 'subscribe command' do
it 'populates subscription_event: "subscribe" if content contains /subscribe' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(subscription_event: 'subscribe')
end
it 'returns the subscribe message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_("Subscribed to notifications."))
end
end
shared_examples 'unsubscribe command' do
it 'populates subscription_event: "unsubscribe" if content contains /unsubscribe' do
issuable.subscribe(developer, project)
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(subscription_event: 'unsubscribe')
end
it 'returns the unsubscribe message' do
issuable.subscribe(developer, project)
_, _, message = service.execute(content, issuable)
expect(message).to eq(_("Unsubscribed from notifications."))
end
end
shared_examples 'due command' do
let(:expected_date) { Date.new(2016, 8, 28) }
it 'populates due_date: Date.new(2016, 8, 28) if content contains /due 2016-08-28' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(due_date: expected_date)
end
it 'returns due_date message: Date.new(2016, 8, 28) if content contains /due 2016-08-28' do
_, _, message = service.execute(content, issuable)
translated_string = _("Set the due date to %{expected_date_to_fs}.")
formatted_message = format(translated_string, expected_date_to_fs: expected_date.to_fs(:medium).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'remove_due_date command' do
before do
issuable.update!(due_date: Date.today)
end
it 'populates due_date: nil if content contains /remove_due_date' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(due_date: nil)
end
it 'returns Removed the due date' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Removed the due date.'))
end
end
shared_examples 'draft command' do
it 'returns wip_event: "draft"' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(wip_event: 'draft')
end
it 'returns the draft message' do
_, _, message = service.execute(content, issuable)
translated_string = _("Marked this %{issuable_to_ability_name_humanize} as a draft.")
formatted_message = format(translated_string, issuable_to_ability_name_humanize: issuable.to_ability_name.humanize(capitalize: false).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'draft/ready command no action' do
it 'returns the no action message if there is no change to the status' do
_, _, message = service.execute(content, issuable)
translated_string = _("No change to this %{issuable_to_ability_name_humanize}'s draft status.")
formatted_message = format(translated_string, issuable_to_ability_name_humanize: issuable.to_ability_name.humanize(capitalize: false).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'ready command' do
it 'returns wip_event: "ready"' do
issuable.update!(title: issuable.draft_title)
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(wip_event: 'ready')
end
it 'returns the ready message' do
issuable.update!(title: issuable.draft_title)
_, _, message = service.execute(content, issuable)
translated_string = _("Marked this %{issuable_to_ability_name_humanize} as ready.")
formatted_message = format(translated_string, issuable_to_ability_name_humanize: issuable.to_ability_name.humanize(capitalize: false).to_s)
expect(message).to eq(formatted_message)
end
end
shared_examples 'estimate command' do
it 'populates time_estimate: 3600 if content contains /estimate 1h' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(time_estimate: 3600)
end
it 'returns the time_estimate formatted message' do
_, _, message = service.execute('/estimate 79d', issuable)
expect(message).to eq(_('Set time estimate to 3mo 3w 4d.'))
end
end
shared_examples 'spend command' do
it 'populates spend_time: 3600 if content contains /spend 1h' do
freeze_time do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(spend_time: {
category: nil,
duration: 3600,
user_id: developer.id,
spent_at: DateTime.current
})
end
end
end
shared_examples 'spend command with negative time' do
it 'populates spend_time: -7200 if content contains -120m' do
freeze_time do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(spend_time: {
category: nil,
duration: -7200,
user_id: developer.id,
spent_at: DateTime.current
})
end
end
it 'returns the spend_time message including the formatted duration and verb' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Subtracted 2h spent time.'))
end
end
shared_examples 'spend command with valid date' do
it 'populates spend time: 1800 with date in date type format' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(spend_time: {
category: nil,
duration: 1800,
user_id: developer.id,
spent_at: Date.parse(date)
})
end
end
shared_examples 'spend command with invalid date' do
it 'will not create any note and timelog' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq({})
end
end
shared_examples 'spend command with future date' do
it 'will not create any note and timelog' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq({})
end
end
shared_examples 'spend command with category' do
it 'populates spend_time with expected attributes' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to match(spend_time: a_hash_including(category: 'pm'))
end
end
shared_examples 'remove_estimate command' do
it 'populates time_estimate: 0 if content contains /remove_estimate' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(time_estimate: 0)
end
it 'returns the remove_estimate message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Removed time estimate.'))
end
end
shared_examples 'remove_time_spent command' do
it 'populates spend_time: :reset if content contains /remove_time_spent' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(spend_time: { duration: :reset, user_id: developer.id })
end
it 'returns the remove_time_spent message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Removed spent time.'))
end
end
shared_examples 'lock command' do
let(:issue) { create(:issue, project: project, discussion_locked: false) }
let(:merge_request) { create(:merge_request, source_project: project, discussion_locked: false) }
it 'returns discussion_locked: true if content contains /lock' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(discussion_locked: true)
end
it 'returns the lock discussion message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Locked the discussion.'))
end
end
shared_examples 'unlock command' do
let(:issue) { create(:issue, project: project, discussion_locked: true) }
let(:merge_request) { create(:merge_request, source_project: project, discussion_locked: true) }
it 'returns discussion_locked: true if content contains /unlock' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(discussion_locked: false)
end
it 'returns the unlock discussion message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Unlocked the discussion.'))
end
end
shared_examples 'failed command' do |error_msg|
let(:msg) { error_msg || try(:output_msg) }
let(:match_msg) { msg ? eq(msg) : be_empty }
it 'populates {} if content contains an unsupported command' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to be_empty
end
it "returns #{error_msg || 'an empty'} message" do
_, _, message = service.execute(content, issuable)
expect(message).to match_msg
end
end
shared_examples 'explain message' do |error_msg|
let(:msg) { error_msg || try(:output_msg) }
let(:match_msg) { msg ? include(msg) : be_empty }
it "returns #{error_msg || 'an empty'} message" do
_, message = service.explain(content, issuable)
expect(message).to match_msg
end
end
shared_examples 'merge immediately command' do
let(:project) { repository_project }
it 'runs merge command if content contains /merge' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(merge: merge_request.diff_head_sha)
end
it 'returns them merge message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Merged this merge request.'))
end
end
shared_examples 'merge automatically command' do
let(:project) { repository_project }
before do
stub_licensed_features(merge_request_approvers: true) if Gitlab.ee?
end
it 'runs merge command if content contains /merge and returns merge message' do
_, updates, message = service.execute(content, issuable)
expect(updates).to eq(merge: merge_request.diff_head_sha)
expect(message).to eq(_('Scheduled to merge this merge request (Merge when checks pass).'))
end
end
shared_examples 'react command' do |command|
it "toggle award 100 emoji if content contains #{command} :100:" do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(emoji_award: "100")
end
it 'returns the reaction message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Toggled :100: emoji reaction.'))
end
end
shared_examples 'copy_metadata command' do
it 'fetches issue or merge request and copies labels and milestone if content contains /copy_metadata reference' do
source_issuable # populate the issue
todo_label # populate this label
inreview_label # populate this label
_, updates, _ = service.execute(content, issuable)
expect(updates[:add_label_ids]).to match_array([inreview_label.id, todo_label.id])
if source_issuable.milestone
expect(updates[:milestone_id]).to eq(source_issuable.milestone.id)
else
expect(updates).not_to have_key(:milestone_id)
end
end
it 'returns the copy metadata message' do
_, _, message = service.execute("/copy_metadata #{source_issuable.to_reference}", issuable)
translated_string = _("Copied labels and milestone from %{source_issuable_to_reference}.")
formatted_message = format(translated_string, source_issuable_to_reference: source_issuable.to_reference.to_s)
expect(message).to eq(formatted_message)
end
end
describe 'move issue command' do
it 'returns the move issue message' do
_, _, message = service.execute("/move #{project.full_path}", issue)
translated_string = _("Moved this issue to %{project_full_path}.")
formatted_message = format(translated_string, project_full_path: project.full_path.to_s)
expect(message).to eq(formatted_message)
end
it 'returns move issue failure message when the referenced issue is not found' do
_, _, message = service.execute('/move invalid', issue)
expect(message).to eq(_("Failed to move this issue because target project doesn't exist."))
end
context "when we pass a work_item" do
let(:work_item) { create(:work_item, :issue) }
let(:move_command) { "/move #{project.full_path}" }
it '/move execution method message' do
_, _, message = service.execute(move_command, work_item)
expect(message).to eq("Moved this issue to #{project.full_path}.")
end
end
end
describe 'clone issue command' do
it 'returns the clone issue message' do
_, _, message = service.execute("/clone #{project.full_path}", issue)
translated_string = _("Cloned this issue to %{project_full_path}.")
formatted_message = format(translated_string, project_full_path: project.full_path.to_s)
expect(message).to eq(formatted_message)
end
it 'returns clone issue failure message when the referenced issue is not found' do
_, _, message = service.execute('/clone invalid', issue)
expect(message).to eq(_("Failed to clone this issue because target project doesn't exist."))
end
context "when we pass a work_item" do
let(:work_item) { create(:work_item, :issue, project: project) }
it '/clone execution method message' do
_, _, message = service.execute("/clone #{project.full_path}", work_item)
expect(message).to eq("Cloned this issue to #{project.full_path}.")
end
end
end
shared_examples 'confidential command' do
it 'marks issue as confidential if content contains /confidential' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(confidential: true)
end
it 'returns the confidential message' do
_, _, message = service.execute(content, issuable)
translated_string = _("Made this %{issuable_type} confidential.")
issuable_type = if issuable.to_ability_name == "work_item"
'item'
else
issuable.to_ability_name.humanize(capitalize: false)
end
formatted_message = format(translated_string, issuable_type: issuable_type.to_s)
expect(message).to eq(formatted_message)
end
context 'when issuable is already confidential' do
before do
issuable.update!(confidential: true)
end
it 'returns an error message' do
_, _, message = service.execute(content, issuable)
expect(message).to eq(_('Could not apply confidential command.'))
end
it 'is not part of the available commands' do
expect(service.available_commands(issuable)).not_to include(a_hash_including(name: :confidential))
end
end
end
shared_examples 'approve command unavailable' do
it 'is not part of the available commands' do
expect(service.available_commands(issuable)).not_to include(a_hash_including(name: :approve))
end
end
shared_examples 'unapprove command unavailable' do
it 'is not part of the available commands' do
expect(service.available_commands(issuable)).not_to include(a_hash_including(name: :unapprove))
end
end
shared_examples 'shrug command' do
it 'adds ¯\_(ツ)_/¯' do
new_content, _, _ = service.execute(content, issuable)
expect(new_content).to eq(described_class::SHRUG)
end
end
shared_examples 'tableflip command' do
it 'adds (╯°□°)╯︵ ┻━┻' do
new_content, _, _ = service.execute(content, issuable)
expect(new_content).to eq(described_class::TABLEFLIP)
end
end
shared_examples 'tag command' do
it 'tags a commit' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(tag_name: tag_name, tag_message: tag_message)
end
it 'returns the tag message' do
_, _, message = service.execute(content, issuable)
if tag_message.present?
translated_string = _(%(Tagged this commit to %{tag_name} with "%{tag_message}".))
formatted_message = format(translated_string, tag_name: tag_name.to_s, tag_message: tag_message)
else
translated_string = _("Tagged this commit to %{tag_name}.")
formatted_message = format(translated_string, tag_name: tag_name.to_s)
end
expect(message).to eq(formatted_message)
end
end
shared_examples 'assign command' do
it 'assigns to me' do
cmd = '/assign me'
_, updates, _ = service.execute(cmd, issuable)
expect(updates).to eq(assignee_ids: [current_user.id])
end
it 'does not assign to group members' do
grp = create(:group)
grp.add_developer(developer)
grp.add_developer(developer2)
cmd = "/assign #{grp.to_reference}"
_, updates, message = service.execute(cmd, issuable)
expect(updates).to be_blank
expect(message).to include(_('Failed to find users'))
end
context 'when there are too many references' do
before do
stub_const('Gitlab::QuickActions::UsersExtractor::MAX_QUICK_ACTION_USERS', 2)
end
it 'says what went wrong' do
cmd = '/assign her and you, me and them'
_, updates, message = service.execute(cmd, issuable)
expect(updates).to be_blank
expect(message).to include(_('Too many references. Quick actions are limited to at most 2 user references'))
end
end
context 'when the user extractor raises an uninticipated error' do
before do
allow_next(Gitlab::QuickActions::UsersExtractor)
.to receive(:execute).and_raise(Gitlab::QuickActions::UsersExtractor::Error)
end
it 'tracks the exception in dev, and reports a generic message in production' do
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).twice
_, updates, message = service.execute('/assign some text', issuable)
expect(updates).to be_blank
expect(message).to include(_('Something went wrong'))
end
end
it 'assigns to users with escaped underscores' do
user = create(:user)
base = user.username
user.update!(username: "#{base}_new")
issuable.project.add_developer(user)
cmd = "/assign @#{base}\\_new"
_, updates, _ = service.execute(cmd, issuable)
expect(updates).to eq(assignee_ids: [user.id])
end
it 'assigns to a single user' do
_, updates, _ = service.execute(content, issuable)
expect(updates).to eq(assignee_ids: [developer.id])
end
it 'returns the assign message' do
_, _, message = service.execute(content, issuable)
translated_string = _("Assigned %{developer_to_reference}.")
formatted_message = format(translated_string, developer_to_reference: developer.to_reference.to_s)
expect(message).to eq(formatted_message)
end
context 'when the reference does not match the exact case' do
let(:user) { create(:user) }
let(:content) { "/assign #{user.to_reference.upcase}" }
it 'assigns to the user' do
issuable.project.add_developer(user)
_, updates, message = service.execute(content, issuable)
translated_string = _("Assigned %{user_to_reference}.")
formatted_message = format(translated_string, user_to_reference: user.to_reference.to_s)
expect(content).not_to include(user.to_reference)
expect(updates).to eq(assignee_ids: [user.id])
expect(message).to eq(formatted_message)
end
end
context 'when the user has a private profile' do
let(:user) { create(:user, :private_profile) }
let(:content) { "/assign #{user.to_reference}" }
it 'assigns to the user' do
issuable.project.add_developer(user)
_, updates, message = service.execute(content, issuable)
translated_string = _("Assigned %{user_to_reference}.")
formatted_message = format(translated_string, user_to_reference: user.to_reference.to_s)
expect(updates).to eq(assignee_ids: [user.id])
expect(message).to eq(formatted_message)
end
end
end
shared_examples 'assign_reviewer command' do
it 'assigns a reviewer to a single user' do
_, updates, message = service.execute(content, issuable)
translated_string = _("Assigned %{developer_to_reference} as reviewer.")
formatted_message = format(translated_string, developer_to_reference: developer.to_reference.to_s)
expect(updates).to eq(reviewer_ids: [developer.id])
expect(message).to eq(formatted_message)
end
end
shared_examples 'unassign_reviewer command' do
it 'removes a single reviewer' do
_, updates, message = service.execute(content, issuable)
translated_string = _("Removed reviewer %{developer_to_reference}.")
formatted_message = format(translated_string, developer_to_reference: developer.to_reference.to_s)
expect(updates).to eq(reviewer_ids: [])
expect(message).to eq(formatted_message)
end
end
it_behaves_like 'reopen command' do
let(:content) { '/reopen' }
let(:issuable) { issue }
end
it_behaves_like 'reopen command' do
let(:content) { '/reopen' }
let(:issuable) { merge_request }
end
it_behaves_like 'close command' do
let(:content) { '/close' }
let(:issuable) { issue }
end
it_behaves_like 'close command' do
let(:content) { '/close' }
let(:issuable) { merge_request }
end
context 'merge command' do
let(:merge_request) { create(:merge_request, source_project: repository_project) }
let(:service) do
described_class.new(
container: project,
current_user: developer,
params: { merge_request_diff_head_sha: merge_request.diff_head_sha }
)
end
it_behaves_like 'merge immediately command' do
let(:content) { '/merge' }
let(:issuable) { merge_request }
end
context 'when the head pipeline of merge request is running' do
before do
create(:ci_pipeline, :detached_merge_request_pipeline, merge_request: merge_request)
merge_request.update_head_pipeline
end
it_behaves_like 'merge automatically command' do
let(:content) { '/merge' }
let(:issuable) { merge_request }
end
end
context 'can not be merged when logged user does not have permissions' do
let(:service) { described_class.new(container: project, current_user: create(:user)) }
it_behaves_like 'failed command', 'Could not apply merge command.' do
let(:content) { "/merge" }
let(:issuable) { merge_request }
end
end
context 'can not be merged when sha does not match' do
let(:service) do
described_class.new(
container: project,
current_user: developer,
params: { merge_request_diff_head_sha: 'othersha' }
)
end
it_behaves_like 'failed command', 'Branch has been updated since the merge was requested.' do
let(:content) { "/merge" }
let(:issuable) { merge_request }
end
end
context 'when sha is missing' do
let(:project) { repository_project }
let(:service) { described_class.new(container: project, current_user: developer) }
it_behaves_like 'failed command', 'The `/merge` quick action requires the SHA of the head of the branch.' do
let(:content) { "/merge" }
let(:issuable) { merge_request }
end
end
context 'issue can not be merged' do
it_behaves_like 'failed command', 'Could not apply merge command.' do
let(:content) { "/merge" }
let(:issuable) { issue }
end
end
context 'non persisted merge request cant be merged' do
it_behaves_like 'failed command', 'Could not apply merge command.' do
let(:content) { "/merge" }