BigW Consortium Gitlab

merge_requests_helper.rb 5.3 KB
Newer Older
1
module MergeRequestsHelper
2
  def new_mr_path_from_push_event(event)
3
    target_project = event.project.default_merge_request_target
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    new_namespace_project_merge_request_path(
      event.project.namespace,
      event.project,
      new_mr_from_push_event(event, target_project)
    )
  end

  def new_mr_from_push_event(event, target_project)
    {
      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
      }
    }
  end

Jarka Kadlecova committed
22
  def mr_widget_refresh_url(mr)
Jarka Kadlecova committed
23 24
    if mr && mr.target_project
      merge_widget_refresh_namespace_project_merge_request_url(mr.target_project.namespace, mr.target_project, mr)
Jarka Kadlecova committed
25 26 27 28 29
    else
      ''
    end
  end

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

37
  def ci_build_details_path(merge_request)
38
    build_url = merge_request.source_project.ci_service.build_page(merge_request.diff_head_sha, merge_request.source_branch)
39 40
    return nil unless build_url

41 42 43 44 45 46 47
    parsed_url = URI.parse(build_url)

    unless parsed_url.userinfo.blank?
      parsed_url.userinfo = ''
    end

    parsed_url.to_s
48
  end
49 50 51

  def merge_path_description(merge_request, separator)
    if merge_request.for_fork?
52
      "Project:Branches: #{@merge_request.source_project_path}:#{@merge_request.source_branch} #{separator} #{@merge_request.target_project.path_with_namespace}:#{@merge_request.target_branch}"
53 54 55 56
    else
      "Branches: #{@merge_request.source_branch} #{separator} #{@merge_request.target_branch}"
    end
  end
57 58

  def issues_sentence(issues)
59 60 61
    # Issuable sorter will sort local issues, then issues from the same
    # namespace, then all other issues.
    issues = Gitlab::IssuableSorter.sort(@project, issues).map do |issue|
62
      issue.to_reference(@project)
63 64
    end
    issues.to_sentence
65
  end
66

67
  def mr_closes_issues
68
    @mr_closes_issues ||= @merge_request.closes_issues(current_user)
69 70
  end

71
  def mr_issues_mentioned_but_not_closing
72
    @mr_issues_mentioned_but_not_closing ||= @merge_request.issues_mentioned_but_not_closing(current_user)
73 74
  end

75 76 77 78
  def mr_change_branches_path(merge_request)
    new_namespace_project_merge_request_path(
      @project.namespace, @project,
      merge_request: {
79 80 81 82
        source_project_id: merge_request.source_project_id,
        target_project_id: merge_request.target_project_id,
        source_branch: merge_request.source_branch,
        target_branch: merge_request.target_branch,
83 84
      },
      change_branches: true
85 86
    )
  end
87

88 89 90 91 92 93 94 95 96 97 98 99 100
  def mr_assign_issues_link
    issues = MergeRequests::AssignIssuesService.new(@project,
                                                    current_user,
                                                    merge_request: @merge_request,
                                                    closes_issues: mr_closes_issues
                                                   ).assignable_issues
    path = assign_related_issues_namespace_project_merge_request_path(@project.namespace, @project, @merge_request)
    if issues.present?
      pluralize_this_issue = issues.count > 1 ? "these issues" : "this issue"
      link_to "Assign yourself to #{pluralize_this_issue}", path, method: :post
    end
  end

101
  def source_branch_with_namespace(merge_request)
102 103 104 105 106 107 108
    namespace = merge_request.source_project_namespace
    branch = merge_request.source_branch

    if merge_request.source_branch_exists?
      namespace = link_to(namespace, project_path(merge_request.source_project))
      branch = link_to(branch, namespace_project_commits_path(merge_request.source_project.namespace, merge_request.source_project, merge_request.source_branch))
    end
Douwe Maan committed
109

110
    if merge_request.for_fork?
Douwe Maan committed
111
      namespace + ":" + branch
112
    else
Douwe Maan committed
113
      branch
114 115
    end
  end
116 117 118 119 120 121 122 123 124 125 126 127 128

  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
129

130 131 132 133
  def target_projects(project)
    [project, project.default_merge_request_target].uniq
  end

134
  def merge_request_button_visibility(merge_request, closed)
135
    return 'hidden' if merge_request.closed? == closed || (merge_request.merged? == closed && !merge_request.closed?) || merge_request.closed_without_fork?
136
  end
137

138
  def merge_request_version_path(project, merge_request, merge_request_diff, start_sha = nil)
139 140 141
    diffs_namespace_project_merge_request_path(
      project.namespace, project, merge_request,
      diff_id: merge_request_diff.id, start_sha: start_sha)
142
  end
143 144 145 146

  def version_index(merge_request_diff)
    @merge_request_diffs.size - @merge_request_diffs.index(merge_request_diff)
  end
147 148 149 150

  def different_base?(version1, version2)
    version1 && version2 && version1.base_commit_sha != version2.base_commit_sha
  end
151 152 153

  def merge_params(merge_request)
    {
154
      merge_when_pipeline_succeeds: true,
155 156 157 158 159 160 161 162
      should_remove_source_branch: true,
      sha: merge_request.diff_head_sha
    }.merge(merge_params_ee(merge_request))
  end

  def merge_params_ee(merge_request)
    {}
  end
163
end