forked from haskell-github/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.hs
More file actions
609 lines (507 loc) · 19.3 KB
/
Options.hs
File metadata and controls
609 lines (507 loc) · 19.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- License : BSD-3-Clause
-- Maintainer : Oleg Grenrus <[email protected]>
--
-- Module with modifiers for pull requests' and issues' listings.
module GitHub.Data.Options (
-- * Common modifiers
stateOpen,
stateClosed,
stateAll,
sortAscending,
sortDescending,
sortByCreated,
sortByUpdated,
-- * Pull Requests
PullRequestMod,
prModToQueryString,
optionsBase,
optionsNoBase,
optionsHead,
optionsNoHead,
sortByPopularity,
sortByLongRunning,
-- * Issues
IssueMod,
issueModToQueryString,
sortByComments,
optionsLabels,
optionsSince,
optionsSinceAll,
optionsAssignedIssues,
optionsCreatedIssues,
optionsMentionedIssues,
optionsSubscribedIssues,
optionsAllIssues,
-- * Repo issues
IssueRepoMod,
issueRepoModToQueryString,
optionsIrrelevantMilestone,
optionsAnyMilestone,
optionsNoMilestone,
optionsIrrelevantAssignee,
optionsAnyAssignee,
optionsNoAssignee,
-- * Data
IssueState (..),
MergeableState (..),
-- * Internal
HasState,
HasDirection,
HasCreatedUpdated,
HasComments,
HasLabels,
HasSince,
) where
import GitHub.Data.Definitions
import GitHub.Data.Id (Id, untagId)
import GitHub.Data.Milestone (Milestone)
import GitHub.Data.Name (Name, untagName)
import GitHub.Internal.Prelude
import Prelude ()
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
-------------------------------------------------------------------------------
-- Data
-------------------------------------------------------------------------------
-- | 'GitHub.Data.Issues.Issue' or 'GitHub.Data.PullRequests.PullRequest' state
data IssueState
= StateOpen
| StateClosed
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Typeable, Data)
instance ToJSON IssueState where
toJSON StateOpen = String "open"
toJSON StateClosed = String "closed"
instance FromJSON IssueState where
parseJSON = withText "IssueState" $ \t -> case T.toLower t of
"open" -> pure StateOpen
"closed" -> pure StateClosed
_ -> fail $ "Unknown IssueState: " <> T.unpack t
instance NFData IssueState where rnf = genericRnf
instance Binary IssueState
-- | 'GitHub.Data.PullRequests.PullRequest' mergeable_state
data MergeableState
= StateUnknown
| StateClean
| StateDirty
| StateUnstable
| StateBlocked
| StateBehind
| StateDraft
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Typeable, Data)
instance ToJSON MergeableState where
toJSON StateUnknown = String "unknown"
toJSON StateClean = String "clean"
toJSON StateDirty = String "dirty"
toJSON StateUnstable = String "unstable"
toJSON StateBlocked = String "blocked"
toJSON StateBehind = String "behind"
toJSON StateDraft = String "draft"
instance FromJSON MergeableState where
parseJSON = withText "MergeableState" $ \t -> case T.toLower t of
"unknown" -> pure StateUnknown
"clean" -> pure StateClean
"dirty" -> pure StateDirty
"unstable" -> pure StateUnstable
"blocked" -> pure StateBlocked
"behind" -> pure StateBehind
"draft" -> pure StateDraft
_ -> fail $ "Unknown MergeableState: " <> T.unpack t
instance NFData MergeableState where rnf = genericRnf
instance Binary MergeableState
data SortDirection
= SortAscending
| SortDescending
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Typeable, Data)
instance NFData SortDirection where rnf = genericRnf
instance Binary SortDirection
-- PR
data SortPR
= SortPRCreated
| SortPRUpdated
| SortPRPopularity
| SortPRLongRunning
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Typeable, Data)
instance NFData SortPR where rnf = genericRnf
instance Binary SortPR
-- Issue
data IssueFilter
= IssueFilterAssigned
| IssueFilterCreated
| IssueFilterMentioned
| IssueFilterSubscribed
| IssueFilterAll
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Typeable, Data)
instance NFData IssueFilter where rnf = genericRnf
instance Binary IssueFilter
data SortIssue
= SortIssueCreated
| SortIssueUpdated
| SortIssueComments
deriving
(Eq, Ord, Show, Enum, Bounded, Generic, Typeable, Data)
instance NFData SortIssue where rnf = genericRnf
instance Binary SortIssue
data FilterBy a
= FilterAny
| FilterNone
| FilterBy a
| FilterNotSpecified
-- ^ e.g. for milestones "any" means "any milestone".
-- I.e. won't show issues without mileston specified
deriving
(Eq, Ord, Show, Generic, Typeable, Data)
-------------------------------------------------------------------------------
-- Classes
-------------------------------------------------------------------------------
class HasState mod where
state :: Maybe IssueState -> mod
stateOpen :: HasState mod => mod
stateOpen = state (Just StateOpen)
stateClosed :: HasState mod => mod
stateClosed = state (Just StateClosed)
stateAll :: HasState mod => mod
stateAll = state Nothing
instance HasState PullRequestMod where
state s = PRMod $ \opts ->
opts { pullRequestOptionsState = s }
instance HasState IssueMod where
state s = IssueMod $ \opts ->
opts { issueOptionsState = s }
instance HasState IssueRepoMod where
state s = IssueRepoMod $ \opts ->
opts { issueRepoOptionsState = s }
class HasDirection mod where
sortDir :: SortDirection -> mod
sortAscending :: HasDirection mod => mod
sortAscending = sortDir SortAscending
sortDescending :: HasDirection mod => mod
sortDescending = sortDir SortDescending
instance HasDirection PullRequestMod where
sortDir x = PRMod $ \opts ->
opts { pullRequestOptionsDirection = x }
instance HasDirection IssueMod where
sortDir x = IssueMod $ \opts ->
opts { issueOptionsDirection = x }
instance HasDirection IssueRepoMod where
sortDir x = IssueRepoMod $ \opts ->
opts { issueRepoOptionsDirection = x }
class HasCreatedUpdated mod where
sortByCreated :: mod
sortByUpdated :: mod
instance HasCreatedUpdated PullRequestMod where
sortByCreated = PRMod $ \opts ->
opts { pullRequestOptionsSort = SortPRCreated }
sortByUpdated = PRMod $ \opts ->
opts { pullRequestOptionsSort = SortPRUpdated }
instance HasCreatedUpdated IssueMod where
sortByCreated = IssueMod $ \opts ->
opts { issueOptionsSort = SortIssueCreated }
sortByUpdated = IssueMod $ \opts ->
opts { issueOptionsSort = SortIssueUpdated }
instance HasCreatedUpdated IssueRepoMod where
sortByCreated = IssueRepoMod $ \opts ->
opts { issueRepoOptionsSort = SortIssueCreated }
sortByUpdated = IssueRepoMod $ \opts ->
opts { issueRepoOptionsSort = SortIssueUpdated }
-------------------------------------------------------------------------------
-- Pull Request
-------------------------------------------------------------------------------
-- | See <https://developer.github.com/v3/pulls/#parameters>.
data PullRequestOptions = PullRequestOptions
{ pullRequestOptionsState :: !(Maybe IssueState)
, pullRequestOptionsHead :: !(Maybe Text)
, pullRequestOptionsBase :: !(Maybe Text)
, pullRequestOptionsSort :: !SortPR
, pullRequestOptionsDirection :: !SortDirection
}
deriving
(Eq, Ord, Show, Generic, Typeable, Data)
defaultPullRequestOptions :: PullRequestOptions
defaultPullRequestOptions = PullRequestOptions
{ pullRequestOptionsState = Just StateOpen
, pullRequestOptionsHead = Nothing
, pullRequestOptionsBase = Nothing
, pullRequestOptionsSort = SortPRCreated
, pullRequestOptionsDirection = SortDescending
}
-- | See <https://developer.github.com/v3/pulls/#parameters>.
newtype PullRequestMod = PRMod (PullRequestOptions -> PullRequestOptions)
instance Semigroup PullRequestMod where
PRMod f <> PRMod g = PRMod (g . f)
instance Monoid PullRequestMod where
mempty = PRMod id
mappend = (<>)
toPullRequestOptions :: PullRequestMod -> PullRequestOptions
toPullRequestOptions (PRMod f) = f defaultPullRequestOptions
prModToQueryString :: PullRequestMod -> QueryString
prModToQueryString = pullRequestOptionsToQueryString . toPullRequestOptions
pullRequestOptionsToQueryString :: PullRequestOptions -> QueryString
pullRequestOptionsToQueryString (PullRequestOptions st head_ base sort dir) =
[ mk "state" state'
, mk "sort" sort'
, mk "direction" direction'
] ++ catMaybes
[ mk "head" <$> head'
, mk "base" <$> base'
]
where
mk k v = (k, Just v)
state' = case st of
Nothing -> "all"
Just StateOpen -> "open"
Just StateClosed -> "closed"
sort' = case sort of
SortPRCreated -> "created"
SortPRUpdated -> "updated"
SortPRPopularity -> "popularity"
SortPRLongRunning -> "long-running"
direction' = case dir of
SortDescending -> "desc"
SortAscending -> "asc"
head' = fmap TE.encodeUtf8 head_
base' = fmap TE.encodeUtf8 base
-------------------------------------------------------------------------------
-- Pull request modifiers
-------------------------------------------------------------------------------
optionsBase :: Text -> PullRequestMod
optionsBase x = PRMod $ \opts ->
opts { pullRequestOptionsBase = Just x }
optionsNoBase :: PullRequestMod
optionsNoBase = PRMod $ \opts ->
opts { pullRequestOptionsBase = Nothing }
optionsHead :: Text -> PullRequestMod
optionsHead x = PRMod $ \opts ->
opts { pullRequestOptionsHead = Just x }
optionsNoHead :: PullRequestMod
optionsNoHead = PRMod $ \opts ->
opts { pullRequestOptionsHead = Nothing }
sortByPopularity :: PullRequestMod
sortByPopularity = PRMod $ \opts ->
opts { pullRequestOptionsSort = SortPRPopularity }
sortByLongRunning :: PullRequestMod
sortByLongRunning = PRMod $ \opts ->
opts { pullRequestOptionsSort = SortPRLongRunning }
-------------------------------------------------------------------------------
-- Issues
-------------------------------------------------------------------------------
-- | See <https://developer.github.com/v3/issues/#parameters>.
data IssueOptions = IssueOptions
{ issueOptionsFilter :: !IssueFilter
, issueOptionsState :: !(Maybe IssueState)
, issueOptionsLabels :: ![Name IssueLabel] -- TODO: change to newtype
, issueOptionsSort :: !SortIssue
, issueOptionsDirection :: !SortDirection
, issueOptionsSince :: !(Maybe UTCTime)
}
deriving
(Eq, Ord, Show, Generic, Typeable, Data)
defaultIssueOptions :: IssueOptions
defaultIssueOptions = IssueOptions
{ issueOptionsFilter = IssueFilterAssigned
, issueOptionsState = Just StateOpen
, issueOptionsLabels = []
, issueOptionsSort = SortIssueCreated
, issueOptionsDirection = SortDescending
, issueOptionsSince = Nothing
}
-- | See <https://developer.github.com/v3/issues/#parameters>.
newtype IssueMod = IssueMod (IssueOptions -> IssueOptions)
instance Semigroup IssueMod where
IssueMod f <> IssueMod g = IssueMod (g . f)
instance Monoid IssueMod where
mempty = IssueMod id
mappend = (<>)
toIssueOptions :: IssueMod -> IssueOptions
toIssueOptions (IssueMod f) = f defaultIssueOptions
issueModToQueryString :: IssueMod -> QueryString
issueModToQueryString = issueOptionsToQueryString . toIssueOptions
issueOptionsToQueryString :: IssueOptions -> QueryString
issueOptionsToQueryString (IssueOptions filt st labels sort dir since) =
[ mk "state" state'
, mk "sort" sort'
, mk "direction" direction'
, mk "filter" filt'
] ++ catMaybes
[ mk "labels" <$> labels'
, mk "since" <$> since'
]
where
mk k v = (k, Just v)
filt' = case filt of
IssueFilterAssigned -> "assigned"
IssueFilterCreated -> "created"
IssueFilterMentioned -> "mentioned"
IssueFilterSubscribed -> "subscribed"
IssueFilterAll -> "all"
state' = case st of
Nothing -> "all"
Just StateOpen -> "open"
Just StateClosed -> "closed"
sort' = case sort of
SortIssueCreated -> "created"
SortIssueUpdated -> "updated"
SortIssueComments -> "comments"
direction' = case dir of
SortDescending -> "desc"
SortAscending -> "asc"
since' = fmap (TE.encodeUtf8 . T.pack . show) since
labels' = TE.encodeUtf8 . T.intercalate "," . fmap untagName <$> nullToNothing labels
nullToNothing :: Foldable f => f a -> Maybe (f a)
nullToNothing xs
| null xs = Nothing
| otherwise = Just xs
-------------------------------------------------------------------------------
-- Issues modifiers
-------------------------------------------------------------------------------
class HasComments mod where
sortByComments :: mod
instance HasComments IssueMod where
sortByComments = IssueMod $ \opts ->
opts { issueOptionsSort = SortIssueComments }
instance HasComments IssueRepoMod where
sortByComments = IssueRepoMod $ \opts ->
opts { issueRepoOptionsSort = SortIssueComments }
class HasLabels mod where
optionsLabels :: Foldable f => f (Name IssueLabel) -> mod
instance HasLabels IssueMod where
optionsLabels lbls = IssueMod $ \opts ->
opts { issueOptionsLabels = toList lbls }
instance HasLabels IssueRepoMod where
optionsLabels lbls = IssueRepoMod $ \opts ->
opts { issueRepoOptionsLabels = toList lbls }
class HasSince mod where
optionsSince :: UTCTime -> mod
optionsSinceAll :: mod
instance HasSince IssueMod where
optionsSince since = IssueMod $ \opts ->
opts { issueOptionsSince = Just since }
optionsSinceAll = IssueMod $ \opts ->
opts { issueOptionsSince = Nothing }
instance HasSince IssueRepoMod where
optionsSince since = IssueRepoMod $ \opts ->
opts { issueRepoOptionsSince = Just since }
optionsSinceAll = IssueRepoMod $ \opts ->
opts { issueRepoOptionsSince = Nothing }
-------------------------------------------------------------------------------
-- Only issues modifiers
-------------------------------------------------------------------------------
optionsAssignedIssues, optionsCreatedIssues, optionsMentionedIssues,
optionsSubscribedIssues, optionsAllIssues :: IssueMod
optionsAssignedIssues = issueFilter IssueFilterAssigned
optionsCreatedIssues = issueFilter IssueFilterCreated
optionsMentionedIssues = issueFilter IssueFilterMentioned
optionsSubscribedIssues = issueFilter IssueFilterSubscribed
optionsAllIssues = issueFilter IssueFilterAll
issueFilter :: IssueFilter -> IssueMod
issueFilter f = IssueMod $ \opts ->
opts { issueOptionsFilter = f }
-------------------------------------------------------------------------------
-- Issues repo
-------------------------------------------------------------------------------
data IssueRepoOptions = IssueRepoOptions
{ issueRepoOptionsMilestone :: !(FilterBy (Id Milestone))
, issueRepoOptionsState :: !(Maybe IssueState)
, issueRepoOptionsAssignee :: !(FilterBy (Name User))
, issueRepoOptionsCreator :: !(Maybe (Name User))
, issueRepoOptionsMentioned :: !(Maybe (Name User))
, issueRepoOptionsLabels :: ![Name IssueLabel]
, issueRepoOptionsSort :: !SortIssue
, issueRepoOptionsDirection :: !SortDirection
, issueRepoOptionsSince :: !(Maybe UTCTime)
}
deriving
(Eq, Ord, Show, Generic, Typeable, Data)
defaultIssueRepoOptions :: IssueRepoOptions
defaultIssueRepoOptions = IssueRepoOptions
{ issueRepoOptionsMilestone = FilterNotSpecified
, issueRepoOptionsState = (Just StateOpen)
, issueRepoOptionsAssignee = FilterNotSpecified
, issueRepoOptionsCreator = Nothing
, issueRepoOptionsMentioned = Nothing
, issueRepoOptionsLabels = []
, issueRepoOptionsSort = SortIssueCreated
, issueRepoOptionsDirection = SortDescending
, issueRepoOptionsSince = Nothing
}
-- | See <https://developer.github.com/v3/issues/#parameters-1>.
newtype IssueRepoMod = IssueRepoMod (IssueRepoOptions -> IssueRepoOptions)
instance Semigroup IssueRepoMod where
IssueRepoMod f <> IssueRepoMod g = IssueRepoMod (g . f)
instance Monoid IssueRepoMod where
mempty = IssueRepoMod id
mappend = (<>)
toIssueRepoOptions :: IssueRepoMod -> IssueRepoOptions
toIssueRepoOptions (IssueRepoMod f) = f defaultIssueRepoOptions
issueRepoModToQueryString :: IssueRepoMod -> QueryString
issueRepoModToQueryString = issueRepoOptionsToQueryString . toIssueRepoOptions
issueRepoOptionsToQueryString :: IssueRepoOptions -> QueryString
issueRepoOptionsToQueryString IssueRepoOptions {..} =
[ mk "state" state'
, mk "sort" sort'
, mk "direction" direction'
] ++ catMaybes
[ mk "milestone" <$> milestone'
, mk "assignee" <$> assignee'
, mk "labels" <$> labels'
, mk "since" <$> since'
, mk "creator" <$> creator'
, mk "mentioned" <$> mentioned'
]
where
mk k v = (k, Just v)
filt f x = case x of
FilterAny -> Just "*"
FilterNone -> Just "none"
FilterBy x' -> Just $ TE.encodeUtf8 $ f x'
FilterNotSpecified -> Nothing
milestone' = filt (T.pack . show . untagId) issueRepoOptionsMilestone
assignee' = filt untagName issueRepoOptionsAssignee
state' = case issueRepoOptionsState of
Nothing -> "all"
Just StateOpen -> "open"
Just StateClosed -> "closed"
sort' = case issueRepoOptionsSort of
SortIssueCreated -> "created"
SortIssueUpdated -> "updated"
SortIssueComments -> "comments"
direction' = case issueRepoOptionsDirection of
SortDescending -> "desc"
SortAscending -> "asc"
since' = TE.encodeUtf8 . T.pack . show <$> issueRepoOptionsSince
labels' = TE.encodeUtf8 . T.intercalate "," . fmap untagName <$> nullToNothing issueRepoOptionsLabels
creator' = TE.encodeUtf8 . untagName <$> issueRepoOptionsCreator
mentioned' = TE.encodeUtf8 . untagName <$> issueRepoOptionsMentioned
-------------------------------------------------------------------------------
-- Issues repo modifiers
-------------------------------------------------------------------------------
-- | Don't care about milestones.
--
-- 'optionsAnyMilestone' means there should be some milestone, but it can be any.
--
-- See <https://developer.github.com/v3/issues/#list-issues-for-a-repository>
optionsIrrelevantMilestone :: IssueRepoMod
optionsIrrelevantMilestone = IssueRepoMod $ \opts ->
opts { issueRepoOptionsMilestone = FilterNotSpecified }
optionsAnyMilestone :: IssueRepoMod
optionsAnyMilestone = IssueRepoMod $ \opts ->
opts { issueRepoOptionsMilestone = FilterAny }
optionsNoMilestone :: IssueRepoMod
optionsNoMilestone = IssueRepoMod $ \opts ->
opts { issueRepoOptionsMilestone = FilterNone }
optionsIrrelevantAssignee :: IssueRepoMod
optionsIrrelevantAssignee = IssueRepoMod $ \opts ->
opts { issueRepoOptionsAssignee = FilterNotSpecified }
optionsAnyAssignee :: IssueRepoMod
optionsAnyAssignee = IssueRepoMod $ \opts ->
opts { issueRepoOptionsAssignee = FilterAny }
optionsNoAssignee :: IssueRepoMod
optionsNoAssignee = IssueRepoMod $ \opts ->
opts { issueRepoOptionsAssignee = FilterNone }