Use CancelModel in Builds endpoint
This commit is contained in:
parent
428422d569
commit
c3e0d6d6bb
|
@ -2,6 +2,7 @@ require 'travis/api/app'
|
||||||
require 'travis/api/workers/build_cancellation'
|
require 'travis/api/workers/build_cancellation'
|
||||||
require 'travis/api/workers/build_restart'
|
require 'travis/api/workers/build_restart'
|
||||||
require 'travis/api/enqueue/services/restart_model'
|
require 'travis/api/enqueue/services/restart_model'
|
||||||
|
require 'travis/api/enqueue/services/cancel_model'
|
||||||
|
|
||||||
class Travis::Api::App
|
class Travis::Api::App
|
||||||
class Endpoint
|
class Endpoint
|
||||||
|
@ -21,7 +22,12 @@ class Travis::Api::App
|
||||||
post '/:id/cancel' do
|
post '/:id/cancel' do
|
||||||
Metriks.meter("api.request.cancel_build").mark
|
Metriks.meter("api.request.cancel_build").mark
|
||||||
|
|
||||||
service = self.service(:cancel_build, params.merge(source: 'api'))
|
if Travis::Features.owner_active?(:enqueue_to_hub, current_user)
|
||||||
|
service = Travis::Enqueue::Services::CancelModel.new(current_user, { build_id: params[:id], source: 'api' })
|
||||||
|
else
|
||||||
|
service = self.service(:cancel_build, params.merge(source: 'api'))
|
||||||
|
end
|
||||||
|
|
||||||
if !service.authorized?
|
if !service.authorized?
|
||||||
json = { error: {
|
json = { error: {
|
||||||
message: "You don't have access to cancel build(#{params[:id]})"
|
message: "You don't have access to cancel build(#{params[:id]})"
|
||||||
|
@ -40,7 +46,12 @@ class Travis::Api::App
|
||||||
status 422
|
status 422
|
||||||
respond_with json
|
respond_with json
|
||||||
else
|
else
|
||||||
Travis::Sidekiq::BuildCancellation.perform_async(id: params[:id], user_id: current_user.id, source: 'api')
|
if service.respond_to?(:enqueue_to_hub)
|
||||||
|
payload = { build_id: params[:id], source: 'api'}
|
||||||
|
service.enqueue_to_hub(payload)
|
||||||
|
else
|
||||||
|
Travis::Sidekiq::BuildCancellation.perform_async(id: params[:id], user_id: current_user.id, source: 'api')
|
||||||
|
end
|
||||||
|
|
||||||
Metriks.meter("api.request.cancel_build.success").mark
|
Metriks.meter("api.request.cancel_build.success").mark
|
||||||
status 204
|
status 204
|
||||||
|
|
|
@ -13,11 +13,6 @@ module Travis
|
||||||
target
|
target
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
|
||||||
cancel
|
|
||||||
end
|
|
||||||
instrument :run
|
|
||||||
|
|
||||||
def messages
|
def messages
|
||||||
messages = []
|
messages = []
|
||||||
messages << { :notice => "The #{type} was successfully cancelled." } if can_cancel?
|
messages << { :notice => "The #{type} was successfully cancelled." } if can_cancel?
|
||||||
|
@ -26,7 +21,7 @@ module Travis
|
||||||
messages
|
messages
|
||||||
end
|
end
|
||||||
|
|
||||||
def cancel(payload)
|
def enqueue_to_hub(payload)
|
||||||
# target may have been retrieved with a :join query, so we need to reset the readonly status
|
# target may have been retrieved with a :join query, so we need to reset the readonly status
|
||||||
if can_cancel?
|
if can_cancel?
|
||||||
target.send(:instance_variable_set, :@readonly, false)
|
target.send(:instance_variable_set, :@readonly, false)
|
||||||
|
@ -41,11 +36,9 @@ module Travis
|
||||||
end
|
end
|
||||||
|
|
||||||
def push_matrix(payload)
|
def push_matrix(payload)
|
||||||
|
|
||||||
target.matrix.each do |job|
|
target.matrix.each do |job|
|
||||||
push(payload, job)
|
push(payload, job)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(payload, job)
|
def push(payload, job)
|
||||||
|
|
|
@ -63,6 +63,16 @@ describe 'Builds' do
|
||||||
response = post "/builds/#{build.id}/cancel", {}, headers
|
response = post "/builds/#{build.id}/cancel", {}, headers
|
||||||
response.status.should == 403
|
response.status.should == 403
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'and enqueues cancel event for the Hub' do
|
||||||
|
before { Travis::Features.activate_owner(:enqueue_to_hub, repo.owner) }
|
||||||
|
|
||||||
|
it 'responds with 403' do
|
||||||
|
response = post "/builds/#{build.id}/cancel", {}, headers
|
||||||
|
response.status.should == 403
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when build is not cancelable' do
|
context 'when build is not cancelable' do
|
||||||
|
@ -72,23 +82,55 @@ describe 'Builds' do
|
||||||
response = post "/builds/#{build.id}/cancel", {}, headers
|
response = post "/builds/#{build.id}/cancel", {}, headers
|
||||||
response.status.should == 422
|
response.status.should == 422
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'and enqueues cancel event for the Hub' do
|
||||||
|
before { Travis::Features.activate_owner(:enqueue_to_hub, repo.owner) }
|
||||||
|
|
||||||
|
it 'responds with 422' do
|
||||||
|
response = post "/builds/#{build.id}/cancel", {}, headers
|
||||||
|
response.status.should == 422
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when build can be canceled' do
|
context 'when build can be canceled' do
|
||||||
before do
|
before do
|
||||||
Travis::Sidekiq::BuildCancellation.stubs(:perform_async)
|
|
||||||
build.matrix.each { |j| j.update_attribute(:state, 'created') }
|
build.matrix.each { |j| j.update_attribute(:state, 'created') }
|
||||||
build.update_attribute(:state, 'created')
|
build.update_attribute(:state, 'created')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'cancels the build' do
|
context 'from the Core' do
|
||||||
Travis::Sidekiq::BuildCancellation.expects(:perform_async).with( id: build.id.to_s, user_id: user.id, source: 'api')
|
before { Travis::Sidekiq::BuildCancellation.stubs(:perform_async) }
|
||||||
post "/builds/#{build.id}/cancel", {}, headers
|
|
||||||
|
it 'cancels the build' do
|
||||||
|
Travis::Sidekiq::BuildCancellation.expects(:perform_async).with( id: build.id.to_s, user_id: user.id, source: 'api')
|
||||||
|
post "/builds/#{build.id}/cancel", {}, headers
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'responds with 204' do
|
||||||
|
response = post "/builds/#{build.id}/cancel", {}, headers
|
||||||
|
response.status.should == 204
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'responds with 204' do
|
context 'and enqueues cancel event for the Hub' do
|
||||||
response = post "/builds/#{build.id}/cancel", {}, headers
|
before { Travis::Features.activate_owner(:enqueue_to_hub, repo.owner) }
|
||||||
response.status.should == 204
|
|
||||||
|
before do
|
||||||
|
build.matrix.each { |j| j.update_attribute(:state, 'created') }
|
||||||
|
build.update_attribute(:state, 'created')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'cancels the build' do
|
||||||
|
::Sidekiq::Client.expects(:push).times(4)
|
||||||
|
post "/builds/#{build.id}/cancel", {}, headers
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'responds with 204' do
|
||||||
|
::Sidekiq::Client.expects(:push).times(4)
|
||||||
|
response = post "/builds/#{build.id}/cancel", {}, headers
|
||||||
|
response.status.should == 204
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user