Create `get_build` for project model

Inside of `Projects::ArtifactsController` and
`Projects::BuildArtifactsController` we fetching the build by id using
active record directly which violates `CodeReuse/ActiveRecord` rubocop
rule. Create `get_build` inside of `project` model which does the same
thing.
This commit is contained in:
Steve Azzopardi 2018-12-06 11:55:50 +01:00
parent 7ac32ae282
commit f9c8822afd
No known key found for this signature in database
GPG Key ID: 605BD4706E7DB47D
4 changed files with 30 additions and 7 deletions

View File

@ -85,11 +85,9 @@ class Projects::ArtifactsController < Projects::ApplicationController
end
end
# rubocop: disable CodeReuse/ActiveRecord
def build_from_id
project.builds.find_by(id: params[:job_id]) if params[:job_id]
project.get_build(params[:job_id]) if params[:job_id]
end
# rubocop: enable CodeReuse/ActiveRecord
def build_from_ref
return unless @ref_name

View File

@ -44,11 +44,9 @@ class Projects::BuildArtifactsController < Projects::ApplicationController
@job ||= job_from_id || job_from_ref
end
# rubocop: disable CodeReuse/ActiveRecord
def job_from_id
project.builds.find_by(id: params[:build_id]) if params[:build_id]
project.get_build(params[:build_id]) if params[:build_id]
end
# rubocop: enable CodeReuse/ActiveRecord
def job_from_ref
return unless @ref_name

View File

@ -652,7 +652,11 @@ class Project < ActiveRecord::Base
end
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)
latest_successful_build_for(job_name, ref) || raise(ActiveRecord::RecordNotFound.new("Couldn't find job #{job_name}"))
end
def get_build(id)
builds.find_by(id: id)
end
def merge_base_commit(first_commit_id, second_commit_id)

View File

@ -2000,6 +2000,29 @@ describe Project do
end
end
describe '#get_build' do
let(:project) { create(:project, :repository) }
let(:ci_pipeline) { create(:ci_pipeline, project: project) }
context 'when build exists' do
context 'build is associated with project' do
let(:build) { create(:ci_build, :success, pipeline: ci_pipeline) }
it { expect(project.get_build(build.id)).to eq(build) }
end
context 'build is not associated with project' do
let(:build) { create(:ci_build, :success) }
it { expect(project.get_build(build.id)).to be_nil }
end
end
context 'build does not exists' do
it { expect(project.get_build(rand 100)).to be_nil }
end
end
describe '#import_status' do
context 'with import_state' do
it 'returns the right status' do