summaryrefslogtreecommitdiff
path: root/modules/3-gh/default.rkt
blob: abe6dd9489093d178d3022baa75a5f05d343478e (plain)
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
#lang racket/base
(require irc/core irc/bot irc/command irc/storage
         racket/runtime-path racket/format
         racket/string racket/file net/url json)
(provide (all-defined-out))

(define-runtime-path here ".")

(define api (string->url "https://api.github.com/graphql"))

(define (gh-graphql query [variables (hasheq)])
  (let* ([data (hasheq 'query query 'variables variables)]
         [token (hash-ref (network-storage "github") 'token)]
         [headers (list (format "Authorization: bearer ~a" token))]
         [port (post-pure-port api (jsexpr->bytes data) headers)]
         [result (read-json port)])
    (close-input-port port)
    result))

(define open-items (file->string (build-path here "open-items.gql")))

(define (gh-open-report gh-user irc-user)
  (define (repo-action repo)
    (for ([item (in-sequences (ref repo 'pullRequests 'nodes)
                              (ref repo 'issues 'nodes))])
      (sleep 2)
      (privmsg irc-user (format "~a - ~a" (ref item 'title) (ref item 'url)))))
  (let* ([r (gh-graphql open-items (hasheq 'user gh-user))]
         [user (ref r 'data 'user)])
    (for* ([user-repo (ref user 'repositories 'nodes)])
      (repo-action user-repo))
    (for* ([org (ref user 'organizations 'nodes)]
           [org-repo (ref org 'repositories 'nodes)])
      (repo-action org-repo))))

(define-command (gh-set-name nick)
  #:help "Set GitHub handle to be associated with your IRC user"
  (ref-set! (user-storage (source))
            (~a nick)
            'github 'name)
  (persist-user (source)))

(define-command (gh-open)
  #:help "Request open issues and PRs for your GitHub handle, sent in private"
  (define irc-name (source))
  (define gh-name
    (ref (user-storage irc-name) 'github 'name))
  (gh-open-report gh-name irc-name))