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

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

      def execute(target_project, direction: :outbound)
        validate_edit!(project, target_project, current_user)

        if project == target_project
          return ServiceResponse.error(message: "Source project cannot be removed from the job token scope")
        end

        link = ::Ci::JobToken::ProjectScopeLink
          .with_access_direction(direction)
          .for_source_and_target(project, target_project)

        unless link
          return ServiceResponse.error(message: "Target project is not in the job token scope")
        end

        if link.destroy
          ServiceResponse.success
        else
          ServiceResponse.error(message: link.errors.full_messages.to_sentence, payload: { project_link: link })
        end
      rescue EditScopeValidations::ValidationError => e
        ServiceResponse.error(message: e.message)
      end
    end
  end
end