<commit_before>module MergeRequests
  class RefreshService < MergeRequests::BaseService
    def execute(oldrev, newrev, ref)
      return true unless ref =~ /heads/

      @branch_name = ref.gsub("refs/heads/", "")
      @fork_merge_requests = @project.fork_merge_requests.opened
      @commits = @project.repository.commits_between(oldrev, newrev)

      close_merge_requests
      reload_merge_requests
      comment_mr_with_commits

      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
      merge_requests = merge_requests.select(&:last_commit)

      merge_requests = merge_requests.select do |merge_request|
        commit_ids.include?(merge_request.last_commit.id)
      end


      merge_requests.uniq.select(&:source_project).each do |merge_request|
        MergeRequests::MergeService.new.execute(merge_request, @current_user, nil)
      end
    end

    # 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
      merge_requests = @project.merge_requests.opened.by_branch(@branch_name).to_a
      merge_requests += @fork_merge_requests.by_branch(@branch_name).to_a
      merge_requests = filter_merge_requests(merge_requests)

      merge_requests.each do |merge_request|
        merge_request.reload_code
        merge_request.mark_as_unchecked
      end
    end

    # Add comment about pushing new commits to merge requests
    def comment_mr_with_commits
      merge_requests = @project.origin_merge_requests.opened.where(source_branch: @branch_name).to_a
      merge_requests += @fork_merge_requests.where(source_branch: @branch_name).to_a
      merge_requests = filter_merge_requests(merge_requests)

      merge_requests.each do |merge_request|
        Note.create_new_commits_note(merge_request, merge_request.project,
                                     @current_user, @commits)
      end
    end

    def filter_merge_requests(merge_requests)
      merge_requests.uniq.select(&:source_project)
    end
  end
end<commit_msg>Improve MR code reloading when push code<commit_after>module MergeRequests
  class RefreshService < MergeRequests::BaseService
    def execute(oldrev, newrev, ref)
      return true unless ref =~ /heads/

      @branch_name = ref.gsub("refs/heads/", "")
      @fork_merge_requests = @project.fork_merge_requests.opened
      @commits = @project.repository.commits_between(oldrev, newrev)

      close_merge_requests
      reload_merge_requests
      comment_mr_with_commits

      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
      merge_requests = merge_requests.select(&:last_commit)

      merge_requests = merge_requests.select do |merge_request|
        commit_ids.include?(merge_request.last_commit.id)
      end


      merge_requests.uniq.select(&:source_project).each do |merge_request|
        MergeRequests::MergeService.new.execute(merge_request, @current_user, nil)
      end
    end

    # 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
      merge_requests = @project.merge_requests.opened.by_branch(@branch_name).to_a
      merge_requests += @fork_merge_requests.by_branch(@branch_name).to_a
      merge_requests = filter_merge_requests(merge_requests)

      merge_requests.each do |merge_request|

        if merge_request.source_branch == @branch_name
          merge_request.reload_code
          merge_request.mark_as_unchecked
        else
          mr_commit_ids = merge_request.commits.map(&:id)
          push_commit_ids = @commits.map(&:id)
          matches = mr_commit_ids & push_commit_ids

          if matches.any?
            merge_request.reload_code
            merge_request.mark_as_unchecked
          else
            merge_request.mark_as_unchecked
          end
        end
      end
    end

    # Add comment about pushing new commits to merge requests
    def comment_mr_with_commits
      merge_requests = @project.origin_merge_requests.opened.where(source_branch: @branch_name).to_a
      merge_requests += @fork_merge_requests.where(source_branch: @branch_name).to_a
      merge_requests = filter_merge_requests(merge_requests)

      merge_requests.each do |merge_request|
        Note.create_new_commits_note(merge_request, merge_request.project,
                                     @current_user, @commits)
      end
    end

    def filter_merge_requests(merge_requests)
      merge_requests.uniq.select(&:source_project)
    end
  end
end
{'fraction_non_alphanumeric': 0.066990110095167, 'fraction_numerical': 0.0, 'mean_word_length': 3.2438638163103723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5316699', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1512, 'n_words': 346}