Commit 5e11a314 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Move sast and codeclimate related code to EE modules

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 176c6604
......@@ -4,7 +4,7 @@ module Ci
include AfterCommitQueue
include Presentable
include Importable
prepend EE::Build
prepend EE::Ci::Build
belongs_to :runner
belongs_to :trigger_request
......@@ -40,8 +40,6 @@ module Ci
scope :with_artifacts_stored_locally, ->() { with_artifacts.where(artifacts_file_store: [nil, ArtifactUploader::LOCAL_STORE]) }
scope :last_month, ->() { where('created_at > ?', Date.today - 1.month) }
scope :manual_actions, ->() { where(when: :manual, status: COMPLETED_STATUSES + [:manual]) }
scope :codequality, ->() { where(name: %w[codequality codeclimate]) }
scope :sast, ->() { where(name: 'sast') }
scope :ref_protected, -> { where(protected: true) }
mount_uploader :artifacts_file, ArtifactUploader
......@@ -472,16 +470,6 @@ module Ci
trace
end
def has_codeclimate_json?
options.dig(:artifacts, :paths) == ['codeclimate.json'] &&
artifacts_metadata?
end
def has_sast_json?
options.dig(:artifacts, :paths) == ['gl-sast-report.json'] &&
artifacts_metadata?
end
def serializable_hash(options = {})
super(options).merge(when: read_attribute(:when))
end
......
......@@ -481,14 +481,6 @@ module Ci
.fabricate!
end
def codeclimate_artifact
artifacts.codequality.find(&:has_codeclimate_json?)
end
def sast_artifact
artifacts.sast.find(&:has_sast_json?)
end
def latest_builds_with_artifacts
@latest_builds_with_artifacts ||= builds.latest.with_artifacts
end
......
module EE
# Build EE mixin
#
# This module is intended to encapsulate EE-specific model logic
# and be included in the `Build` model
module Build
extend ActiveSupport::Concern
included do
after_save :stick_build_if_status_changed
end
def shared_runners_minutes_limit_enabled?
runner && runner.shared? && project.shared_runners_minutes_limit_enabled?
end
def stick_build_if_status_changed
return unless status_changed?
return unless running?
::Gitlab::Database::LoadBalancing::Sticking.stick(:build, id)
end
end
end
module EE
module Ci
# Build EE mixin
#
# This module is intended to encapsulate EE-specific model logic
# and be included in the `Build` model
module Build
extend ActiveSupport::Concern
included do
scope :codequality, ->() { where(name: %w[codequality codeclimate]) }
scope :sast, ->() { where(name: 'sast') }
after_save :stick_build_if_status_changed
end
def shared_runners_minutes_limit_enabled?
runner && runner.shared? && project.shared_runners_minutes_limit_enabled?
end
def stick_build_if_status_changed
return unless status_changed?
return unless running?
::Gitlab::Database::LoadBalancing::Sticking.stick(:build, id)
end
def has_codeclimate_json?
options.dig(:artifacts, :paths) == ['codeclimate.json'] &&
artifacts_metadata?
end
def has_sast_json?
options.dig(:artifacts, :paths) == ['gl-sast-report.json'] &&
artifacts_metadata?
end
end
end
end
......@@ -12,6 +12,14 @@ module EE
result
end
def codeclimate_artifact
artifacts.codequality.find(&:has_codeclimate_json?)
end
def sast_artifact
artifacts.sast.find(&:has_sast_json?)
end
end
end
end
......@@ -161,4 +161,38 @@ describe Ci::Build do
it { expect(build.has_codeclimate_json?).to be_falsey }
end
end
describe '#has_sast_json?' do
context 'valid build' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'sast',
pipeline: pipeline,
options: {
artifacts: {
paths: ['gl-sast-report.json']
}
}
)
end
it { expect(build.has_sast_json?).to be_truthy }
end
context 'invalid build' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'sast',
pipeline: pipeline,
options: {}
)
end
it { expect(build.has_sast_json?).to be_falsey }
end
end
end
require 'spec_helper'
describe Ci::Pipeline do
let(:user) { create(:user) }
set(:project) { create(:project) }
let(:pipeline) do
create(:ci_empty_pipeline, status: :created, project: project)
end
describe '.failure_reasons' do
it 'contains failure reasons about exceeded limits' do
expect(described_class.failure_reasons)
.to include 'activity_limit_exceeded', 'size_limit_exceeded'
end
end
describe '#codeclimate_artifact' do
context 'has codequality job' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'codequality',
pipeline: pipeline,
options: {
artifacts: {
paths: ['codeclimate.json']
}
}
)
end
it { expect(pipeline.codeclimate_artifact).to eq(build) }
end
context 'no codequality job' do
before do
create(:ci_build, pipeline: pipeline)
end
it { expect(pipeline.codeclimate_artifact).to be_nil }
end
end
describe '#sast_artifact' do
context 'has sast job' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'sast',
pipeline: pipeline,
options: {
artifacts: {
paths: ['gl-sast-report.json']
}
}
)
end
it { expect(pipeline.sast_artifact).to eq(build) }
end
context 'no sast job' do
before do
create(:ci_build, pipeline: pipeline)
end
it { expect(pipeline.sast_artifact).to be_nil }
end
end
end
......@@ -1472,60 +1472,4 @@ describe Ci::Pipeline, :mailer do
expect(query_count).to eq(1)
end
end
describe '#codeclimate_artifact' do
context 'has codequality job' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'codequality',
pipeline: pipeline,
options: {
artifacts: {
paths: ['codeclimate.json']
}
}
)
end
it { expect(pipeline.codeclimate_artifact).to eq(build) }
end
context 'no codequality job' do
before do
create(:ci_build, pipeline: pipeline)
end
it { expect(pipeline.codeclimate_artifact).to be_nil }
end
end
describe '#sast_artifact' do
context 'has sast job' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'sast',
pipeline: pipeline,
options: {
artifacts: {
paths: ['gl-sast-report.json']
}
}
)
end
it { expect(pipeline.sast_artifact).to eq(build) }
end
context 'no sast job' do
before do
create(:ci_build, pipeline: pipeline)
end
it { expect(pipeline.sast_artifact).to be_nil }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment