BigW Consortium Gitlab

Add some fixes and refactoring after review

parent 1bbf2c2c
......@@ -510,23 +510,17 @@ module Ci
def steps
[
Gitlab::Ci::Build::Response::Step.from_commands(self),
Gitlab::Ci::Build::Response::Step.from_after_script(self)
Gitlab::Ci::Build::Step.from_commands(self),
Gitlab::Ci::Build::Step.from_after_script(self)
].compact
end
def image
image = Gitlab::Ci::Build::Response::Image.new(options[:image])
return unless image.valid?
image
Gitlab::Ci::Build::Image.from_image(self)
end
def services
services = options[:services].map do |service|
Gitlab::Ci::Build::Response::Image.new(service)
end
services.select(&:valid?).compact
Gitlab::Ci::Build::Image.from_services(self)
end
def artifacts
......
......@@ -176,6 +176,7 @@ module API
end
desc 'Upload artifacts for job' do
success Entities::JobRequest::Response
http_codes [[201, 'Artifact uploaded'],
[400, 'Bad request'],
[403, 'Forbidden'],
......@@ -186,7 +187,7 @@ module API
requires :id, type: Integer, desc: %q(Job's ID)
optional :token, type: String, desc: %q(Job's authentication token)
optional :expire_in, type: String, desc: %q(Specify when artifacts should expire)
optional 'file', type: File, desc: %q(Artifact's file)
optional :file, type: File, desc: %q(Artifact's file)
optional 'file.path', type: String, desc: %q(path to locally stored body (generated by Workhorse))
optional 'file.name', type: String, desc: %q(real filename as send in Content-Disposition (generated by Workhorse))
optional 'file.type', type: String, desc: %q(real content type as send in Content-Type (generated by Workhorse))
......
module Gitlab
module Ci
module Build
module Response
class Image
attr_reader :name
class Image
attr_reader :name
def initialize(image)
type = image.class
@name = image if type == String
class << self
def from_image(job)
image = Gitlab::Ci::Build::Image.new(job.options[:image])
return unless image.valid?
image
end
def valid?
@name != nil
def from_services(job)
services = job.options[:services].to_a.map do |service|
Gitlab::Ci::Build::Image.new(service)
end
services.select(&:valid?).compact
end
end
def initialize(image)
type = image.class
@name = image if type == String
end
def valid?
@name.present?
end
end
end
end
......
module Gitlab
module Ci
module Build
module Response
class Step
CONDITION_ON_FAILURE = 'on_failure'.freeze
CONDITION_ON_SUCCESS = 'on_success'.freeze
CONDITION_ALWAYS = 'always'.freeze
attr_reader :name, :script, :when, :allow_failure, :timeout
class << self
def from_commands(build)
self.new(:script,
build.commands,
build.timeout,
CONDITION_ON_SUCCESS,
false)
end
def from_after_script(build)
after_script = build.options[:after_script]
return unless after_script
self.new(:after_script,
after_script,
build.timeout,
CONDITION_ALWAYS,
true)
end
end
def initialize(name, script, timeout, when_condition = CONDITION_ON_SUCCESS, allow_failure = true)
@name = name
@script = script.split("\n")
@timeout = timeout
@when = when_condition
@allow_failure = allow_failure
end
end
end
end
end
end
module Gitlab
module Ci
module Build
class Step
WHEN_ON_FAILURE = 'on_failure'.freeze
WHEN_ON_SUCCESS = 'on_success'.freeze
WHEN_ALWAYS = 'always'.freeze
attr_reader :name, :script, :timeout, :when, :allow_failure
class << self
def from_commands(job)
self.new(:script).tap do |step|
step.script = job.commands
step.timeout = job.timeout
step.when = WHEN_ON_SUCCESS
end
end
def from_after_script(job)
after_script = job.options[:after_script]
return unless after_script
self.new(:after_script).tap do |step|
step.script = after_script
step.timeout = job.timeout
step.when = WHEN_ALWAYS
step.allow_failure_on
end
end
end
def initialize(name)
@name = name
@allow_failure = false
end
def script=(script)
@script = script.split("\n")
end
def timeout=(timeout)
@timeout = timeout
end
def when=(when_condition)
@when = when_condition
end
def allow_failure_on
@allow_failure = true
end
end
end
end
end
......@@ -173,5 +173,9 @@ FactoryGirl.define do
}
end
end
trait :no_options do
options { {} }
end
end
end
require 'spec_helper'
describe Gitlab::Ci::Build::Image do
let(:job) { create(:ci_build, :no_options) }
describe '#from_image' do
subject { described_class.from_image(job) }
context 'when image is defined in job' do
let(:image_name) { 'ruby:2.1' }
let(:job) { create(:ci_build, options: { image: image_name } ) }
it { is_expected.to be_kind_of(described_class) }
it { expect(subject.name).to eq(image_name) }
context 'when image name is empty' do
let(:image_name) { '' }
it { is_expected.to eq(nil) }
end
end
context 'when image is not defined in job' do
it { is_expected.to eq(nil) }
end
end
describe '#from_services' do
subject { described_class.from_services(job) }
context 'when services are defined in job' do
let(:service_image_name) { 'postgres' }
let(:job) { create(:ci_build, options: { services: [service_image_name] }) }
it { is_expected.to be_kind_of(Array) }
it { is_expected.not_to be_empty }
it { expect(subject[0].name).to eq(service_image_name) }
context 'when service image name is empty' do
let(:service_image_name) { '' }
it { is_expected.to be_kind_of(Array) }
it { is_expected.to be_empty }
end
end
context 'when services are not defined in job' do
it { is_expected.to be_kind_of(Array) }
it { is_expected.to be_empty }
end
end
end
\ No newline at end of file
require 'spec_helper'
describe Gitlab::Ci::Build::Step do
let(:job) { create(:ci_build, :no_options, commands: "ls -la\ndate") }
describe '#from_commands' do
subject { described_class.from_commands(job) }
it { expect(subject.name).to eq(:script) }
it { expect(subject.script).to eq(['ls -la', 'date']) }
it { expect(subject.timeout).to eq(job.timeout) }
it { expect(subject.when).to eq('on_success') }
it { expect(subject.allow_failure).to be_falsey }
end
describe '#from_after_script' do
subject { described_class.from_after_script(job) }
context 'when after_script is empty' do
it { is_expected.to be(nil) }
end
context 'when after_script is not empty' do
let(:job) { create(:ci_build, options: { after_script: "ls -la\ndate" }) }
it { expect(subject.name).to eq(:after_script) }
it { expect(subject.script).to eq(['ls -la', 'date']) }
it { expect(subject.timeout).to eq(job.timeout) }
it { expect(subject.when).to eq('always') }
it { expect(subject.allow_failure).to be_truthy }
end
end
end
\ No newline at end of file
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