Welcome to mirror list, hosted at ThFree Co, Russian Federation.

event_entity.rb « profile « serializers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f3c1a927084b04acbbf150edce9b961c0575171d (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
# frozen_string_literal: true

module Profile
  class EventEntity < Grape::Entity
    include ActionView::Helpers::SanitizeHelper
    include RequestAwareEntity
    include MarkupHelper
    include MergeRequestsHelper
    include EventsHelper

    expose :created_at, if: ->(event) { include_private_event?(event) }
    expose(:action, if: ->(event) { include_private_event?(event) }) { |event| event_action(event) }

    expose :ref, if: ->(event) { event.visible_to_user?(current_user) && event.push_action? } do
      expose(:ref_type, as: :type)
      expose(:ref_count, as: :count)
      expose(:ref_name, as: :name)
      expose(:path) { |event| ref_path(event) }
      expose(:new_ref?, as: :is_new)
      expose(:rm_ref?, as: :is_removed)
    end

    expose :commit, if: ->(event) { event.visible_to_user?(current_user) && event.push_action? } do
      expose(:truncated_sha) { |event| Commit.truncate_sha(event.commit_id) }
      expose(:path) { |event| project_commit_path(event.project, event.commit_id) }
      expose(:title) { |event| event_commit_title(event.commit_title) }
      expose(:count) { |event| event.commits_count } # rubocop:disable Style/SymbolProc
      expose(:create_mr_path) { |event| commit_create_mr_path(event) }
      expose(:from_truncated_sha) { |event| commit_from(event) if event.commit_from }
      expose(:to_truncated_sha) { |event| Commit.truncate_sha(event.commit_to) if event.commit_to }

      expose :compare_path, if: ->(event) { event.push_with_commits? && event.commits_count > 1 } do |event|
        project = event.project
        from = event.md_ref? ? event.commit_from : project.default_branch
        project_compare_path(project, from: from, to: event.commit_to)
      end
    end

    expose :author, if: ->(event) { include_private_event?(event) }, using: ::API::Entities::UserBasic

    expose :noteable, if: ->(event) { event.visible_to_user?(current_user) && event.note? } do
      expose(:type) { |event| event.target.noteable_type }
      expose(:reference_link_text) { |event| event.target.noteable.reference_link_text }
      expose(:web_url) { |event| Gitlab::UrlBuilder.build(event.target.noteable) }
      expose(:first_line_in_markdown) do |event|
        first_line_in_markdown(event.target, :note, 150, project: event.project)
      end
    end

    expose :target, if: ->(event) { event.target && event.visible_to_user?(current_user) } do
      expose(:id) { |event| event.target.id }
      expose(:target_type, as: :type)
      expose(:target_title, as: :title)
      expose(:issue_type, if: ->(event) { event.work_item? || event.issue? }) do |event|
        event.target.issue_type
      end

      expose :reference_link_text, if: ->(event) { event.target.respond_to?(:reference_link_text) } do |event|
        event.target.reference_link_text
      end

      expose :web_url do |event|
        if event.wiki_page?
          event_wiki_page_target_url(event)
        else
          Gitlab::UrlBuilder.build(event.target)
        end
      end
    end

    expose :resource_parent, if: ->(event) { event.visible_to_user?(current_user) } do
      expose(:type) { |event| resource_parent_type(event) }
      expose(:full_name) { |event| event.resource_parent&.full_name }
      expose(:full_path) { |event| event.resource_parent&.full_path }
      expose(:web_url) { |event| event.resource_parent&.web_url }
      expose(:avatar_url) { |event| event.resource_parent&.avatar_url }
    end

    private

    def current_user
      request.current_user
    end

    def target_user
      request.target_user
    end

    def include_private_event?(event)
      event.visible_to_user?(current_user) || target_user.include_private_contributions?
    end

    def commit_from(event)
      if event.md_ref?
        Commit.truncate_sha(event.commit_from)
      else
        event.project.default_branch
      end
    end

    def event_action(event)
      if event.visible_to_user?(current_user)
        event.action
      elsif target_user.include_private_contributions?
        'private'
      end
    end

    def ref_path(event)
      project = event.project
      commits_link = project_commits_path(project, event.ref_name)
      should_link = if event.tag?
                      project.repository.tag_exists?(event.ref_name)
                    else
                      project.repository.branch_exists?(event.ref_name)
                    end

      should_link ? commits_link : nil
    end

    def commit_create_mr_path(event)
      if event.new_ref? &&
          create_mr_button_from_event?(event) &&
          event.authored_by?(current_user)
        create_mr_path_from_push_event(event)
      end
    end

    def resource_parent_type(event)
      if event.project
        "project"
      elsif event.group
        "group"
      end
    end
  end
end