issues_helper.rb 3.5 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
module IssuesHelper
2
  def issue_css_classes(issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3
    classes = "issue"
Andrew8xx8's avatar
Andrew8xx8 committed
4
    classes << " closed" if issue.closed?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6 7
    classes << " today" if issue.today?
    classes
  end
8

9 10 11 12
  # Returns an OpenStruct object suitable for use by <tt>options_from_collection_for_select</tt>
  # to allow filtering issues by an unassigned User or Milestone
  def unassigned_filter
    # Milestone uses :title, Issue uses :name
13
    OpenStruct.new(id: 0, title: 'None (backlog)', name: 'Unassigned')
14
  end
15

16
  def url_for_issue(issue_iid, project = @project, options = {})
skv's avatar
skv committed
17
    return '' if project.nil?
Andrew8xx8's avatar
Andrew8xx8 committed
18

19
    url =
20 21
      if options[:internal]
        url_for_internal_issue(issue_iid, project, options)
22
      else
23
        url_for_tracker_issue(issue_iid, project, options)
24 25 26 27 28 29
      end

    # Ensure we return a valid URL to prevent possible XSS.
    URI.parse(url).to_s
  rescue URI::InvalidURIError
    ''
30 31
  end

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  def url_for_tracker_issue(issue_iid, project, options)
    if options[:only_path]
      project.issues_tracker.issue_path(issue_iid)
    else
      project.issues_tracker.issue_url(issue_iid)
    end
  end

  def url_for_internal_issue(issue_iid, project = @project, options = {})
    helpers = Gitlab::Routing.url_helpers

    if options[:only_path]
      helpers.namespace_project_issue_path(namespace_id: project.namespace, project_id: project, id: issue_iid)
    else
      helpers.namespace_project_issue_url(namespace_id: project.namespace, project_id: project, id: issue_iid)
    end
  end

50
  def status_box_class(item)
51
    if item.try(:expired?)
52
      'status-box-expired'
53
    elsif item.try(:merged?)
54
      'status-box-mr-merged'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
55
    elsif item.closed?
56
      'status-box-mr-closed'
57
    elsif item.try(:upcoming?)
58
      'status-box-upcoming'
59
    else
60
      'status-box-open'
61 62
    end
  end
63

64
  def issue_button_visibility(issue, closed)
65 66 67
    return 'hidden' if issue.closed? == closed
  end

68 69 70 71
  def confidential_icon(issue)
    icon('eye-slash') if issue.confidential?
  end

72
  def award_user_list(awards, current_user, limit: 10)
73
    names = awards.map do |award|
74
      award.user == current_user ? 'You' : award.user.name
75 76
    end

77
    current_user_name = names.delete('You')
78
    names = names.insert(0, current_user_name).compact.first(limit)
79

80
    names << "#{awards.size - names.size} more." if awards.size > names.size
81

82
    names.to_sentence
Valery Sizov's avatar
Valery Sizov committed
83 84
  end

85 86 87 88
  def award_state_class(awards, current_user)
    if !current_user
      "disabled"
    elsif current_user && awards.find { |a| a.user_id == current_user.id }
Valery Sizov's avatar
Valery Sizov committed
89 90 91 92
      "active"
    else
      ""
    end
Valery Sizov's avatar
Valery Sizov committed
93 94
  end

barthc's avatar
barthc committed
95 96 97 98 99 100 101 102
  def award_user_authored_class(award)
    if award == 'thumbsdown' || award == 'thumbsup'
      'user-authored js-user-authored'
    else
      ''
    end
  end

Valery Sizov's avatar
Valery Sizov committed
103
  def awards_sort(awards)
104
    awards.sort_by do |award, award_emojis|
Valery Sizov's avatar
Valery Sizov committed
105 106 107 108 109 110 111 112 113 114
      if award == "thumbsup"
        0
      elsif award == "thumbsdown"
        1
      else
        2
      end
    end.to_h
  end

115
  def link_to_discussions_to_resolve(merge_request, single_discussion = nil)
116 117 118 119 120 121 122
    link_text = merge_request.to_reference
    link_text += " (discussion #{single_discussion.first_note.id})" if single_discussion

    path = if single_discussion
             Gitlab::UrlBuilder.build(single_discussion.first_note)
           else
             project = merge_request.project
123
             project_merge_request_path(project, merge_request)
124 125 126 127 128
           end

    link_to link_text, path
  end

129
  # Required for Banzai::Filter::IssueReferenceFilter
130
  module_function :url_for_issue
131 132
  module_function :url_for_internal_issue
  module_function :url_for_tracker_issue
gitlabhq's avatar
gitlabhq committed
133
end