2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-24 11:54:22 -04:00
|
|
|
module Gitlab
|
|
|
|
module RepoPath
|
|
|
|
NotFoundError = Class.new(StandardError)
|
|
|
|
|
2020-12-15 10:09:59 -05:00
|
|
|
# Returns an array containing:
|
|
|
|
# - The repository container
|
|
|
|
# - The related project (if available)
|
|
|
|
# - The repository type
|
|
|
|
# - The original container path (if redirected)
|
|
|
|
#
|
|
|
|
# @returns [HasRepository, Project, String, String]
|
2020-01-27 10:08:51 -05:00
|
|
|
def self.parse(path)
|
2020-12-01 07:09:17 -05:00
|
|
|
repo_path = path.delete_prefix('/').delete_suffix('.git')
|
2018-05-11 07:49:52 -04:00
|
|
|
|
2019-03-18 12:51:11 -04:00
|
|
|
# Detect the repo type based on the path, the first one tried is the project
|
|
|
|
# type, which does not have a suffix.
|
|
|
|
Gitlab::GlRepository.types.each do |_name, type|
|
|
|
|
# If the project path does not end with the defined suffix, try the next
|
|
|
|
# type.
|
|
|
|
# We'll always try to find a project with an empty suffix (for the
|
|
|
|
# `Gitlab::GlRepository::PROJECT` type.
|
2020-01-27 10:08:51 -05:00
|
|
|
next unless type.valid?(repo_path)
|
2019-03-18 12:51:11 -04:00
|
|
|
|
2020-06-17 11:08:36 -04:00
|
|
|
# Removing the suffix (.wiki, .design, ...) from the project path
|
2020-01-27 10:08:51 -05:00
|
|
|
full_path = repo_path.chomp(type.path_suffix)
|
2021-06-22 11:06:55 -04:00
|
|
|
container, project = find_container(type, full_path)
|
|
|
|
next unless container
|
2019-03-18 12:51:11 -04:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
redirected_path = repo_path if redirected?(container, repo_path)
|
|
|
|
return [container, project, type, redirected_path]
|
2017-04-28 12:52:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-05 06:51:42 -04:00
|
|
|
# When a project did not exist, the parsed repo_type would be empty.
|
|
|
|
# In that case, we want to continue with a regular project repository. As we
|
|
|
|
# could create the project if the user pushing is allowed to do so.
|
2020-03-04 19:07:49 -05:00
|
|
|
[nil, nil, Gitlab::GlRepository.default_type, nil]
|
|
|
|
end
|
|
|
|
|
2020-12-15 10:09:59 -05:00
|
|
|
# Returns an array containing:
|
|
|
|
# - The repository container
|
|
|
|
# - The related project (if available)
|
|
|
|
#
|
|
|
|
# @returns [HasRepository, Project, String]
|
2020-03-04 19:07:49 -05:00
|
|
|
def self.find_container(type, full_path)
|
2021-06-22 11:06:55 -04:00
|
|
|
return [nil, nil] if full_path.blank?
|
2020-12-15 10:09:59 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
if type.snippet?
|
2021-06-22 11:06:55 -04:00
|
|
|
snippet = find_snippet(full_path)
|
2020-03-04 19:07:49 -05:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
[snippet, snippet&.project]
|
2020-10-05 11:08:56 -04:00
|
|
|
elsif type.wiki?
|
2021-06-22 11:06:55 -04:00
|
|
|
wiki = find_wiki(full_path)
|
2020-10-05 11:08:56 -04:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
[wiki, wiki.try(:project)]
|
2020-03-04 19:07:49 -05:00
|
|
|
else
|
2021-06-22 11:06:55 -04:00
|
|
|
project = find_project(full_path)
|
2020-03-04 19:07:49 -05:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
[project, project]
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
2017-04-28 12:52:09 -04:00
|
|
|
end
|
|
|
|
|
2020-06-17 11:08:36 -04:00
|
|
|
def self.find_project(project_path)
|
2021-06-22 11:06:55 -04:00
|
|
|
Project.find_by_full_path(project_path, follow_redirects: true)
|
2019-12-27 10:08:16 -05:00
|
|
|
end
|
|
|
|
|
2020-12-15 10:09:59 -05:00
|
|
|
def self.redirected?(container, container_path)
|
|
|
|
container && container.full_path.casecmp(container_path) != 0
|
2017-06-15 20:03:54 -04:00
|
|
|
end
|
2020-03-04 19:07:49 -05:00
|
|
|
|
|
|
|
# Snippet_path can be either:
|
|
|
|
# - snippets/1
|
|
|
|
# - h5bp/html5-boilerplate/snippets/53
|
|
|
|
def self.find_snippet(snippet_path)
|
|
|
|
snippet_id, project_path = extract_snippet_info(snippet_path)
|
2021-06-22 11:06:55 -04:00
|
|
|
return unless snippet_id
|
2020-12-15 10:09:59 -05:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
project = find_project(project_path) if project_path
|
2020-03-04 19:07:49 -05:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
Snippet.find_by_id_and_project(id: snippet_id, project: project)
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
# Wiki path can be either:
|
|
|
|
# - namespace/project
|
|
|
|
# - group/subgroup/project
|
2020-12-15 10:09:59 -05:00
|
|
|
#
|
|
|
|
# And also in EE:
|
|
|
|
# - group
|
|
|
|
# - group/subgroup
|
|
|
|
def self.find_wiki(container_path)
|
|
|
|
container = Routable.find_by_full_path(container_path, follow_redirects: true)
|
|
|
|
|
|
|
|
# In CE, Group#wiki is not available so this will return nil for a group path.
|
2021-06-22 11:06:55 -04:00
|
|
|
container&.try(:wiki)
|
2020-10-05 11:08:56 -04:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
def self.extract_snippet_info(snippet_path)
|
|
|
|
path_segments = snippet_path.split('/')
|
|
|
|
snippet_id = path_segments.pop
|
2020-12-15 10:09:59 -05:00
|
|
|
path_segments.pop # Remove 'snippets' from path
|
|
|
|
project_path = File.join(path_segments).presence
|
2020-03-04 19:07:49 -05:00
|
|
|
|
|
|
|
[snippet_id, project_path]
|
|
|
|
end
|
2017-03-24 11:54:22 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::RepoPath.singleton_class.prepend_mod_with('Gitlab::RepoPath::ClassMethods')
|