summaryrefslogtreecommitdiff
path: root/app/services/slash_commands/interpret_service.rb
blob: f7b9547bd2b74cdd0c4e28cd70bbdab30ac69536 (plain)
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
module SlashCommands
  class InterpretService < BaseService
    include Gitlab::SlashCommands::Dsl

    attr_reader :noteable

    # Takes a text and interpret the commands that are extracted from it.
    # Returns a hash of changes to be applied to a record.
    def execute(content, noteable)
      @noteable = noteable
      @updates = {}

      opts = {
        noteable:     noteable,
        current_user: current_user,
        project:      project
      }

      content, commands = extractor.extract_commands(content, opts)

      commands.each do |name, *args|
        definition = self.class.command_definitions_by_name[name.to_sym]
        next unless definition

        definition.execute(self, opts, *args)
      end

      [content, @updates]
    end

    private

    def extractor
      Gitlab::SlashCommands::Extractor.new(self.class.command_definitions)
    end

    desc do
      "Close this #{noteable.to_ability_name.humanize(capitalize: false)}"
    end
    condition do
      noteable.persisted? &&
        noteable.open? &&
        current_user.can?(:"update_#{noteable.to_ability_name}", noteable)
    end
    command :close do
      @updates[:state_event] = 'close'
    end

    desc do
      "Reopen this #{noteable.to_ability_name.humanize(capitalize: false)}"
    end
    condition do
      noteable.closed? &&
        current_user.can?(:"update_#{noteable.to_ability_name}", noteable)
    end
    command :reopen, :open do
      @updates[:state_event] = 'reopen'
    end

    desc 'Change title'
    params '<New title>'
    condition do
      noteable.persisted? &&
        current_user.can?(:"update_#{noteable.to_ability_name}", noteable)
    end
    command :title do |title_param|
      @updates[:title] = title_param
    end

    desc 'Assign'
    params '@user'
    condition do
      current_user.can?(:"admin_#{noteable.to_ability_name}", project)
    end
    command :assign do |assignee_param|
      user = extract_references(assignee_param, :user).first
      user ||= User.find_by(username: assignee_param)
      user ||= User.find_by(name: assignee_param)

      @updates[:assignee_id] = user.id if user
    end

    desc 'Remove assignee'
    condition do
      noteable.assignee_id? &&
        current_user.can?(:"admin_#{noteable.to_ability_name}", project)
    end
    command :unassign, :remove_assignee do
      @updates[:assignee_id] = nil
    end

    desc 'Set milestone'
    params '%"milestone"'
    condition do
      current_user.can?(:"admin_#{noteable.to_ability_name}", project) &&
        project.milestones.active.any?
    end
    command :milestone do |milestone_param|
      milestone = extract_references(milestone_param, :milestone).first
      milestone ||= project.milestones.find_by(title: milestone_param.strip)

      @updates[:milestone_id] = milestone.id if milestone
    end

    desc 'Remove milestone'
    condition do
      noteable.milestone_id? &&
        current_user.can?(:"admin_#{noteable.to_ability_name}", project)
    end
    command :clear_milestone, :remove_milestone do
      @updates[:milestone_id] = nil
    end

    desc 'Add label(s)'
    params '~label1 ~"label 2"'
    condition do
      current_user.can?(:"admin_#{noteable.to_ability_name}", project) &&
        project.labels.any?
    end
    command :label, :labels do |labels_param|
      label_ids = find_label_ids(labels_param)

      @updates[:add_label_ids] = label_ids unless label_ids.empty?
    end

    desc 'Remove label(s)'
    params '~label1 ~"label 2"'
    condition do
      noteable.labels.any? &&
        current_user.can?(:"admin_#{noteable.to_ability_name}", project)
    end
    command :unlabel, :remove_label, :remove_labels do |labels_param|
      label_ids = find_label_ids(labels_param)

      @updates[:remove_label_ids] = label_ids unless label_ids.empty?
    end

    desc 'Remove all labels'
    condition do
      noteable.labels.any? &&
        current_user.can?(:"admin_#{noteable.to_ability_name}", project)
    end
    command :clear_labels, :clear_label do
      @updates[:label_ids] = []
    end

    desc 'Add a todo'
    condition do
      noteable.persisted? &&
        !TodoService.new.todo_exist?(noteable, current_user)
    end
    command :todo do
      @updates[:todo_event] = 'add'
    end

    desc 'Mark todo as done'
    condition do
        TodoService.new.todo_exist?(noteable, current_user)
    end
    command :done do
      @updates[:todo_event] = 'done'
    end

    desc 'Subscribe'
    condition do
      noteable.persisted? &&
        !noteable.subscribed?(current_user)
    end
    command :subscribe do
      @updates[:subscription_event] = 'subscribe'
    end

    desc 'Unsubscribe'
    condition do
      noteable.persisted? &&
        noteable.subscribed?(current_user)
    end
    command :unsubscribe do
      @updates[:subscription_event] = 'unsubscribe'
    end

    desc 'Set due date'
    params '<in 2 days; this Friday; December 31st>'
    condition do
      noteable.respond_to?(:due_date) &&
        current_user.can?(:"update_#{noteable.to_ability_name}", noteable)
    end
    command :due, :due_date do |due_date_param|
      due_date = Chronic.parse(due_date_param).try(:to_date)

      @updates[:due_date] = due_date if due_date
    end

    desc 'Remove due date'
    condition do
      noteable.respond_to?(:due_date) &&
        noteable.due_date? &&
        current_user.can?(:"update_#{noteable.to_ability_name}", noteable)
    end
    command :clear_due_date do
      @updates[:due_date] = nil
    end

    # This is a dummy command, so that it appears in the autocomplete commands
    desc 'CC'
    params '@user'
    command :cc

    def find_label_ids(labels_param)
      label_ids_by_reference = extract_references(labels_param, :label).map(&:id)
      labels_ids_by_name = @project.labels.where(name: labels_param.split).select(:id)

      label_ids_by_reference | labels_ids_by_name
    end

    def extract_references(arg, type)
      ext = Gitlab::ReferenceExtractor.new(project, current_user)
      ext.analyze(arg, author: current_user)

      ext.references(type)
    end
  end
end