summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/project_config/external_project.rb
blob: b29825514585cbd4e85ce36e84ce60c52a2000f9 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

module Gitlab
  module Ci
    class ProjectConfig
      class ExternalProject < Source
        def content
          strong_memoize(:content) do
            next unless external_project_path?

            path_file, path_project, ref = extract_location_tokens

            config_location = { 'project' => path_project, 'file' => path_file }
            config_location['ref'] = ref if ref.present?

            YAML.dump('include' => [config_location])
          end
        end

        def contains_internal_include?
          true
        end

        def source
          :external_project_source
        end

        private

        # Example: path/to/.gitlab-ci.yml@another-group/another-project
        def external_project_path?
          ci_config_path =~ /\A.+(yml|yaml)@.+\z/
        end

        # Example: path/to/.gitlab-ci.yml@another-group/another-project:refname
        def extract_location_tokens
          path_file, path_project = ci_config_path.split('@', 2)

          if path_project.include? ":"
            project, ref = path_project.split(':', 2)
            [path_file, project, ref]
          else
            [path_file, path_project]
          end
        end
      end
    end
  end
end