summaryrefslogtreecommitdiff
path: root/app/helpers/submodule_helper.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-01-09 10:41:08 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-01-09 10:41:08 +0000
commit66f47187da83f122b48b21ff1a8096e0d9f9e7fd (patch)
treea966c43b1a973a61d362dc7077e24ed12cb52ce3 /app/helpers/submodule_helper.rb
parent877eefdb6d765fd9fd437b8328ecbe00cb07438a (diff)
downloadgitlab-ce-66f47187da83f122b48b21ff1a8096e0d9f9e7fd.tar.gz
Add latest changes from gitlab-org/security/gitlab@15-7-stable-ee
Diffstat (limited to 'app/helpers/submodule_helper.rb')
-rw-r--r--app/helpers/submodule_helper.rb78
1 files changed, 44 insertions, 34 deletions
diff --git a/app/helpers/submodule_helper.rb b/app/helpers/submodule_helper.rb
index e3e2f423da3..c38d69df8e4 100644
--- a/app/helpers/submodule_helper.rb
+++ b/app/helpers/submodule_helper.rb
@@ -17,40 +17,26 @@ module SubmoduleHelper
url = File.join(Gitlab.config.gitlab.url, repository.project.full_path)
end
- if url =~ %r{([^/:]+)/([^/]+(?:\.git)?)\Z}
- namespace = Regexp.last_match(1)
- project = Regexp.last_match(2)
- gitlab_hosts = [Gitlab.config.gitlab.url,
- Gitlab.config.gitlab_shell.ssh_path_prefix]
-
- gitlab_hosts.each do |host|
- if url.start_with?(host)
- namespace, _, project = url.sub(host, '').rpartition('/')
- break
- end
- end
-
- namespace.delete_prefix!('/')
- project.rstrip!
- project.delete_suffix!('.git')
-
- if self_url?(url, namespace, project)
- [
- url_helpers.namespace_project_path(namespace, project),
- url_helpers.namespace_project_tree_path(namespace, project, submodule_item_id),
- (url_helpers.namespace_project_compare_path(namespace, project, to: submodule_item_id, from: old_submodule_item_id) if old_submodule_item_id)
- ]
- elsif relative_self_url?(url)
- relative_self_links(url, submodule_item_id, old_submodule_item_id, repository.project)
- elsif gist_github_dot_com_url?(url)
- gist_github_com_tree_links(namespace, project, submodule_item_id)
- elsif github_dot_com_url?(url)
- github_com_tree_links(namespace, project, submodule_item_id, old_submodule_item_id)
- elsif gitlab_dot_com_url?(url)
- gitlab_com_tree_links(namespace, project, submodule_item_id, old_submodule_item_id)
- else
- [sanitize_submodule_url(url), nil, nil]
- end
+ namespace, project = extract_namespace_project(url)
+
+ if namespace.blank? || project.blank?
+ return [sanitize_submodule_url(url), nil, nil]
+ end
+
+ if self_url?(url, namespace, project)
+ [
+ url_helpers.namespace_project_path(namespace, project),
+ url_helpers.namespace_project_tree_path(namespace, project, submodule_item_id),
+ (url_helpers.namespace_project_compare_path(namespace, project, to: submodule_item_id, from: old_submodule_item_id) if old_submodule_item_id)
+ ]
+ elsif relative_self_url?(url)
+ relative_self_links(url, submodule_item_id, old_submodule_item_id, repository.project)
+ elsif gist_github_dot_com_url?(url)
+ gist_github_com_tree_links(namespace, project, submodule_item_id)
+ elsif github_dot_com_url?(url)
+ github_com_tree_links(namespace, project, submodule_item_id, old_submodule_item_id)
+ elsif gitlab_dot_com_url?(url)
+ gitlab_com_tree_links(namespace, project, submodule_item_id, old_submodule_item_id)
else
[sanitize_submodule_url(url), nil, nil]
end
@@ -58,6 +44,30 @@ module SubmoduleHelper
protected
+ def extract_namespace_project(url)
+ namespace_fragment, _, project = url.rpartition('/')
+ namespace = namespace_fragment.rpartition(%r{[:/]}).last
+
+ return [nil, nil] unless project.present? && namespace.present?
+
+ gitlab_hosts = [Gitlab.config.gitlab.url,
+ Gitlab.config.gitlab_shell.ssh_path_prefix]
+
+ matching_host = gitlab_hosts.find do |host|
+ url.start_with?(host)
+ end
+
+ if matching_host
+ namespace, _, project = url.delete_prefix(matching_host).rpartition('/')
+ end
+
+ namespace.delete_prefix!('/')
+ project.rstrip!
+ project.delete_suffix!('.git')
+
+ [namespace, project]
+ end
+
def gist_github_dot_com_url?(url)
url =~ %r{gist\.github\.com[/:][^/]+/[^/]+\Z}
end