rosa-build/app/helpers/pull_request_helper.rb

62 lines
2.5 KiB
Ruby
Raw Normal View History

2012-06-25 17:04:43 +01:00
module PullRequestHelper
2012-09-27 17:32:28 +01:00
def merge_activity comments, commits
common_comments, pull_comments = comments.partition {|c| c.automatic || c.data.blank?}
common_comments = common_comments.map{ |c| [c.created_at, c] }
pull_comments = pull_comments.group_by(&:data).map{|data, c| [c.first.created_at, [data || {}, [c].flatten]]}
commits = commits.map{ |c| [(c.committed_date || c.authored_date), c] }
(common_comments + pull_comments + commits).sort_by{ |c| c[0] }.map{ |c| c[1] }
2012-09-27 17:32:28 +01:00
end
2013-07-12 15:21:46 +01:00
def pull_status_label pull_status, options = {}
2015-03-23 09:58:39 +00:00
statuses = {'ready' => 'success', 'closed' => 'default', 'merged' => 'info', 'blocked' => 'warning'}
options[:class] = "#{options[:class]} label label-#{statuses[pull_status]}"
content_tag :span, t("projects.pull_requests.statuses.#{pull_status}"), options
2012-07-16 17:27:41 +01:00
end
2012-06-25 17:04:43 +01:00
def pull_status pull
2012-07-18 19:02:27 +01:00
if %w(blocked merged closed ready open).include? pull.status
2014-01-21 04:51:49 +00:00
t("projects.pull_requests.#{pull.status}", user: pull.issue.closer.try(:uname), to_ref: show_ref(pull, 'to'),
from_ref: show_ref(pull, 'from'), time: pull.issue.closed_at).html_safe
2012-06-25 17:04:43 +01:00
else
raise "pull id (#{pull.id}) wrong status #{pull.status} "
end
end
2012-07-16 17:27:41 +01:00
def pull_header pull
str = "#{t '.header'} #{t 'from'} \
#{show_ref pull, 'from'} \
#{t 'into'} \
#{show_ref pull, 'to'}"
2012-10-27 19:04:28 +01:00
str << " #{t 'by'} #{link_to pull.user.uname, user_path(pull.user)}" if pull.user# pull.persisted?
2012-07-16 17:27:41 +01:00
str.html_safe
end
#helper for helpers
def show_ref pull, which, limit = 30
2012-07-17 17:40:48 +01:00
project, ref = pull.send("#{which}_project"), pull.send("#{which}_ref")
2012-10-27 19:45:24 +01:00
fullname = if which == 'to'
2012-10-27 19:04:28 +01:00
"#{project.owner.uname.truncate limit}/#{project.name.truncate limit}"
elsif which == 'from'
"#{pull.from_project_owner_uname.truncate limit}/#{pull.from_project_name.truncate limit}"
end
link_to "#{fullname}: #{ref.truncate limit}", ref_path(project, ref), class: 'btn btn-primary'
2012-07-17 17:40:48 +01:00
end
def ref_path project, ref
2012-10-27 19:04:28 +01:00
if project && project.repo.branches_and_tags.map(&:name).include?(ref)
tree_path(project, ref)
else
'#'
end
end
2012-09-28 18:46:02 +01:00
def ref_selector_options(project, current)
res = []
value = Proc.new {|t| [t.name.truncate(50), t.name]}
res << [I18n.t('layout.git.repositories.branches'), project.repo.branches.map(&value).sort]
res << [I18n.t('layout.git.repositories.tags'), project.repo.tags.map(&value).sort]
2012-09-28 18:46:02 +01:00
grouped_options_for_select(res, current)
end
2012-07-17 17:40:48 +01:00
end