aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue/GroupBy.hs
blob: 62f212e8ff3474119e339fd2513f840c39684493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Issue.GroupBy
  ( groupByArg,
    groupIssuesBy,
  )
where

import Data.Map (Map)
import Data.Map qualified as M
import Data.Maybe (mapMaybe)
import Data.Text qualified as T
import Issue (Issue (..))
import Issue.Tag (Tag (..))
import Issue.Tag qualified as I
import Options.Applicative qualified as O

-- TODO Add issues marker as internal tags
--
-- The internal makers `TODO`, `FIXME`, etc. should be available via the
-- internal tag @type

-- TODO Add author and editor as internal tags

groupByArg :: O.Parser (Maybe T.Text)
groupByArg =
  O.optional
    ( O.option
        (O.maybeReader (parse . T.pack))
        ( O.long "group-by"
            <> O.metavar "TAG"
            <> O.help "Group selected issues."
        )
    )
  where
    parse s
      | "@" `T.isPrefixOf` s = Just (T.drop 1 s)
      | otherwise = Nothing

groupIssuesBy :: T.Text -> [Issue] -> Map T.Text [Issue]
groupIssuesBy groupBy issues =
  foldl
    ( \collected issue ->
        foldl
          (flip $ M.alter (Just . maybe [issue] (issue :)))
          collected
          (groupsOfIssue groupBy issue)
    )
    M.empty
    issues
  where
    groupsOfIssue group issue =
      mapMaybe I.tagValue $
        filter (\(Tag key _) -> key == group) (issue.tags ++ issue.internalTags)