BigW Consortium Gitlab

issues_helper.rb 4.42 KB
Newer Older
gitlabhq committed
1
module IssuesHelper
2
  def issue_css_classes(issue)
Dmitriy Zaporozhets committed
3
    classes = "issue"
Andrew8xx8 committed
4
    classes << " closed" if issue.closed?
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 committed
17
    return '' if project.nil?
Andrew8xx8 committed
18

19 20 21 22 23 24 25 26 27 28 29
    url =
      if options[:only_path]
        project.issues_tracker.issue_path(issue_iid)
      else
        project.issues_tracker.issue_url(issue_iid)
      end

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

32
  def bulk_update_milestone_options
33
    milestones = @project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
34 35 36
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', params[:milestone_id])
37 38
  end

39
  def milestone_options(object)
40
    milestones = object.project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
41
    milestones.unshift(object.milestone) if object.milestone.present? && object.milestone.closed?
42 43 44
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', object.milestone_id)
45
  end
46

47 48 49
  def project_options(issuable, current_user, ability: :read_project)
    projects = current_user.authorized_projects
    projects = projects.select do |project|
50
      current_user.can?(ability, project)
51 52
    end

53 54 55
    no_project = OpenStruct.new(id: 0, name_with_namespace: 'No project')
    projects.unshift(no_project)
    projects.delete(issuable.project)
56

57
    options_from_collection_for_select(projects, :id, :name_with_namespace)
58 59
  end

60
  def status_box_class(item)
61
    if item.try(:expired?)
62
      'status-box-expired'
63
    elsif item.try(:merged?)
64
      'status-box-merged'
Dmitriy Zaporozhets committed
65
    elsif item.closed?
66
      'status-box-closed'
67
    elsif item.try(:upcoming?)
68
      'status-box-upcoming'
69
    else
70
      'status-box-open'
71 72
    end
  end
73

74
  def issue_button_visibility(issue, closed)
75 76 77
    return 'hidden' if issue.closed? == closed
  end

78
  def merge_requests_sentence(merge_requests)
79 80 81 82 83
    # Sorting based on the `!123` or `group/project!123` reference will sort
    # local merge requests first.
    merge_requests.map do |merge_request|
      merge_request.to_reference(@project)
    end.sort.to_sentence(last_word_connector: ', or ')
84 85
  end

86 87 88 89
  def confidential_icon(issue)
    icon('eye-slash') if issue.confidential?
  end

90
  def award_user_list(awards, current_user, limit: 10)
91
    names = awards.map do |award|
92
      award.user == current_user ? 'You' : award.user.name
93 94
    end

95
    current_user_name = names.delete('You')
96
    names = names.insert(0, current_user_name).compact.first(limit)
97

98
    names << "#{awards.size - names.size} more." if awards.size > names.size
99

100
    names.to_sentence
Valery Sizov committed
101 102
  end

103 104 105 106
  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 committed
107 108 109 110
      "active"
    else
      ""
    end
Valery Sizov committed
111 112
  end

113 114 115 116 117 118 119 120
  def award_user_authored_class(award)
    if award == 'thumbsdown' || award == 'thumbsup'
      'user-authored js-user-authored'
    else
      ''
    end
  end

Valery Sizov committed
121 122 123 124 125 126 127 128 129 130 131 132
  def awards_sort(awards)
    awards.sort_by do |award, notes|
      if award == "thumbsup"
        0
      elsif award == "thumbsdown"
        1
      else
        2
      end
    end.to_h
  end

133 134
  def due_date_options
    options = [
Rémy Coutable committed
135 136 137 138 139
      Issue::AnyDueDate,
      Issue::NoDueDate,
      Issue::DueThisWeek,
      Issue::DueThisMonth,
      Issue::Overdue
140
    ]
141 142

    options_from_collection_for_select(options, 'name', 'title', params[:due_date])
143 144
  end

145
  def link_to_discussions_to_resolve(merge_request, single_discussion = nil)
146 147 148 149 150 151 152 153 154 155 156 157 158
    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
             namespace_project_merge_request_path(project.namespace, project, merge_request)
           end

    link_to link_text, path
  end

159
  # Required for Banzai::Filter::IssueReferenceFilter
160
  module_function :url_for_issue
gitlabhq committed
161
end