diff options
Diffstat (limited to 'app/IssueEvent.hs')
-rw-r--r-- | app/IssueEvent.hs | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/app/IssueEvent.hs b/app/IssueEvent.hs index a7a190a..c82dba5 100644 --- a/app/IssueEvent.hs +++ b/app/IssueEvent.hs @@ -1,33 +1,58 @@ -module IssueEvent (IssueEvent (..)) where +{-# OPTIONS_GHC -fno-warn-orphans #-} -import Data.Binary (Binary) +module IssueEvent + ( IssueEvent (..), + ) +where + +import Data.Binary (Binary (..)) +import Data.Function ((&)) +import Data.Text qualified as T +import Data.Text.IO qualified as T +import Data.Text.Lazy qualified as LT +import Data.Text.Lazy.Encoding qualified as LT import GHC.Generics (Generic) +import GHC.Records (HasField (..)) import Git.CommitHash (CommitHash) -import Issue (Issue) +import Issue (Issue (..)) import Issue.Render qualified as I import Patch (Patch) +import Patch qualified as A +import Process (sh) import Render ((<<<)) import Render qualified as P +import System.FilePath ((</>)) +import System.IO.Temp (withSystemTempDirectory) +import System.Process.Typed (setWorkingDir) data IssueEvent = IssueCreated { hash :: CommitHash, - issue :: Issue, - patch :: Patch + issue :: Issue } | IssueChanged { hash :: CommitHash, oldIssue :: Issue, - issue :: Issue, - patch :: Patch + issue :: Issue } | IssueDeleted { hash :: CommitHash, - issue :: Issue, - patch :: Patch + issue :: Issue } deriving (Show, Generic, Binary) +instance HasField "patch" IssueEvent (IO Patch) where + getField (IssueCreated {..}) = diff "" issue.rawText + getField (IssueChanged {..}) = diff oldIssue.rawText issue.rawText + getField (IssueDeleted {..}) = diff issue.rawText "" + +diff :: T.Text -> T.Text -> IO A.Patch +diff old new = withSystemTempDirectory "diff" $ \tmp -> do + let cwd = tmp + T.writeFile (tmp </> "old") old + T.writeFile (tmp </> "new") new + A.parse . LT.toStrict . LT.decodeUtf8 <$> sh ("git diff --no-index -- old new || :" & setWorkingDir cwd) + instance P.Render IssueEvent where render = P.render . P.Detailed |