diff --git a/app/controllers/concerns/wiki_actions.rb b/app/controllers/concerns/wiki_actions.rb index 4f0351f200344cdbf2425eb6e3ed43138ae80fc3..fc4f9aa340981db4fe1b0090df5d3fa1a312352a 100644 --- a/app/controllers/concerns/wiki_actions.rb +++ b/app/controllers/concerns/wiki_actions.rb @@ -115,9 +115,6 @@ def update @error = response.message render 'shared/wikis/edit' end - rescue WikiPage::PageChangedError, WikiPage::PageRenameError => e - @error = e.message - render 'shared/wikis/edit' end # rubocop:enable Gitlab/ModuleWithInstanceVariables diff --git a/app/services/wiki_pages/update_service.rb b/app/services/wiki_pages/update_service.rb index f2fc6b37c34d95afa56ca8e22e08356c8653aafb..88275f8c4171210fb46e1b6f007b914e0f0d2439 100644 --- a/app/services/wiki_pages/update_service.rb +++ b/app/services/wiki_pages/update_service.rb @@ -2,6 +2,8 @@ module WikiPages class UpdateService < WikiPages::BaseService + UpdateError = Class.new(StandardError) + def execute(page) # this class is not thread safe! @old_slug = page.slug @@ -10,11 +12,15 @@ def execute(page) execute_hooks(page) ServiceResponse.success(payload: { page: page }) else - ServiceResponse.error( - message: _('Could not update wiki page'), - payload: { page: page } - ) + raise UpdateError, s_('Could not update wiki page') end + rescue UpdateError, WikiPage::PageChangedError, WikiPage::PageRenameError => e + page.update_attributes(@params) # rubocop:disable Rails/ActiveRecordAliases + + ServiceResponse.error( + message: e.message, + payload: { page: page } + ) end def usage_counter_action diff --git a/changelogs/unreleased/200002-changes-will-be-lost-if-multiple-people-edit-a-wiki-page.yml b/changelogs/unreleased/200002-changes-will-be-lost-if-multiple-people-edit-a-wiki-page.yml new file mode 100644 index 0000000000000000000000000000000000000000..e1c5815c77b7fc3231ae2f78811bbf430809f98a --- /dev/null +++ b/changelogs/unreleased/200002-changes-will-be-lost-if-multiple-people-edit-a-wiki-page.yml @@ -0,0 +1,5 @@ +--- +title: Preserve user changes in the wiki editor if multiple people edit the page +merge_request: 61120 +author: +type: fixed diff --git a/spec/support/shared_examples/features/wiki/user_updates_wiki_page_shared_examples.rb b/spec/support/shared_examples/features/wiki/user_updates_wiki_page_shared_examples.rb index e5f05217ff044c479cca98ccc2d91c3e2ddcd307..db2a96d9649fb5759a09dc0300b9e2b9c01f0ba5 100644 --- a/spec/support/shared_examples/features/wiki/user_updates_wiki_page_shared_examples.rb +++ b/spec/support/shared_examples/features/wiki/user_updates_wiki_page_shared_examples.rb @@ -117,14 +117,6 @@ expect(page).to have_selector('.atwho-view') end - it 'shows the error message', :js do - wiki_page.update(content: 'Update') # rubocop:disable Rails/SaveBang - - click_button('Save changes') - - expect(page).to have_content('Someone edited the page the same time you did.') - end - it 'updates a page', :js do fill_in('Content', with: 'Updated Wiki Content') click_on('Save changes') @@ -145,6 +137,18 @@ end it_behaves_like 'wiki file attachments' + + context 'when multiple people edit the page at the same time' do + it 'preserves user changes in the wiki editor', :js do + wiki_page.update(content: 'Some Other Updates') # rubocop:disable Rails/SaveBang + + fill_in('Content', with: 'Updated Wiki Content') + click_on('Save changes') + + expect(page).to have_content('Someone edited the page the same time you did.') + expect(find('textarea#wiki_content').value).to eq('Updated Wiki Content') + end + end end context 'when the page is in a subdir', :js do