summaryrefslogtreecommitdiff
path: root/app/models/project.rb
diff options
context:
space:
mode:
authorSteve Azzopardi <steveazz@outlook.com>2018-12-06 11:25:25 +0100
committerSteve Azzopardi <steveazz@outlook.com>2019-01-07 17:29:26 +0100
commit7ac32ae282fa2d35a3651de08f35aad1b85ffca0 (patch)
treea29770af7276aa89fcea3cdcb0e075046aee1b86 /app/models/project.rb
parentf2c7f3d0aa9dfe94fa5e1ab421f86c01350f4cc9 (diff)
downloadgitlab-ce-7ac32ae282fa2d35a3651de08f35aad1b85ffca0.tar.gz
Refactor project.latest_successful_builds_for def
`project.latest_successful_builds_for(ref)` is being used to find a single job all the time. This results into us having to call `find_by` inside of the controller which violates our CodeReuse/ActiveRecord rubocop rule. Refactor `project.latest_successful_builds_for(ref)` to `project.latest_successful_build_for(job_name, ref)` which will execute the `find_by` inside of the model. Also create `project.latest_successful_build_for!(job_name, ref)` which raises an exception instead of returning nil.
Diffstat (limited to 'app/models/project.rb')
-rw-r--r--app/models/project.rb14
1 files changed, 5 insertions, 9 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index e2f010a0432..681c7c71934 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -644,19 +644,15 @@ class Project < ActiveRecord::Base
end
# ref can't be HEAD, can only be branch/tag name or SHA
- def latest_successful_builds_for(ref = default_branch)
+ def latest_successful_build_for(job_name, ref = default_branch)
latest_pipeline = ci_pipelines.latest_successful_for(ref)
+ return unless latest_pipeline
- if latest_pipeline
- latest_pipeline.builds.latest.with_artifacts_archive
- else
- builds.none
- end
+ latest_pipeline.builds.latest.with_artifacts_archive.find_by(name: job_name)
end
- def latest_successful_build_for(job_name, ref = default_branch)
- builds = latest_successful_builds_for(ref)
- builds.find_by!(name: job_name)
+ def latest_successful_build_for!(job_name, ref = default_branch)
+ latest_successful_build_for(job_name, ref) or raise ActiveRecord::RecordNotFound.new("Couldn't find job #{job_name}", job_name)
end
def merge_base_commit(first_commit_id, second_commit_id)