Class: Ci::Catalog::ComponentsProject

Inherits:
Object
  • Object
show all
Defined in:
app/models/ci/catalog/components_project.rb

Overview

This class represents a project that contains one or more CI/CD components. It is responsible for retrieving the data of a component file.

Defined Under Namespace

Classes: ComponentData

Constant Summary collapse

TEMPLATE_FILE =
'template.yml'
TEMPLATES_DIR =
'templates'
TEMPLATE_PATH_REGEX =
'^templates\/[\w-]+(?:\/template)?\.yml$'
COMPONENTS_LIMIT =
30

Instance Method Summary collapse

Constructor Details

#initialize(project, sha = project&.commit&.sha) ⇒ ComponentsProject

Returns a new instance of ComponentsProject.



15
16
17
18
# File 'app/models/ci/catalog/components_project.rb', line 15

def initialize(project, sha = project&.commit&.sha)
  @project = project
  @sha = sha
end

Instance Method Details

#extract_component_name(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/ci/catalog/components_project.rb', line 24

def extract_component_name(path)
  return unless path.match?(TEMPLATE_PATH_REGEX)

  dirname = File.dirname(path)
  filename = File.basename(path, '.*')

  if dirname == TEMPLATES_DIR
    filename
  else
    File.basename(dirname)
  end
end

#extract_spec(blob) ⇒ Object

Raises:

  • (result.error_class)


37
38
39
40
41
42
43
# File 'app/models/ci/catalog/components_project.rb', line 37

def extract_spec(blob)
  result = Gitlab::Ci::Config::Yaml::Loader.new(blob).load_uninterpolated_yaml

  raise result.error_class, result.error unless result.valid?

  result.spec
end

#fetch_component(component_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/ci/catalog/components_project.rb', line 45

def fetch_component(component_name)
  return ComponentData.new unless component_name.index('/').nil?

  path = simple_template_path(component_name)
  content = fetch_content(path)

  if content.nil?
    path = complex_template_path(component_name)
    content = fetch_content(path)
  end

  ComponentData.new(content: content, path: path)
end

#fetch_component_paths(ref, limit: COMPONENTS_LIMIT) ⇒ Object



20
21
22
# File 'app/models/ci/catalog/components_project.rb', line 20

def fetch_component_paths(ref, limit: COMPONENTS_LIMIT)
  project.repository.search_files_by_regexp(TEMPLATE_PATH_REGEX, ref, limit: limit)
end

#find_catalog_component(component_name) ⇒ Object

TODO: This may retrieve the wrong component object if a simple and a complex component have the same name for the given catalog resource version. We should complete gitlab.com/gitlab-org/gitlab/-/issues/450737 to ensure unique component names.



62
63
64
65
66
67
68
# File 'app/models/ci/catalog/components_project.rb', line 62

def find_catalog_component(component_name)
  # Multiple versions of a project can have the same sha, so we return the latest one.
  version = project.catalog_resource_versions.by_sha(sha).latest
  return unless version

  version.components.template.find_by_name(component_name)
end