summaryrefslogtreecommitdiff
path: root/app/services/merge_requests/build_service.rb
blob: 22b9b91a957b7198aa3342e117fee7e928140e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module MergeRequests
  class BuildService < MergeRequests::BaseService
    def execute
      @issue_iid = params.delete(:issue_iid)

      self.merge_request = MergeRequest.new(params)
      merge_request.compare_commits = []
      merge_request.source_project  = find_source_project
      merge_request.target_project  = find_target_project
      merge_request.target_branch   = find_target_branch
      merge_request.can_be_created  = branches_valid?

      # compare branches only if branches are valid, otherwise
      # compare_branches may raise an error
      if merge_request.can_be_created
        compare_branches
        assign_title_and_description
      end

      merge_request
    end

    private

    attr_accessor :merge_request

    delegate :target_branch,
             :target_branch_ref,
             :target_project,
             :source_branch,
             :source_branch_ref,
             :source_project,
             :compare_commits,
             :wip_title,
             :description,
             :errors,
             to: :merge_request

    def find_source_project
      return source_project if source_project.present? && can?(current_user, :read_project, source_project)

      project
    end

    def find_target_project
      return target_project if target_project.present? && can?(current_user, :read_project, target_project)

      project.default_merge_request_target
    end

    def find_target_branch
      target_branch || target_project.default_branch
    end

    def source_branch_specified?
      params[:source_branch].present?
    end

    def target_branch_specified?
      params[:target_branch].present?
    end

    def branches_valid?
      return false unless source_branch_specified? || target_branch_specified?

      validate_branches
      errors.blank?
    end

    def compare_branches
      compare = CompareService.new(
        source_project,
        source_branch_ref
      ).execute(
        target_project,
        target_branch_ref
      )

      if compare
        merge_request.compare_commits = compare.commits
        merge_request.compare = compare
      end
    end

    def validate_branches
      add_error('You must select source and target branch') unless branches_present?
      add_error('You must select different branches') if same_source_and_target?
      add_error("Source branch \"#{source_branch}\" does not exist") unless source_branch_exists?
      add_error("Target branch \"#{target_branch}\" does not exist") unless target_branch_exists?
    end

    def add_error(message)
      errors.add(:base, message)
    end

    def branches_present?
      target_branch.present? && source_branch.present?
    end

    def same_source_and_target?
      source_project == target_project && target_branch == source_branch
    end

    def source_branch_exists?
      source_branch.blank? || source_project.commit(source_branch)
    end

    def target_branch_exists?
      target_branch.blank? || target_project.commit(target_branch)
    end

    # When your branch name starts with an iid followed by a dash this pattern will be
    # interpreted as the user wants to close that issue on this project.
    #
    # For example:
    # - Issue 112 exists, title: Emoji don't show up in commit title
    # - Source branch is: 112-fix-mep-mep
    #
    # Will lead to:
    # - Appending `Closes #112` to the description
    # - Setting the title as 'Resolves "Emoji don't show up in commit title"' if there is
    #   more than one commit in the MR
    #
    def assign_title_and_description
      assign_title_and_description_from_single_commit
      assign_title_from_issue

      merge_request.title ||= source_branch.titleize.humanize
      merge_request.title = wip_title if compare_commits.empty?

      append_closes_description
    end

    def append_closes_description
      return unless issue_iid

      closes_issue = "Closes ##{issue_iid}"

      if description.present?
        merge_request.description += closes_issue.prepend("\n\n")
      else
        merge_request.description = closes_issue
      end
    end

    def assign_title_and_description_from_single_commit
      commits = compare_commits

      return unless commits&.count == 1

      commit = commits.first
      merge_request.title ||= commit.title
      merge_request.description ||= commit.description.try(:strip)
    end

    def assign_title_from_issue
      return unless issue && issue.is_a?(Issue)

      merge_request.title = "Resolve \"#{issue.title}\""
    end

    def issue_iid
      @issue_iid ||= source_branch.match(/\A(\d+)-/).try(:[], 1)
    end

    def issue
      @issue ||= target_project.get_issue(issue_iid, current_user)
    end
  end
end