Commit c8102d93 authored by Grzegorz Bizon's avatar Grzegorz Bizon Committed by Grzegorz Bizon

Add build eraseable feature implementation

parent 89b18120
...@@ -56,10 +56,10 @@ class Projects::BuildsController < Projects::ApplicationController ...@@ -56,10 +56,10 @@ class Projects::BuildsController < Projects::ApplicationController
render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha) render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha)
end end
def destroy def erase
@build.destroy @build.erase!
redirect_to namespace_project_builds_path(project.namespace, project), redirect_to namespace_project_build_path(project.namespace, project, @build),
notice: "Build ##{@build.id} has been sucessfully removed!" notice: "Build ##{@build.id} has been sucessfully erased!"
end end
private private
......
...@@ -4,11 +4,26 @@ module Ci ...@@ -4,11 +4,26 @@ module Ci
include ActiveSupport::Concern include ActiveSupport::Concern
def erase! def erase!
raise NotImplementedError raise StandardError, 'Build not eraseable!' unless eraseable?
remove_artifacts_file!
remove_artifacts_metadata!
erase_trace!
end end
def erased? def eraseable?
raise NotImpementedError artifacts_file.exists? || File.file?(path_to_trace)
end
def erase_url
if eraseable?
erase_namespace_project_build_path(project.namespace, project, self)
end
end
private
def erase_trace!
File.truncate(path_to_trace, 0) if File.file?(path_to_trace)
end end
end end
end end
......
...@@ -113,10 +113,11 @@ ...@@ -113,10 +113,11 @@
- elsif @build.retry_url - elsif @build.retry_url
= link_to "Retry", @build.retry_url, class: 'btn btn-sm btn-primary', method: :post = link_to "Retry", @build.retry_url, class: 'btn btn-sm btn-primary', method: :post
= link_to '', class: 'btn btn-sm btn-danger', method: :delete, - if @build.eraseable?
data: { confirm: 'Are you sure you want to remove this?' } do = link_to @build.erase_url, class: 'btn btn-sm btn-warning', method: :put,
= icon('remove') data: { confirm: 'Are you sure you want to erase this build?' } do
Remove = icon('eraser')
Erase
.clearfix .clearfix
- if @build.duration - if @build.duration
......
...@@ -617,6 +617,7 @@ Rails.application.routes.draw do ...@@ -617,6 +617,7 @@ Rails.application.routes.draw do
get :status get :status
post :cancel post :cancel
post :retry post :retry
put :erase
end end
resource :artifacts, only: [] do resource :artifacts, only: [] do
......
require 'spec_helper'
describe Ci::Build::Eraseable, models: true do
context 'build is not eraseable' do
let!(:build) { create(:ci_build) }
describe '#erase!' do
it { expect { build.erase! }.to raise_error(StandardError, /Build not eraseable!/ )}
end
describe '#eraseable?' do
subject { build.eraseable? }
it { is_expected.to eq false }
end
describe '#erase_url' do
subject { build.erase_url }
it { is_expected.to be_falsy }
end
end
context 'build is eraseable' do
let!(:build) { create(:ci_build_with_trace, :artifacts) }
describe '#erase!' do
before { build.erase! }
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
end
describe '#eraseable?' do
subject { build.eraseable? }
it { is_expected.to eq true }
end
describe '#erase_url' do
subject { build.erase_url }
it { is_expected.to be_truthy }
end
context 'metadata and build trace are not available' do
let!(:build) { create(:ci_build, :artifacts) }
before { build.remove_artifacts_metadata! }
describe '#erase!' do
it 'should not raise error' do
expect { build.erase! }.to_not raise_error
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