From 595aeafd4581df64f808e14c4afdc4e19b093898 Mon Sep 17 00:00:00 2001
From: Eduardo Trujillo <ed@chromabits.com>
Date: Sat, 26 Dec 2020 20:20:33 -0800
Subject: [PATCH] feat(Parsers): Handle commits without scopes

---
 src/Shift/Parsers.hs           |  6 +++---
 src/Shift/Rendering.hs         |  4 ++--
 src/Shift/Types.hs             |  2 +-
 test/Test/Shift/ParsersSpec.hs | 24 ++++++++++++++++++------
 4 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/Shift/Parsers.hs b/src/Shift/Parsers.hs
index b0b062e..4a74084 100644
--- a/src/Shift/Parsers.hs
+++ b/src/Shift/Parsers.hs
@@ -60,8 +60,8 @@ someCharsTill = someTill anySingle
 conventionalCommit :: Parser ConventionalCommit
 conventionalCommit = do
   cType <- commitType
-  void (char '(')
-  cScope <- someCharsTill (try $ string "): ")
+  cScope <- optional (char '(' *> someCharsTill (try $ char ')'))
+  void $ string ": "
   cSubject <- someCharsTill (choice [eol >> pure (), eof])
 
   cBody <- spaced . manyCharsTill . lookAhead $ do
@@ -76,7 +76,7 @@ conventionalCommit = do
   pure $
     ConventionalCommit
       cType
-      (cs cScope)
+      (cs <$> cScope)
       (cs cSubject)
       (cs cBody)
       cBreakingChanges
diff --git a/src/Shift/Rendering.hs b/src/Shift/Rendering.hs
index 2c8a6b3..c9e2f89 100644
--- a/src/Shift/Rendering.hs
+++ b/src/Shift/Rendering.hs
@@ -12,7 +12,7 @@ import Control.Monad.Trans.Writer (execWriterT)
 import Control.Monad.Trans.Writer.Lazy (tell)
 import Data.Git (Commit, Ref, RefName (refNameRaw), commitAuthor)
 import Data.List (sortOn)
-import Data.Maybe (catMaybes)
+import Data.Maybe (fromMaybe, fromMaybe, catMaybes)
 import Data.String.Conversions (cs)
 import Data.Text (Text)
 import qualified Data.Text as T (pack, take)
@@ -70,7 +70,7 @@ renderConventionalCommit (ref, commit, pc) = do
           renderedRef
             <> catMaybes
               [ Just $ textNode " ",
-                Just . bold $ pc ^. ccScope <> ":",
+                Just . bold $ fromMaybe "" (pc ^. ccScope) <> ":",
                 Just $ textNode " ",
                 Just . textNode $ pc ^. ccSubject,
                 Just $ textNode " ",
diff --git a/src/Shift/Types.hs b/src/Shift/Types.hs
index 35cf8aa..34340c4 100644
--- a/src/Shift/Types.hs
+++ b/src/Shift/Types.hs
@@ -98,7 +98,7 @@ data BreakingChange = BreakingChange
 
 data ConventionalCommit = ConventionalCommit
   { _ccType :: CommitType,
-    _ccScope :: Text,
+    _ccScope :: Maybe Text,
     _ccSubject :: Text,
     _ccBody :: Text,
     _ccBreakingChanges :: [BreakingChange],
diff --git a/test/Test/Shift/ParsersSpec.hs b/test/Test/Shift/ParsersSpec.hs
index ed6a7c7..5dfc19e 100644
--- a/test/Test/Shift/ParsersSpec.hs
+++ b/test/Test/Shift/ParsersSpec.hs
@@ -84,19 +84,31 @@ spec = do
           (Just "Go to example.org to download a new OS.")
   -- commit
   describe "commit" $ do
-    it "parses a regular commit" $ do
+    it "parses a conventional commit" $ do
       parse commit "" "feat(src): Add README file"
         `shouldParse` ( PCConventional $
                           ConventionalCommit
                             { _ccType = CTFeature,
-                              _ccScope = "src",
+                              _ccScope = Just "src",
                               _ccSubject = "Add README file",
                               _ccBody = "",
                               _ccBreakingChanges = [],
                               _ccAffectedTickets = []
                             }
                       )
-    it "parses a regular commit with breaking changes and a body" $ do
+    it "parses a conventional commit (without scope)" $ do
+      parse commit "" "feat: Add README file"
+        `shouldParse` ( PCConventional $
+                          ConventionalCommit
+                            { _ccType = CTFeature,
+                              _ccScope = Nothing,
+                              _ccSubject = "Add README file",
+                              _ccBody = "",
+                              _ccBreakingChanges = [],
+                              _ccAffectedTickets = []
+                            }
+                      )
+    it "parses a conventional commit with breaking changes and a body" $ do
       parse
         commit
         ""
@@ -110,7 +122,7 @@ spec = do
         `shouldParse` ( PCConventional $
                           ConventionalCommit
                             { _ccType = CTFeature,
-                              _ccScope = "src",
+                              _ccScope = Just "src",
                               _ccSubject = "Add README file",
                               _ccBody = "This is an example commit body.",
                               _ccBreakingChanges =
@@ -121,7 +133,7 @@ spec = do
                               _ccAffectedTickets = []
                             }
                       )
-    it "parses a regular commit (kitchen sink)" $ do
+    it "parses a conventional commit (kitchen sink)" $ do
       parse
         commit
         ""
@@ -137,7 +149,7 @@ spec = do
         `shouldParse` ( PCConventional $
                           ConventionalCommit
                             { _ccType = CTFeature,
-                              _ccScope = "src",
+                              _ccScope = Just "src",
                               _ccSubject = "Add README file",
                               _ccBody = "This is an example commit body.",
                               _ccBreakingChanges =
-- 
GitLab