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

master :: src / FunBot /

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
{- 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
    ( Repository (..)
    , ProjectObject (..)
    , Commit (..)
    , Push (..)
    , Tag (..)
    , Issue (..)
    , Note (..)
    , MergeRequest (..)
    , NewsItem (..)
    , Paste (..)
    , ExtEvent (..)
    )
where

import Control.Applicative
import Data.Aeson
import Data.Aeson.Types (Parser, typeMismatch)
import Data.Text (Text)

-- | A version control repository.
data Repository = Repository
    { -- | The repository's name. For example, @funbot-ext-events@.
      repoName  :: Text
      -- | The repository's namespace, i.e. username or team name under which
      -- the repository is being managed. For example, @fr33domlover@.
    , repoSpace :: Text
      -- | Network location where the repository is hosted. For now this should
      -- in most cases be the website URL's host part, but could be something
      -- else if/when we have distributed repository sharing. For example,
      -- @notabug.org@.
    , repoHost  :: Text
    }
    deriving Show

instance FromJSON Repository where
    parseJSON (Object o) =
        Repository <$>
        o .: "name" <*>
        o .: "space" <*>
        o .: "host"
    parseJSON v          = typeMismatch "Repository" v

instance ToJSON Repository where
    toJSON repo = object
        [ "name"  .= repoName  repo
        , "space" .= repoSpace repo
        , "host"  .= repoHost  repo
        ]

-- | An object that is part of a project, such as a merge request or an issue
-- or a series of commits being pushed.
data ProjectObject a = ProjectObject
    { -- | The project repository.
      poRepo :: Repository
      -- | The object itself.
    , poObj  :: a
    }
    deriving Show

instance FromJSON a => FromJSON (ProjectObject a) where
    parseJSON (Object o) =
        ProjectObject <$>
        o .: "repo" <*>
        o .: "obj"
    parseJSON v          = typeMismatch "ProjectObject" v

instance ToJSON a => ToJSON (ProjectObject a) where
    toJSON po = object
        [ "repo" .= poRepo po
        , "obj"  .= poObj  po
        ]

-- | A version control system commit, i.e. a set of changes with a description.
data Commit = Commit
    { -- | The author name. For example, @John Doe@.
      commitAuthor   :: Text
      -- | The commit title. For example, @Support encrypted messages@.
    , commitTitle    :: Text
      -- | A web view URL at which the commit details can be displayed.
    , commitUrl      :: Text
      -- | A list of files (relative to the repository top level) added to the
      -- repository in this commit. For example, @src\/FunBot\/ExtEvents.hs@.
    , commitAdded    :: [Text]
      -- | A list of files modified in the repository in this commit.
    , commitModified :: [Text]
      -- | A list of files removed from the repository in this commit.
    , commitRemoved  :: [Text]
    }
    deriving Show

instance FromJSON Commit where
    parseJSON (Object o) =
        Commit <$>
        o .: "author" <*>
        o .: "title" <*>
        o .: "url" <*>
        o .: "added" <*>
        o .: "modified" <*>
        o .: "removed"
    parseJSON v          = typeMismatch "Commit" v

instance ToJSON Commit where
    toJSON (Commit author title url added modified removed) = object
        [ "author"   .= author
        , "title"    .= title
        , "url"      .= url
        , "added"    .= added
        , "modified" .= modified
        , "removed"  .= removed
        ]

-- | A version control push operation, i.e. one or more commits being added to
-- a specific branch or a repsitory. Note that tags can be pushed too, but for
-- that see the 'Tag' type.
data Push = Push
    { pushBranch  :: Text
    , pushCommits :: [Commit]
    }
    deriving Show

instance FromJSON Push where
    parseJSON (Object o) =
        Push <$>
        o .: "branch" <*>
        o .: "commits"
    parseJSON v          = typeMismatch "Push" v

instance ToJSON Push where
    toJSON (Push branch commits) = object
        [ "branch"  .= branch
        , "commits" .= commits
        ]

-- | A version control tag. Allows a specific state of the repository to be
-- referred by name.
data Tag = Tag
    { -- | Tag author name, for example @John Doe@.
      tagAuthor :: Text
      -- | Tag label, for example @0.3.4.1@ (referring to a release version).
    , tagRef    :: Text
    }
    deriving Show

instance FromJSON Tag where
    parseJSON (Object o) =
        Tag <$>
        o .: "author" <*>
        o .: "ref"
    parseJSON v          = typeMismatch "Tag" v

instance ToJSON Tag where
    toJSON tag = object
        [ "author" .= tagAuthor tag
        , "ref"    .= tagRef tag
        ]

-- | A bug, request or other work item attached to a project.
data Issue = Issue
    { -- | Issue author name. For example, @John Doe@.
      issueAuthor    :: Text
      -- | The issue's unique identifier.
    , issueId        :: Int
      -- | Issue title text.
    , issueTitle     :: Text
      -- | A web view URL at which the issue's details can be viewed and
      -- perhaps modified.
    , issueUrl       :: Text
      -- | An action being applied to the issue, which is causing the event to
      -- be sent to the FunBot instance.
    , issueAction    :: Text
    }
    deriving Show

instance FromJSON Issue where
    parseJSON (Object o) =
        Issue <$>
        o .: "author" <*>
        o .: "id" <*>
        o .: "title" <*>
        o .: "url" <*>
        o .: "action"
    parseJSON v          = typeMismatch "Issue" v

instance ToJSON Issue where
    toJSON issue = object
        [ "author" .= issueAuthor issue
        , "id"     .= issueId issue
        , "title"  .= issueTitle issue
        , "url"    .= issueUrl issue
        , "action" .= issueAction issue
        ]

-- | A comment made on some project object.
data Note = Note
    { -- | Note author name, e.g. @John Doe@.
      noteAuthor    :: Text
      -- | Note content for the bot to display. Since notes can be long, this
      -- should be only the portion for the bot to display. The bot should
      -- examine this field and shorten long notes, but you shouldn't rely on
      -- it and provide the right short part here.
    , noteContent   :: Text
      -- | The project object being commented on, e.g. @issue #419@.
    , noteTarget    :: Text
      -- | A web view URL at which the note can be viewed.
    , noteUrl       :: Text
    }
    deriving Show

instance FromJSON Note where
    parseJSON (Object o) =
        Note <$>
        o .: "author" <*>
        o .: "content" <*>
        o .: "target" <*>
        o .: "url"
    parseJSON v          = typeMismatch "Note" v

instance ToJSON Note where
    toJSON (Note author content target url) = object
        [ "author"  .= author
        , "content" .= content
        , "target"  .= target
        , "url"     .= url
        ]

-- | A request from a user to merge their code changes into the project.
data MergeRequest = MergeRequest
    { -- | Merge request author name, e.g. @John Doe@.
      mrAuthor    :: Text
      -- | Merge request unique identifier.
    , mrId        :: Int
      -- | Merge request title.
    , mrTitle     :: Text
      -- | A web view URL at which the merge request can be viewed and perhaps
      -- edited.
    , mrUrl       :: Text
      -- | An action being applied to the merge request, which is causing the
      -- event to be sent to the FunBot instance.
    , mrAction    :: Text
    }
    deriving Show

instance FromJSON MergeRequest where
    parseJSON (Object o) =
        MergeRequest <$>
        o .: "author" <*>
        o .: "id" <*>
        o .: "title" <*>
        o .: "url" <*>
        o .: "action"
    parseJSON v          = typeMismatch "MergeRequest" v

instance ToJSON MergeRequest where
    toJSON mr = object
        [ "author" .= mrAuthor mr
        , "id"     .= mrId mr
        , "title"  .= mrTitle mr
        , "url"    .= mrUrl mr
        , "action" .= mrAction mr
        ]

-- | A news feed item.
data NewsItem = NewsItem
    { -- | Short label identifying the new feed to which the item belongs.
      itemFeedLabel :: Text
      -- | The name of the feed to which the item belongs.
    , itemFeedTitle :: Maybe Text
      -- | Item title.
    , itemTitle     :: Text
      -- | Item author name.
    , itemAuthor    :: Maybe Text
      -- | URL at which the item's content can be viewed.
    , itemUrl       :: Maybe Text
    }
    deriving Show

instance FromJSON NewsItem where
    parseJSON (Object o) =
        NewsItem <$>
        o .: "feed-label" <*>
        o .: "feed-title" <*>
        o .: "title" <*>
        o .: "author" <*>
        o .: "url"
    parseJSON v          = typeMismatch "NewsItem" v

instance ToJSON NewsItem where
    toJSON (NewsItem fLabel fTitle title author url) = object
        [ "feed-label" .= fLabel
        , "feed-title" .= fTitle
        , "title"      .= title
        , "author"     .= author
        , "url"        .= url
        ]

-- | A piece of text uploaded to a website for quick sharing.
data Paste = Paste
    { -- | Paste author name or IRC nickname.
      pasteAuthor  :: Text
      -- | Verb indicating the action done with the paste.
    , pasteVerb    :: Text
      -- | Paste title.
    , pasteTitle   :: Text
      -- | URL at which the paste content can be viewed.
    , pasteUrl     :: Text
      -- | An IRC channel into which to announce the paste.
    , pasteChannel :: Text
    }
    deriving Show

instance FromJSON Paste where
    parseJSON (Object o) =
        Paste <$>
        o .: "author" <*>
        o .: "verb" <*>
        o .: "title" <*>
        o .: "url" <*>
        o .: "channel"
    parseJSON v          = typeMismatch "Paste" v

instance ToJSON Paste where
    toJSON (Paste author verb title url chan) = object
        [ "author"  .= author
        , "verb"    .= verb
        , "title"   .= title
        , "url"     .= url
        , "channel" .= chan
        ]

-- | An event coming from one of the external event sources.
data ExtEvent
    -- | Git commits pushed into a repo.
    = GitPushEvent (ProjectObject Push)
    -- | A git tag pushed into a repo.
    | GitTagEvent (ProjectObject Tag)
    -- | An issue related event.
    | IssueEvent (ProjectObject Issue)
    -- | A merge request related event.
    | MergeRequestEvent (ProjectObject MergeRequest)
    -- | A comment made on an issue or MR or some other object.
    | NoteEvent (ProjectObject Note)
    -- | 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 Text Text
    deriving Show

text :: Parser Text -> Text -> Parser Text
text parser expected = do
    got <- parser
    if got == expected
        then return got
        else empty

instance FromJSON ExtEvent where
    parseJSON (Object o) =
        let kind = text $ o .: "type"
            event label ctor = kind label *> (ctor <$> o .: "data")
        in  event "push"  GitPushEvent      <|>
            event "tag"   GitTagEvent       <|>
            event "mr"    MergeRequestEvent <|>
            event "note"  NoteEvent         <|>
            event "issue" IssueEvent        <|>
            event "news"  NewsEvent         <|>
            event "paste" PasteEvent
    parseJSON v          = typeMismatch "ExtEvent" v

instance ToJSON ExtEvent where
    toJSON (GitPushEvent commits) = object [ "type" .= ("push" :: Text)
                                           , "data" .= commits
                                           ]
    toJSON (GitTagEvent tag)      = object [ "type" .= ("tag" :: Text)
                                           , "data" .= tag
                                           ]
    toJSON (IssueEvent issue)     = object [ "type" .= ("issue" :: Text)
                                           , "data" .= issue
                                           ]
    toJSON (MergeRequestEvent mr) = object [ "type" .= ("mr" :: Text)
                                           , "data" .= mr
                                           ]
    toJSON (NoteEvent note)       = object [ "type" .= ("note" :: Text)
                                           , "data" .= note
                                           ]
    toJSON (NewsEvent item)       = object [ "type" .= ("news" :: Text)
                                           , "data" .= item
                                           ]
    toJSON (PasteEvent paste)     = object [ "type" .= ("paste" :: Text)
                                           , "data" .= paste
                                           ]
    toJSON (WelcomeEvent _ _)     = object []
[See repo JSON]