FEATURE: Add more control over created categories (#25)

FEATURE: Add more control over created categories (#25)

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 856a74b..073ccd6 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -17,3 +17,8 @@ en:
     code_review_auto_unassign_on_approve: 'Automatically unassign topic on approve'
     code_review_github_organizations: 'Github organizations (comma separated) containing repos that should be synced here'
     code_review_approve_approved_prs: 'Auto approve commits that exist in approved pull requests'
+    code_review_default_mute_new_categories: 'Mute for all users categories created by the plugin'
+    code_review_default_parent_category: 'Default parent category for categories created by the plugin'
+  discourse_code_review:
+    category_description: |
+      This category was created to contain code reviews for %{repo_name}. Topics are created for each commit or pull request.
diff --git a/config/settings.yml b/config/settings.yml
index 91258fb..de56024 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -24,3 +24,7 @@ plugins:
   code_review_auto_unassign_on_approve: true
   code_review_github_organizations: ""
   code_review_approve_approved_prs: false
+  code_review_default_mute_new_categories: false
+  code_review_default_parent_category:
+    type: category
+    default: ""
diff --git a/lib/discourse_code_review/state/github_repo_categories.rb b/lib/discourse_code_review/state/github_repo_categories.rb
index fd35c15..99bb05e 100644
--- a/lib/discourse_code_review/state/github_repo_categories.rb
+++ b/lib/discourse_code_review/state/github_repo_categories.rb
@@ -17,11 +17,21 @@ module DiscourseCodeReview::State::GithubRepoCategories
         if !category
           short_name = find_category_name(repo_name.split("/", 2).last)
 
-          category =
-            Category.create!(
-              name: short_name,
-              user: Discourse.system_user
-            )
+          category = Category.new(
+            name: short_name,
+            user: Discourse.system_user,
+            description: I18n.t('discourse_code_review.category_description', repo_name: repo_name)
+          )
+
+          if SiteSetting.code_review_default_parent_category.present?
+            category.parent_category_id = SiteSetting.code_review_default_parent_category.to_i
+          end
+
+          category.save!
+
+          if SiteSetting.code_review_default_mute_new_categories
+            SiteSetting.default_categories_muted = (SiteSetting.default_categories_muted.split("|") << category.id).join("|")
+          end
 
           category.custom_fields[GITHUB_REPO_NAME] = repo_name
           category.save_custom_fields
diff --git a/spec/discourse_code_review/lib/importer_spec.rb b/spec/discourse_code_review/lib/importer_spec.rb
index f0fe3f1..f14c7fc 100644
--- a/spec/discourse_code_review/lib/importer_spec.rb
+++ b/spec/discourse_code_review/lib/importer_spec.rb
@@ -8,8 +8,36 @@ module DiscourseCodeReview
       Topic.find(topic_id).posts.order(:id).first
     end
 
-    it "can look up a category id consistently" do
+    let(:parent_category) { Fabricate(:category) }
+    let(:repo) { GithubRepo.new("discourse/discourse", Octokit::Client.new, nil) }
+
+    it "creates categories with a description" do
+      category = Category.find_by(id: Importer.new(repo).category_id)
+
+      description = I18n.t("discourse_code_review.category_description", repo_name: "discourse/discourse").strip
+      expect(category.description).to include(description)
+      expect(category.topic.first_post.raw).to include(description)
+    end
 
+    it "mutes categories when code_review_default_mute_new_categories is true" do
+      SiteSetting.code_review_default_mute_new_categories = true
+
+      category = Category.find_by(id: Importer.new(repo).category_id)
+
+      expect(SiteSetting.default_categories_muted.split("|").map(&:to_i)).to include(category.id)
+      expect(category.parent_category_id).to eq(nil)
+    end
+
+    it "sets parent category when code_review_default_parent_category is persent" do
+      SiteSetting.code_review_default_parent_category = parent_category.id
+
+      category = Category.find_by(id: Importer.new(repo).category_id)
+
+      expect(SiteSetting.default_categories_muted.split("|").map(&:to_i)).not_to include(category.id)
+      expect(category.parent_category_id).to eq(parent_category.id)
+    end
+
+    it "can look up a category id consistently" do
       # lets muck stuff up first ... and create a dupe category
       Category.create!(name: 'discourse', user: Discourse.system_user)
 

GitHub sha: 2f876980

This commit appears in #25 which was approved by eviltrout and ZogStriP. It was merged by udan11.