summaryrefslogtreecommitdiff
path: root/app/services/ci/job_token_scope/add_project_service.rb
blob: d03ae434b69a750406b25abdc90e913da0d6cd06 (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
# frozen_string_literal: true

module Ci
  module JobTokenScope
    class AddProjectService < ::BaseService
      include EditScopeValidations

      def execute(target_project)
        validate_edit!(project, target_project, current_user)

        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)
      rescue EditScopeValidations::ValidationError => e
        ServiceResponse.error(message: e.message)
      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