Commit 76afae02 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'fj-fix-git-error-message-update-snippet' into 'master'

Show git error message updating snippet

See merge request gitlab-org/gitlab!26570
parents a03444a7 636e8fc4
......@@ -28,4 +28,11 @@ module SnippetsActions
def convert_line_endings(content)
params[:line_ending] == 'raw' ? content : content.gsub(/\r\n/, "\n")
end
def check_repository_error
repository_error = snippet.errors.delete(:repository)
flash.now[:alert] = repository_error if repository_error
recaptcha_check_with_fallback(repository_error.nil?) { render :edit }
end
end
......@@ -62,7 +62,7 @@ class Projects::SnippetsController < Projects::ApplicationController
service_response = Snippets::UpdateService.new(project, current_user, update_params).execute(@snippet)
@snippet = service_response.payload[:snippet]
recaptcha_check_with_fallback { render :edit }
check_repository_error
end
def show
......
......@@ -64,7 +64,7 @@ class SnippetsController < ApplicationController
service_response = Snippets::UpdateService.new(nil, current_user, update_params).execute(@snippet)
@snippet = service_response.payload[:snippet]
recaptcha_check_with_fallback { render :edit }
check_repository_error
end
def show
......
......@@ -52,7 +52,7 @@ module Snippets
create_commit(snippet) if snippet.repository_exists?
end
rescue
snippet.errors.add(:base, 'Error updating the snippet')
snippet.errors.add(:repository, 'Error updating the snippet')
false
end
......
---
title: Show git error message updating snippet
merge_request: 26570
author:
type: fixed
......@@ -3,9 +3,9 @@
require 'spec_helper'
describe 'Projects > Snippets > User updates a snippet' do
let(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, namespace: user.namespace) }
let!(:snippet) { create(:project_snippet, project: project, author: user) }
let(:user) { create(:user) }
before do
stub_feature_flags(snippets_vue: false)
......@@ -13,16 +13,33 @@ describe 'Projects > Snippets > User updates a snippet' do
sign_in(user)
visit(project_snippet_path(project, snippet))
end
it 'updates a snippet' do
page.within('.detail-page-header') do
first(:link, 'Edit').click
end
end
it 'updates a snippet' do
fill_in('project_snippet_title', with: 'Snippet new title')
click_button('Save')
expect(page).to have_content('Snippet new title')
end
context 'when the git operation fails' do
before do
allow_next_instance_of(Snippets::UpdateService) do |instance|
allow(instance).to receive(:create_commit).and_raise(StandardError)
end
fill_in('project_snippet_title', with: 'Snippet new title')
click_button('Save')
end
it 'renders edit page and displays the error' do
expect(page).to have_content('Error updating the snippet')
expect(page).to have_content('Edit Snippet')
end
end
end
......@@ -8,7 +8,7 @@ describe 'User edits snippet', :js do
let(:file_name) { 'test.rb' }
let(:content) { 'puts "test"' }
let(:user) { create(:user) }
let_it_be(:user) { create(:user) }
let(:snippet) { create(:personal_snippet, :public, file_name: file_name, content: content, author: user) }
before do
......@@ -58,4 +58,21 @@ describe 'User edits snippet', :js do
expect(page).to have_no_xpath("//i[@class='fa fa-lock']")
expect(page).to have_xpath("//i[@class='fa fa-globe']")
end
context 'when the git operation fails' do
before do
allow_next_instance_of(Snippets::UpdateService) do |instance|
allow(instance).to receive(:create_commit).and_raise(StandardError)
end
fill_in 'personal_snippet_title', with: 'New Snippet Title'
click_button('Save changes')
end
it 'renders edit page and displays the error' do
expect(page).to have_content('Error updating the snippet')
expect(page).to have_content('Edit Snippet')
end
end
end
......@@ -148,7 +148,7 @@ describe Snippets::UpdateService do
response = subject
expect(response).to be_error
expect(response.payload[:snippet].errors.full_messages).to eq ['Error updating the snippet']
expect(response.payload[:snippet].errors.full_messages).to eq ['Repository Error updating the snippet']
end
end
......@@ -173,7 +173,7 @@ describe Snippets::UpdateService do
response = subject
expect(response).to be_error
expect(response.payload[:snippet].errors.full_messages).to eq ['Error updating the snippet']
expect(response.payload[:snippet].errors.full_messages).to eq ['Repository Error updating the snippet']
end
it 'returns error if snippet does not have a snippet_repository' do
......
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