Move code for playing an action to separate service

This commit is contained in:
Grzegorz Bizon 2017-04-12 11:46:24 +02:00
parent b09465f38d
commit 6c6bc400d1
3 changed files with 21 additions and 13 deletions

View file

@ -122,18 +122,9 @@ module Ci
end
def play(current_user)
unless can_play?(current_user)
raise Gitlab::Access::AccessDeniedError
end
# Try to queue a current build
if self.enqueue
self.update(user: current_user)
self
else
# Otherwise we need to create a duplicate
Ci::Build.retry(self, current_user)
end
Ci::PlayBuildService
.new(project, current_user)
.execute(self)
end
def cancelable?

View file

@ -17,7 +17,7 @@ module Ci
alias_method :build, :subject
def can_play_action?
return false unless build.playable?
return false unless build.action?
::Gitlab::UserAccess
.new(user, project: build.project)

View file

@ -0,0 +1,17 @@
module Ci
class PlayBuildService < ::BaseService
def execute(build)
unless can?(current_user, :play_build, build)
raise Gitlab::Access::AccessDeniedError
end
# Try to enqueue thebuild, otherwise create a duplicate.
#
if build.enqueue
build.tap { |action| action.update(user: current_user) }
else
Ci::Build.retry(build, current_user)
end
end
end
end