refresh_service.rb 7.47 KB
Newer Older
1
2
3
module MergeRequests
  class RefreshService < MergeRequests::BaseService
    def execute(oldrev, newrev, ref)
4
      return true unless Gitlab::Git.branch_ref?(ref)
5

6
      @oldrev, @newrev = oldrev, newrev
7
      @branch_name = Gitlab::Git.ref_name(ref)
8

9
      Gitlab::GitalyClient.allow_n_plus_1_calls(&method(:find_new_commits))
10
11
      # Be sure to close outstanding MRs before reloading them to avoid generating an
      # empty diff during a manual merge
12
13
      close_upon_missing_source_branch_ref
      post_merge_manually_merged
14
      reload_merge_requests
15
      reset_merge_when_pipeline_succeeds
16
      mark_pending_todos_done
17
      cache_merge_requests_closing_issues
18

19
20
      # Leave a system note if a branch was deleted/added
      if branch_added? || branch_removed?
21
        comment_mr_branch_presence_changed
22
      end
23

24
      comment_mr_with_commits
25
      mark_mr_as_wip_from_commits
26
      execute_mr_web_hooks
27
28
29
30
31
32

      true
    end

    private

33
34
35
36
37
38
39
40
41
42
43
    def close_upon_missing_source_branch_ref
      # MergeRequest#reload_diff ignores not opened MRs. This means it won't
      # create an `empty` diff for `closed` MRs without a source branch, keeping
      # the latest diff state as the last _valid_ one.
      merge_requests_for_source_branch.reject(&:source_branch_exists?).each do |mr|
        MergeRequests::CloseService
          .new(mr.target_project, @current_user)
          .execute(mr)
      end
    end

44
45
46
47
    # Collect open merge requests that target same branch we push into
    # and close if push to master include last commit from merge request
    # We need this to close(as merged) merge requests that were merged into
    # target branch manually
48
    def post_merge_manually_merged
49
      commit_ids = @commits.map(&:id)
50
      merge_requests = @project.merge_requests.preload(:latest_merge_request_diff).opened.where(target_branch: @branch_name).to_a
51
      merge_requests = merge_requests.select(&:diff_head_commit)
52
53

      merge_requests = merge_requests.select do |merge_request|
54
55
        commit_ids.include?(merge_request.diff_head_sha) &&
          merge_request.merge_request_diff.state != 'empty'
56
57
      end

58
      filter_merge_requests(merge_requests).each do |merge_request|
59
60
61
        MergeRequests::PostMergeService
          .new(merge_request.target_project, @current_user)
          .execute(merge_request)
62
63
64
      end
    end

65
    def force_push?
66
      Gitlab::Checks::ForcePush.force_push?(@project, @oldrev, @newrev)
67
68
    end

69
70
71
    # Refresh merge request diff if we push to source or target branch of merge request
    # Note: we should update merge requests from forks too
    def reload_merge_requests
72
73
      merge_requests = @project.merge_requests.opened
        .by_source_or_target_branch(@branch_name).to_a
Stan Hu's avatar
Stan Hu committed
74

75
76
77
78
79
80
      # Fork merge requests
      merge_requests += MergeRequest.opened
        .where(source_branch: @branch_name, source_project: @project)
        .where.not(target_project: @project).to_a

      filter_merge_requests(merge_requests).each do |merge_request|
81
        if merge_request.source_branch == @branch_name || force_push?
82
          merge_request.reload_diff(current_user)
83
        else
84
          mr_commit_ids = merge_request.commit_shas
85
86
          push_commit_ids = @commits.map(&:id)
          matches = mr_commit_ids & push_commit_ids
87
          merge_request.reload_diff(current_user) if matches.any?
88
89
        end

90
        merge_request.mark_as_unchecked
91
        UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id)
92
      end
93
94
95
96

      # Upcoming method calls need the refreshed version of
      # @source_merge_requests diffs (for MergeRequest#commit_shas for instance).
      merge_requests_for_source_branch(reload: true)
97
98
    end

99
100
    def reset_merge_when_pipeline_succeeds
      merge_requests_for_source_branch.each(&:reset_merge_when_pipeline_succeeds)
101
102
    end

103
104
105
106
107
108
    def mark_pending_todos_done
      merge_requests_for_source_branch.each do |merge_request|
        todo_service.merge_request_push(merge_request, @current_user)
      end
    end

109
110
111
    def find_new_commits
      if branch_added?
        @commits = []
112

113
114
        merge_request = merge_requests_for_source_branch.first
        return unless merge_request
115

116
117
118
        begin
          # Since any number of commits could have been made to the restored branch,
          # find the common root to see what has been added.
119
          common_ref = @project.repository.merge_base(merge_request.diff_head_sha, @newrev)
120
121
122
123
          # If the a commit no longer exists in this repo, gitlab_git throws
          # a Rugged::OdbError. This is fixed in https://gitlab.com/gitlab-org/gitlab_git/merge_requests/52
          @commits = @project.repository.commits_between(common_ref, @newrev) if common_ref
        rescue
124
        end
125
126
127
128
129
130
131
      elsif branch_removed?
        # No commits for a deleted branch.
        @commits = []
      else
        @commits = @project.repository.commits_between(@oldrev, @newrev)
      end
    end
132

133
134
135
136
137
    # Add comment about branches being deleted or added to merge requests
    def comment_mr_branch_presence_changed
      presence = branch_added? ? :add : :delete

      merge_requests_for_source_branch.each do |merge_request|
138
        SystemNoteService.change_branch_presence(
Gabriel Mazetto's avatar
Gabriel Mazetto committed
139
          merge_request, merge_request.project, @current_user,
140
            :source, @branch_name, presence)
141
142
143
144
145
      end
    end

    # Add comment about pushing new commits to merge requests
    def comment_mr_with_commits
146
147
      return unless @commits.present?

148
      merge_requests_for_source_branch.each do |merge_request|
149
        mr_commit_ids = Set.new(merge_request.commit_shas)
150
151
152
153
154

        new_commits, existing_commits = @commits.partition do |commit|
          mr_commit_ids.include?(commit.id)
        end

155
156
157
        SystemNoteService.add_commits(merge_request, merge_request.project,
                                      @current_user, new_commits,
                                      existing_commits, @oldrev)
158
159
160
      end
    end

161
162
163
164
    def mark_mr_as_wip_from_commits
      return unless @commits.present?

      merge_requests_for_source_branch.each do |merge_request|
165
        commit_shas = merge_request.commit_shas
166
167
168
169

        wip_commit = @commits.detect do |commit|
          commit.work_in_progress? && commit_shas.include?(commit.sha)
        end
170
171
172
173
174
175
176
177
178
179
180
181
182

        if wip_commit && !merge_request.work_in_progress?
          merge_request.update(title: merge_request.wip_title)
          SystemNoteService.add_merge_request_wip_from_commit(
            merge_request,
            merge_request.project,
            @current_user,
            wip_commit
          )
        end
      end
    end

183
184
    # Call merge request webhook with update branches
    def execute_mr_web_hooks
185
      merge_requests_for_source_branch.each do |merge_request|
186
        execute_hooks(merge_request, 'update', old_rev: @oldrev)
187
188
189
      end
    end

190
191
192
    # If the merge requests closes any issues, save this information in the
    # `MergeRequestsClosingIssues` model (as a performance optimization).
    def cache_merge_requests_closing_issues
193
      @project.merge_requests.where(source_branch: @branch_name).each do |merge_request|
194
195
196
197
        merge_request.cache_merge_request_closes_issues!(@current_user)
      end
    end

198
199
200
    def filter_merge_requests(merge_requests)
      merge_requests.uniq.select(&:source_project)
    end
201

202
203
    def merge_requests_for_source_branch(reload: false)
      @source_merge_requests = nil if reload
204
      @source_merge_requests ||= merge_requests_for(@branch_name)
205
206
207
208
209
210
211
212
213
    end

    def branch_added?
      Gitlab::Git.blank_ref?(@oldrev)
    end

    def branch_removed?
      Gitlab::Git.blank_ref?(@newrev)
    end
214
215
  end
end