-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathDefinitions.hs
More file actions
388 lines (316 loc) · 11.7 KB
/
Definitions.hs
File metadata and controls
388 lines (316 loc) · 11.7 KB
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
module GitHub.Data.Definitions where
import GitHub.Internal.Prelude
import Prelude ()
import Control.Monad (mfilter)
import Data.Aeson.Types (Parser)
import Network.HTTP.Client (HttpException)
import qualified Control.Exception as E
import qualified Data.ByteString as BS
import qualified Data.Text as T
import GitHub.Data.Id (Id)
import GitHub.Data.Name (Name)
import GitHub.Data.URL (URL (..))
-- | Errors have been tagged according to their source, so you can more easily
-- dispatch and handle them.
data Error
= HTTPError !HttpException -- ^ A HTTP error occurred. The actual caught error is included.
| ParseError !Text -- ^ An error in the parser itself.
| JsonError !Text -- ^ The JSON is malformed or unexpected.
| UserError !Text -- ^ Incorrect input.
deriving (Show)
instance E.Exception Error
-- | Type of the repository owners.
data OwnerType = OwnerUser | OwnerOrganization | OwnerBot
deriving (Eq, Ord, Enum, Bounded, Show, Read, Generic, Data)
instance NFData OwnerType
instance Binary OwnerType
data SimpleUser = SimpleUser
{ simpleUserId :: !(Id User)
, simpleUserLogin :: !(Name User)
, simpleUserAvatarUrl :: !URL
, simpleUserUrl :: !URL
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData SimpleUser
instance Binary SimpleUser
data SimpleOrganization = SimpleOrganization
{ simpleOrganizationId :: !(Id Organization)
, simpleOrganizationLogin :: !(Name Organization)
, simpleOrganizationUrl :: !URL
, simpleOrganizationAvatarUrl :: !URL
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData SimpleOrganization
instance Binary SimpleOrganization
-- | Sometimes we don't know the type of the owner, e.g. in 'Repo'
data SimpleOwner = SimpleOwner
{ simpleOwnerId :: !(Id Owner)
, simpleOwnerLogin :: !(Name Owner)
, simpleOwnerUrl :: !URL
, simpleOwnerAvatarUrl :: !URL
, simpleOwnerType :: !OwnerType
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData SimpleOwner
instance Binary SimpleOwner
data User = User
{ userId :: !(Id User)
, userLogin :: !(Name User)
, userName :: !(Maybe Text)
, userType :: !OwnerType -- ^ Should always be 'OwnerUser' or 'OwnerBot'
, userCreatedAt :: !UTCTime
, userPublicGists :: !Int
, userAvatarUrl :: !URL
, userFollowers :: !Int
, userFollowing :: !Int
, userHireable :: !(Maybe Bool)
, userBlog :: !(Maybe Text)
, userBio :: !(Maybe Text)
, userPublicRepos :: !Int
, userLocation :: !(Maybe Text)
, userCompany :: !(Maybe Text)
, userEmail :: !(Maybe Text)
, userUrl :: !URL
, userHtmlUrl :: !URL
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData User
instance Binary User
data Organization = Organization
{ organizationId :: !(Id Organization)
, organizationLogin :: !(Name Organization)
, organizationName :: !(Maybe Text)
, organizationType :: !OwnerType -- ^ Should always be 'OwnerOrganization'
, organizationBlog :: !(Maybe Text)
, organizationLocation :: !(Maybe Text)
, organizationFollowers :: !Int
, organizationCompany :: !(Maybe Text)
, organizationAvatarUrl :: !URL
, organizationPublicGists :: !Int
, organizationHtmlUrl :: !URL
, organizationEmail :: !(Maybe Text)
, organizationFollowing :: !Int
, organizationPublicRepos :: !Int
, organizationUrl :: !URL
, organizationCreatedAt :: !UTCTime
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData Organization
instance Binary Organization
-- | In practice you can't have concrete values of 'Owner'.
newtype Owner = Owner (Either User Organization)
deriving (Show, Data, Eq, Ord, Generic)
instance NFData Owner
instance Binary Owner
fromOwner :: Owner -> Either User Organization
fromOwner (Owner owner) = owner
-- JSON instances
instance FromJSON OwnerType where
parseJSON = withText "OwnerType" $ \t -> case T.toLower t of
"user" -> pure $ OwnerUser
"organization" -> pure $ OwnerOrganization
"bot" -> pure $ OwnerBot
_ -> fail $ "Unknown OwnerType: " <> T.unpack t
instance FromJSON SimpleUser where
parseJSON = withObject "SimpleUser" $ \obj -> do
SimpleUser
<$> obj .: "id"
<*> obj .: "login"
<*> obj .: "avatar_url"
<*> obj .: "url"
instance FromJSON SimpleOrganization where
parseJSON = withObject "SimpleOrganization" $ \obj ->
SimpleOrganization
<$> obj .: "id"
<*> obj .: "login"
<*> obj .: "url"
<*> obj .: "avatar_url"
instance FromJSON SimpleOwner where
parseJSON = withObject "SimpleOwner" $ \obj -> do
SimpleOwner
<$> obj .: "id"
<*> obj .: "login"
<*> obj .: "url"
<*> obj .: "avatar_url"
<*> obj .: "type"
parseUser :: Object -> Parser User
parseUser obj = User
<$> obj .: "id"
<*> obj .: "login"
<*> obj .:? "name"
<*> obj .: "type"
<*> obj .: "created_at"
<*> obj .: "public_gists"
<*> obj .: "avatar_url"
<*> obj .: "followers"
<*> obj .: "following"
<*> obj .:? "hireable"
<*> obj .:? "blog"
<*> obj .:? "bio"
<*> obj .: "public_repos"
<*> obj .:? "location"
<*> obj .:? "company"
<*> obj .:? "email"
<*> obj .: "url"
<*> obj .: "html_url"
parseOrganization :: Object -> Parser Organization
parseOrganization obj = Organization
<$> obj .: "id"
<*> obj .: "login"
<*> obj .:? "name"
<*> obj .: "type"
<*> obj .:? "blog"
<*> obj .:? "location"
<*> obj .: "followers"
<*> obj .:? "company"
<*> obj .: "avatar_url"
<*> obj .: "public_gists"
<*> obj .: "html_url"
<*> obj .:? "email"
<*> obj .: "following"
<*> obj .: "public_repos"
<*> obj .: "url"
<*> obj .: "created_at"
instance FromJSON User where
parseJSON = mfilter ((/= OwnerOrganization) . userType) . withObject "User" parseUser
instance FromJSON Organization where
parseJSON = withObject "Organization" parseOrganization
instance FromJSON Owner where
parseJSON = withObject "Owner" $ \obj -> do
t <- obj .: "type"
case t of
OwnerUser -> Owner . Left <$> parseUser obj
OwnerBot -> Owner . Left <$> parseUser obj
OwnerOrganization -> Owner . Right <$> parseOrganization obj
-- | Filter members returned in the list.
data OrgMemberFilter
= OrgMemberFilter2faDisabled -- ^ Members without two-factor authentication enabled. Available for organization owners.
| OrgMemberFilterAll -- ^ All members the authenticated user can see.
deriving (Show, Eq, Ord, Enum, Bounded, Data, Generic)
-- | Filter members returned by their role.
data OrgMemberRole
= OrgMemberRoleAll -- ^ All members of the organization, regardless of role.
| OrgMemberRoleAdmin -- ^ Organization owners.
| OrgMemberRoleMember -- ^ Non-owner organization members.
deriving (Show, Eq, Ord, Enum, Bounded, Data, Generic)
-- | Request query string
type QueryString = [(BS.ByteString, Maybe BS.ByteString)]
-- | Count of elements
type Count = Int
data MembershipRole
= MembershipRoleMember
| MembershipRoleAdmin
| MembershipRoleBillingManager
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Data)
instance NFData MembershipRole
instance Binary MembershipRole
instance FromJSON MembershipRole where
parseJSON = withText "MembershipRole" $ \t -> case T.toLower t of
"member" -> pure MembershipRoleMember
"admin" -> pure MembershipRoleAdmin
"billing_manager" -> pure MembershipRoleBillingManager
_ -> fail $ "Unknown MembershipRole: " <> T.unpack t
data MembershipState
= MembershipPending
| MembershipActive
deriving (Show, Data, Eq, Ord, Generic)
instance NFData MembershipState
instance Binary MembershipState
instance FromJSON MembershipState where
parseJSON = withText "MembershipState" $ \t -> case T.toLower t of
"active" -> pure MembershipActive
"pending" -> pure MembershipPending
_ -> fail $ "Unknown MembershipState: " <> T.unpack t
data Membership = Membership
{ membershipUrl :: !URL
, membershipState :: !MembershipState
, membershipRole :: !MembershipRole
, membershipOrganizationUrl :: !URL
, membershipOrganization :: !SimpleOrganization
, membershipUser :: !SimpleUser
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData Membership
instance Binary Membership
instance FromJSON Membership where
parseJSON = withObject "Membership" $ \o -> Membership
<$> o .: "url"
<*> o .: "state"
<*> o .: "role"
<*> o .: "organization_url"
<*> o .: "organization"
<*> o .: "user"
-------------------------------------------------------------------------------
-- IssueNumber
-------------------------------------------------------------------------------
newtype IssueNumber = IssueNumber Int
deriving (Eq, Ord, Show, Generic, Data)
unIssueNumber :: IssueNumber -> Int
unIssueNumber (IssueNumber i) = i
instance Hashable IssueNumber
instance Binary IssueNumber
instance NFData IssueNumber where
rnf (IssueNumber s) = rnf s
instance FromJSON IssueNumber where
parseJSON = fmap IssueNumber . parseJSON
instance ToJSON IssueNumber where
toJSON = toJSON . unIssueNumber
-------------------------------------------------------------------------------
-- IssueLabel
-------------------------------------------------------------------------------
data IssueLabel = IssueLabel
{ labelColor :: !Text
, labelUrl :: !URL
, labelName :: !(Name IssueLabel)
, labelDesc :: !(Maybe Text)
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData IssueLabel
instance Binary IssueLabel
instance FromJSON IssueLabel where
parseJSON = withObject "IssueLabel" $ \o -> IssueLabel
<$> o .: "color"
<*> o .:? "url" .!= URL "" -- in events there aren't URL
<*> o .: "name"
<*> o .:? "description"
-------------------------------------------------------------------------------
-- NewIssueLabel
-------------------------------------------------------------------------------
data NewIssueLabel = NewIssueLabel
{ newLabelColor :: !Text
, newLabelName :: !(Name NewIssueLabel)
, newLabelDesc :: !(Maybe Text)
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData NewIssueLabel
instance Binary NewIssueLabel
instance ToJSON NewIssueLabel where
toJSON (NewIssueLabel color lblName lblDesc) = object $ filter notNull
[ "name" .= lblName
, "color" .= color
, "description" .= lblDesc
]
where
notNull (_, Null) = False
notNull (_, _) = True
-------------------------------------------------------------------------------
-- UpdateIssueLabel
-------------------------------------------------------------------------------
data UpdateIssueLabel = UpdateIssueLabel
{ updateLabelColor :: !Text
, updateLabelName :: !(Name UpdateIssueLabel)
, updateLabelDesc :: !(Maybe Text)
}
deriving (Show, Data, Eq, Ord, Generic)
instance NFData UpdateIssueLabel
instance Binary UpdateIssueLabel
instance ToJSON UpdateIssueLabel where
toJSON (UpdateIssueLabel color lblName lblDesc) = object $ filter notNull
[ "new_name" .= lblName
, "color" .= color
, "description" .= lblDesc
]
where
notNull (_, Null) = False
notNull (_, _) = True