Enqueue cancel message on build cancel v3 endpoint

This commit is contained in:
Ana Rosas 2016-06-14 15:15:59 +02:00
parent 75460b2427
commit e097c8889e
3 changed files with 133 additions and 0 deletions

View File

@ -1,6 +1,25 @@
module Travis::API::V3 module Travis::API::V3
class Models::Permission < Model class Models::Permission < Model
ROLES = %w(admin push pull)
belongs_to :user belongs_to :user
belongs_to :repository belongs_to :repository
class << self
def by_roles(roles)
roles = Array(roles).select { |role| ROLES.include?(role.to_s) }
roles.empty? ? none : where(has_roles(roles))
end
def has_roles(roles)
roles.inject(has_role(roles.shift)) do |sql, role|
sql.or(has_role(role))
end
end
def has_role(role)
arel_table[role].eq(true)
end
end
end end
end end

View File

@ -22,5 +22,13 @@ module Travis::API::V3
def starred_repository_ids def starred_repository_ids
@starred_repository_ids ||= stars.map(&:repository_id) @starred_repository_ids ||= stars.map(&:repository_id)
end end
def permission?(roles, options = {})
roles, options = nil, roles if roles.is_a?(Hash)
scope = permissions.where(options)
scope = scope.by_roles(roles) if roles
scope.any?
end
end end
end end

View File

@ -8,6 +8,7 @@ describe Travis::API::V3::Services::Build::Cancel do
before do before do
Travis::Features.stubs(:owner_active?).returns(true) Travis::Features.stubs(:owner_active?).returns(true)
Travis::Features.stubs(:owner_active?).with(:enqueue_to_hub, repo.owner).returns(false)
@original_sidekiq = Sidekiq::Client @original_sidekiq = Sidekiq::Client
Sidekiq.send(:remove_const, :Client) # to avoid a warning Sidekiq.send(:remove_const, :Client) # to avoid a warning
Sidekiq::Client = [] Sidekiq::Client = []
@ -227,6 +228,111 @@ describe Travis::API::V3::Services::Build::Cancel do
end end
end end
describe "existing repository, push & pull access, cancelable, enqueues message for Hub" do
let(:params) {{}}
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
before do
Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, push: true, pull: true)
Travis::Features.stubs(:owner_active?).with(:enqueue_to_hub, repo.owner).returns(true)
end
describe "started state" do
before { build.update_attribute(:state, "started") }
before { post("/v3/build/#{build.id}/cancel", params, headers) }
example { expect(last_response.status).to be == 202 }
example { expect(JSON.load(body).to_s).to include(
"@type",
"build",
"@href",
"@representation",
"minimal",
"cancel",
"id",
"state_change")
}
example { expect(sidekiq_payload).to be == {
"id" => "#{build.id}",
"user_id"=> repo.owner_id,
"source" => "api",
"type" => "build"}
}
example { expect(Sidekiq::Client.last['queue']).to be == 'hub' }
example { expect(Sidekiq::Client.last['class']).to be == 'Travis::Hub::Sidekiq::Worker' }
end
describe "queued state" do
before { build.update_attribute(:state, "queued") }
before { post("/v3/build/#{build.id}/cancel", params, headers) }
example { expect(last_response.status).to be == 202 }
example { expect(JSON.load(body).to_s).to include(
"@type",
"build",
"@href",
"@representation",
"minimal",
"cancel",
"id",
"state_change")
}
example { expect(sidekiq_payload).to be == {
"id" => "#{build.id}",
"user_id"=> repo.owner_id,
"source" => "api",
"type" => "build"}
}
example { expect(Sidekiq::Client.last['queue']).to be == 'hub' }
example { expect(Sidekiq::Client.last['class']).to be == 'Travis::Hub::Sidekiq::Worker' }
end
describe "received state" do
before { build.update_attribute(:state, "received") }
before { post("/v3/build/#{build.id}/cancel", params, headers) }
example { expect(last_response.status).to be == 202 }
example { expect(JSON.load(body).to_s).to include(
"@type",
"build",
"@href",
"@representation",
"minimal",
"cancel",
"id",
"state_change")
}
example { expect(sidekiq_payload).to be == {
"id" => "#{build.id}",
"user_id"=> repo.owner_id,
"source" => "api",
"type" => "build"}
}
example { expect(Sidekiq::Client.last['queue']).to be == 'hub' }
example { expect(Sidekiq::Client.last['class']).to be == 'Travis::Hub::Sidekiq::Worker' }
end
describe "setting id has no effect" do
let(:params) {{ id: 42 }}
before { post("/v3/build/#{build.id}/cancel", params, headers) }
example { expect(sidekiq_payload).to be == {
"id" => "#{build.id}",
"user_id"=> repo.owner_id,
"source" => "api",
"type" => "build"}
}
end
end
# TODO decided to discuss further with rkh as this use case doesn't really exist at the moment # TODO decided to discuss further with rkh as this use case doesn't really exist at the moment
# and 'fixing' the query requires modifying workers that v2 uses, thereby running the risk of breaking v2, # and 'fixing' the query requires modifying workers that v2 uses, thereby running the risk of breaking v2,
# and also because in 6 months or so travis-hub will be able to cancel builds without using travis-core at all. # and also because in 6 months or so travis-hub will be able to cancel builds without using travis-core at all.