-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHub.elm
435 lines (341 loc) · 11 KB
/
GitHub.elm
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
module GitHub
exposing
( APIError
, Repo
, Issue
, IssueState(..)
, IssueLabel
, Comment
, User
, fetchOrgMembers
, fetchOrgRepos
, fetchRepoIssues
, fetchIssue
, fetchIssueComments
, issueScore
, reactionCodes
, reactionScore
)
import Date exposing (Date)
import Dict exposing (Dict)
import Http
import HttpBuilder
import Json.Decode
import Json.Decode.Extra exposing ((|:))
import Regex exposing (Regex)
import String
import Task exposing (Task)
import Pagination
type alias Token =
String
type alias APIError =
{ message : String
}
type alias Repo =
{ id : Int
, url : String
, owner : User
, name : String
, openIssues : Int
}
type alias Issue =
{ repo : Repo
, id : Int
, createdAt : Date
, updatedAt : Date
, url : String
, state : IssueState
, isPullRequest : Bool
, user : User
, number : Int
, title : String
, commentCount : Int
, reactions : Reactions
, labels : List IssueLabel
}
type alias IssueLabel =
{ name : String
, color : String
}
type IssueState
= IssueStateOpen
| IssueStateClosed
type alias Comment =
{ issue : Issue
, id : Int
, createdAt : Date
, updatedAt : Date
, url : String
, user : User
, reactions : Reactions
}
type alias User =
{ id : Int
, url : String
, login : String
, avatar : String
}
type alias Reactions =
{ plusOne : Int
, minusOne : Int
, laugh : Int
, confused : Int
, heart : Int
, hooray : Int
}
reactionCodes : Reactions -> List ( String, Int )
reactionCodes reactions =
[ ( "👍", reactions.plusOne )
, ( "👎", reactions.minusOne )
, ( "😄", reactions.laugh )
, ( "😕", reactions.confused )
, ( "🎉", reactions.heart )
, ( "💖", reactions.hooray )
]
issueScore : Issue -> Int
issueScore { reactions, commentCount, isPullRequest } =
reactionScore reactions
+ (2 * commentCount)
+ (if isPullRequest then
1000
else
0
)
reactionScore : Reactions -> Int
reactionScore reactions =
List.sum
[ 2 * reactions.plusOne
, -2 * reactions.minusOne
, 1 * reactions.laugh
, -1 * reactions.confused
, 3 * reactions.heart
, 3 * reactions.hooray
]
auth : String -> List (String, String)
auth token =
if token == "" then
[]
else
[ ("Authorization", "token " ++ token) ]
authHeaders : String -> List Http.Header
authHeaders = List.map (uncurry Http.header) << auth
fetchOrgMembers : Token -> String -> Task Http.Error (List User)
fetchOrgMembers token org =
Pagination.fetchAll
("https://api.github.com/orgs/" ++ org ++ "/members?per_page=100")
(authHeaders token)
(rfc5988Strategy decodeUser)
Nothing
fetchOrgRepos : Token -> String -> Task Http.Error (List Repo)
fetchOrgRepos token org =
Pagination.fetchAll
("https://api.github.com/orgs/" ++ org ++ "/repos?per_page=100")
(authHeaders token)
(rfc5988Strategy decodeRepo)
Nothing
fetchRepoIssues : Token -> Repo -> Task Http.Error (List Issue)
fetchRepoIssues token repo =
if repo.openIssues == 0 then
Task.succeed []
else
Pagination.fetchAll
("https://api.github.com/repos/" ++ repo.owner.login ++ "/" ++ repo.name ++ "/issues?per_page=100")
(Http.header "Accept" "application/vnd.github.squirrel-girl-preview" :: authHeaders token)
(rfc5988Strategy (decodeIssue repo))
Nothing
fetchIssue : Token -> Repo -> Int -> Task Http.Error Issue
fetchIssue token repo number =
HttpBuilder.get ("https://api.github.com/repos/" ++ repo.owner.login ++ "/" ++ repo.name ++ "/issues/" ++ toString number)
|> HttpBuilder.withHeaders (auth token)
|> HttpBuilder.withHeader "Accept" "application/vnd.github.squirrel-girl-preview"
|> HttpBuilder.withExpect (Http.expectJson (decodeIssue repo))
|> HttpBuilder.toTask
fetchIssueComments : Token -> Issue -> Task Http.Error (List Comment)
fetchIssueComments token issue =
Pagination.fetchAll
("https://api.github.com/repos/" ++ issue.repo.owner.login ++ "/" ++ issue.repo.name ++ "/issues/" ++ toString issue.number ++ "/comments?per_page=100")
(Http.header "Accept" "application/vnd.github.squirrel-girl-preview" :: authHeaders token)
(rfc5988Strategy (decodeComment issue))
Nothing
decodeError : Json.Decode.Decoder APIError
decodeError =
Json.Decode.map APIError
(Json.Decode.field "message" Json.Decode.string)
decodeRepo : Json.Decode.Decoder Repo
decodeRepo =
Json.Decode.map5 Repo
(Json.Decode.field "id" Json.Decode.int)
(Json.Decode.field "html_url" Json.Decode.string)
(Json.Decode.field "owner" decodeUser)
(Json.Decode.field "name" Json.Decode.string)
(Json.Decode.field "open_issues_count" Json.Decode.int)
decodeIssue : Repo -> Json.Decode.Decoder Issue
decodeIssue repo =
Json.Decode.succeed (Issue repo)
|: (Json.Decode.field "id" Json.Decode.int)
|: (Json.Decode.field "created_at" Json.Decode.Extra.date)
|: (Json.Decode.field "updated_at" Json.Decode.Extra.date)
|: (Json.Decode.field "html_url" Json.Decode.string)
|: (Json.Decode.field "state" decodeIssueState)
|: (Json.Decode.map ((/=) Nothing) << Json.Decode.maybe <| Json.Decode.field "pull_request" Json.Decode.value)
|: (Json.Decode.field "user" decodeUser)
|: (Json.Decode.field "number" Json.Decode.int)
|: (Json.Decode.field "title" Json.Decode.string)
|: (Json.Decode.field "comments" <| excludeTracksuitComment (Json.Decode.int))
|: (Json.Decode.field "reactions" decodeReactions)
|: (Json.Decode.field "labels" <| Json.Decode.list decodeIssueLabel)
decodeIssueState : Json.Decode.Decoder IssueState
decodeIssueState =
customDecoder Json.Decode.string <|
\x ->
case x of
"open" ->
Ok IssueStateOpen
"closed" ->
Ok IssueStateClosed
_ ->
Err ("unknown issue state: " ++ x)
decodeIssueLabel : Json.Decode.Decoder IssueLabel
decodeIssueLabel =
Json.Decode.map2 IssueLabel
(Json.Decode.field "name" Json.Decode.string)
(Json.Decode.field "color" Json.Decode.string)
decodeComment : Issue -> Json.Decode.Decoder Comment
decodeComment issue =
Json.Decode.map6 (Comment issue)
(Json.Decode.field "id" Json.Decode.int)
(Json.Decode.field "created_at" Json.Decode.Extra.date)
(Json.Decode.field "updated_at" Json.Decode.Extra.date)
(Json.Decode.field "html_url" Json.Decode.string)
(Json.Decode.field "user" decodeUser)
(Json.Decode.field "reactions" decodeReactions)
decodeUser : Json.Decode.Decoder User
decodeUser =
Json.Decode.map4 User
(Json.Decode.field "id" Json.Decode.int)
(Json.Decode.field "html_url" Json.Decode.string)
(Json.Decode.field "login" Json.Decode.string)
(Json.Decode.field "avatar_url" Json.Decode.string)
excludeTracksuitComment : Json.Decode.Decoder Int -> Json.Decode.Decoder Int
excludeTracksuitComment =
Json.Decode.map (flip (-) 1)
decodeReactions : Json.Decode.Decoder Reactions
decodeReactions =
Json.Decode.map6 Reactions
(Json.Decode.field "+1" Json.Decode.int)
(Json.Decode.field "-1" Json.Decode.int)
(Json.Decode.field "laugh" Json.Decode.int)
(Json.Decode.field "confused" Json.Decode.int)
(Json.Decode.field "heart" Json.Decode.int)
(Json.Decode.field "hooray" Json.Decode.int)
rfc5988Strategy : Json.Decode.Decoder a -> Pagination.Strategy Int a
rfc5988Strategy decode =
{ onPage = flip addParams
, nextPage =
parseLink nextRel
, previousPage =
parseLink previousRel
, content =
Json.Decode.list decode
}
parseLink : String -> Http.Response a -> Maybe Int
parseLink rel response =
Dict.get "Link" response.headers
|> Maybe.andThen
(\commaSeparatedCraziness ->
let
headers =
String.split ", " commaSeparatedCraziness
parsed =
Dict.fromList <| List.filterMap parseLinkTuple headers
in
Dict.get rel parsed |> Maybe.andThen parseParams
)
previousRel : String
previousRel =
"prev"
nextRel : String
nextRel =
"next"
linkHeaderRegex : Regex
linkHeaderRegex =
Regex.regex ("<([^>]+)>; rel=\"(" ++ previousRel ++ "|" ++ nextRel ++ ")\"")
parseLinkTuple : String -> Maybe ( String, String )
parseLinkTuple header =
case Regex.find (Regex.AtMost 1) linkHeaderRegex header of
[] ->
Nothing
{ submatches } :: _ ->
case submatches of
(Just url) :: (Just rel) :: _ ->
Just ( rel, url )
_ ->
Nothing
parseParams : String -> Maybe Int
parseParams =
fromQuery << Tuple.second << extractQuery
addParams : String -> Int -> String
addParams url page =
let
( baseURL, query ) =
extractQuery url
in
setQuery baseURL (Dict.union query (toQuery page))
extractQuery : String -> ( String, Dict String String )
extractQuery url =
case String.split "?" url of
baseURL :: query :: _ ->
( baseURL, parseQuery query )
_ ->
( url, Dict.empty )
setQuery : String -> Dict String String -> String
setQuery baseURL query =
let
params =
String.join "&" <|
List.map (\( k, v ) -> k ++ "=" ++ v) (Dict.toList query)
in
if params == "" then
baseURL
else
baseURL ++ "?" ++ params
parseQuery : String -> Dict String String
parseQuery query =
let
parseParam p =
case String.split "=" p of
k :: vs ->
( k, String.join "=" vs )
[] ->
( "", "" )
in
Dict.fromList <|
List.map parseParam <|
String.split "&" query
fromQuery : Dict String String -> Maybe Int
fromQuery query =
let
num =
Maybe.withDefault 1 (
Dict.get "page" query
|> Maybe.andThen parseNum
)
in
Just num
toQuery : Int -> Dict String String
toQuery page =
Dict.singleton "page" (toString page)
parseNum : String -> Maybe Int
parseNum =
Result.toMaybe << String.toInt
customDecoder : Json.Decode.Decoder b -> (b -> Result String a) -> Json.Decode.Decoder a
customDecoder decoder toResult =
Json.Decode.andThen
(\a ->
case toResult a of
Ok b ->
Json.Decode.succeed b
Err err ->
Json.Decode.fail err
)
decoder