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
|
# frozen_string_literal: true
RSpec.shared_examples 'issuable quick actions' do
before do
stub_const('QuickAction', Struct.new(:action_text, :expectation, :before_action, keyword_init: true) do
# Pass a block as :before_action if
# issuable state needs to be changed before
# the quick action is executed.
def call_before_action
before_action.call if before_action
end
def skip_access_check
action_text["/todo"] ||
action_text["/done"] ||
action_text["/subscribe"] ||
action_text["/shrug"] ||
action_text["/tableflip"]
end
end)
end
let(:unlabel_expectation) do
->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.labels).to be_empty
else
expect(noteable.labels).not_to be_empty
end
}
end
# Quick actions shared by issues and merge requests
let(:issuable_quick_actions) do
[
QuickAction.new(
action_text: "/subscribe",
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable).to be_subscribed(note_author, issuable.project)
else
expect(noteable).not_to be_subscribed(note_author, issuable.project)
end
}
),
QuickAction.new(
action_text: "/unsubscribe",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable).not_to be_subscribed(note_author, issuable.project)
}
),
QuickAction.new(
action_text: "/todo",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.todos.count == 1).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/done",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.todos.last.done?).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/close",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.closed?).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/reopen",
before_action: -> {
issuable.close
},
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.open?).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/assign @#{user.username}",
expectation: ->(noteable, can_use_quick_action) {
if noteable.allows_multiple_assignees?
expect(noteable.assignees == [old_assignee, user]).to eq(can_use_quick_action)
else
expect(noteable.assignees == [user]).to eq(can_use_quick_action)
end
}
),
QuickAction.new(
action_text: "/unassign",
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.assignees).to be_empty
else
expect(noteable.assignees).not_to be_empty
end
}
),
QuickAction.new(
action_text: "/title new title",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.title == "new title").to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/lock",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.discussion_locked?).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/unlock",
before_action: -> {
issuable.update!(discussion_locked: true)
},
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable).not_to be_discussion_locked
else
expect(noteable).to be_discussion_locked
end
}
),
QuickAction.new(
action_text: "/milestone %\"sprint\"",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.milestone == milestone).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/remove_milestone",
before_action: -> {
issuable.update!(milestone_id: milestone.id)
},
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.milestone_id).to be_nil
else
expect(noteable.milestone_id).to eq(milestone.id)
end
}
),
QuickAction.new(
action_text: "/label ~feature",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.labels&.last&.id == feature_label.id).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/labels ~feature",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.labels&.last&.id == feature_label.id).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/unlabel",
expectation: unlabel_expectation
),
QuickAction.new(
action_text: "/remove_label",
expectation: unlabel_expectation
),
QuickAction.new(
action_text: "/award :100:",
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.award_emoji.last.name).to eq("100")
else
expect(noteable.award_emoji).to be_empty
end
}
),
QuickAction.new(
action_text: "/estimate 1d 2h 3m",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.time_estimate == 36180).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/remove_estimate",
before_action: -> {
issuable.update!(time_estimate: 30000)
},
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.time_estimate).to be_zero
else
expect(noteable.time_estimate).to eq(30000)
end
}
),
QuickAction.new(
action_text: "/spend 1d 2h 3m",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.total_time_spent == 36180).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/remove_time_spent",
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.total_time_spent == 0)
else
expect(noteable.timelogs).to be_empty
end
}
),
QuickAction.new(
action_text: "/shrug oops",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.notes&.last&.note == "HELLO\noops ¯\\_(ツ)_/¯\nWORLD").to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/tableflip oops",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.notes&.last&.note == "HELLO\noops (╯°□°)╯︵ ┻━┻\nWORLD").to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: "/copy_metadata #{issue_2.to_reference}",
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.labels).to eq(issue_2.labels)
expect(noteable.milestone).to eq(issue_2.milestone)
else
expect(noteable.labels).not_to eq(issue_2.labels)
expect(noteable.milestone).not_to eq(issue_2.milestone)
end
}
)
]
end
let(:old_assignee) { create(:user) }
before do
project.add_developer(old_assignee)
issuable.update!(assignees: [old_assignee])
end
context 'when user can update issuable' do
let_it_be(:developer) { create(:user) }
let(:note_author) { developer }
before do
project.add_developer(developer)
end
it 'saves the note and updates the issue' do
quick_actions.each do |quick_action|
note_text = %(HELLO\n#{quick_action.action_text}\nWORLD)
quick_action.call_before_action
note = described_class.new(project, developer, note_params.merge(note: note_text)).execute
noteable = note.noteable
# shrug and tablefip quick actions modifies the note text
# on these cases we need to skip this assertion
if !quick_action.action_text["shrug"] && !quick_action.action_text["tableflip"]
expect(note.note).to eq "HELLO\nWORLD"
end
quick_action.expectation.call(noteable, true)
end
end
end
context 'when user cannot update issuable' do
let_it_be(:non_member) { create(:user) }
let(:note_author) { non_member }
it 'applies commands that user can execute' do
quick_actions.each do |quick_action|
note_text = %(HELLO\n#{quick_action.action_text}\nWORLD)
quick_action.call_before_action
note = described_class.new(project, non_member, note_params.merge(note: note_text)).execute
noteable = note.noteable
if quick_action.skip_access_check
quick_action.expectation.call(noteable, true)
else
quick_action.expectation.call(noteable, false)
end
end
end
end
end
|