Class: Gitlab::Template::Finders::RepoTemplateFinder

Inherits:
BaseTemplateFinder show all
Defined in:
lib/gitlab/template/finders/repo_template_finder.rb

Constant Summary collapse

FileNotFoundError =

Raised when file is not found

Class.new(StandardError)

Instance Method Summary collapse

Methods inherited from BaseTemplateFinder

#category_directory, filter_regex

Constructor Details

#initialize(project, base_dir, extension, categories = {}) ⇒ RepoTemplateFinder

Returns a new instance of RepoTemplateFinder.



11
12
13
14
15
16
17
18
# File 'lib/gitlab/template/finders/repo_template_finder.rb', line 11

def initialize(project, base_dir, extension, categories = {})
  @categories     = categories
  @extension      = extension
  @repository     = project&.repository
  @commit         = @repository.head_commit if @repository&.exists?

  super(base_dir)
end

Instance Method Details

#find(key) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/template/finders/repo_template_finder.rb', line 27

def find(key)
  file_name = "#{key}#{@extension}"

  # The key is untrusted input, so ensure we can't be directed outside
  # of base_dir inside the repository
  Gitlab::PathTraversal.check_path_traversal!(file_name)

  directory = select_directory(file_name)
  raise FileNotFoundError if directory.nil?

  File.join(category_directory(directory), file_name)
end

#list_files_for(dir) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/template/finders/repo_template_finder.rb', line 40

def list_files_for(dir)
  return [] unless @commit

  dir = "#{dir}/" unless dir.end_with?('/')

  entries = @repository.tree(:head, dir).entries

  paths = entries.map(&:path)
  paths.select { |f| f =~ self.class.filter_regex(@extension) }
end

#read(path) ⇒ Object

Raises:



20
21
22
23
24
25
# File 'lib/gitlab/template/finders/repo_template_finder.rb', line 20

def read(path)
  blob = @repository.blob_at(@commit.id, path) if @commit
  raise FileNotFoundError if blob.nil?

  blob.data
end