FIX: brute force title length until it works for fancy_title max length (#61)

FIX: brute force title length until it works for fancy_title max length (#61)

Given the automatic nature of this post creation, we can’t ask user to modify input and have to do it ourselves beforehand.

diff --git a/lib/discourse_code_review/state/commit_topics.rb b/lib/discourse_code_review/state/commit_topics.rb
index b3772b7..290f9f9 100644
--- a/lib/discourse_code_review/state/commit_topics.rb
+++ b/lib/discourse_code_review/state/commit_topics.rb
@@ -94,10 +94,22 @@ module DiscourseCodeReview::State::CommitTopics
 
             tags << SiteSetting.code_review_commit_tag
 
+            truncated_title = title
+            iterations = 0
+            while Topic.fancy_title(truncated_title).length > Topic.max_fancy_title_length
+              if iterations >= 3
+                truncated_title = "Automatic title for commit #{commit[:hash][0...8]}"
+                break
+              end
+              iterations += 1
+              truncation = [10, Topic.max_fancy_title_length - iterations * 50].max
+              truncated_title = truncated_title.truncate(truncation)
+            end
+
             post = PostCreator.create!(
               user,
               raw: raw,
-              title: Topic.fancy_title(title).truncate(Topic.max_fancy_title_length),
+              title: truncated_title,
               created_at: commit[:date],
               category: category_id,
               tags: tags,

GitHub sha: 2f7d07b5

This commit appears in #61 which was approved by davidtaylorhq. It was merged by jjaffeux.