Skip to content

Instantly share code, notes, and snippets.

@hasufell
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasufell/f5e741eaa3bc08bc5610 to your computer and use it in GitHub Desktop.
Save hasufell/f5e741eaa3bc08bc5610 to your computer and use it in GitHub Desktop.
-- |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)
@hasufell
Copy link
Author

test:

findBugsInCommitMsg  (T.pack "app-misc/foo: fix compilation with bar\n\nsome random comments\nGentoo-Bug: 12389, 9812389, 9813289\nGentoo-Bug: 838232")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment