Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-13 18:08:52 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-13 18:08:52 +0300
commit0ab47b994caa80c5587f33dc818626b66cfdafe2 (patch)
tree5ef3976d2f84e3368903a67ba2dbd87a74b9a43c /app/models
parent1308dc5eb484ab0f8064989fc551ebdb4b1a7976 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models')
-rw-r--r--app/models/commit.rb10
-rw-r--r--app/models/concerns/has_repository.rb8
-rw-r--r--app/models/personal_snippet.rb4
-rw-r--r--app/models/project.rb5
-rw-r--r--app/models/project_snippet.rb4
-rw-r--r--app/models/repository.rb8
-rw-r--r--app/models/snippet.rb46
-rw-r--r--app/models/snippet_repository.rb13
-rw-r--r--app/models/storage/hashed.rb18
9 files changed, 101 insertions, 15 deletions
diff --git a/app/models/commit.rb b/app/models/commit.rb
index 46222bbc4cd..d8a3bbfeeb2 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -244,6 +244,8 @@ class Commit
# Discover issues should be closed when this commit is pushed to a project's
# default branch.
def closes_issues(current_user = self.committer)
+ return unless repository.repo_type.project?
+
Gitlab::ClosingIssueExtractor.new(project, current_user).closed_by_message(safe_message)
end
@@ -297,7 +299,11 @@ class Commit
end
def merge_requests
- @merge_requests ||= project&.merge_requests&.by_commit_sha(sha)
+ strong_memoize(:merge_requests) do
+ next MergeRequest.none unless repository.repo_type.project? && project
+
+ project.merge_requests.by_commit_sha(sha)
+ end
end
def method_missing(method, *args, &block)
@@ -507,7 +513,7 @@ class Commit
end
def commit_reference(from, referable_commit_id, full: false)
- base = project&.to_reference_base(from, full: full)
+ base = container.to_reference_base(from, full: full)
if base.present?
"#{base}#{self.class.reference_prefix}#{referable_commit_id}"
diff --git a/app/models/concerns/has_repository.rb b/app/models/concerns/has_repository.rb
index 66c2f57bedd..d04a6408a21 100644
--- a/app/models/concerns/has_repository.rb
+++ b/app/models/concerns/has_repository.rb
@@ -1,9 +1,17 @@
# frozen_string_literal: true
+# This concern is created to handle repository actions.
+# It should be include inside any object capable
+# of directly having a repository, like project or snippet.
+#
+# It also includes `Referable`, therefore the method
+# `to_reference` should be overriden in case the object
+# needs any special behavior.
module HasRepository
extend ActiveSupport::Concern
include Gitlab::ShellAdapter
include AfterCommitQueue
+ include Referable
include Gitlab::Utils::StrongMemoize
delegate :base_dir, :disk_path, to: :storage
diff --git a/app/models/personal_snippet.rb b/app/models/personal_snippet.rb
index 1b5be8698b1..5940265b17a 100644
--- a/app/models/personal_snippet.rb
+++ b/app/models/personal_snippet.rb
@@ -2,4 +2,8 @@
class PersonalSnippet < Snippet
include WithUploads
+
+ def web_url(only_path: nil)
+ Gitlab::Routing.url_helpers.snippet_url(self, only_path: only_path)
+ end
end
diff --git a/app/models/project.rb b/app/models/project.rb
index 0ed2510dbf4..bc652a19986 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -9,7 +9,6 @@ class Project < ApplicationRecord
include AccessRequestable
include Avatarable
include CacheMarkdownField
- include Referable
include Sortable
include AfterCommitQueue
include CaseSensitivity
@@ -2336,6 +2335,10 @@ class Project < ApplicationRecord
false
end
+ def self_monitoring?
+ Gitlab::CurrentSettings.self_monitoring_project_id == id
+ end
+
private
def closest_namespace_setting(name)
diff --git a/app/models/project_snippet.rb b/app/models/project_snippet.rb
index ffb08e10f1f..6045ec71c6e 100644
--- a/app/models/project_snippet.rb
+++ b/app/models/project_snippet.rb
@@ -5,4 +5,8 @@ class ProjectSnippet < Snippet
validates :project, presence: true
validates :secret, inclusion: { in: [false] }
+
+ def web_url(only_path: nil)
+ Gitlab::Routing.url_helpers.project_snippet_url(project, self, only_path: only_path)
+ end
end
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 37a20404ae7..c439d0700f1 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -1131,7 +1131,11 @@ class Repository
end
def project
- container
+ if repo_type.snippet?
+ container.project
+ else
+ container
+ end
end
private
@@ -1145,7 +1149,7 @@ class Repository
Gitlab::Git::Commit.find(raw_repository, oid_or_ref)
end
- ::Commit.new(commit, project) if commit
+ ::Commit.new(commit, container) if commit
end
def cache
diff --git a/app/models/snippet.rb b/app/models/snippet.rb
index 77ec683f584..e2b72dfde7a 100644
--- a/app/models/snippet.rb
+++ b/app/models/snippet.rb
@@ -6,7 +6,6 @@ class Snippet < ApplicationRecord
include CacheMarkdownField
include Noteable
include Participable
- include Referable
include Sortable
include Awardable
include Mentionable
@@ -15,10 +14,11 @@ class Snippet < ApplicationRecord
include Gitlab::SQL::Pattern
include FromUnion
include IgnorableColumns
-
+ include HasRepository
extend ::Gitlab::Utils::Override
ignore_column :storage_version, remove_with: '12.9', remove_after: '2020-03-22'
+ ignore_column :repository_storage, remove_with: '12.10', remove_after: '2020-04-22'
cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
@@ -42,6 +42,7 @@ class Snippet < ApplicationRecord
has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :user_mentions, class_name: "SnippetUserMention"
+ has_one :snippet_repository, inverse_of: :snippet
delegate :name, :email, to: :author, prefix: true, allow_nil: true
@@ -254,6 +255,47 @@ class Snippet < ApplicationRecord
super
end
+ def repository
+ @repository ||= Repository.new(full_path, self, disk_path: disk_path, repo_type: Gitlab::GlRepository::SNIPPET)
+ end
+
+ def storage
+ @storage ||= Storage::Hashed.new(self, prefix: Storage::Hashed::SNIPPET_REPOSITORY_PATH_PREFIX)
+ end
+
+ # This is the full_path used to identify the
+ # the snippet repository. It will be used mostly
+ # for logging purposes.
+ def full_path
+ return unless persisted?
+
+ @full_path ||= begin
+ components = []
+ components << project.full_path if project_id?
+ components << '@snippets'
+ components << self.id
+ components.join('/')
+ end
+ end
+
+ def repository_storage
+ snippet_repository&.shard_name ||
+ Gitlab::CurrentSettings.pick_repository_storage
+ end
+
+ def create_repository
+ return if repository_exists?
+
+ repository.create_if_not_exists
+
+ track_snippet_repository if repository_exists?
+ end
+
+ def track_snippet_repository
+ repository = snippet_repository || build_snippet_repository
+ repository.update!(shard_name: repository_storage, disk_path: disk_path)
+ end
+
class << self
# Searches for snippets with a matching title or file name.
#
diff --git a/app/models/snippet_repository.rb b/app/models/snippet_repository.rb
new file mode 100644
index 00000000000..ba2a061a5f4
--- /dev/null
+++ b/app/models/snippet_repository.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class SnippetRepository < ApplicationRecord
+ include Shardable
+
+ belongs_to :snippet, inverse_of: :snippet_repository
+
+ class << self
+ def find_snippet(disk_path)
+ find_by(disk_path: disk_path)&.snippet
+ end
+ end
+end
diff --git a/app/models/storage/hashed.rb b/app/models/storage/hashed.rb
index 898e75194db..3dea50ab98b 100644
--- a/app/models/storage/hashed.rb
+++ b/app/models/storage/hashed.rb
@@ -2,14 +2,15 @@
module Storage
class Hashed
- attr_accessor :project
- delegate :gitlab_shell, :repository_storage, to: :project
+ attr_accessor :container
+ delegate :gitlab_shell, :repository_storage, to: :container
REPOSITORY_PATH_PREFIX = '@hashed'
+ SNIPPET_REPOSITORY_PATH_PREFIX = '@snippets'
POOL_PATH_PREFIX = '@pools'
- def initialize(project, prefix: REPOSITORY_PATH_PREFIX)
- @project = project
+ def initialize(container, prefix: REPOSITORY_PATH_PREFIX)
+ @container = container
@prefix = prefix
end
@@ -20,9 +21,10 @@ module Storage
"#{@prefix}/#{disk_hash[0..1]}/#{disk_hash[2..3]}" if disk_hash
end
- # Disk path is used to build repository and project's wiki path on disk
+ # Disk path is used to build repository path on disk
#
- # @return [String] combination of base_dir and the repository own name without `.git` or `.wiki.git` extensions
+ # @return [String] combination of base_dir and the repository own name
+ # without `.git`, `.wiki.git`, or any other extension
def disk_path
"#{base_dir}/#{disk_hash}" if disk_hash
end
@@ -33,10 +35,10 @@ module Storage
private
- # Generates the hash for the project path and name on disk
+ # Generates the hash for the repository path and name on disk
# If you need to refer to the repository on disk, use the `#disk_path`
def disk_hash
- @disk_hash ||= Digest::SHA2.hexdigest(project.id.to_s) if project.id
+ @disk_hash ||= Digest::SHA2.hexdigest(container.id.to_s) if container.id
end
end
end