blob: c82dba5bab5d812b2fe2224e9469d2d2b8ed03b5 (
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
73
74
75
76
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
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.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
}
| IssueChanged
{ hash :: CommitHash,
oldIssue :: Issue,
issue :: Issue
}
| IssueDeleted
{ hash :: CommitHash,
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
instance P.Render (P.Detailed IssueEvent) where
render (P.Detailed issueEvent) =
P.Summarized issueEvent
<<< P.hardline @P.AnsiStyle
<<< issueEvent.patch
instance P.Render (P.Summarized IssueEvent) where
render (P.Summarized issueEvent) =
P.Summarized issueEvent.hash
<<< ( case issueEvent of
IssueCreated {} ->
P.styled [P.color P.Blue] "created"
IssueChanged {} ->
P.styled [P.color P.Blue] "changed"
IssueDeleted {} ->
P.styled [P.color P.Blue] "deleted"
)
<<< I.IssueTitle issueEvent.issue
|