summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-09-12 15:54:25 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-09-14 13:20:05 +0200
commit3e999684f9c3b7306dea7a7feee90536ee100ccf (patch)
tree224eabd4599d31a70e799b8ff7a94b759f826865 /app
parent1140fcce4f8b5463f451356b76fea125826478b2 (diff)
downloadgitlab-ce-3e999684f9c3b7306dea7a7feee90536ee100ccf.tar.gz
Memoize pipelines for project download buttons
This adds Project#latest_successful_pipeline_for and Project#latest_successful_pipeline_for_default_branch. The 2nd method memoizes the result (taking nil values into account) to ensure the underlying query isn't executed multiple times when viewing a project's homepage. See https://gitlab.com/gitlab-org/gitlab-ce/issues/36878#note_40073607 for more information.
Diffstat (limited to 'app')
-rw-r--r--app/models/project.rb17
-rw-r--r--app/views/projects/buttons/_download.html.haml2
2 files changed, 18 insertions, 1 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index ff5638dd155..add272cc5ce 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1163,6 +1163,23 @@ class Project < ActiveRecord::Base
pipelines.order(id: :desc).find_by(sha: sha, ref: ref)
end
+ def latest_successful_pipeline_for_default_branch
+ if defined?(@latest_successful_pipeline_for_default_branch)
+ return @latest_successful_pipeline_for_default_branch
+ end
+
+ @latest_successful_pipeline_for_default_branch =
+ pipelines.latest_successful_for(default_branch)
+ end
+
+ def latest_successful_pipeline_for(ref = nil)
+ if ref && ref != default_branch
+ pipelines.latest_successful_for(ref)
+ else
+ latest_successful_pipeline_for_default_branch
+ end
+ end
+
def enable_ci
project_feature.update_attribute(:builds_access_level, ProjectFeature::ENABLED)
end
diff --git a/app/views/projects/buttons/_download.html.haml b/app/views/projects/buttons/_download.html.haml
index 883922dbf04..219ccdc8164 100644
--- a/app/views/projects/buttons/_download.html.haml
+++ b/app/views/projects/buttons/_download.html.haml
@@ -1,4 +1,4 @@
-- pipeline = local_assigns.fetch(:pipeline) { project.pipelines.latest_successful_for(ref) }
+- pipeline = local_assigns.fetch(:pipeline) { project.latest_successful_pipeline_for(ref) }
- if !project.empty_repo? && can?(current_user, :download_code, project)
.project-action-button.dropdown.inline>