-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.elm
673 lines (539 loc) · 20 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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
module GitHubStats exposing (CurrentData(..), Model, Msg(..), Organization, PullRequestButton(..), Repo, RepoPullRequest, RepoPullRequests, Repos, User, UserPullRequest, UserPullRequests, Users, baseUrl, bearerTokenForm, decodeOrganization, decodeRepo, decodeRepoPullRequest, decodeRepoPullRequests, decodeRepos, decodeUser, decodeUserPullRequest, decodeUserPullRequests, decodeUsers, displayOrganization, displayRepo, displayRepoPullRequests, displayTable, displayUser, displayUserPullRequests, getRepoPullRequests, getUserPullRequests, getUsersAndRepos, init, initialModel, locChange, main, pullRequestQuantityButtons, repoPullRequestDataRow, request, site, update, userPullRequestDataRow, view)
import Html exposing (..)
import Html.Attributes exposing (attribute, class, href, id, scope, target, title)
import Html.Attributes.A11y exposing (columnHeader, group, labelledBy, search)
import Html.Events exposing (onClick, onInput)
import Http
import Json.Decode exposing (at, decodeString, field, int, string)
import Json.Decode.Pipeline exposing (decode, hardcoded, optional, required, requiredAt)
import Json.Encode as Encode
import Regex exposing (regex, replace)
import Svg exposing (path, svg)
import Svg.Attributes exposing (d, fill, viewBox)
type alias Model =
{ message : String
, organization : Maybe Organization
, userPullRequests : Maybe UserPullRequests
, repoPullRequests : Maybe RepoPullRequests
, filterVisible : Bool
, currentData : CurrentData
, currentUser : String
, currentRepo : String
, bearerToken : Maybe String
}
type alias Organization =
{ users : List Users
, repos : List Repos
}
type alias Users =
{ node : User }
type alias Repos =
{ node : Repo }
type alias User =
{ id : String
, login : String
}
type alias Repo =
{ id : String
, name : String
}
type alias UserPullRequests =
{ nodes : List UserPullRequest }
type alias RepoPullRequests =
{ nodes : List RepoPullRequest }
type alias UserPullRequest =
{ typename : String
, prNumber : Int
, title : String
, createdAt : String
, additions : Int
, deletions : Int
, repoName : String
, url : String
}
type alias RepoPullRequest =
{ prNumber : Int
, title : String
, createdAt : String
, additions : Int
, deletions : Int
, author : String
, url : String
}
type CurrentData
= ByUser
| ByRepo
type PullRequestButton
= UserPullRequestButton
| RepoPullRequestButton
-- UPDATE
type Msg
= ParseOrgJson (Result Http.Error String)
| ParseUserPullRequestJson (Result Http.Error String)
| ParseRepoPullRequestJson (Result Http.Error String)
| DisplayUserData String
| DisplayRepoData String
| UpdateUserPullRequestQuantity String
| UpdateRepoPullRequestQuantity String
| ToggleFilter
| UpdateBearerToken String
| SubmitForm
| None
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ParseOrgJson (Ok res) ->
case decodeString decodeOrganization res of
Ok res ->
( { model | organization = Just res, message = "" }, Cmd.none )
Err error ->
( { model | message = error, organization = Nothing }, Cmd.none )
ParseOrgJson (Err res) ->
( { model | organization = Nothing }, Cmd.none )
DisplayUserData user ->
let
json =
Regex.replace Regex.All (Regex.regex "user") (\_ -> user) getUserPullRequests
token =
model.bearerToken
in
case token of
Just token ->
( { model | currentUser = user, currentData = ByUser }, Http.send ParseUserPullRequestJson (request token json) )
Nothing ->
( { model | bearerToken = Nothing }, Cmd.none )
DisplayRepoData repo ->
let
json =
Regex.replace Regex.All (Regex.regex "repoName") (\_ -> repo) getRepoPullRequests
token =
model.bearerToken
in
case token of
Just token ->
( { model | currentRepo = repo, currentData = ByRepo }, Http.send ParseRepoPullRequestJson (request token json) )
Nothing ->
( { model | bearerToken = Nothing }, Cmd.none )
ParseUserPullRequestJson (Ok res) ->
case decodeString decodeUserPullRequests res of
Ok res ->
( { model | userPullRequests = Just res }, Cmd.none )
Err error ->
( { model | message = error }, Cmd.none )
ParseUserPullRequestJson (Err res) ->
( { model | message = toString res }, Cmd.none )
ParseRepoPullRequestJson (Ok res) ->
case decodeString decodeRepoPullRequests res of
Ok res ->
( { model | repoPullRequests = Just res }, Cmd.none )
Err error ->
( { model | message = error }, Cmd.none )
ParseRepoPullRequestJson (Err res) ->
( { model | message = toString res }, Cmd.none )
UpdateUserPullRequestQuantity num ->
let
json =
Regex.replace Regex.All (Regex.regex "10") (\_ -> num) getUserPullRequests
updated =
Regex.replace Regex.All (Regex.regex "user") (\_ -> model.currentUser) json
token =
model.bearerToken
in
case token of
Just token ->
( model, Http.send ParseUserPullRequestJson (request token updated) )
Nothing ->
( { model | bearerToken = Nothing }, Cmd.none )
UpdateRepoPullRequestQuantity num ->
let
json =
Regex.replace Regex.All (Regex.regex "10") (\_ -> num) getRepoPullRequests
updated =
Regex.replace Regex.All (Regex.regex "repoName") (\_ -> model.currentRepo) json
token =
model.bearerToken
in
case token of
Just token ->
( model, Http.send ParseRepoPullRequestJson (request token updated) )
Nothing ->
( { model | bearerToken = Nothing }, Cmd.none )
UpdateBearerToken token ->
let
bearerToken =
"Bearer " ++ token
in
( { model | bearerToken = Just bearerToken }, Http.send ParseOrgJson (request bearerToken getUsersAndRepos) )
SubmitForm ->
let
token =
model.bearerToken
in
case token of
Just token ->
( model, Http.send ParseOrgJson (request token getUsersAndRepos) )
Nothing ->
( model, Cmd.none )
ToggleFilter ->
( { model | filterVisible = not model.filterVisible }, Cmd.none )
None ->
( model, Cmd.none )
decodeOrganization =
decode Organization
|> requiredAt [ "data", "organization", "team", "members", "edges" ] (Json.Decode.list decodeUsers)
|> requiredAt [ "data", "organization", "repositories", "edges" ] (Json.Decode.list decodeRepos)
decodeUsers =
decode Users
|> required "node" decodeUser
decodeRepos =
decode Repos
|> required "node" decodeRepo
decodeUser =
decode User
|> required "id" string
|> required "login" string
decodeRepo =
decode Repo
|> required "id" string
|> required "name" string
decodeUserPullRequests =
decode UserPullRequests
|> requiredAt [ "data", "search", "nodes" ] (Json.Decode.list decodeUserPullRequest)
decodeRepoPullRequests =
decode RepoPullRequests
|> requiredAt [ "data", "repository", "pullRequests", "nodes" ] (Json.Decode.list decodeRepoPullRequest)
decodeUserPullRequest =
decode UserPullRequest
|> required "__typename" string
|> required "number" int
|> required "title" string
|> required "createdAt" string
|> required "additions" int
|> required "deletions" int
|> requiredAt [ "repository", "name" ] string
|> required "url" string
decodeRepoPullRequest =
decode RepoPullRequest
|> required "number" int
|> required "title" string
|> required "createdAt" string
|> required "additions" int
|> required "deletions" int
|> requiredAt [ "author", "login" ] string
|> required "url" string
-- VIEW
view : Model -> Html Msg
view model =
div [ class "site-wrapper" ] (site model)
site : Model -> List (Html Msg)
site model =
let
token =
model.bearerToken
in
case token of
Just token ->
[ displayOrganization model
, main_ []
[ displayTable model ]
]
Nothing ->
[ bearerTokenForm model ]
bearerTokenForm : Model -> Html Msg
bearerTokenForm model =
div [ class "entry-form" ]
[ input
[ Html.Attributes.type_ "text"
, attribute "placeholder" "Enter your GitHub token"
, onInput UpdateBearerToken
]
[]
, button
[ class "btn", onClick SubmitForm ]
[ text "Go!" ]
]
displayOrganization : Model -> Html Msg
displayOrganization model =
let
organization =
model.organization
in
case organization of
Just organization ->
let
users =
organization.users
repos =
organization.repos
userNodes =
List.map .node users
repoNodes =
List.map .node repos
in
header
[ class
(if model.filterVisible then
"is-visible"
else
""
)
, search
]
[ button [ onClick ToggleFilter, class "filter", attribute "type" "button" ]
[ svgFilter
, span [] [ text "Filter" ]
]
, div [ class "wrapper" ]
[ h2 [] [ text "Filter By" ]
, section [ class "list-authors" ]
[ h3 [] [ text "Author" ]
, ul [ class "list-reset" ] (List.map displayUser userNodes)
]
, section [ class "list-repos" ]
[ h3 [] [ text "Repo" ]
, ul [ class "list-reset" ] (List.map displayRepo repoNodes)
]
]
]
Nothing ->
div [] []
svgFilter : Html.Html msg
svgFilter =
svg
[ viewBox "18 13 63 71" ]
[ path
[ fill "#FFFFFF"
, d "M80 15l-3-2H21l-3 2v3l15 24c3 6 5 13 5 20v20l2 2 1 1 2-1 15-7 2-3V62c0-7 2-14 5-20l15-24v-3zM60 39c-4 7-6 15-6 23v10l-10 5V62c0-8-2-16-6-23L26 19h46L60 39z"
]
[]
]
displayUser : User -> Html Msg
displayUser user =
li [] [ a [ href "#", onClick (DisplayUserData user.login) ] [ text user.login ] ]
displayRepo : Repo -> Html Msg
displayRepo repo =
li [] [ a [ href "#", onClick (DisplayRepoData repo.name) ] [ text repo.name ] ]
displayTable : Model -> Html Msg
displayTable model =
case model.currentData of
ByUser ->
displayUserPullRequests model
ByRepo ->
displayRepoPullRequests model
displayUserPullRequests : Model -> Html Msg
displayUserPullRequests model =
let
pullRequests =
model.userPullRequests
in
case pullRequests of
Just pullRequests ->
div [ class "table-wrapper" ]
[ pullRequestQuantityButtons UserPullRequestButton
, table [ labelledBy "table-header" ]
[ caption [ id "table-header" ] [ h1 [] [ text ("Pull Requests by " ++ model.currentUser) ] ]
, thead []
[ tr []
[ th [ class "th-prno", columnHeader, scope "col" ] [ text "PR" ]
, th [ class "th-title", columnHeader, scope "col" ] [ text "Title" ]
, th [ class "th-repo", columnHeader, scope "col" ] [ text "Repo" ]
, th [ class "th-date", columnHeader, scope "col" ] [ text "Date Created" ]
, th [ class "th-loc", columnHeader, scope "col" ] [ text "LoC" ]
]
]
, tbody [] (List.map userPullRequestDataRow pullRequests.nodes)
]
]
Nothing ->
div [] []
pullRequestQuantityButtons : PullRequestButton -> Html Msg
pullRequestQuantityButtons pullRequestsButton =
case pullRequestsButton of
UserPullRequestButton ->
div [ class "button-wrapper", group ]
[ label [] [ text "Rows " ]
, button [ class "btn", onClick (UpdateUserPullRequestQuantity "10") ] [ text "10" ]
, button [ class "btn", onClick (UpdateUserPullRequestQuantity "20") ] [ text "20" ]
, button [ class "btn", onClick (UpdateUserPullRequestQuantity "50") ] [ text "50" ]
]
RepoPullRequestButton ->
div [ class "button-wrapper", group ]
[ label [] [ text "Rows " ]
, button [ class "btn", onClick (UpdateRepoPullRequestQuantity "10") ] [ text "10" ]
, button [ class "btn", onClick (UpdateRepoPullRequestQuantity "20") ] [ text "20" ]
, button [ class "btn", onClick (UpdateRepoPullRequestQuantity "50") ] [ text "50" ]
]
displayRepoPullRequests : Model -> Html Msg
displayRepoPullRequests model =
let
pullRequests =
model.repoPullRequests
in
case pullRequests of
Just pullRequests ->
div [ class "table-wrapper" ]
[ pullRequestQuantityButtons RepoPullRequestButton
, table [ labelledBy "table-header" ]
[ caption [ id "table-header" ] [ h1 [] [ text ("Pull Requests in " ++ model.currentRepo) ] ]
, thead []
[ tr []
[ th [ class "th-prno", columnHeader, scope "col" ] [ text "PR" ]
, th [ class "th-title", columnHeader, scope "col" ] [ text "Title" ]
, th [ class "th-author", columnHeader, scope "col" ] [ text "Author" ]
, th [ class "th-date", columnHeader, scope "col" ] [ text "Date Created" ]
, th [ class "th-loc", columnHeader, scope "col" ] [ text "LoC" ]
]
]
, tbody [] (List.map repoPullRequestDataRow pullRequests.nodes)
]
]
Nothing ->
div [] []
userPullRequestDataRow : UserPullRequest -> Html Msg
userPullRequestDataRow pullRequest =
tr []
[ th [ attribute "data-title" "pr-no", scope "row" ] [ a [ href pullRequest.url, target "_blank", title "View this PR on GitHub" ] [ text (toString pullRequest.prNumber) ] ]
, td [ attribute "data-title" "pr-title" ] [ text pullRequest.title ]
, td [ attribute "data-title" "repo-title" ] [ text pullRequest.repoName ]
, td [ attribute "data-title" "pr-date" ] [ text pullRequest.createdAt ]
, td [ attribute "data-title" "pr-loc" ]
[ ul [ class "list-reset" ]
[ li [ attribute "aria-label" "LoC Differences in this PR", title "Difference" ] [ text (locChange pullRequest.additions pullRequest.deletions) ]
, li [ attribute "aria-label" "LoC Additions in this PR", title "Additions" ] [ text (toString pullRequest.additions) ]
, li [ attribute "aria-label" "LoC Deletions in this PR", title "Deletions" ] [ text (toString pullRequest.deletions) ]
]
]
]
repoPullRequestDataRow : RepoPullRequest -> Html Msg
repoPullRequestDataRow pullRequest =
tr []
[ th [ attribute "data-title" "pr-no", scope "row" ] [ a [ href pullRequest.url, target "_blank", title "View this PR on GitHub" ] [ text (toString pullRequest.prNumber) ] ]
, td [ attribute "data-title" "pr-title" ] [ text pullRequest.title ]
, td [ attribute "data-title" "pr-author" ] [ text pullRequest.author ]
, td [ attribute "data-title" "pr-date" ] [ text pullRequest.createdAt ]
, td [ attribute "data-title" "pr-loc" ]
[ ul [ class "list-reset" ]
[ li [ attribute "aria-label" "LoC Differences in this PR", title "Difference" ] [ text (locChange pullRequest.additions pullRequest.deletions) ]
, li [ attribute "aria-label" "LoC Additions in this PR", title "Additions" ] [ text (toString pullRequest.additions) ]
, li [ attribute "aria-label" "LoC Deletions in this PR", title "Deletions" ] [ text (toString pullRequest.deletions) ]
]
]
]
locChange : Int -> Int -> String
locChange additions deletions =
let
diff =
additions - deletions
in
toString diff
request : String -> String -> Http.Request String
request token query =
let
headers =
[ Http.header "Authorization" token
]
in
Http.request
{ method = "POST"
, headers = headers
, url = baseUrl
, body = Http.jsonBody (Encode.object [ ( "query", Encode.string query ) ])
, expect = Http.expectString
, timeout = Nothing
, withCredentials = False
}
getUsersAndRepos : String
getUsersAndRepos =
"""
query {
organization(login: "FracturedAtlas") {
repositories(first: 11, orderBy: {field: UPDATED_AT, direction: DESC}) {
edges {
node {
id
name
}
}
}
team(slug: "fa-dev") {
members(first: 10, orderBy: {field: LOGIN, direction: ASC}) {
edges {
node {
id
login
}
}
}
}
}
}
"""
getUserPullRequests : String
getUserPullRequests =
"""
query {
search(type: ISSUE, query: "org:fracturedatlas type:PR is:MERGED author:user sort:updated-desc", last: 10) {
nodes {
__typename
... on PullRequest {
number
title
createdAt
additions
deletions
url
repository {
name
}
}
}
}
}
"""
getRepoPullRequests : String
getRepoPullRequests =
"""
query {
repository(owner: "FracturedAtlas", name: "repoName") {
pullRequests(first: 10, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
number
title
createdAt
additions
deletions
url
author {
login
}
}
}
}
}
"""
baseUrl : String
baseUrl =
"https://api.github.com/graphql"
init : ( Model, Cmd Msg )
init =
( initialModel, Cmd.none )
initialModel : Model
initialModel =
{ organization = Nothing
, userPullRequests = Nothing
, repoPullRequests = Nothing
, message = "Waiting for a response..."
, filterVisible = True
, currentData = ByUser
, currentUser = ""
, currentRepo = ""
, bearerToken = Nothing
}
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}