#lang racket/base (require irc/bot irc/command irc/storage racket/runtime-path 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)) 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))