diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-05 00:07:49 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-05 00:07:49 +0000 |
commit | 77237c5a6b9044f58beabc54d3589e5fa09cbfba (patch) | |
tree | f43188047fe8955f6cf78e05ae9c2e8f6a019e0b /lib/gitlab/repo_path.rb | |
parent | 2fd92f2dc784ade9cb4e1c33dd60cbfad7b86818 (diff) | |
download | gitlab-ce-77237c5a6b9044f58beabc54d3589e5fa09cbfba.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/repo_path.rb')
-rw-r--r-- | lib/gitlab/repo_path.rb | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/lib/gitlab/repo_path.rb b/lib/gitlab/repo_path.rb index e8c749cac14..da74c38d0ec 100644 --- a/lib/gitlab/repo_path.rb +++ b/lib/gitlab/repo_path.rb @@ -19,22 +19,33 @@ module Gitlab # Removing the suffix (.wiki, .design, ...) from the project path full_path = repo_path.chomp(type.path_suffix) - - project, was_redirected = find_project(full_path) + container, project, was_redirected = find_container(type, full_path) redirected_path = repo_path if was_redirected - # If we found a matching project, then the type was matched, no need to - # continue looking. - return [project, type, redirected_path] if project + return [container, project, type, redirected_path] if container end # 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. - [nil, Gitlab::GlRepository.default_type, nil] + [nil, nil, Gitlab::GlRepository.default_type, nil] + end + + def self.find_container(type, full_path) + if type.snippet? + snippet, was_redirected = find_snippet(full_path) + + [snippet, snippet&.project, was_redirected] + else + project, was_redirected = find_project(full_path) + + [project, project, was_redirected] + end end def self.find_project(project_path) + return [nil, false] if project_path.blank? + project = Project.find_by_full_path(project_path, follow_redirects: true) [project, redirected?(project, project_path)] @@ -43,6 +54,27 @@ module Gitlab def self.redirected?(project, project_path) project && project.full_path.casecmp(project_path) != 0 end + + # Snippet_path can be either: + # - snippets/1 + # - h5bp/html5-boilerplate/snippets/53 + def self.find_snippet(snippet_path) + return [nil, false] if snippet_path.blank? + + snippet_id, project_path = extract_snippet_info(snippet_path) + project, was_redirected = find_project(project_path) + + [Snippet.find_by_id_and_project(id: snippet_id, project: project), was_redirected] + end + + def self.extract_snippet_info(snippet_path) + path_segments = snippet_path.split('/') + snippet_id = path_segments.pop + path_segments.pop # Remove snippets from path + project_path = File.join(path_segments) + + [snippet_id, project_path] + end end end |