diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-11-10 22:57:11 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-11-16 18:01:14 +0300 |
commit | ab16a6fb34c0f3e4d9afed3332c559868201e606 (patch) | |
tree | e878395b0f198b3db1cc014bad02361628f6206e /app/models/commit_collection.rb | |
parent | 81e94ce1761b48b73be2a8d71938dfe934921e35 (diff) |
Optimise getting the pipeline status of commits
This adds an optimised way of getting the latest pipeline status for a
list of Commit objects (or just a single one).
Diffstat (limited to 'app/models/commit_collection.rb')
-rw-r--r-- | app/models/commit_collection.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/app/models/commit_collection.rb b/app/models/commit_collection.rb new file mode 100644 index 00000000000..dd93af9df64 --- /dev/null +++ b/app/models/commit_collection.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +# A collection of Commit instances for a specific project and Git reference. +class CommitCollection + include Enumerable + + 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 + + # 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 + statuses = project.pipelines.latest_status_per_commit(map(&:id), ref) + + 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 |