2021-07-01 15:07:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
module JobTokenScope
|
|
|
|
class AddProjectService < ::BaseService
|
2021-07-01 18:07:29 +00:00
|
|
|
include EditScopeValidations
|
2021-07-01 15:07:40 +00:00
|
|
|
|
|
|
|
def execute(target_project)
|
2021-07-01 18:07:29 +00:00
|
|
|
validate_edit!(project, target_project, current_user)
|
2021-07-01 15:07:40 +00:00
|
|
|
|
|
|
|
link = add_project!(target_project)
|
|
|
|
ServiceResponse.success(payload: { project_link: link })
|
|
|
|
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
ServiceResponse.error(message: "Target project is already in the job token scope")
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
ServiceResponse.error(message: e.message)
|
2021-07-01 18:07:29 +00:00
|
|
|
rescue EditScopeValidations::ValidationError => e
|
|
|
|
ServiceResponse.error(message: e.message)
|
2021-07-01 15:07:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_project!(target_project)
|
|
|
|
::Ci::JobToken::ProjectScopeLink.create!(
|
|
|
|
source_project: project,
|
|
|
|
target_project: target_project,
|
|
|
|
added_by: current_user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|