Commit 536928a3 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor duplicate code for sast, codequality and performance features

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 42654600
...@@ -7,6 +7,10 @@ module EE ...@@ -7,6 +7,10 @@ module EE
module Build module Build
extend ActiveSupport::Concern extend ActiveSupport::Concern
CODEQUALITY_FILE = 'codeclimate.json'.freeze
SAST_FILE = 'gl-sast-report.json'.freeze
PERFORMANCE_FILE = 'performance.json'.freeze
included do included do
scope :codequality, ->() { where(name: %w[codequality codeclimate]) } scope :codequality, ->() { where(name: %w[codequality codeclimate]) }
scope :performance, ->() { where(name: %w[performance deploy]) } scope :performance, ->() { where(name: %w[performance deploy]) }
...@@ -27,17 +31,21 @@ module EE ...@@ -27,17 +31,21 @@ module EE
end end
def has_codeclimate_json? def has_codeclimate_json?
options.dig(:artifacts, :paths) == ['codeclimate.json'] && has_artifact?(CODEQUALITY_FILE)
artifacts_metadata?
end end
def has_performance_json? def has_performance_json?
options.dig(:artifacts, :paths) == ['performance.json'] && has_artifact?(PERFORMANCE_FILE)
artifacts_metadata?
end end
def has_sast_json? def has_sast_json?
options.dig(:artifacts, :paths) == ['gl-sast-report.json'] && has_artifact?(SAST_FILE)
end
private
def has_artifact?(name)
options.dig(:artifacts, :paths) == [name] &&
artifacts_metadata? artifacts_metadata?
end end
end end
......
...@@ -7,7 +7,7 @@ module EE ...@@ -7,7 +7,7 @@ module EE
expose :head_path, if: -> (mr, _) { can?(current_user, :read_build, mr.head_codeclimate_artifact) } do |merge_request| expose :head_path, if: -> (mr, _) { can?(current_user, :read_build, mr.head_codeclimate_artifact) } do |merge_request|
raw_project_build_artifacts_url(merge_request.source_project, raw_project_build_artifacts_url(merge_request.source_project,
merge_request.head_codeclimate_artifact, merge_request.head_codeclimate_artifact,
path: 'codeclimate.json') path: Ci::Build::CODEQUALITY_FILE)
end end
expose :head_blob_path, if: -> (mr, _) { mr.head_pipeline_sha } do |merge_request| expose :head_blob_path, if: -> (mr, _) { mr.head_pipeline_sha } do |merge_request|
...@@ -17,7 +17,7 @@ module EE ...@@ -17,7 +17,7 @@ module EE
expose :base_path, if: -> (mr, _) { can?(current_user, :read_build, mr.base_codeclimate_artifact) } do |merge_request| expose :base_path, if: -> (mr, _) { can?(current_user, :read_build, mr.base_codeclimate_artifact) } do |merge_request|
raw_project_build_artifacts_url(merge_request.target_project, raw_project_build_artifacts_url(merge_request.target_project,
merge_request.base_codeclimate_artifact, merge_request.base_codeclimate_artifact,
path: 'codeclimate.json') path: Ci::Build::CODEQUALITY_FILE)
end end
expose :base_blob_path, if: -> (mr, _) { mr.base_pipeline_sha } do |merge_request| expose :base_blob_path, if: -> (mr, _) { mr.base_pipeline_sha } do |merge_request|
...@@ -29,13 +29,13 @@ module EE ...@@ -29,13 +29,13 @@ module EE
expose :head_path, if: -> (mr, _) { can?(current_user, :read_build, mr.head_performance_artifact) } do |merge_request| expose :head_path, if: -> (mr, _) { can?(current_user, :read_build, mr.head_performance_artifact) } do |merge_request|
raw_project_build_artifacts_url(merge_request.source_project, raw_project_build_artifacts_url(merge_request.source_project,
merge_request.head_performance_artifact, merge_request.head_performance_artifact,
path: 'performance.json') path: Ci::Build::PERFORMANCE_FILE)
end end
expose :base_path, if: -> (mr, _) { can?(current_user, :read_build, mr.base_performance_artifact) } do |merge_request| expose :base_path, if: -> (mr, _) { can?(current_user, :read_build, mr.base_performance_artifact) } do |merge_request|
raw_project_build_artifacts_url(merge_request.target_project, raw_project_build_artifacts_url(merge_request.target_project,
merge_request.base_performance_artifact, merge_request.base_performance_artifact,
path: 'performance.json') path: Ci::Build::PERFORMANCE_FILE)
end end
end end
...@@ -43,7 +43,7 @@ module EE ...@@ -43,7 +43,7 @@ module EE
expose :path do |merge_request| expose :path do |merge_request|
raw_project_build_artifacts_url(merge_request.source_project, raw_project_build_artifacts_url(merge_request.source_project,
merge_request.sast_artifact, merge_request.sast_artifact,
path: 'gl-sast-report.json') path: Ci::Build::SAST_FILE)
end end
expose :blob_path, if: -> (mr, _) { mr.head_pipeline_sha } do |merge_request| expose :blob_path, if: -> (mr, _) { mr.head_pipeline_sha } do |merge_request|
......
...@@ -128,23 +128,29 @@ describe Ci::Build do ...@@ -128,23 +128,29 @@ describe Ci::Build do
end end
end end
describe '#has_codeclimate_json?' do ARTIFACTS_METHODS = {
has_codeclimate_json?: 'codeclimate.json',
has_performance_json?: 'performance.json',
has_sast_json?: 'gl-sast-report.json'
}.freeze
ARTIFACTS_METHODS.each do |method, filename|
describe "##{method}" do
context 'valid build' do context 'valid build' do
let!(:build) do let!(:build) do
create( create(
:ci_build, :ci_build,
:artifacts, :artifacts,
name: 'codequality',
pipeline: pipeline, pipeline: pipeline,
options: { options: {
artifacts: { artifacts: {
paths: ['codeclimate.json'] paths: [filename]
} }
} }
) )
end end
it { expect(build.has_codeclimate_json?).to be_truthy } it { expect(build.send(method)).to be_truthy }
end end
context 'invalid build' do context 'invalid build' do
...@@ -152,81 +158,13 @@ describe Ci::Build do ...@@ -152,81 +158,13 @@ describe Ci::Build do
create( create(
:ci_build, :ci_build,
:artifacts, :artifacts,
name: 'codequality',
pipeline: pipeline, pipeline: pipeline,
options: {} options: {}
) )
end end
it { expect(build.has_codeclimate_json?).to be_falsey } it { expect(build.send(method)).to be_falsey }
end end
end end
describe '#has_performance_json?' do
context 'valid build' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'performance',
pipeline: pipeline,
options: {
artifacts: {
paths: ['performance.json']
}
}
)
end
it { expect(build.has_performance_json?).to be_truthy }
end
context 'invalid build' do
let!(:build) do
create(
:ci_build,
:artifacts,
name: 'performance',
pipeline: pipeline,
options: {}
)
end
it { expect(build.has_performance_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
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