Types and tools for interacting with FunBot's external events, to be used by FunBot itself and by programs communicating with it.
[[ 🗃
^br6Go funbot-ext-events
]] ::
[📥 Inbox]
[📤 Outbox]
[🐤 Followers]
[🤝 Collaborators]
[🛠 Commits]
Clone
HTTPS:
git clone https://vervis.peers.community/repos/br6Go
SSH:
git clone USERNAME@vervis.peers.community:br6Go
Branches
Tags
ExtEvents.hs
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | {- This file is part of funbot-ext-events.
-
- Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
-- For json field names
{-# LANGUAGE OverloadedStrings #-}
module FunBot.ExtEvents
( Branch (..)
, Commit (..)
, Push (..)
, Tag (..)
, MergeRequest (..)
, NewsItem (..)
, Paste (..)
, ExtEvent (..)
)
where
import Control.Applicative
import Control.Monad (mzero)
import Data.Aeson
import Data.Aeson.Types (Parser)
import qualified Data.Text as T
data Branch = Branch
{ branchName :: String
, branchRepo :: String
, branchRepoOwner :: String
}
deriving Show
data Commit = Commit
{ commitAuthor :: String
, commitTitle :: String
, commitUrl :: String
}
deriving Show
data Push = Push
{ pushBranch :: Branch
, pushCommits :: [Commit]
}
deriving Show
data Tag = Tag
{ tagAuthor :: String
, tagRef :: String
, tagRepo :: String
, tagRepoOwner :: String
}
deriving Show
data MergeRequest = MergeRequest
{ mrAuthor :: String
, mrId :: Int
, mrRepo :: String
, mrRepoOwner :: String
, mrTitle :: String
, mrUrl :: String
, mrAction :: String
}
deriving Show
data NewsItem = NewsItem
{ itemFeedLabel :: String
, itemFeedTitle :: Maybe String
, itemTitle :: String
, itemAuthor :: Maybe String
, itemUrl :: Maybe String
}
deriving Show
data Paste = Paste
{ pasteAuthor :: String
, pasteVerb :: String
, pasteTitle :: String
, pasteUrl :: String
, pasteChannel :: String
}
deriving Show
-- | An event coming from one of the external event sources.
data ExtEvent
-- | Git commits pushed into a repo.
= GitPushEvent Push
-- | A git tag pushed into a repo.
| GitTagEvent Tag
-- | A merge request related event.
| MergeRequestEvent MergeRequest
-- | New news item published.
| NewsEvent NewsItem
-- | Paste related event in a paste server.
| PasteEvent Paste
-- | A new IRC user needs to be welcomed to a channel.
| WelcomeEvent String String
deriving Show
instance FromJSON Branch where
parseJSON (Object o) =
Branch <$>
o .: "name" <*>
o .: "repo" <*>
o .: "user"
parseJSON _ = mzero
instance ToJSON Branch where
toJSON (Branch name repo owner) = object
[ "name" .= name
, "repo" .= repo
, "user" .= owner
]
instance FromJSON Commit where
parseJSON (Object o) =
Commit <$>
o .: "author" <*>
o .: "title" <*>
o .: "url"
parseJSON _ = mzero
instance ToJSON Commit where
toJSON (Commit author title url) = object
[ "author" .= author
, "title" .= title
, "url" .= url
]
instance FromJSON Push where
parseJSON (Object o) =
Push <$>
o .: "branch" <*>
o .: "commits"
parseJSON _ = mzero
instance ToJSON Push where
toJSON (Push branch commits) = object
[ "branch" .= branch
, "commits" .= commits
]
instance FromJSON Tag where
parseJSON (Object o) =
Tag <$>
o .: "author" <*>
o .: "ref" <*>
o .: "repo" <*>
o .: "user"
parseJSON _ = mzero
instance ToJSON Tag where
toJSON tag = object
[ "author" .= tagAuthor tag
, "ref" .= tagRef tag
, "repo" .= tagRepo tag
, "user" .= tagRepoOwner tag
]
instance FromJSON MergeRequest where
parseJSON (Object o) =
MergeRequest <$>
o .: "author" <*>
o .: "id" <*>
o .: "repo" <*>
o .: "user" <*>
o .: "title" <*>
o .: "url" <*>
o .: "action"
parseJSON _ = mzero
instance ToJSON MergeRequest where
toJSON mr = object
[ "author" .= mrAuthor mr
, "id" .= mrId mr
, "repo" .= mrRepo mr
, "user" .= mrRepoOwner mr
, "title" .= mrTitle mr
, "url" .= mrUrl mr
, "action" .= mrAction mr
]
instance FromJSON NewsItem where
parseJSON (Object o) =
NewsItem <$>
o .: "feed-label" <*>
o .: "feed-title" <*>
o .: "title" <*>
o .: "author" <*>
o .: "url"
parseJSON _ = mzero
instance ToJSON NewsItem where
toJSON (NewsItem fLabel fTitle title author url) = object
[ "feed-label" .= fLabel
, "feed-title" .= fTitle
, "title" .= title
, "author" .= author
, "url" .= url
]
instance FromJSON Paste where
parseJSON (Object o) =
Paste <$>
o .: "author" <*>
o .: "verb" <*>
o .: "title" <*>
o .: "url" <*>
o .: "channel"
parseJSON _ = mzero
instance ToJSON Paste where
toJSON (Paste author verb title url chan) = object
[ "author" .= author
, "verb" .= verb
, "title" .= title
, "url" .= url
, "channel" .= chan
]
text :: Parser T.Text -> T.Text -> Parser T.Text
text parser expected = do
got <- parser
if got == expected
then return got
else mzero
instance FromJSON ExtEvent where
parseJSON (Object o) =
let kind = text $ o .: "type"
in kind "push" *> (GitPushEvent <$> o .: "data") <|>
kind "tag" *> (GitTagEvent <$> o .: "data") <|>
kind "mr" *> (MergeRequestEvent <$> o .: "data") <|>
kind "news" *> (NewsEvent <$> o .: "data") <|>
kind "paste" *> (PasteEvent <$> o .: "data")
parseJSON _ = mzero
instance ToJSON ExtEvent where
toJSON (GitPushEvent commits) = object [ "type" .= ("push" :: T.Text)
, "data" .= commits
]
toJSON (GitTagEvent tag) = object [ "type" .= ("tag" :: T.Text)
, "data" .= tag
]
toJSON (MergeRequestEvent mr) = object [ "type" .= ("mr" :: T.Text)
, "data" .= mr
]
toJSON (NewsEvent item) = object [ "type" .= ("news" :: T.Text)
, "data" .= item
]
toJSON (PasteEvent paste) = object [ "type" .= ("paste" :: T.Text)
, "data" .= paste
]
toJSON (WelcomeEvent _ _) = object []
|