Commit 1d0ecbd4 authored by Rubén Dávila's avatar Rubén Dávila

Sync the changes to LFS when locking/unlocking files in the UI

parent 98f57443
......@@ -20,7 +20,6 @@ module Lfs
def unlock_file
forced = params[:force] == true
lock = project.lfs_file_locks.find(params[:id])
if lock.can_be_unlocked_by?(current_user, forced)
lock.destroy!
......@@ -32,5 +31,15 @@ module Lfs
error("#{lock.path} is locked by GitLab User #{lock.user_id}", 403)
end
end
def lock
return @lock if defined?(@lock)
@lock = if params[:id].present?
project.lfs_file_locks.find(params[:id])
elsif params[:path].present?
project.lfs_file_locks.find_by!(path: params[:path])
end
end
end
end
---
title: Add support for LFS File Locking API
merge_request: 4091
author:
type: added
class Projects::PathLocksController < Projects::ApplicationController
include PathLocksHelper
include ExtractsPath
# Authorize
before_action :require_non_empty_project
before_action :authorize_push_code!, only: [:toggle]
before_action :check_license
before_action :assign_ref_vars, only: :toggle
before_action :lfs_blob_ids, only: :toggle
def index
@path_locks = @project.path_locks.page(params[:page])
......@@ -15,9 +18,9 @@ class Projects::PathLocksController < Projects::ApplicationController
path_lock = @project.path_locks.find_by(path: params[:path])
if path_lock
PathLocks::UnlockService.new(project, current_user).execute(path_lock)
unlock_file(path_lock)
else
PathLocks::LockService.new(project, current_user).execute(params[:path])
lock_file
end
head :ok
......@@ -50,4 +53,35 @@ class Projects::PathLocksController < Projects::ApplicationController
redirect_to admin_license_path
end
end
def lock_file
path_lock = PathLocks::LockService.new(project, current_user).execute(params[:path])
if path_lock.persisted? && sync_with_lfs?
Lfs::LockFileService.new(project, current_user, path: params[:path]).execute
end
end
def unlock_file(path_lock)
PathLocks::UnlockService.new(project, current_user).execute(path_lock)
if sync_with_lfs?
Lfs::UnlockFileService.new(project, current_user, path: path_lock.path, force: true).execute
end
end
# Override get_id from ExtractsPath in this case is just the root of the default branch.
def get_id
@ref ||= project.repository.root_ref
end
def lfs_file?
blob = project.repository.blob_at_branch(get_id, params[:path])
@lfs_blob_ids.include?(blob.id)
end
def sync_with_lfs?
project.lfs_enabled? && lfs_file?
end
end
require 'rails_helper'
describe Projects::PathLocksController, type: :request do
let(:project) { create(:project, :repository) }
let(:user) { project.owner }
let(:viewer) { user }
before do
login_as(viewer)
end
describe 'POST #toggle' do
context 'when locking a file' do
context 'when LFS is enabled' do
before do
allow_any_instance_of(Projects::PathLocksController).to receive(:sync_with_lfs?).and_return(true)
end
it 'locks the file' do
expect { toggle_lock('README.md') }.to change { PathLock.count }.to(1)
expect(response).to have_gitlab_http_status(200)
end
it "locks the file in LFS" do
expect { toggle_lock('README.md') }.to change { LfsFileLock.count }.to(1)
end
end
context 'when LFS is not enabled' do
it 'locks the file' do
expect { toggle_lock('README.md') }.to change { PathLock.count }.to(1)
expect(response).to have_gitlab_http_status(200)
end
it "doesn't lock the file in LFS" do
expect { toggle_lock('README.md') }.not_to change { LfsFileLock.count }
end
end
end
context 'when unlocking a file' do
context 'when LFS is enabled' do
before do
allow_any_instance_of(Projects::PathLocksController).to receive(:sync_with_lfs?).and_return(true)
toggle_lock('README.md')
end
it 'unlocks the file' do
expect { toggle_lock('README.md') }.to change { PathLock.count }.to(0)
expect(response).to have_gitlab_http_status(200)
end
it "unlocks the file in LFS" do
expect { toggle_lock('README.md') }.to change { LfsFileLock.count }.to(0)
end
end
end
context 'when LFS is not enabled' do
before do
toggle_lock('README.md')
end
it 'unlocks the file' do
expect { toggle_lock('README.md') }.to change { PathLock.count }.to(0)
expect(response).to have_gitlab_http_status(200)
end
end
end
def toggle_lock(path)
post toggle_project_path_locks_path(project), path: path
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