summaryrefslogtreecommitdiff
path: root/app/services/projects/autocomplete_service.rb
blob: 223461e88b647a11c05f2fdabecee29ea2a09078 (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
module Projects
  class AutocompleteService < BaseService
    def issues
      IssuesFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title])
    end

    def milestones
      @project.milestones.active.reorder(due_date: :asc, title: :asc).select([:iid, :title])
    end

    def merge_requests
      MergeRequestsFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title])
    end

    def labels
      LabelsFinder.new(current_user, project_id: project.id).execute.
        pluck(:title, :color).map { |l| { title: l.first, color: l.second } }
    end

    def unlabels(noteable)
      return [] unless noteable && noteable.respond_to?(:labels)

      noteable.labels.pluck(:title, :color).map { |l| { title: l.first, color: l.second } }
    end

    def commands(noteable, type)
      noteable ||=
        case type
        when 'Issue'
          @project.issues.build
        when 'MergeRequest'
          @project.merge_requests.build
        end

      return [] unless noteable && noteable.is_a?(Issuable)

      opts = {
        project: project,
        issuable: noteable,
        current_user: current_user
      }
      SlashCommands::InterpretService.command_definitions.map do |definition|
        next unless definition.available?(opts)

        definition.to_h(opts)
      end.compact
    end
  end
end