Class: SnippetRepository

Inherits:
ApplicationRecord show all
Includes:
EachBatch, Shardable
Defined in:
app/models/snippet_repository.rb

Constant Summary collapse

DEFAULT_EMPTY_FILE_NAME =
'snippetfile'
EMPTY_FILE_PATTERN =
/^#{DEFAULT_EMPTY_FILE_NAME}(\d+)\.txt$/
CommitError =
Class.new(StandardError)
InvalidPathError =
Class.new(CommitError)
InvalidSignatureError =
Class.new(CommitError)

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shardable

#shard_name, #shard_name=

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.find_snippet(disk_path) ⇒ Object



36
37
38
# File 'app/models/snippet_repository.rb', line 36

def find_snippet(disk_path)
  find_by(disk_path: disk_path)&.snippet
end

Instance Method Details

#multi_files_action(user, files = [], **options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/snippet_repository.rb', line 41

def multi_files_action(user, files = [], **options)
  return if files.nil? || files.empty?

  lease_key = "multi_files_action:#{snippet_id}"

  lease = Gitlab::ExclusiveLease.new(lease_key, timeout: 120)
  raise CommitError, 'Snippet is already being updated' unless uuid = lease.try_obtain

  options[:actions] = transform_file_entries(files)

  # The Gitaly calls perform HTTP requests for permissions check
  # Stick to the primary in order to make those requests aware that
  # primary database must be used to fetch the data
  self.class.sticking.stick(:user, user.id)

  capture_git_error { repository.commit_files(user, **options) }
ensure
  Gitlab::ExclusiveLease.cancel(lease_key, uuid)
end