summaryrefslogtreecommitdiff
path: root/app/services/ci/play_build_service.rb
blob: 2d6b6aeee14cd0a044fa48466d18dbdd00e00696 (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
36
# frozen_string_literal: true

module Ci
  class PlayBuildService < ::BaseService
    def execute(build, job_variables_attributes = nil)
      check_access!(build, job_variables_attributes)

      # Try to enqueue the build, otherwise create a duplicate.
      #
      if build.enqueue
        build.tap do |build|
          build.update!(user: current_user, job_variables_attributes: job_variables_attributes || [])

          AfterRequeueJobService.new(project, current_user).execute(build)
        end
      else
        # Retrying in Ci::PlayBuildService is a legacy process that should be removed.
        # Instead, callers should explicitly execute Ci::RetryBuildService.
        # See https://gitlab.com/gitlab-org/gitlab/-/issues/347493.
        build.retryable? ? Ci::Build.retry(build, current_user) : build
      end
    end

    private

    def check_access!(build, job_variables_attributes)
      raise Gitlab::Access::AccessDeniedError unless can?(current_user, :play_job, build)

      if job_variables_attributes.present? && !can?(current_user, :set_pipeline_variables, project)
        raise Gitlab::Access::AccessDeniedError
      end
    end
  end
end

Ci::PlayBuildService.prepend_mod_with('Ci::PlayBuildService')