GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

refresh_service.rb 5.57 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

      find_new_commits
10 11 12
      # Be sure to close outstanding MRs before reloading them to avoid generating an
      # empty diff during a manual merge
      close_merge_requests
13
      reload_merge_requests
14
      reset_merge_when_build_succeeds
15
      mark_pending_todos_done
16

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

22
      comment_mr_with_commits
23
      execute_mr_web_hooks
24 25 26 27 28 29 30 31 32 33 34 35 36

      true
    end

    private

    # 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
    def close_merge_requests
      commit_ids = @commits.map(&:id)
      merge_requests = @project.merge_requests.opened.where(target_branch: @branch_name).to_a
37
      merge_requests = merge_requests.select(&:diff_head_commit)
38 39

      merge_requests = merge_requests.select do |merge_request|
40
        commit_ids.include?(merge_request.diff_head_sha)
41 42 43
      end

      merge_requests.uniq.select(&:source_project).each do |merge_request|
44
        MergeRequests::PostMergeService.
45
          new(merge_request.target_project, @current_user).
46
          execute(merge_request)
47 48 49
      end
    end

50
    def force_push?
51
      Gitlab::ForcePushCheck.force_push?(@project, @oldrev, @newrev)
52 53
    end

54 55 56
    # 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
Stan Hu's avatar
Stan Hu committed
57
      merge_requests = @project.merge_requests.opened.by_branch(@branch_name).to_a
58
      merge_requests += fork_merge_requests.by_branch(@branch_name).to_a
Stan Hu's avatar
Stan Hu committed
59 60 61
      merge_requests = filter_merge_requests(merge_requests)

      merge_requests.each do |merge_request|
62
        if merge_request.source_branch == @branch_name || force_push?
63
          merge_request.reload_diff
64 65 66 67
        else
          mr_commit_ids = merge_request.commits.map(&:id)
          push_commit_ids = @commits.map(&:id)
          matches = mr_commit_ids & push_commit_ids
68
          merge_request.reload_diff if matches.any?
69
        end
70 71

        merge_request.mark_as_unchecked
72 73 74
      end
    end

75
    def reset_merge_when_build_succeeds
76
      merge_requests_for_source_branch.each(&:reset_merge_when_build_succeeds)
77 78
    end

79 80 81 82 83 84
    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

85 86 87
    def find_new_commits
      if branch_added?
        @commits = []
88

89 90
        merge_request = merge_requests_for_source_branch.first
        return unless merge_request
91

92 93 94
        begin
          # Since any number of commits could have been made to the restored branch,
          # find the common root to see what has been added.
95
          common_ref = @project.repository.merge_base(merge_request.diff_head_sha, @newrev)
96 97 98 99
          # 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
100
        end
101 102 103 104 105 106 107
      elsif branch_removed?
        # No commits for a deleted branch.
        @commits = []
      else
        @commits = @project.repository.commits_between(@oldrev, @newrev)
      end
    end
108

109 110 111 112 113
    # 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|
114
        SystemNoteService.change_branch_presence(
Gabriel Mazetto's avatar
Gabriel Mazetto committed
115
          merge_request, merge_request.project, @current_user,
116
            :source, @branch_name, presence)
117 118 119 120 121
      end
    end

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

124
      merge_requests_for_source_branch.each do |merge_request|
125 126 127 128 129 130
        mr_commit_ids = Set.new(merge_request.commits.map(&:id))

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

131 132 133
        SystemNoteService.add_commits(merge_request, merge_request.project,
                                      @current_user, new_commits,
                                      existing_commits, @oldrev)
134 135 136
      end
    end

137 138
    # Call merge request webhook with update branches
    def execute_mr_web_hooks
139
      merge_requests_for_source_branch.each do |merge_request|
140 141 142 143
        execute_hooks(merge_request, 'update')
      end
    end

144 145 146
    def filter_merge_requests(merge_requests)
      merge_requests.uniq.select(&:source_project)
    end
147

148
    def merge_requests_for_source_branch
149 150
      @source_merge_requests ||= begin
        merge_requests = @project.origin_merge_requests.opened.where(source_branch: @branch_name).to_a
151
        merge_requests += fork_merge_requests.where(source_branch: @branch_name).to_a
152 153
        filter_merge_requests(merge_requests)
      end
154
    end
155 156 157 158 159 160 161 162 163 164 165 166

    def fork_merge_requests
      @fork_merge_requests ||= @project.fork_merge_requests.opened
    end

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

    def branch_removed?
      Gitlab::Git.blank_ref?(@newrev)
    end
167 168
  end
end