Commit 237a32cc authored by James Edwards-Jones's avatar James Edwards-Jones

Avoid failed integrity check by linking LfsObjectProjects sooner

Attempted commits were failing due to the pre-recieve LfsIntegrity
check. This is skipped during tests making this failure silent.
parent 1baac921
module Files module Files
class CreateService < Files::BaseService class CreateService < Files::BaseService
def create_commit! def create_commit!
Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer| transformer = Lfs::FileTransformer.new(project, @branch_name)
content_or_lfs_pointer = transformer.new_file(@file_path, @file_content)
create_transformed_commit(content_or_lfs_pointer) result = transformer.new_file(@file_path, @file_content)
end
create_transformed_commit(result.content)
end end
private private
......
...@@ -3,11 +3,11 @@ module Files ...@@ -3,11 +3,11 @@ module Files
UPDATE_FILE_ACTIONS = %w(update move delete).freeze UPDATE_FILE_ACTIONS = %w(update move delete).freeze
def create_commit! def create_commit!
Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer| transformer = Lfs::FileTransformer.new(project, @branch_name)
actions = actions_after_lfs_transformation(transformer, params[:actions])
commit_actions!(actions) actions = actions_after_lfs_transformation(transformer, params[:actions])
end
commit_actions!(actions)
end end
private private
......
module Lfs module Lfs
# Usage: Open a `.link_lfs_objects` block, call `new_file` on the yielded object # Usage: Calling `new_file` check to see if a file should be in LFS and
# as many times as needed. LfsObjectProject links will be saved if the # return a transformed result with `content` and `encoding` to commit.
# return value of the block is truthy.
# #
# Calling `new_file` will check the path to see if it should be in LFS, # For LFS an LfsObject linked to the project is stored and an LFS
# save and LFS pointer of needed and return its content to be saved in # pointer returned. If the file isn't in LFS the untransformed content
# a commit. If the file isn't LFS the untransformed content is returned. # is returned to save in the commit.
#
# transformer = Lfs::FileTransformer.new(project, @branch_name)
# content_or_lfs_pointer = transformer.new_file(file_path, content).content
# create_transformed_commit(content_or_lfs_pointer)
# #
# Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer|
# content_or_lfs_pointer = transformer.new_file(file_path, file_content)
# create_transformed_commit(content_or_lfs_pointer)
# end
class FileTransformer class FileTransformer
attr_reader :project, :branch_name attr_reader :project, :branch_name
...@@ -21,20 +20,12 @@ module Lfs ...@@ -21,20 +20,12 @@ module Lfs
@branch_name = branch_name @branch_name = branch_name
end end
def self.link_lfs_objects(project, branch_name)
transformer = new(project, branch_name)
result = yield(transformer)
transformer.after_transform! if result
result
end
def new_file(file_path, file_content) def new_file(file_path, file_content)
if project.lfs_enabled? && lfs_file?(file_path) if project.lfs_enabled? && lfs_file?(file_path)
lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content) lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)
lfs_object = create_lfs_object!(lfs_pointer_file, file_content) lfs_object = create_lfs_object!(lfs_pointer_file, file_content)
on_success_actions << -> { link_lfs_object!(lfs_object) } link_lfs_object!(lfs_object)
lfs_pointer_file.pointer lfs_pointer_file.pointer
else else
...@@ -42,20 +33,12 @@ module Lfs ...@@ -42,20 +33,12 @@ module Lfs
end end
end end
def after_transform!
on_success_actions.map(&:call)
end
private private
def lfs_file?(file_path) def lfs_file?(file_path)
repository.attributes_at(branch_name, file_path)['filter'] == 'lfs' repository.attributes_at(branch_name, file_path)['filter'] == 'lfs'
end end
def on_success_actions
@on_success_actions ||= []
end
def create_lfs_object!(lfs_pointer_file, file_content) def create_lfs_object!(lfs_pointer_file, file_content)
LfsObject.find_or_create_by(oid: lfs_pointer_file.sha256, size: lfs_pointer_file.size) do |lfs_object| LfsObject.find_or_create_by(oid: lfs_pointer_file.sha256, size: lfs_pointer_file.size) do |lfs_object|
lfs_object.file = CarrierWaveStringFile.new(file_content) lfs_object.file = CarrierWaveStringFile.new(file_content)
......
...@@ -55,44 +55,23 @@ describe Lfs::FileTransformer do ...@@ -55,44 +55,23 @@ describe Lfs::FileTransformer do
end end
end end
it 'sets up after_transform! to link LfsObjects to project' do it 'links LfsObjects to project' do
subject.new_file(file_path, file_content) expect do
subject.new_file(file_path, file_content)
expect { subject.after_transform! }.to change { project.lfs_objects.count }.by(1) end.to change { project.lfs_objects.count }.by(1)
end end
end
end
describe '.link_lfs_objects' do context 'when LfsObject already exists' do
context 'with lfs enabled' do let(:lfs_pointer) { Gitlab::Git::LfsPointerFile.new(file_content) }
before do
allow(project).to receive(:lfs_enabled?).and_return(true)
end
context 'when given a block' do before do
it 'links LfsObject to the project automatically' do create(:lfs_object, oid: lfs_pointer.sha256, size: lfs_pointer.size)
expect do
described_class.link_lfs_objects(project, branch_name) do |t|
t.new_file(file_path, file_content)
end
end.to change { project.lfs_objects.count }.by(1)
end end
it 'skips linking LfsObjects if the block returns falsey' do it 'links LfsObjects to project' do
expect do expect do
described_class.link_lfs_objects(project, branch_name) do |t| subject.new_file(file_path, file_content)
t.new_file(file_path, file_content) end.to change { project.lfs_objects.count }.by(1)
false
end
end.not_to change { project.lfs_objects.count }
end
it 'returns the result of the block' do
result = described_class.link_lfs_objects(project, branch_name) do |t|
:dummy_commit
end
expect(result).to eq :dummy_commit
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment