Last active
August 29, 2015 14:27
-
-
Save hasufell/f5e741eaa3bc08bc5610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- |This module provides 'findBugsInCommitMsg' to find | |
-- valid gentoo bug references in a commit message, | |
-- see <https://wiki.gentoo.org/wiki/Gentoo_git_workflow>. | |
module ParseBugs ( | |
findBugsInCommitMsg | |
) where | |
import Control.Applicative ((<|>), (<$>), (<*>)) | |
import Data.Attoparsec.Text ( | |
Parser | |
, choice | |
, decimal | |
, many' | |
, parseOnly | |
, space | |
, string | |
) | |
import Data.Either (rights) | |
import qualified Data.Text as T | |
import Data.Text (Text) | |
-- |Parser for the "Gentoo-Bug: " prefix. | |
bugPrefixP :: Parser Text | |
bugPrefixP = | |
choice | |
(fmap (string . T.pack) | |
["Gentoo-Bug: " | |
, "Bug: " | |
] | |
) | |
-- |Parser for the various allowed bugzilla urls. | |
bugUrlP :: Parser Text | |
bugUrlP = | |
choice | |
(fmap (string . T.pack) | |
["https://bugs.gentoo.org/show_bug.cgi?id=" | |
, "http://bugs.gentoo.org/show_bug.cgi?id=" | |
, "https://bugs.gentoo.org/" | |
, "http://bugs.gentoo.org/" | |
] | |
) | |
-- |Parser for a bug number. | |
bugIntP :: Parser Integer | |
bugIntP = decimal | |
-- |Parser for a list of bug numbers, as in: "28383, 38383, 9595". | |
bugIntListP :: Parser [Integer] | |
bugIntListP = | |
(:) | |
<$> bugIntP | |
<*> many' (many' space *> (string . T.pack $ ",") *> many' space *> bugIntP) | |
-- |Parser for a valid short bug reference form, e.g.: | |
-- "Gentoo-Bug: 393030, 83893". | |
bugShortP :: Parser [Integer] | |
bugShortP = bugPrefixP *> bugIntListP | |
-- |Parser for a valid long bug reference form, e.g.: | |
-- "Gentoo-Bug: https://bugs.gentoo.org/38328923". | |
bugLongP :: Parser Integer | |
bugLongP = bugPrefixP *> bugUrlP *> bugIntP | |
-- |Parser for all valid short and long bug reference forms. | |
bugP :: Parser [Integer] | |
bugP = | |
bugShortP <|> ((: []) <$> bugLongP) | |
-- |Finds gentoo bug references in a commit message. | |
findBugsInCommitMsg :: Text -> [Integer] | |
findBugsInCommitMsg commitmsg = | |
concat | |
. rights | |
$ fmap (parseOnly bugP) (T.lines commitmsg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test: