BigW Consortium Gitlab

events_helper.rb 5.04 KB
Newer Older
1 2
module EventsHelper
  def link_to_author(event)
3
    author = event.author
4

5
    if author
6
      link_to author.name, user_path(author.username), title: author.name
7 8 9 10 11 12
    else
      event.author_name
    end
  end

  def event_action_name(event)
13 14 15 16 17 18 19 20 21
    target =  if event.target_type
                if event.note?
                  event.note_target_type
                else
                  event.target_type.titleize.downcase
                end
              else
                'project'
              end
22 23 24

    [event.action_name, target].join(" ")
  end
25

26
  def event_filter_link(key, tooltip)
27
    key = key.to_s
28 29
    active = 'active' if @event_filter.active?(key)
    link_opts = {
30
      class: "event-filter-link",
31 32 33
      id:    "#{key}_event_filter",
      title: "Filter by #{tooltip.downcase}",
    }
34

35 36 37 38
    content_tag :li, class: active do
      link_to request.path, link_opts do
        content_tag(:span, ' ' + tooltip)
      end
39 40
    end
  end
41

42
  def event_filter_visible(feature_key)
43 44
    return true unless @project

45
    @project.feature_available?(feature_key, current_user)
46 47
  end

48 49 50 51 52 53
  def comments_visible?
    event_filter_visible(:repository) ||
      event_filter_visible(:merge_requests) ||
      event_filter_visible(:issues)
  end

54 55 56 57 58 59 60 61
  def event_preposition(event)
    if event.push? || event.commented? || event.target
      "at"
    elsif event.milestone?
      "in"
    end
  end

62
  def event_feed_title(event)
63 64 65 66 67 68 69 70 71
    words = []
    words << event.author_name
    words << event_action_name(event)

    if event.push?
      words << event.ref_type
      words << event.ref_name
      words << "at"
    elsif event.commented?
72
      words << event.note_target_reference
73
      words << "at"
74 75 76
    elsif event.milestone?
      words << "##{event.target_iid}" if event.target_iid
      words << "in"
77
    elsif event.target
78
      words << "##{event.target_iid}:"
79 80
      words << event.target.title if event.target.respond_to?(:title)
      words << "at"
81
    end
82 83 84 85

    words << event.project_name

    words.join(" ")
86 87 88 89
  end

  def event_feed_url(event)
    if event.issue?
Vinnie Okada committed
90 91
      namespace_project_issue_url(event.project.namespace, event.project,
                                  event.issue)
92
    elsif event.merge_request?
Vinnie Okada committed
93 94
      namespace_project_merge_request_url(event.project.namespace,
                                          event.project, event.merge_request)
95
    elsif event.commit_note?
Vinnie Okada committed
96 97
      namespace_project_commit_url(event.project.namespace, event.project,
                                   event.note_target)
98 99
    elsif event.note?
      if event.note_target
100
        event_note_target_path(event)
101
      end
102
    elsif event.push?
103 104 105 106 107 108 109 110 111 112
      push_event_feed_url(event)
    end
  end

  def push_event_feed_url(event)
    if event.push_with_commits? && event.md_ref?
      if event.commits_count > 1
        namespace_project_compare_url(event.project.namespace, event.project,
                                      from: event.commit_from, to:
                                      event.commit_to)
113
      else
114 115
        namespace_project_commit_url(event.project.namespace, event.project,
                                     id: event.commit_to)
116
      end
117 118 119
    else
      namespace_project_commits_url(event.project.namespace, event.project,
                                    event.ref_name)
120 121 122 123 124 125 126 127
    end
  end

  def event_feed_summary(event)
    if event.issue?
      render "events/event_issue", issue: event.issue
    elsif event.push?
      render "events/event_push", event: event
128 129 130 131
    elsif event.merge_request?
      render "events/event_merge_request", merge_request: event.merge_request
    elsif event.note?
      render "events/event_note", note: event.note
132 133
    end
  end
134 135

  def event_note_target_path(event)
136
    if event.commit_note?
137 138 139 140 141 142 143 144 145
      namespace_project_commit_path(event.project.namespace,
                                    event.project,
                                    event.note_target,
                                    anchor: dom_id(event.target))
    elsif event.project_snippet_note?
      namespace_project_snippet_path(event.project.namespace,
                                     event.project,
                                     event.note_target,
                                     anchor: dom_id(event.target))
146
    else
Vinnie Okada committed
147 148
      polymorphic_path([event.project.namespace.becomes(Namespace),
                        event.project, event.note_target],
149
                        anchor: dom_id(event.target))
150 151
    end
  end
152 153 154

  def event_note_title_html(event)
    if event.note_target
155 156
      link_to(event_note_target_path(event), title: event.target_title, class: 'has-tooltip') do
        "#{event.note_target_type} #{event.note_target_reference}"
157 158
      end
    else
159
      content_tag(:strong, '(deleted)')
160 161
    end
  end
162

163 164
  def event_note(text, options = {})
    text = first_line_in_markdown(text, 150, options)
165
    sanitize(text, tags: %w(a img b pre code p span))
166
  end
167 168

  def event_commit_title(message)
169
    (message.split("\n").first || "").truncate(70)
170 171 172
  rescue
    "--broken encoding"
  end
173

174
  def event_row_class(event)
175
    if event.body?
176 177 178 179 180
      "event-block"
    else
      "event-inline"
    end
  end
181
end