debian-mirror-gitlab/lib/gitlab/template/finders/repo_template_finder.rb

68 lines
1.9 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-12-05 23:21:45 +05:30
# Searches and reads files present on each GitLab project repository
2016-09-13 17:45:13 +05:30
module Gitlab
module Template
module Finders
class RepoTemplateFinder < BaseTemplateFinder
# Raised when file is not found
2017-08-17 22:00:37 +05:30
FileNotFoundError = Class.new(StandardError)
2016-09-13 17:45:13 +05:30
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
def read(path)
blob = @repository.blob_at(@commit.id, path) if @commit
raise FileNotFoundError if blob.nil?
2018-03-17 18:26:18 +05:30
2016-09-13 17:45:13 +05:30
blob.data
end
def find(key)
file_name = "#{key}#{@extension}"
2018-12-13 13:39:08 +05:30
# The key is untrusted input, so ensure we can't be directed outside
# of base_dir inside the repository
Gitlab::Utils.check_path_traversal!(file_name)
2016-09-13 17:45:13 +05:30
directory = select_directory(file_name)
raise FileNotFoundError if directory.nil?
2018-11-20 20:47:30 +05:30
File.join(category_directory(directory), file_name)
2016-09-13 17:45:13 +05:30
end
def list_files_for(dir)
return [] unless @commit
2019-02-15 15:39:39 +05:30
dir = "#{dir}/" unless dir.end_with?('/')
2016-09-13 17:45:13 +05:30
entries = @repository.tree(:head, dir).entries
2018-11-20 20:47:30 +05:30
paths = entries.map(&:path)
paths.select { |f| f =~ self.class.filter_regex(@extension) }
2016-09-13 17:45:13 +05:30
end
private
def select_directory(file_name)
return [] unless @commit
# Insert root as directory
2018-11-20 20:47:30 +05:30
directories = ["", *@categories.keys]
2016-09-13 17:45:13 +05:30
directories.find do |category|
2018-11-20 20:47:30 +05:30
path = File.join(category_directory(category), file_name)
2016-09-13 17:45:13 +05:30
@repository.blob_at(@commit.id, path)
end
end
end
end
end
end