Class: Snippet
- Inherits:
-
ApplicationRecord
show all
- Extended by:
- Gitlab::Utils::Override
- Includes:
- AfterCommitQueue, Awardable, CacheMarkdownField, CanMoveRepositoryStorage, CreatedAtFilterable, EachBatch, Editable, FromUnion, Gitlab::SQL::Pattern, Gitlab::VisibilityLevel, HasRepository, IgnorableColumns, Mentionable, Noteable, Participable, Redactable, Sortable, Spammable
- Defined in:
- app/models/snippet.rb
Constant Summary
collapse
- MAX_FILE_COUNT =
10
- DESCRIPTION_LENGTH_MAX =
1.megabyte
CanMoveRepositoryStorage::RepositoryReadOnlyError
Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_TERM
Constants included
from Noteable
Noteable::MAX_NOTES_LIMIT
CacheMarkdownField::INVALIDATED_BY
Constants included
from Redactable
Redactable::UNSUBSCRIBE_PATTERN
Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE, Gitlab::VisibilityLevel::PUBLIC
ApplicationRecord::MAX_PLUCK
ResetOnUnionError::MAX_RESET_PERIOD
Instance Attribute Summary
Attributes included from Noteable
#system_note_timestamp
#skip_markdown_cache_validation
Class Method Summary
collapse
Instance Method Summary
collapse
extended, extensions, included, method_added, override, prepended, queue_verification, verify!
#run_after_commit, #run_after_commit_or_now
#git_transfer_in_progress?, #reference_counter, #set_repository_read_only!, #set_repository_writable!
#after_change_head_branch_does_not_exist, #after_repository_change_head, #commit, #commit_by, #commits_by, #default_branch_from_group_preferences, #default_branch_from_preferences, #empty_repo?, #http_url_to_repo, #lfs_enabled?, #lfs_http_url_to_repo, #reload_default_branch, #repo_exists?, #repository_exists?, #root_ref?, #ssh_url_to_repo, #url_to_repo, #valid_repo?, #web_url
#gitlab_shell
Methods included from Referable
#referable_inspect, #reference_link_text, #to_reference_base
split_query_to_search_terms
Methods included from Editable
#edited?, #last_edited_by
Methods included from Spammable
#allow_possible_spam?, #check_for_spam, #clear_spam_flags!, #invalidate_if_spam, #needs_recaptcha!, #recaptcha_error!, #render_recaptcha?, #spam, #spam!, #spam_description, #spam_title, #spammable_attribute_changed?, #spammable_entity_type, #spammable_text, #submittable_as_spam?, #submittable_as_spam_by?, #unrecoverable_spam_error!
#all_references, #create_cross_references!, #create_new_cross_references!, #directly_addressed_users, #extractors, #gfm_reference, #local_reference, #matches_cross_reference_regex?, #mentioned_users, #referenced_group_users, #referenced_groups, #referenced_mentionables, #referenced_project_users, #referenced_projects, #referenced_users, #user_mention_class, #user_mention_identifier
Methods included from Awardable
#awarded_emoji?, #downvotes, #emoji_awardable?, #grouped_awards, #upvotes, #user_authored?, #user_can_award?
#participant?, #participants, #visible_participants
Methods included from Noteable
#after_note_created, #after_note_destroyed, #base_class_name, #broadcast_notes_changed, #capped_notes_count, #commenters, #creatable_note_email_address, #discussion_ids_relation, #discussion_notes, #discussion_root_note_ids, #discussions, #discussions_can_be_resolved_by?, #discussions_rendered_on_frontend?, #discussions_resolvable?, #discussions_resolved?, #discussions_to_be_resolved, #grouped_diff_discussions, #has_any_diff_note_positions?, #human_class_name, #lockable?, #noteable_target_type_name, #preloads_discussion_diff_highlighting?, #real_time_notes_enabled?, #resolvable_discussions, #supports_creating_notes_by_email?, #supports_discussions?, #supports_replying_to_individual_notes?, #supports_resolvable_notes?, #supports_suggestion?
#attribute_invalidated?, #banzai_render_context, #cached_html_for, #cached_html_up_to_date?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #local_version, #mentionable_attributes_changed?, #mentioned_filtered_user_ids_for, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #store_mentions!, #updated_cached_html_for
allowed_for?, allowed_level?, allowed_levels, closest_allowed_level, #internal?, level_name, level_value, levels_for_user, non_restricted_level?, options, #private?, #public?, public_visibility_restricted?, restricted_level?, string_level, string_options, string_values, valid_level?, #visibility, #visibility=, #visibility_attribute_present?, #visibility_attribute_value, #visibility_level_attributes, #visibility_level_previous_changes, #visibility_level_value
cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order
#serializable_hash
Constructor Details
#initialize(attributes = {}) ⇒ Snippet
Returns a new instance of Snippet.
206
207
208
209
210
211
212
213
214
215
|
# File 'app/models/snippet.rb', line 206
def initialize(attributes = {})
attributes ||= {}
unless visibility_attribute_present?(attributes)
attributes[:visibility_level] = Gitlab::CurrentSettings.default_snippet_visibility
end
super
end
|
Class Method Details
.find_by_id_and_project(id:, project:) ⇒ Object
190
191
192
|
# File 'app/models/snippet.rb', line 190
def find_by_id_and_project(id:, project:)
Snippet.find_by(id: id, project: project)
end
|
.find_by_project_title_trunc_created_at(project, title, created_at) ⇒ Object
194
195
196
197
198
199
|
# File 'app/models/snippet.rb', line 194
def find_by_project_title_trunc_created_at(project, title, created_at)
where(project: project, title: title)
.find_by(
"date_trunc('second', created_at at time zone :tz) at time zone :tz = :created_at",
tz: created_at.zone, created_at: created_at)
end
|
.for_project_with_user(project, user = nil) ⇒ Object
157
158
159
160
161
162
163
164
165
|
# File 'app/models/snippet.rb', line 157
def for_project_with_user(project, user = nil)
return none unless project.snippets_visible?(user)
if project.member?(user)
project.snippets
else
project.snippets.public_to_user(user)
end
end
|
.link_reference_pattern ⇒ Object
186
187
188
|
# File 'app/models/snippet.rb', line 186
def link_reference_pattern
@link_reference_pattern ||= compose_link_reference_pattern('snippets', /(?<snippet>\d+)/)
end
|
.max_file_limit ⇒ Object
201
202
203
|
# File 'app/models/snippet.rb', line 201
def max_file_limit
MAX_FILE_COUNT
end
|
.only_include_authorized_projects(current_user) ⇒ Object
147
148
149
150
151
152
153
154
155
|
# File 'app/models/snippet.rb', line 147
def only_include_authorized_projects(current_user)
where(
'EXISTS (?)',
ProjectAuthorization
.select(1)
.where('project_id = snippets.project_id')
.where(user_id: current_user.id)
)
end
|
.only_include_projects_visible_to(current_user = nil) ⇒ Object
131
132
133
134
135
|
# File 'app/models/snippet.rb', line 131
def only_include_projects_visible_to(current_user = nil)
levels = Gitlab::VisibilityLevel.levels_for_user(current_user)
joins(:project).where(projects: { visibility_level: levels })
end
|
.only_include_projects_with_snippets_enabled(include_private: false) ⇒ Object
137
138
139
140
141
142
143
144
145
|
# File 'app/models/snippet.rb', line 137
def only_include_projects_with_snippets_enabled(include_private: false)
column = ProjectFeature.access_level_attribute(:snippets)
levels = [ProjectFeature::ENABLED, ProjectFeature::PUBLIC]
levels << ProjectFeature::PRIVATE if include_private
joins(project: :project_feature)
.where(project_features: { column => levels })
end
|
.only_personal_snippets ⇒ Object
123
124
125
|
# File 'app/models/snippet.rb', line 123
def only_personal_snippets
where(project_id: nil)
end
|
.only_project_snippets ⇒ Object
127
128
129
|
# File 'app/models/snippet.rb', line 127
def only_project_snippets
where.not(project_id: nil)
end
|
.parent_class ⇒ Object
107
108
109
|
# File 'app/models/snippet.rb', line 107
def parent_class
::Project
end
|
.reference_pattern ⇒ Object
Pattern used to extract ‘$123` snippet references from text
This pattern supports cross-project references.
179
180
181
182
183
184
|
# File 'app/models/snippet.rb', line 179
def reference_pattern
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}(?<snippet>\d+)
}x
end
|
.reference_prefix ⇒ Object
172
173
174
|
# File 'app/models/snippet.rb', line 172
def reference_prefix
'$'
end
|
.sanitized_file_name(file_name) ⇒ Object
111
112
113
|
# File 'app/models/snippet.rb', line 111
def sanitized_file_name(file_name)
file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
end
|
.search(query) ⇒ Object
Searches for snippets with a matching title, description or file name.
This method uses ILIKE on PostgreSQL.
query - The search query as a String.
Returns an ActiveRecord::Relation.
103
104
105
|
# File 'app/models/snippet.rb', line 103
def search(query)
fuzzy_search(query, [:title, :description, :file_name])
end
|
.visible_to_or_authored_by(user) ⇒ Object
167
168
169
170
|
# File 'app/models/snippet.rb', line 167
def visible_to_or_authored_by(user)
query = where(visibility_level: Gitlab::VisibilityLevel.levels_for_user(user))
query.or(where(author_id: user.id))
end
|
.with_optional_visibility(value = nil) ⇒ Object
115
116
117
118
119
120
121
|
# File 'app/models/snippet.rb', line 115
def with_optional_visibility(value = nil)
if value
where(visibility_level: value)
else
all
end
end
|
Instance Method Details
#all_files ⇒ Object
227
228
229
|
# File 'app/models/snippet.rb', line 227
def all_files
list_files(default_branch)
end
|
#as_json(options = {}) ⇒ Object
282
283
284
285
286
287
|
# File 'app/models/snippet.rb', line 282
def as_json(options = {})
options[:except] = Array.wrap(options[:except])
options[:except] << :secret_token
super
end
|
#blobs(paths = []) ⇒ Object
235
236
237
238
239
240
241
242
|
# File 'app/models/snippet.rb', line 235
def blobs(paths = [])
return [] unless repository_exists?
paths = all_files if paths.empty?
items = paths.map { |path| [default_branch, path] }
repository.blobs_at(items).compact
end
|
#can_cache_field?(field) ⇒ Boolean
#check_for_spam? ⇒ Boolean
264
265
266
|
# File 'app/models/snippet.rb', line 264
def check_for_spam?(*)
visibility_level_changed?(to: Snippet::PUBLIC) || (public? && spammable_attribute_changed?)
end
|
#content_html_invalidated? ⇒ Boolean
41
42
43
|
# File 'app/models/snippet.rb', line 41
def content_html_invalidated?
default_content_html_invalidator || file_name_changed?
end
|
#create_repository ⇒ Object
333
334
335
336
337
338
|
# File 'app/models/snippet.rb', line 333
def create_repository
return if repository_exists? && snippet_repository
repository.create_if_not_exists(default_branch)
track_snippet_repository(repository.storage)
end
|
#default_branch ⇒ Object
325
326
327
|
# File 'app/models/snippet.rb', line 325
def default_branch
super || Gitlab::DefaultBranch.value(object: project)
end
|
#default_content_html_invalidator ⇒ Object
If file_name changes, it invalidates content
40
|
# File 'app/models/snippet.rb', line 40
alias_method :default_content_html_invalidator, :content_html_invalidated?
|
#embeddable? ⇒ Boolean
256
257
258
|
# File 'app/models/snippet.rb', line 256
def embeddable?
Ability.allowed?(nil, :read_snippet, self)
end
|
#file_name ⇒ Object
248
249
250
|
# File 'app/models/snippet.rb', line 248
def file_name
super.to_s
end
|
#file_name_on_repo ⇒ Object
353
354
355
356
357
|
# File 'app/models/snippet.rb', line 353
def file_name_on_repo
return if repository.empty?
list_files(default_branch).first
end
|
#full_path ⇒ Object
312
313
314
315
316
317
318
319
320
321
322
|
# File 'app/models/snippet.rb', line 312
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
|
#hexdigest ⇒ Object
349
350
351
|
# File 'app/models/snippet.rb', line 349
def hexdigest
Digest::SHA256.hexdigest("#{title}#{description}#{created_at}#{updated_at}")
end
|
#hook_attrs ⇒ Object
244
245
246
|
# File 'app/models/snippet.rb', line 244
def hook_attrs
attributes.merge('url' => Gitlab::UrlBuilder.build(self))
end
|
#list_files(ref = nil) ⇒ Object
359
360
361
362
363
|
# File 'app/models/snippet.rb', line 359
def list_files(ref = nil)
return [] if repository.empty?
repository.ls_files(ref || default_branch)
end
|
#multiple_files? ⇒ Boolean
365
366
367
|
# File 'app/models/snippet.rb', line 365
def multiple_files?
list_files.size > 1
end
|
#notes_with_associations ⇒ Object
260
261
262
|
# File 'app/models/snippet.rb', line 260
def notes_with_associations
notes.includes(:author)
end
|
#repository ⇒ Object
290
291
292
|
# File 'app/models/snippet.rb', line 290
def repository
@repository ||= Gitlab::GlRepository::SNIPPET.repository_for(self)
end
|
#repository_size_checker ⇒ Object
295
296
297
298
299
300
301
302
303
|
# File 'app/models/snippet.rb', line 295
def repository_size_checker
strong_memoize(:repository_size_checker) do
::Gitlab::RepositorySizeChecker.new(
current_size_proc: -> { repository.size.megabytes },
limit: Gitlab::CurrentSettings.snippet_size_limit,
namespace: nil
)
end
end
|
#repository_storage ⇒ Object
329
330
331
|
# File 'app/models/snippet.rb', line 329
def repository_storage
snippet_repository&.shard_name || Repository.pick_storage_shard
end
|
#supports_recaptcha? ⇒ Boolean
268
269
270
|
# File 'app/models/snippet.rb', line 268
def supports_recaptcha?
true
end
|
#to_ability_name ⇒ Object
272
273
274
|
# File 'app/models/snippet.rb', line 272
def to_ability_name
'snippet'
end
|
#to_reference(from = nil, full: false) ⇒ Object
217
218
219
220
221
222
223
224
225
|
# File 'app/models/snippet.rb', line 217
def to_reference(from = nil, full: false)
reference = "#{self.class.reference_prefix}#{id}"
if project.present?
"#{project.to_reference_base(from, full: full)}#{reference}"
else
reference
end
end
|
#track_snippet_repository(shard) ⇒ Object
340
341
342
343
|
# File 'app/models/snippet.rb', line 340
def track_snippet_repository(shard)
snippet_repo = snippet_repository || build_snippet_repository
snippet_repo.update!(shard_name: shard, disk_path: disk_path)
end
|
#valid_secret_token?(token) ⇒ Boolean
276
277
278
279
280
|
# File 'app/models/snippet.rb', line 276
def valid_secret_token?(token)
return false unless token && secret_token
ActiveSupport::SecurityUtils.secure_compare(token.to_s, secret_token.to_s)
end
|
#visibility_level_field ⇒ Object
252
253
254
|
# File 'app/models/snippet.rb', line 252
def visibility_level_field
:visibility_level
end
|