From 22e9af3c8b8aedf7f46b786be968862b74a2d07e Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 27 Jan 2020 12:08:35 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- lib/banzai/filter/repository_link_filter.rb | 9 +- lib/gitlab/auth/saml/auth_hash.rb | 2 +- lib/gitlab/ci/pipeline/chain/populate.rb | 11 +- lib/gitlab/dependency_linker/godeps_json_linker.rb | 6 +- lib/gitlab/middleware/go.rb | 2 +- lib/gitlab/shell.rb | 159 ++++++++++++++------- 6 files changed, 134 insertions(+), 55 deletions(-) (limited to 'lib') diff --git a/lib/banzai/filter/repository_link_filter.rb b/lib/banzai/filter/repository_link_filter.rb index 14cd607cc50..d448238c6e4 100644 --- a/lib/banzai/filter/repository_link_filter.rb +++ b/lib/banzai/filter/repository_link_filter.rb @@ -101,11 +101,18 @@ module Banzai def rebuild_relative_uri(uri) file_path = nested_file_path_if_exists(uri) + resource_type = uri_type(file_path) + + # Repository routes are under /-/ scope now. + # Since we craft a path without using route helpers we must + # ensure - is added here. + prefix = '-' if %w(tree blob raw commits).include?(resource_type.to_s) uri.path = [ relative_url_root, project.full_path, - uri_type(file_path), + prefix, + resource_type, Addressable::URI.escape(ref).gsub('#', '%23'), Addressable::URI.escape(file_path) ].compact.join('/').squeeze('/').chomp('/') diff --git a/lib/gitlab/auth/saml/auth_hash.rb b/lib/gitlab/auth/saml/auth_hash.rb index b0df9757bbd..a2b0dfd5c66 100644 --- a/lib/gitlab/auth/saml/auth_hash.rb +++ b/lib/gitlab/auth/saml/auth_hash.rb @@ -28,7 +28,7 @@ module Gitlab end def extract_authn_context(document) - REXML::XPath.first(document, "//*[name()='saml:AuthnStatement' or name()='saml2:AuthnStatement']/*[name()='saml:AuthnContext' or name()='saml2:AuthnContext']/*[name()='saml:AuthnContextClassRef' or name()='saml2:AuthnContextClassRef']/text()").to_s + REXML::XPath.first(document, "//*[name()='saml:AuthnStatement' or name()='saml2:AuthnStatement' or name()='AuthnStatement']/*[name()='saml:AuthnContext' or name()='saml2:AuthnContext' or name()='AuthnContext']/*[name()='saml:AuthnContextClassRef' or name()='saml2:AuthnContextClassRef' or name()='AuthnContextClassRef']/text()").to_s end end end diff --git a/lib/gitlab/ci/pipeline/chain/populate.rb b/lib/gitlab/ci/pipeline/chain/populate.rb index 3a40c7b167c..f9ae37aa273 100644 --- a/lib/gitlab/ci/pipeline/chain/populate.rb +++ b/lib/gitlab/ci/pipeline/chain/populate.rb @@ -17,7 +17,7 @@ module Gitlab # pipeline.stages = @command.stage_seeds.map(&:to_resource) - if pipeline.stages.none? + if stage_names.empty? return error('No stages / jobs for this pipeline.') end @@ -31,6 +31,15 @@ module Gitlab def break? pipeline.errors.any? end + + private + + def stage_names + # We filter out `.pre/.post` stages, as they alone are not considered + # a complete pipeline: + # https://gitlab.com/gitlab-org/gitlab/issues/198518 + pipeline.stages.map(&:name) - ::Gitlab::Ci::Config::EdgeStagesInjector::EDGES + end end end end diff --git a/lib/gitlab/dependency_linker/godeps_json_linker.rb b/lib/gitlab/dependency_linker/godeps_json_linker.rb index d24c137793e..9166e9091ac 100644 --- a/lib/gitlab/dependency_linker/godeps_json_linker.rb +++ b/lib/gitlab/dependency_linker/godeps_json_linker.rb @@ -12,10 +12,12 @@ module Gitlab def link_dependencies link_json('ImportPath') do |path| case path + when %r{\A(?github\.com/#{REPO_REGEX})/(?.+)\z} + "https://#{$~[:repo]}/tree/master/#{$~[:path]}" when %r{\A(?gitlab\.com/#{NESTED_REPO_REGEX})\.git/(?.+)\z}, - %r{\A(?git(lab|hub)\.com/#{REPO_REGEX})/(?.+)\z} + %r{\A(?gitlab\.com/#{REPO_REGEX})/(?.+)\z} - "https://#{$~[:repo]}/tree/master/#{$~[:path]}" + "https://#{$~[:repo]}/-/tree/master/#{$~[:path]}" when /\Agolang\.org/ "https://godoc.org/#{path}" else diff --git a/lib/gitlab/middleware/go.rb b/lib/gitlab/middleware/go.rb index f207d91235f..53508938c49 100644 --- a/lib/gitlab/middleware/go.rb +++ b/lib/gitlab/middleware/go.rb @@ -60,7 +60,7 @@ module Gitlab end meta_import_tag = tag :meta, name: 'go-import', content: "#{import_prefix} git #{repository_url}" - meta_source_tag = tag :meta, name: 'go-source', content: "#{import_prefix} #{project_url} #{project_url}/tree/#{branch}{/dir} #{project_url}/blob/#{branch}{/dir}/{file}#L{line}" + meta_source_tag = tag :meta, name: 'go-source', content: "#{import_prefix} #{project_url} #{project_url}/-/tree/#{branch}{/dir} #{project_url}/-/blob/#{branch}{/dir}/{file}#L{line}" head_tag = content_tag :head, meta_import_tag + meta_source_tag html_tag = content_tag :html, head_tag + body_tag [html_tag, 200] diff --git a/lib/gitlab/shell.rb b/lib/gitlab/shell.rb index 290c4cff329..726ecd81824 100644 --- a/lib/gitlab/shell.rb +++ b/lib/gitlab/shell.rb @@ -11,18 +11,26 @@ module Gitlab Error = Class.new(StandardError) class << self + # Retrieve GitLab Shell secret token + # + # @return [String] secret token def secret_token @secret_token ||= begin File.read(Gitlab.config.gitlab_shell.secret_file).chomp end end + # Ensure gitlab shell has a secret token stored in the secret_file + # if that was never generated, generate a new one def ensure_secret_token! return if File.exist?(File.join(Gitlab.config.gitlab_shell.path, '.gitlab_shell_secret')) generate_and_link_secret_token end + # Returns required GitLab shell version + # + # @return [String] version from the manifest file def version_required @version_required ||= File.read(Rails.root .join('GITLAB_SHELL_VERSION')).strip @@ -48,24 +56,31 @@ module Gitlab end end - # Convenience methods for initializing a new repository with a Project model. + # Initialize a new project repository using a Project model + # + # @param [Project] project + # @return [Boolean] whether repository could be created def create_project_repository(project) create_repository(project.repository_storage, project.disk_path, project.full_path) end + # Initialize a new wiki repository using a Project model + # + # @param [Project] project + # @return [Boolean] whether repository could be created def create_wiki_repository(project) create_repository(project.repository_storage, project.wiki.disk_path, project.wiki.full_path) end # Init new repository # - # storage - the shard key - # disk_path - project disk path - # gl_project_path - project name - # - # Ex. + # @example Create a repository # create_repository("default", "path/to/gitlab-ci", "gitlab/gitlab-ci") # + # @param [String] storage the shard key + # @param [String] disk_path project path on disk + # @param [String] gl_project_path project name + # @return [Boolean] whether repository could be created def create_repository(storage, disk_path, gl_project_path) relative_path = disk_path.dup relative_path << '.git' unless relative_path.end_with?('.git') @@ -82,29 +97,39 @@ module Gitlab false end + # Import wiki repository from external service + # + # @param [Project] project + # @param [Gitlab::LegacyGithubImport::WikiFormatter, Gitlab::BitbucketImport::WikiFormatter] wiki_formatter + # @return [Boolean] whether repository could be imported def import_wiki_repository(project, wiki_formatter) import_repository(project.repository_storage, wiki_formatter.disk_path, wiki_formatter.import_url, project.wiki.full_path) end + # Import project repository from external service + # + # @param [Project] project + # @return [Boolean] whether repository could be imported def import_project_repository(project) import_repository(project.repository_storage, project.disk_path, project.import_url, project.full_path) end # Import repository # - # storage - project's storage name - # name - project disk path - # url - URL to import from - # - # Ex. - # import_repository("nfs-file06", "gitlab/gitlab-ci", "https://gitlab.com/gitlab-org/gitlab-test.git") + # @example Import a repository + # import_repository("nfs-file06", "gitlab/gitlab-ci", "https://gitlab.com/gitlab-org/gitlab-test.git", "gitlab/gitlab-ci") # - def import_repository(storage, name, url, gl_project_path) + # @param [String] storage project's storage name + # @param [String] disk_path project path on disk + # @param [String] url from external resource to import from + # @param [String] gl_project_path project name + # @return [Boolean] whether repository could be imported + def import_repository(storage, disk_path, url, gl_project_path) if url.start_with?('.', '/') raise Error.new("don't use disk paths with import_repository: #{url.inspect}") end - relative_path = "#{name}.git" + relative_path = "#{disk_path}.git" cmd = GitalyGitlabProjects.new(storage, relative_path, gl_project_path) success = cmd.import_project(url, git_timeout) @@ -113,27 +138,31 @@ module Gitlab success end - # storage - project's storage path - # path - project disk path - # new_path - new project disk path + # Move or rename a repository # - # Ex. + # @example Move/rename a repository # mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new") - def mv_repository(storage, path, new_path) - return false if path.empty? || new_path.empty? + # + # @param [String] storage project's storage path + # @param [String] disk_path current project path on disk + # @param [String] new_disk_path new project path on disk + # @return [Boolean] whether repository could be moved/renamed on disk + def mv_repository(storage, disk_path, new_disk_path) + return false if disk_path.empty? || new_disk_path.empty? - Gitlab::Git::Repository.new(storage, "#{path}.git", nil, nil).rename("#{new_path}.git") + Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).rename("#{new_disk_path}.git") true rescue => e - Gitlab::ErrorTracking.track_exception(e, path: path, new_path: new_path, storage: storage) + Gitlab::ErrorTracking.track_exception(e, path: disk_path, new_path: new_disk_path, storage: storage) false end # Fork repository to new path - # source_project - forked-from Project - # target_project - forked-to Project + # + # @param [Project] source_project forked-from Project + # @param [Project] target_project forked-to Project def fork_repository(source_project, target_project) forked_from_relative_path = "#{source_project.disk_path}.git" fork_args = [target_project.repository_storage, "#{target_project.disk_path}.git", target_project.full_path] @@ -145,29 +174,32 @@ module Gitlab # for rm_namespace. Given the underlying implementation removes the name # passed as second argument on the passed storage. # - # storage - project's storage path - # name - project disk path - # - # Ex. + # @example Remove a repository # remove_repository("/path/to/storage", "gitlab/gitlab-ci") - def remove_repository(storage, name) - return false if name.empty? + # + # @param [String] storage project's storage path + # @param [String] disk_path current project path on disk + def remove_repository(storage, disk_path) + return false if disk_path.empty? - Gitlab::Git::Repository.new(storage, "#{name}.git", nil, nil).remove + Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).remove true rescue => e - Rails.logger.warn("Repository does not exist: #{e} at: #{name}.git") # rubocop:disable Gitlab/RailsLogger - Gitlab::ErrorTracking.track_exception(e, path: name, storage: storage) + Rails.logger.warn("Repository does not exist: #{e} at: #{disk_path}.git") # rubocop:disable Gitlab/RailsLogger + Gitlab::ErrorTracking.track_exception(e, path: disk_path, storage: storage) false end # Add new key to authorized_keys # - # Ex. + # @example Add new key # add_key("key-42", "sha-rsa ...") # + # @param [String] key_id identifier of the key + # @param [String] key_content key content (public certificate) + # @return [Boolean] whether key could be added def add_key(key_id, key_content) return unless self.authorized_keys_enabled? @@ -176,39 +208,45 @@ module Gitlab # Batch-add keys to authorized_keys # - # Ex. + # @example # batch_add_keys(Key.all) + # + # @param [Array] keys + # @return [Boolean] whether keys could be added def batch_add_keys(keys) return unless self.authorized_keys_enabled? gitlab_authorized_keys.batch_add_keys(keys) end - # Remove ssh key from authorized_keys + # Remove SSH key from authorized_keys # - # Ex. + # @example Remove a key # remove_key("key-342") # - def remove_key(id, _ = nil) + # @param [String] key_id + # @return [Boolean] whether key could be removed or not + def remove_key(key_id, _ = nil) return unless self.authorized_keys_enabled? - gitlab_authorized_keys.rm_key(id) + gitlab_authorized_keys.rm_key(key_id) end - # Remove all ssh keys from gitlab shell + # Remove all SSH keys from gitlab shell # - # Ex. + # @example Remove all keys # remove_all_keys # + # @return [Boolean] whether keys could be removed or not def remove_all_keys return unless self.authorized_keys_enabled? gitlab_authorized_keys.clear end - # Remove ssh keys from gitlab shell that are not in the DB + # Remove SSH keys from gitlab shell that are not in the DB # - # Ex. + # @example Remove keys not on the database # remove_keys_not_found_in_db # # rubocop: disable CodeReuse/ActiveRecord @@ -234,11 +272,12 @@ module Gitlab # Add empty directory for storing repositories # - # Ex. + # @example Add new namespace directory # add_namespace("default", "gitlab") # + # @param [String] storage project's storage path + # @param [String] name namespace name def add_namespace(storage, name) - # https://gitlab.com/gitlab-org/gitlab-foss/issues/58012 Gitlab::GitalyClient.allow_n_plus_1_calls do Gitlab::GitalyClient::NamespaceService.new(storage).add(name) end @@ -249,9 +288,11 @@ module Gitlab # Remove directory from repositories storage # Every repository inside this directory will be removed too # - # Ex. + # @example Remove namespace directory # rm_namespace("default", "gitlab") # + # @param [String] storage project's storage path + # @param [String] name namespace name def rm_namespace(storage, name) Gitlab::GitalyClient::NamespaceService.new(storage).remove(name) rescue GRPC::InvalidArgument => e @@ -261,9 +302,12 @@ module Gitlab # Move namespace directory inside repositories storage # - # Ex. + # @example Move/rename a namespace directory # mv_namespace("/path/to/storage", "gitlab", "gitlabhq") # + # @param [String] storage project's storage path + # @param [String] old_name current namespace name + # @param [String] new_name new namespace name def mv_namespace(storage, old_name, new_name) Gitlab::GitalyClient::NamespaceService.new(storage).rename(old_name, new_name) rescue GRPC::InvalidArgument => e @@ -272,11 +316,17 @@ module Gitlab false end - def url_to_repo(path) - Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git" + # Return a SSH url for a given project path + # + # @param [String] full_path project path (URL) + # @return [String] SSH URL + def url_to_repo(full_path) + Gitlab.config.gitlab_shell.ssh_path_prefix + "#{full_path}.git" end # Return GitLab shell version + # + # @return [String] version def version gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION" @@ -285,12 +335,23 @@ module Gitlab end end + # Check if repository exists on disk + # + # @example Check if repository exists + # repository_exists?('default', 'gitlab-org/gitlab.git') + # + # @return [Boolean] whether repository exists or not + # @param [String] storage project's storage path + # @param [Object] dir_name repository dir name def repository_exists?(storage, dir_name) Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists? rescue GRPC::Internal false end + # Return hooks folder path used by projects + # + # @return [String] path def hooks_path File.join(gitlab_shell_path, 'hooks') end -- cgit v1.2.3