summaryrefslogtreecommitdiff
path: root/app/serializers/ci/trigger_entity.rb
blob: 005a9b752ed362d08f0ee0a56c98bcd5c8b732d4 (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
# frozen_string_literal: true

module Ci
  class TriggerEntity < Grape::Entity
    include Gitlab::Routing
    include Gitlab::Allowable

    expose :description
    expose :owner, using: UserEntity
    expose :last_used

    expose :token do |trigger|
      can_admin_trigger?(trigger) ? trigger.token : trigger.short_token
    end

    expose :has_token_exposed do |trigger|
      can_admin_trigger?(trigger)
    end

    expose :can_access_project do |trigger|
      trigger.can_access_project?
    end

    expose :project_trigger_path, if: -> (trigger) { can_manage_trigger?(trigger) } do |trigger|
      project_trigger_path(options[:project], trigger)
    end

    expose :edit_project_trigger_path, if: -> (trigger) { can_admin_trigger?(trigger) } do |trigger|
      edit_project_trigger_path(options[:project], trigger)
    end

    private

    def can_manage_trigger?(trigger)
      can?(options[:current_user], :manage_trigger, trigger)
    end

    def can_admin_trigger?(trigger)
      can?(options[:current_user], :admin_trigger, trigger)
    end
  end
end