summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz.bizon@ntsn.pl>2016-02-01 11:59:05 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-02-19 17:24:03 +0100
commitc8102d938bf8f1c73058281b2724730c6d88a53b (patch)
tree85d2c7d1ffeead37a1d3869da2a9ca593803fc20
parent89b181204c9f8d05761463bbf0f5cb1a6be24611 (diff)
downloadgitlab-ce-c8102d938bf8f1c73058281b2724730c6d88a53b.tar.gz
Add build eraseable feature implementation
-rw-r--r--app/controllers/projects/builds_controller.rb8
-rw-r--r--app/models/ci/build/eraseable.rb21
-rw-r--r--app/views/projects/builds/show.html.haml9
-rw-r--r--config/routes.rb1
-rw-r--r--spec/models/ci/build/eraseable_spec.rb62
5 files changed, 90 insertions, 11 deletions
diff --git a/app/controllers/projects/builds_controller.rb b/app/controllers/projects/builds_controller.rb
index 3d373e3cc2d..c7c03208c9e 100644
--- a/app/controllers/projects/builds_controller.rb
+++ b/app/controllers/projects/builds_controller.rb
@@ -56,10 +56,10 @@ class Projects::BuildsController < Projects::ApplicationController
render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha)
end
- def destroy
- @build.destroy
- redirect_to namespace_project_builds_path(project.namespace, project),
- notice: "Build ##{@build.id} has been sucessfully removed!"
+ def erase
+ @build.erase!
+ redirect_to namespace_project_build_path(project.namespace, project, @build),
+ notice: "Build ##{@build.id} has been sucessfully erased!"
end
private
diff --git a/app/models/ci/build/eraseable.rb b/app/models/ci/build/eraseable.rb
index bc427b112ec..df686852f7d 100644
--- a/app/models/ci/build/eraseable.rb
+++ b/app/models/ci/build/eraseable.rb
@@ -4,11 +4,26 @@ module Ci
include ActiveSupport::Concern
def erase!
- raise NotImplementedError
+ raise StandardError, 'Build not eraseable!' unless eraseable?
+ remove_artifacts_file!
+ remove_artifacts_metadata!
+ erase_trace!
end
- def erased?
- raise NotImpementedError
+ def eraseable?
+ 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
diff --git a/app/views/projects/builds/show.html.haml b/app/views/projects/builds/show.html.haml
index 4451793250f..4dcac0de67a 100644
--- a/app/views/projects/builds/show.html.haml
+++ b/app/views/projects/builds/show.html.haml
@@ -113,10 +113,11 @@
- elsif @build.retry_url
= link_to "Retry", @build.retry_url, class: 'btn btn-sm btn-primary', method: :post
- = link_to '', class: 'btn btn-sm btn-danger', method: :delete,
- data: { confirm: 'Are you sure you want to remove this?' } do
- = icon('remove')
- Remove
+ - if @build.eraseable?
+ = link_to @build.erase_url, class: 'btn btn-sm btn-warning', method: :put,
+ data: { confirm: 'Are you sure you want to erase this build?' } do
+ = icon('eraser')
+ Erase
.clearfix
- if @build.duration
diff --git a/config/routes.rb b/config/routes.rb
index 507bcbc53d7..94a9d378be0 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -617,6 +617,7 @@ Rails.application.routes.draw do
get :status
post :cancel
post :retry
+ put :erase
end
resource :artifacts, only: [] do
diff --git a/spec/models/ci/build/eraseable_spec.rb b/spec/models/ci/build/eraseable_spec.rb
new file mode 100644
index 00000000000..6735f3ed314
--- /dev/null
+++ b/spec/models/ci/build/eraseable_spec.rb
@@ -0,0 +1,62 @@
+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