summaryrefslogtreecommitdiff
path: root/app/services/ci/build_cancel_service.rb
blob: a23418ed7381bdebaa25d7196730c2c467653784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module Ci
  class BuildCancelService
    def initialize(build, user)
      @build = build
      @user = user
    end

    def execute
      return forbidden unless allowed?
      return unprocessable_entity unless build.cancelable?

      build.cancel

      ServiceResponse.success(payload: build)
    end

    private

    attr_reader :build, :user

    def allowed?
      user.can?(:update_build, build)
    end

    def forbidden
      ServiceResponse.error(message: 'Forbidden', http_status: :forbidden)
    end

    def unprocessable_entity
      ServiceResponse.error(message: 'Unprocessable entity', http_status: :unprocessable_entity)
    end
  end
end