From c9b4a26b12aabe201fd55c93a610004d4d095825 Mon Sep 17 00:00:00 2001 From: Eduardo Trujillo <ed@chromabits.com> Date: Sat, 26 Dec 2020 19:59:56 -0800 Subject: [PATCH] fix(Parsers): Improve handling of more complex commit messages --- src/Shift/Parsers.hs | 16 ++++++++++++---- test/Test/Shift/ParsersSpec.hs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Shift/Parsers.hs b/src/Shift/Parsers.hs index f13f91f..b0b062e 100644 --- a/src/Shift/Parsers.hs +++ b/src/Shift/Parsers.hs @@ -1,9 +1,10 @@ +{-# LANGUAGE ApplicativeDo #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ViewPatterns #-} -{-# LANGUAGE ApplicativeDo #-} module Shift.Parsers where +import Control.Applicative (optional) import Control.Monad (void) import Data.HashSet (HashSet, fromList) import Data.List.NonEmpty (NonEmpty ((:|))) @@ -35,7 +36,6 @@ import Text.Megaparsec (<|>), ) import Text.Megaparsec.Char (char, eol, spaceChar, string) -import Control.Applicative (optional) type Parser = Parsec Void Text @@ -117,14 +117,22 @@ breakingChange = do do shortDescription <- cs <$> manyCharsTill eol - body <- optional (cs <$> (void eol *> someCharsTill (try (spaced eof <|> (skipSome ticketChange *> eof))))) + body <- optional (cs <$> (void eol *> someCharsTill sectionEnd)) return $ BreakingChange shortDescription body + where + sectionEnd = try (spaced eof) <|> lookAheadToTicketChange + lookAheadToTicketChange = try . void $ lookAhead (eol *> eol *> spaced ticketChange) ticketChange :: Parser (HashSet TicketChange) ticketChange = do tcAction <- spaced $ manyCharsTill (string ": ") - tcTickets <- some (char '#' *> manyCharsTill (void (some spaceChar) <|> void eol)) + tcTickets <- + some + ( char '#' + *> manyCharsTill + (void (string ", ") <|> void (some spaceChar) <|> void eol) + ) pure . fromList $ (\x -> (cs tcAction, cs x)) <$> tcTickets diff --git a/test/Test/Shift/ParsersSpec.hs b/test/Test/Shift/ParsersSpec.hs index 33e9da2..184106d 100644 --- a/test/Test/Shift/ParsersSpec.hs +++ b/test/Test/Shift/ParsersSpec.hs @@ -2,6 +2,7 @@ module Test.Shift.ParsersSpec where +import Data.HashSet (fromList) import Shift (BreakingChange (BreakingChange)) import Shift.Parsers (breakingChange, commit, commitType, manyCharsTill, someCharsTill, spaced) import Shift.Types (CommitType (CTFeature), ConventionalCommit (ConventionalCommit, _ccAffectedTickets, _ccBody, _ccBreakingChanges, _ccScope, _ccSubject, _ccType), ParsedCommit (PCConventional)) @@ -96,4 +97,36 @@ spec = do _ccBreakingChanges = [BreakingChange "This will require a new OS upgrade." (Just "Go to example.org to download a new OS.")], _ccAffectedTickets = [] } + ) + it "parses a regular commit (kitchen sink)" $ do + parse + commit + "" + "feat(src): Add README file\n\ + \\n\ + \This is an example commit body.\n\ + \\n\ + \BREAKING CHANGE: This will require a new OS upgrade.\n\ + \\n\ + \Go to example.org to download a new OS.\n\ + \\n\ + \Fixes: #123, #234\n" + `shouldParse` ( PCConventional $ + ConventionalCommit + { _ccType = CTFeature, + _ccScope = "src", + _ccSubject = "Add README file", + _ccBody = "This is an example commit body.", + _ccBreakingChanges = + [ BreakingChange + "This will require a new OS upgrade." + (Just "Go to example.org to download a new OS.") + ], + _ccAffectedTickets = + [ fromList + [ ("Fixes", "234"), + ("Fixes", "123") + ] + ] + } ) \ No newline at end of file -- GitLab