FIX: Add rake task to backfill full sha in posts (#65)

FIX: Add rake task to backfill full sha in posts (#65)

We want to be able to go back and fill in the full sha at the bottom of code-review posts. This rake task is idempotent, and will not update the same posts based on the regex in the SQL query limiting results to shorter shas.

diff --git a/plugin.rb b/plugin.rb
index 2cd317f..e27eca0 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -332,3 +332,43 @@ Rake::Task.define_task code_review_tag_commits: :environment do
     )
   end
 end
+
+Rake::Task.define_task code_review_full_sha_backfill: :environment do
+  posts_with_commit = Post
+    .joins("INNER JOIN topics ON topics.id = posts.topic_id")
+    .joins("INNER JOIN topic_custom_fields ON topics.id = topic_custom_fields.topic_id")
+    .includes(topic: :_custom_fields)
+    .where("topic_custom_fields.name = '#{DiscourseCodeReview::COMMIT_HASH}' AND
+            topics.deleted_at IS NULL AND
+            posts.deleted_at IS NULL AND
+            posts.post_number = 1 AND
+            posts.raw ~ 'sha: [0-9a-f]{6,10}' AND
+            posts.raw !~ 'sha: [0-9a-f]{11,60}'")
+
+  total = posts_with_commit.count
+  incr = 0
+
+  puts "Found #{total} posts with a commit sha from the discourse-code-review plugin."
+
+  posts_with_commit.find_each do |post_with_commit|
+    puts "Replacing sha in post #{post_with_commit.id}..."
+
+    full_git_sha = post_with_commit.topic.custom_fields[DiscourseCodeReview::COMMIT_HASH]
+    new_raw = post_with_commit.raw.gsub(/sha: [0-9a-f]{6,10}\b/, "sha: #{full_git_sha}")
+
+    if new_raw == post_with_commit.raw
+      puts "Nothing to change for post #{post_with_commit.id}, continuing. (new raw same as old raw)"
+      incr += 1
+      puts "Completed #{incr}/#{total}."
+      next
+    end
+
+    post_with_commit.update(raw: new_raw)
+    post_with_commit.rebake!
+
+    incr += 1
+    puts "Completed #{incr}/#{total}."
+  end
+
+  puts "All complete."
+end
diff --git a/spec/lib/tasks/code_review_sha_backfill_spec.rb b/spec/lib/tasks/code_review_sha_backfill_spec.rb
new file mode 100644
index 0000000..328287a
--- /dev/null
+++ b/spec/lib/tasks/code_review_sha_backfill_spec.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe "tasks/code_review_sha_backfill" do
+  before do
+    Discourse::Application.load_tasks
+  end
+
+  def raw
+    <<~RAW
+    [excerpt]
+    This is the commit message.
+
+    And some more info.
+[/excerpt]
+
+`‍``diff
+git diff data
+@@ -68,10 +68,10 @@
+some code
+`‍``
+[GitHub](https://github.com/discourse/discourse/commit/c187ede3c67f23478bc2d3c20187bd98ac025b9e)
+ <small>sha: c187ede3</small>
+    RAW
+  end
+
+  describe "discourse_code_review:full_sha_backfill" do
+    let(:topic) { Fabricate(:topic) }
+    let(:post) { Fabricate(:post, topic: topic, raw: raw) }
+
+    before do
+      topic.custom_fields[DiscourseCodeReview::COMMIT_HASH] = 'c187ede3c67f23478bc2d3c20187bd98ac025b9e'
+      topic.save_custom_fields
+      post.rebake!
+      Rake::Task['code_review_full_sha_backfill'].reenable
+    end
+
+    it "updates the post raw with the post revisor to have the full sha" do
+      original_raw = post.raw
+      Rake::Task['code_review_full_sha_backfill'].invoke
+      post.reload
+
+      expect(post.raw.chomp).to eq(original_raw.gsub("sha: c187ede3", "sha: c187ede3c67f23478bc2d3c20187bd98ac025b9e").chomp)
+    end
+
+    it "is idempotent based on raw not changing and the query not getting longer shas" do
+      Rake::Task['code_review_full_sha_backfill'].invoke
+      post_baked_at = post.reload.baked_at
+      Rake::Task['code_review_full_sha_backfill'].reenable
+
+      Rake::Task['code_review_full_sha_backfill'].invoke
+      expect(post.reload.baked_at).to eq_time(post_baked_at)
+    end
+  end
+end

GitHub sha: 2e4f6066

This commit appears in #65 which was approved by SamSaffron. It was merged by martin.