merge_requests_helper.rb 2.67 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
module MergeRequestsHelper
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2
  def new_mr_path_from_push_event(event)
3
    target_project = event.project.forked_from_project || event.project
Vinnie Okada's avatar
Vinnie Okada committed
4 5
    new_namespace_project_merge_request_path(
      event.project.namespace,
6
      event.project,
7
      new_mr_from_push_event(event, target_project)
8 9 10 11
    )
  end

  def new_mr_path_for_fork_from_push_event(event)
Vinnie Okada's avatar
Vinnie Okada committed
12 13
    new_namespace_project_merge_request_path(
      event.project.namespace,
14 15
      event.project,
      new_mr_from_push_event(event, event.project.forked_from_project)
16 17 18 19
    )
  end

  def new_mr_from_push_event(event, target_project)
20
    {
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21 22 23 24 25 26
      merge_request: {
        source_project_id: event.project.id,
        target_project_id: target_project.id,
        source_branch: event.branch_name,
        target_branch: target_project.repository.root_ref
      }
27
    }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
29

30
  def mr_css_classes(mr)
31
    classes = "merge-request"
32
    classes << " closed" if mr.closed?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33 34 35
    classes << " merged" if mr.merged?
    classes
  end
36

37
  def ci_build_details_path(merge_request)
Valery Sizov's avatar
Valery Sizov committed
38
    merge_request.source_project.ci_service.build_page(merge_request.last_commit.sha, merge_request.source_branch)
39
  end
40 41 42

  def merge_path_description(merge_request, separator)
    if merge_request.for_fork?
43
      "Project:Branches: #{@merge_request.source_project_path}:#{@merge_request.source_branch} #{separator} #{@merge_request.target_project.path_with_namespace}:#{@merge_request.target_branch}"
44 45 46 47
    else
      "Branches: #{@merge_request.source_branch} #{separator} #{@merge_request.target_branch}"
    end
  end
48 49 50 51

  def issues_sentence(issues)
    issues.map { |i| "##{i.iid}" }.to_sentence
  end
52 53 54 55 56 57 58 59 60 61 62 63

  def mr_change_branches_path(merge_request)
    new_namespace_project_merge_request_path(
      @project.namespace, @project,
      merge_request: {
        source_project_id: @merge_request.source_project_id,
        target_project_id: @merge_request.target_project_id,
        source_branch: @merge_request.source_branch,
        target_branch: nil
      }
    )
  end
64 65 66 67 68 69 70 71 72 73

  def source_branch_with_namespace(merge_request)
    if merge_request.for_fork?
      namespace = link_to(merge_request.source_project_namespace,
        project_path(merge_request.source_project))
      namespace + ":#{merge_request.source_branch}"
    else
      merge_request.source_branch
    end
  end
74 75 76 77 78 79 80 81 82 83 84 85 86

  def format_mr_branch_names(merge_request)
    source_path = merge_request.source_project_path
    target_path = merge_request.target_project_path
    source_branch = merge_request.source_branch
    target_branch = merge_request.target_branch

    if source_path == target_path
      [source_branch, target_branch]
    else
      ["#{source_path}:#{source_branch}", "#{target_path}:#{target_branch}"]
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
87
end