Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better History Control - Rollbacks and Revertions (215, 340) #1144

Draft
wants to merge 85 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
e3c7f04
Add close reason as comment to history
Taeir Jul 21, 2023
0f75cb5
Use comment scrubber to disallow a few more elements
Taeir Jul 21, 2023
a785b41
Disallow some more markdown and remove newlines from comments
Taeir Jul 21, 2023
f18c526
Add basis of rolling back history elements
Taeir Jul 21, 2023
15a5924
Add predecessor finding
Taeir Jul 21, 2023
da318a8
Cleanup & fix when elements remain the same
Taeir Jul 21, 2023
2d922a1
Fix bug
Taeir Jul 22, 2023
19873ec
Add extra JSON column to post history
Taeir Jul 22, 2023
a366dfe
Store info into history where we can
Taeir Jul 22, 2023
582a057
Merge branch '215-record-close-reason-in-history' into 340-rollback-s…
Taeir Jul 22, 2023
7497328
Store more information and add proper display of rollbacks
Taeir Jul 22, 2023
405eb45
Add extra message constructing
Taeir Jul 22, 2023
3f1b238
Remove extra message construction
Taeir Jul 22, 2023
ac24fd9
Add logic for reapplication
Taeir Jul 22, 2023
9c67321
Change approach
Taeir Jul 22, 2023
513a390
Cleanup
Taeir Jul 22, 2023
d9e48d4
Add missing dependent destroy
Taeir Jul 22, 2023
c61d908
TODO add authentication based on rollback type
Taeir Jul 22, 2023
2c6773a
Add permission checks
Taeir Jul 23, 2023
b055300
Add beginnings of revertion
Taeir Jul 23, 2023
dfa1010
Add revertion logic
Taeir Jul 28, 2023
a40529e
Extract permission checks into submethods to reduce complexity
Taeir Jul 31, 2023
4a933c7
Change from JSON field to individual fields
Taeir Jul 31, 2023
957d199
Add restoration to specific event logic
Taeir Jul 31, 2023
89eede1
Add localization strings for new errors
Taeir Jul 31, 2023
c483131
Merge branch 'history-redaction' into revert-to-state-support
Taeir Jul 31, 2023
58937f5
Add logic for rolling back history hidden
Taeir Jul 31, 2023
7bd8d46
Fix bug in find_predecessor
Taeir Jul 31, 2023
bfa08f2
Optimize migration for large datasets
Taeir Jul 31, 2023
b95832b
Load post associated with history item
Taeir Aug 1, 2023
7888aec
Split methods into multiple smaller ones
Taeir Aug 1, 2023
22c9388
Add reverted_with information for revertions
Taeir Aug 1, 2023
eb6fed1
Add history revealed
Taeir Aug 1, 2023
ea07598
Add revert to button
Taeir Aug 1, 2023
a05abcc
Merge branch 'develop' into revert-to-state-support
Taeir Aug 1, 2023
8ce2546
Use revision consistently
Taeir Aug 4, 2023
bb7a4f7
Merge branch 'develop' into revert-to-state-support
Taeir Aug 4, 2023
38b6f79
Add test case
Taeir Aug 4, 2023
cb1e328
Add additional test cases for edit rollback
Taeir Aug 4, 2023
d57f807
More find_predecessor to PostHistory
Taeir Aug 4, 2023
b59bd13
Add close reason and duplicate post belongs_to relations
Taeir Aug 4, 2023
f97f601
Fix privilege error
Taeir Aug 4, 2023
6171828
Fix close reason not registered upon rollback
Taeir Aug 4, 2023
95d70cf
Attribute rollback events to current user
Taeir Aug 4, 2023
7b567bd
Add tests for rollbacks
Taeir Aug 4, 2023
e45b534
Fix comments tests
Taeir Aug 4, 2023
01e04c2
Fix test
Taeir Aug 4, 2023
80926c3
Merge branch 'develop' into revert-to-state-support
Taeir Aug 4, 2023
2e5b249
Fix history revealing not revealing preceding hide event itself
Taeir Aug 4, 2023
23fc5b4
Allow history_revelead to be rolled back
Taeir Aug 4, 2023
c06886b
Add tests for rollback close/delete
Taeir Aug 4, 2023
a77b8f6
Add revert history overview
Taeir Aug 6, 2023
d5a4781
Add support for undoing revealing history
Taeir Aug 6, 2023
cb4a8bb
Fix closing a post does not store close reason in history
Taeir Aug 6, 2023
f94846d
Rename rollback to undo, revert_to to roll_back_to
Taeir Aug 6, 2023
b7c265e
Update style with input from @ArtOfCode-
Taeir Aug 6, 2023
822a961
Merge branch 'develop' into revert-to-state-support
Taeir Aug 6, 2023
5a24a64
Fix bug
Taeir Aug 6, 2023
edd9718
Fix various issues and improve efficiency
Taeir Aug 6, 2023
e3aaa49
Handle empty changeset
Taeir Aug 6, 2023
6f9cbd4
Deal with empty edit comment
Taeir Aug 6, 2023
1e5b8e0
Add edit comment to events upon rollback
Taeir Aug 6, 2023
4913715
Actually render success
Taeir Aug 6, 2023
8755c59
Cleanup changes to ensure it actually contains changes to make
Taeir Aug 6, 2023
b7a0e18
Support deletion of post history again
Taeir Aug 6, 2023
1a773bc
Small bugfix for tags changes
Taeir Aug 6, 2023
7d6f9be
Improve overview display
Taeir Aug 6, 2023
85563f2
Fix reopen privileges
Taeir Aug 6, 2023
c9428b8
Add undo
Taeir Aug 6, 2023
f376fa1
Remove support for rolling back close/delete events
Taeir Aug 6, 2023
6f07f95
Extra post updates into PostActions concern
Taeir Aug 7, 2023
a2bcf0a
Improve text of actions on post history overview
Taeir Aug 7, 2023
0f9a366
Fix page name
Taeir Aug 7, 2023
f7285e1
Make terminology consistent
Taeir Aug 7, 2023
bae62a1
Fix cannot rollback to initial revision
Taeir Aug 7, 2023
0d83a15
Add warning when rolling back to hidden events
Taeir Aug 7, 2023
bb18e29
Consider history hiding in allowing rollbacks
Taeir Aug 7, 2023
8955a3d
Remove outdated tests
Taeir Aug 7, 2023
356187c
Add confirmation
Taeir Aug 7, 2023
a7f25f5
Fix revert update
Taeir Aug 7, 2023
4b9d2d3
Fix undo
Taeir Aug 7, 2023
4acf18c
Fix tests
Taeir Aug 7, 2023
310fa9c
Fix rubocop issue
Taeir Aug 7, 2023
c75dbf8
Add test for rollbacks
Taeir Aug 7, 2023
c313035
Simplify test
Taeir Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Store more information and add proper display of rollbacks
  • Loading branch information
Taeir committed Jul 22, 2023
commit 74973283731aa5a6583a0720caef78b14e820976
31 changes: 17 additions & 14 deletions app/controllers/post_history_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@ def rollback
comment: "Rollback of [##{index}: #{@history.post_history_type.name}](#{post_history_url(@post, anchor: index)})"
}

# Do the actual rollback
unless rollback_post_history
flash[:danger] = "Unable to rollback revision: #{@post.errors.full_messages.join(', ')}"
redirect_to post_history_path(@post)
return
end
# Do the actual rollback within a transaction with the history to ensure we don't end up in inconsistent states
PostHistory.transaction do
unless rollback_post_history
flash[:danger] = "Unable to rollback revision: #{@post.errors.full_messages.join(', ')}"
redirect_to post_history_path(@post)
return
end

opts.merge(after: @post.body_markdown, after_title: @post.title, after_tags: @post.tags.to_a)
opts.merge(after: @post.body_markdown, after_title: @post.title, after_tags: @post.tags.to_a)

# Record in the history that this element was rolled back
new_history = PostHistory.history_rolled_back(@post, current_user, **opts)
# Record in the history that this element was rolled back
new_history = PostHistory.history_rolled_back(@post, current_user, **opts)

# Store that the previous history was rolled back
# TODO Add migration
# @history.update(rolled_back_with: new_history)
# Set the original to be rolled back
@history.extra = {} unless @history.extra
@history.extra[:rolled_back_with] = new_history.id
@history.save!
end

flash[:success] = 'History successfully rolled back'
redirect_to post_history_path(@post)
Expand All @@ -64,9 +67,9 @@ def rollback_post_history
last_activity: DateTime.now, last_activity_by: current_user)
when 'question_reopened'
predecessor = find_predecessor('question_closed')
# TODO We need to find the close reason properly, this doesn't exist
@post.update(closed: true, closed_by: predecessor.user, closed_at: predecessor.created_at,
close_reason: predecessor.close_reason, duplicate_post: nil,
close_reason_id: predecessor.extra&.fetch('close_reason_id', nil),
duplicate_post_id: predecessor.extra&.fetch('duplicate_post_id', nil),
last_activity: DateTime.now, last_activity_by: current_user)
when 'post_edited'
@post.title = @history.before_title if @history.before_title && @history.before_title != @history.after_title
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ def close

if @post.update(closed: true, closed_by: current_user, closed_at: DateTime.now, last_activity: DateTime.now,
last_activity_by: current_user, close_reason: reason, duplicate_post: duplicate_of)
PostHistory.question_closed(@post, current_user, comment: comment)
PostHistory.question_closed(@post, current_user, comment: comment, extra: {
close_reason_id: reason.id, duplicate_post_id: duplicate_of&.id
})
render json: { status: 'success' }
else
render json: { status: 'failed', message: helpers.i18ns('posts.cant_close_post'),
Expand Down
5 changes: 5 additions & 0 deletions app/models/post_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def tags_added
after_tags - before_tags
end

# @return [Boolean] whether this history item was rolled back
def rolled_back?
extra.present? && !!extra.fetch('rolled_back_with', nil)
end

def self.method_missing(name, *args, **opts)
unless args.length >= 2
raise NoMethodError
Expand Down
8 changes: 7 additions & 1 deletion app/views/post_history/post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
<% @history.each.with_index do |event, index| %>
<details class="history-event" id="<%= @history.size - index %>">
<summary>
<strong>#<%= @history.size - index %>: <%= event.post_history_type.name.humanize %></strong>
<strong>
<% if event.rolled_back? %>
<del>#<%= @history.size - index %>: <%= event.post_history_type.name.humanize %></del>
<% else %>
#<%= @history.size - index %>: <%= event.post_history_type.name.humanize %>
<% end %>
</strong>
<span class="has-color-tertiary-600 history-meta">
<span>
by
Expand Down