- Add followup button and category

  • Add followup button and category

  • Improve diff message

From b5fd413213ed223de6490e8cd658e26f6b454e17 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Tue, 20 Nov 2018 14:50:39 +1100
Subject: [PATCH] - Add followup button and category

- Improve diff message

diff --git a/assets/javascripts/discourse/initializers/init-code-review.js.es6 b/assets/javascripts/discourse/initializers/init-code-review.js.es6
index 794d28a..659c6dd 100644
--- a/assets/javascripts/discourse/initializers/init-code-review.js.es6
+++ b/assets/javascripts/discourse/initializers/init-code-review.js.es6
@@ -4,7 +4,9 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
 import DiscourseURL from "discourse/lib/url";
 
 function initialize(api) {
+  api.addPostSmallActionIcon("followup", "clock-o");
   api.addPostSmallActionIcon("approved", "thumbs-up");
+
   api.registerConnectorClass(
     "topic-footer-main-buttons-before-create",
     "approve",
@@ -12,7 +14,30 @@ function initialize(api) {
       setupComponent(args) {
         this.set("topic", args.topic);
       },
+      shouldRender: function(args, component) {
+        const currentUser = api.getCurrentUser();
+        if (!currentUser) {
+          return false;
+        }
+        if (component.get("site.mobileView")) {
+          return false;
+        }
+        return currentUser.get("staff");
+      },
       actions: {
+        followupCommit() {
+          let topicId = this.get("topic.id");
+          return ajax("/code-review/followup.json", {
+            type: "POST",
+            data: { topic_id: topicId }
+          })
+            .then(result => {
+              if (result.next_topic_url) {
+                DiscourseURL.routeTo(result.next_topic_url);
+              }
+            })
+            .catch(popupAjaxError);
+        },
         approveCommit() {
           let topicId = this.get("topic.id");
           return ajax("/code-review/approve.json", {
diff --git a/assets/javascripts/discourse/templates/connectors/topic-footer-main-buttons-before-create/approve.hbs b/assets/javascripts/discourse/templates/connectors/topic-footer-main-buttons-before-create/approve.hbs
index 2cc0209..9246ff6 100644
--- a/assets/javascripts/discourse/templates/connectors/topic-footer-main-buttons-before-create/approve.hbs
+++ b/assets/javascripts/discourse/templates/connectors/topic-footer-main-buttons-before-create/approve.hbs
@@ -4,3 +4,10 @@
   label="code_review.approve.label"
   icon="thumbs-up"
   action="approveCommit"}}
+
+{{d-button
+  class="btn follow-up-commit-button"
+  title="code_review.followup.title"
+  label="code_review.followup.label"
+  icon="clock-o"
+  action="followupCommit"}}
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index e18f593..2f823bc 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2,7 +2,11 @@ en:
   js:
     action_codes:
       approved: "Approved"
+      followup: "Follow Up"
     code_review:
       approve:
         title: "Approve commit"
         label: "Approve"
+      followup:
+        title: "Follow up commit"
+        label: "Follow Up"
diff --git a/config/settings.yml b/config/settings.yml
index 660937b..43a20e8 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -7,3 +7,6 @@ plugins:
   code_review_approved_category_id:
     hidden: true
     default: -1
+  code_review_followup_category_id:
+    hidden: true
+    default: -1
diff --git a/jobs/import_commits.rb b/jobs/import_commits.rb
index 8825b6b..cf3f34f 100644
--- a/jobs/import_commits.rb
+++ b/jobs/import_commits.rb
@@ -130,9 +130,14 @@ module Jobs
         context = ""
         if comment[:line_content]
           context = <<~MD
-            > #{comment[:path]}
-            >
-            > `#{comment[:line_content]}`
+            [quote]
+            #{comment[:path]}
+
+            ```diff
+            #{comment[:line_content]}
+            ```
+
+            [/quote]
 
           MD
         end
diff --git a/plugin.rb b/plugin.rb
index 71aabb7..f42d8c4 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -107,10 +107,13 @@ after_initialize do
 
         line_content = nil
 
-        if hash[:path].present?
-          file = git("show #{hash[:commit_id]}:#{hash[:path]}")
-          if file.present? && hash[:line].present?
-            line_content = file.split("\n")[hash[:line] - 1]
+        if hash[:path].present? && hash[:position].present?
+          diff = git("diff #{hash[:commit_id]}~1 #{hash[:commit_id]} #{hash[:path]}")
+          if diff.present?
+            # 5 is preamble
+            start = [hash[:position] + 5 - 3, 5].max
+            finish = hash[:position] + 5 + 3
+            line_content = diff.split("\n")[start..finish].join("\n")
           end
         end
 
@@ -209,6 +212,25 @@ after_initialize do
     before_action :ensure_logged_in
     before_action :ensure_staff
 
+    def followup
+      topic = Topic.find_by(id: params[:topic_id])
+
+      PostRevisor.new(topic.ordered_posts.first, topic)
+        .revise!(current_user,
+          category_id: SiteSetting.code_review_followup_category_id)
+
+      topic.add_moderator_post(
+        current_user,
+        nil,
+        bump: false,
+        post_type: Post.types[:small_action],
+        action_code: "followup"
+      )
+
+      render_next_topic
+
+    end
+
     def approve
       topic = Topic.find_by(id: params[:topic_id])
 
@@ -224,6 +246,13 @@ after_initialize do
         action_code: "approved"
       )
 
+      render_next_topic
+
+    end
+
+    protected
+
+    def render_next_topic
       next_topic = Topic
         .where(category_id: SiteSetting.code_review_pending_category_id)
         .where('topics.id not in (select categories.topic_id from categories where categories.id = category_id)')
@@ -241,31 +270,28 @@ after_initialize do
 
   DiscourseCodeReview::Engine.routes.draw do
     post '/approve' => 'code_review#approve'
+    post '/followup' => 'code_review#followup'
   end
 
   Discourse::Application.routes.append do
     mount ::DiscourseCodeReview::Engine, at: '/code-review'
   end
 
-  if !Category.exists?(id: SiteSetting.code_review_pending_category_id)
-    category = Category.find_by(name: 'pending')
-    category ||= Category.create!(
-      name: 'pending',
-      user: Discourse.system_user
-    )
+  def ensure_category(name)
+    if !Category.exists?(id: SiteSetting.send("code_review_#{name}_category_id"))
+      category = Category.find_by(name: name)
+      category ||= Category.create!(
+        name: name,
+        user: Discourse.system_user
+      )
 
-    SiteSetting.code_review_pending_category_id = category.id
+      SiteSetting.send "code_review_#{name}_category_id=", category.id
+    end
   end
 
-  if !Category.exists?(id: SiteSetting.code_review_approved_category_id)
-    category = Category.find_by(name: 'approved')
-    category ||= Category.create!(
-      name: 'approved',
-      user: Discourse.system_user
-    )
-
-    SiteSetting.code_review_approved_category_id = category.id
-  end
+  ensure_category("pending")
+  ensure_category("approved")
+  ensure_category("followup")
 
   on(:post_process_cooked) do |doc, post|
     if post.post_number > 1 && post.raw.present? && (topic = post.topic) && (hash = topic.custom_fields[DiscourseCodeReview::CommitHash])

GitHub

1 Like