Update relevant build fields when build is erased

This commit is contained in:
Grzegorz Bizon 2016-02-02 12:12:03 +01:00 committed by Grzegorz Bizon
parent bae3701073
commit 2c7f36f430
4 changed files with 58 additions and 15 deletions

View file

@ -57,7 +57,7 @@ class Projects::BuildsController < Projects::ApplicationController
end end
def erase def erase
@build.erase! @build.erase!(erased_by: current_user)
redirect_to namespace_project_build_path(project.namespace, project, @build), redirect_to namespace_project_build_path(project.namespace, project, @build),
notice: "Build ##{@build.id} has been sucessfully erased!" notice: "Build ##{@build.id} has been sucessfully erased!"
end end

View file

@ -39,7 +39,7 @@
module Ci module Ci
class Build < CommitStatus class Build < CommitStatus
include Gitlab::Application.routes.url_helpers include Gitlab::Application.routes.url_helpers
include Eraseable include Build::Eraseable
LAZY_ATTRIBUTES = ['trace'] LAZY_ATTRIBUTES = ['trace']
@ -208,8 +208,8 @@ module Ci
end end
end end
def trace_empty? def has_trace?
raw_trace.blank? raw_trace.present?
end end
def raw_trace def raw_trace

View file

@ -1,17 +1,23 @@
module Ci module Ci
class Build class Build
module Eraseable module Eraseable
include ActiveSupport::Concern extend ActiveSupport::Concern
def erase! included do
belongs_to :erased_by, class_name: 'User'
end
def erase!(opts = {})
raise StandardError, 'Build not eraseable!' unless eraseable? raise StandardError, 'Build not eraseable!' unless eraseable?
remove_artifacts_file! remove_artifacts_file!
remove_artifacts_metadata! remove_artifacts_metadata!
erase_trace! erase_trace!
update_erased!(opts[:erased_by])
end end
def eraseable? def eraseable?
complete? && (artifacts_file.exists? || !trace_empty?) complete? && (artifacts? || has_trace?)
end end
def erase_url def erase_url
@ -25,6 +31,13 @@ module Ci
def erase_trace! def erase_trace!
File.truncate(path_to_trace, 0) if File.file?(path_to_trace) File.truncate(path_to_trace, 0) if File.file?(path_to_trace)
end end
def update_erased!(user = nil)
self.erased_by = user if user
self.erased_at = Time.now
self.erased = true
self.save!
end
end end
end end
end end

View file

@ -1,6 +1,28 @@
require 'spec_helper' require 'spec_helper'
describe Ci::Build::Eraseable, models: true do describe Ci::Build::Eraseable, models: true do
shared_examples 'eraseable' do
it 'should remove artifact file' do
expect(build.artifacts_file.exists?).to be_falsy
end
it 'should remove artifact metadata file' do
expect(build.artifacts_metadata.exists?).to be_falsy
end
it 'should erase build trace in trace file' do
expect(File.zero?(build.path_to_trace)).to eq true
end
it 'should set erased to true' do
expect(build.erased?).to be true
end
it 'should set erase date' do
expect(build.erased_at).to_not be_falsy
end
end
context 'build is not eraseable' do context 'build is not eraseable' do
let!(:build) { create(:ci_build) } let!(:build) { create(:ci_build) }
@ -23,18 +45,26 @@ describe Ci::Build::Eraseable, models: true do
let!(:build) { create(:ci_build_with_trace, :success, :artifacts) } let!(:build) { create(:ci_build_with_trace, :success, :artifacts) }
describe '#erase!' do describe '#erase!' do
before { build.erase! } before { build.erase!(erased_by: user) }
it 'should remove artifact file' do context 'erased by user' do
expect(build.artifacts_file.exists?).to be_falsy let!(:user) { create(:user, username: 'eraser') }
include_examples 'eraseable'
it 'should record user who erased a build' do
expect(build.erased_by).to eq user
end
end end
it 'should remove artifact metadata file' do context 'erased by system' do
expect(build.artifacts_metadata.exists?).to be_falsy let(:user) { nil }
end
it 'should erase build trace in trace file' do include_examples 'eraseable'
expect(File.zero?(build.path_to_trace)).to eq true
it 'should not set user who erased a build' do
expect(build.erased_by).to be_nil
end
end end
end end