module History.Issues ( Issues (..), ) where import Backend qualified import Data.Binary (Binary) import Data.Function (on) import Data.List (nub) import Data.Map qualified as M import Data.Proxy (Proxy) import Data.Text qualified as T import GHC.Generics (Generic) import History.Plan (Id, Planable, Proto, assume, propagate, protoOf) import History.Scramble (Scramble (..), getScrambleOf) import Issue qualified as I data Issues = Issues { commitHash :: Backend.CommitHash, issues :: M.Map T.Text I.Issue, filesSeen :: [FilePath] } deriving (Show, Generic, Binary) instance Planable Issues where type Id Issues = Backend.CommitHash type Proto Issues = Scramble protoOf :: Proxy Issues -> Backend.CommitHash -> IO Scramble protoOf _ = getScrambleOf assume :: Scramble -> Issues assume (Scramble {..}) = Issues {filesSeen = filesChanged, ..} propagate :: [Id Issues] -> Issues -> Scramble -> Issues propagate _ topIssues bottomScramble = Issues { commitHash = topIssues.commitHash, issues = M.mergeWithKey ( \_ topIssue bottomIssue -> Just $ topIssue { I.provenance = I.Provenance { first = bottomIssue.provenance.first, last = if ((/=) `on` (.rawTextHash)) topIssue bottomIssue then topIssue.provenance.last else bottomIssue.provenance.last } } ) (\topIssues -> topIssues) ( \bottomIssues -> M.map ( \bottomIssue -> if bottomIssue.file `elem` topIssues.filesSeen then bottomIssue {I.closed = True} else bottomIssue ) bottomIssues ) topIssues.issues bottomScramble.issues, filesSeen = nub (topIssues.filesSeen ++ bottomScramble.filesChanged) }