aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue.hs
blob: efb61b73af19d961892f0ec1d6f2a8878aff11e0 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Issue (Issue (..), Provenance (..), fromMatch, id) where

import Data.Binary (Binary)
import Data.List (find, foldl')
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Generics (Generic)
import Issue.Provenance (Provenance (..), provenanceFromHEAD)
import Issue.Tag (Tag (..))
import Issue.Tag qualified as I
import Issue.Text qualified as I
import TreeGrepper.Match (Match (..))
import TreeGrepper.Match qualified as G
import TreeGrepper.Result (Result (..))
import TreeGrepper.Result qualified as G
import Prelude hiding (id)

data Issue = Issue
  { title :: Text,
    description :: Maybe Text,
    file :: String,
    provenance :: Maybe Provenance,
    start :: G.Position,
    end :: G.Position,
    tags :: [Tag],
    internalTags :: [Tag]
  }
  deriving (Show, Binary, Generic)

id :: Issue -> Maybe String
id issue =
  (\(Tag _ v) -> T.unpack <$> v)
    =<< find (\(Tag k _) -> k == "id") (issue.tags ++ issue.internalTags)

fromMatch :: FilePath -> G.Result -> G.Match -> IO (Maybe Issue)
fromMatch cwd result match = do
  provenance <- provenanceFromHEAD cwd

  pure
    ( if any (\marker -> T.isPrefixOf marker title') issueMarkers
        then
          Just
            Issue
              { title = title,
                description = description,
                file = result.file,
                provenance = provenance,
                start = match.start,
                end = match.end,
                tags = maybe [] I.extractTags description,
                internalTags = I.internalTags title provenance
              }
        else Nothing
    )
  where
    (title', description) = I.extractText result.file_type match.text
    title = stripIssueMarkers title'

issueMarkers :: [Text]
issueMarkers =
  [ "TODO",
    "FIXME",
    "QUESTION"
  ]

stripIssueMarkers :: Text -> Text
stripIssueMarkers text =
  foldl' (stripIssueMarker) text issueMarkers

stripIssueMarker :: Text -> Text -> Text
stripIssueMarker text marker =
  maybe text T.stripStart (T.stripPrefix marker text)