commit_collection.rb 1.38 KB
Newer Older
1 2 3 4 5
# frozen_string_literal: true

# A collection of Commit instances for a specific project and Git reference.
class CommitCollection
  include Enumerable
6
  include Gitlab::Utils::StrongMemoize
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  attr_reader :project, :ref, :commits

  # project - The project the commits belong to.
  # commits - The Commit instances to store.
  # ref - The name of the ref (e.g. "master").
  def initialize(project, commits, ref = nil)
    @project = project
    @commits = commits
    @ref = ref
  end

  def each(&block)
    commits.each(&block)
  end

23 24
  def authors
    emails = without_merge_commits.map(&:author_email).uniq
25 26 27 28

    User.by_any_email(emails)
  end

29 30
  def without_merge_commits
    strong_memoize(:without_merge_commits) do
Sean McGivern's avatar
Sean McGivern committed
31
      commits.reject(&:merge_commit?)
32 33 34
    end
  end

35 36 37 38 39
  # Sets the pipeline status for every commit.
  #
  # Setting this status ahead of time removes the need for running a query for
  # every commit we're displaying.
  def with_pipeline_status
40
    statuses = project.ci_pipelines.latest_status_per_commit(map(&:id), ref)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

    each do |commit|
      commit.set_status_for_ref(ref, statuses[commit.id])
    end

    self
  end

  def respond_to_missing?(message, inc_private = false)
    commits.respond_to?(message, inc_private)
  end

  # rubocop:disable GitlabSecurity/PublicSend
  def method_missing(message, *args, &block)
    commits.public_send(message, *args, &block)
  end
end