FEATURE: use tags instead of using categories

FEATURE: use tags instead of using categories

From c3c072341a0012f9a61a9c404da6cd05a82532fc Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Fri, 23 Nov 2018 13:32:00 +1100
Subject: [PATCH] FEATURE: use tags instead of using categories


diff --git a/app/controllers/discourse_code_review/code_review_controller.rb b/app/controllers/discourse_code_review/code_review_controller.rb
index e4b1f51..a8f817d 100644
--- a/app/controllers/discourse_code_review/code_review_controller.rb
+++ b/app/controllers/discourse_code_review/code_review_controller.rb
@@ -5,9 +5,16 @@ class ::DiscourseCodeReview::CodeReviewController < ::ApplicationController
   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)
+    tags = topic.tags.pluck(:name)
+
+    tags -= [
+      SiteSetting.code_review_approved_tag,
+      SiteSetting.code_review_pending_tag
+    ]
+
+    tags << SiteSetting.code_review_followup_tag
+
+    DiscourseTagging.tag_topic_by_names(topic, Guardian.new(current_user), tags)
 
     topic.add_moderator_post(
       current_user,
@@ -17,16 +24,24 @@ class ::DiscourseCodeReview::CodeReviewController < ::ApplicationController
       action_code: "followup"
     )
 
-    render_next_topic
+    render_next_topic(topic.category_id)
 
   end
 
   def approve
+
     topic = Topic.find_by(id: params[:topic_id])
 
-    PostRevisor.new(topic.ordered_posts.first, topic)
-      .revise!(current_user,
-        category_id: SiteSetting.code_review_approved_category_id)
+    tags = topic.tags.pluck(:name)
+
+    tags -= [
+      SiteSetting.code_review_followup_tag,
+      SiteSetting.code_review_pending_tag
+    ]
+
+    tags << SiteSetting.code_review_approved_tag
+
+    DiscourseTagging.tag_topic_by_names(topic, Guardian.new(current_user), tags)
 
     topic.add_moderator_post(
       current_user,
@@ -36,16 +51,17 @@ class ::DiscourseCodeReview::CodeReviewController < ::ApplicationController
       action_code: "approved"
     )
 
-    render_next_topic
+    render_next_topic(topic.category_id)
 
   end
 
   protected
 
-  def render_next_topic
+  def render_next_topic(category_id)
     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)')
+      .joins(:tags)
+      .where('tags.name = ?', SiteSetting.code_review_pending_tag)
+      .where(category_id: category_id)
       .where('user_id <> ?', current_user.id)
       .order('bumped_at asc')
       .first
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 1e0fbe9..f63de84 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1,5 +1,9 @@
 en:
   site_settings:
-    code_review_github_repo: 'name of github repo you wish to track eg: discourse/discourse'
+    code_review_github_repo: 'name of github repos you wish to track eg: discourse/discourse'
+    code_review_category_name: 'category for github repo'
     code_review_enabled: 'enable Discourse code review'
     code_review_api_username: 'OAuth key of this username will be used for API calls, this means we get 5000 an hour vs 60'
+    code_review_pending_tag: 'Tag to apply to pending commits'
+    code_review_followup_tag: 'Tag to apply to follow up commits'
+    code_review_approved_tag: 'Tag to apply to approved commits'
diff --git a/config/settings.yml b/config/settings.yml
index 9acf4d7..b56bc19 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -1,13 +1,8 @@
 plugins:
   code_review_enabled: false
   code_review_github_repo: ""
+  code_review_category_name: ""
   code_review_api_username: ""
-  code_review_pending_category_id:
-    hidden: true
-    default: -1
-  code_review_approved_category_id:
-    hidden: true
-    default: -1
-  code_review_followup_category_id:
-    hidden: true
-    default: -1
+  code_review_pending_tag: "pending"
+  code_review_approved_tag: "approved"
+  code_review_followup_tag: "follow-up"
diff --git a/jobs/import_commits.rb b/jobs/import_commits.rb
index cf3f34f..e0e2a4c 100644
--- a/jobs/import_commits.rb
+++ b/jobs/import_commits.rb
@@ -7,6 +7,22 @@ module Jobs
 
       return unless SiteSetting.code_review_enabled && SiteSetting.code_review_github_repo.present?
 
+      if SiteSetting.code_review_category_name.blank?
+        Rails.logger.warn("You must set the code review category name site setting")
+        return
+      end
+
+      category = Category.find_by(name: SiteSetting.code_review_category_name)
+      if !category
+        Rails.logger.warn("Can not find the category '#{SiteSetting.code_review_category_name}' so commits will not be updated")
+        return
+      end
+
+      import_commits(category_id: category.id)
+      import_comments
+    end
+
+    def import_commits(category_id:)
       DiscourseCodeReview.commits_since.each do |commit|
 
         link = <<~LINK
@@ -29,9 +45,10 @@ module Jobs
             user,
             raw: raw,
             title: title,
-            skip_validations: true,
             created_at: commit[:date],
-            category: SiteSetting.code_review_pending_category_id
+            category: category_id,
+            tags: [SiteSetting.code_review_pending_tag],
+            skip_validations: true,
           )
 
           TopicCustomField.create!(
@@ -43,8 +60,6 @@ module Jobs
           DiscourseCodeReview.last_commit = commit[:hash]
         end
       end
-
-      import_comments
     end
 
     def ensure_user(email:, name:, github_login: nil, github_id: nil)
diff --git a/plugin.rb b/plugin.rb
index 342e589..d41b712 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -61,6 +61,11 @@ end
 
 after_initialize do
 
+  if !SiteSetting.tagging_enabled
+    Rails.logger.warn("The code review plugin requires tagging, enabling it!")
+    SiteSetting.tagging_enabled = true
+  end
+
   module ::DiscourseCodeReview
     PluginName = 'discourse-code-review'
 
@@ -232,22 +237,6 @@ after_initialize do
     mount ::DiscourseCodeReview::Engine, at: '/code-review'
   end
 
-  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.send "code_review_#{name}_category_id=", category.id
-    end
-  end
-
-  ensure_category("pending")
-  ensure_category("approved")
-  ensure_category("followup")
-
   on(:post_process_cooked) do |doc, post|
     if post.post_number > 1 && !post.whisper? && post.raw.present? && (topic = post.topic) && (hash = topic.custom_fields[DiscourseCodeReview::CommitHash])
 
diff --git a/spec/requests/discourse_code_review/code_review_controller_spec.rb b/spec/requests/discourse_code_review/code_review_controller_spec.rb
index 66687f7..49b83cb 100644
--- a/spec/requests/discourse_code_review/code_review_controller_spec.rb
+++ b/spec/requests/discourse_code_review/code_review_controller_spec.rb
@@ -5,14 +5,45 @@ describe DiscourseCodeReview::CodeReviewController do
     SiteSetting.code_review_enabled = true
   end
   context '.approve' do
-    it 'does not allow you to approve your own commit' do
+    it 'allows you to approve your own commit' do
+
+      another_commit = create_post(
+        raw: "this is an old commit",
+        tags: [SiteSetting.code_review_pending_tag],
+        user: Fabricate(:admin)
+      )
+
       user = Fabricate(:admin)
-      commit = create_post(raw: "this is a fake commit", user: user)
+      commit = create_post(raw: "this is a fake commit", user: user, tags: ["hi", SiteSetting.code_review_pending_tag])
 
       sign_in user
 
       post '/code-review/approve.json', params: { topic_id: commit.topic_id }
       expect(response.status).to eq(200)
+
+      json = JSON.parse(response

GitHub

1 Like