From fbff17c34e0e87ffd3c678d29242e2563a4eb2d2 Mon Sep 17 00:00:00 2001 From: tilpner Date: Sun, 22 Apr 2018 23:40:12 +0200 Subject: Add github module --- modules/3-gh/default.rkt | 48 +++++++++++++++++++++++++++++++++++++++++++++ modules/3-gh/open-items.gql | 25 +++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 modules/3-gh/default.rkt create mode 100644 modules/3-gh/open-items.gql (limited to 'modules') diff --git a/modules/3-gh/default.rkt b/modules/3-gh/default.rkt new file mode 100644 index 0000000..0705fe4 --- /dev/null +++ b/modules/3-gh/default.rkt @@ -0,0 +1,48 @@ +#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)) diff --git a/modules/3-gh/open-items.gql b/modules/3-gh/open-items.gql new file mode 100644 index 0000000..e7fa240 --- /dev/null +++ b/modules/3-gh/open-items.gql @@ -0,0 +1,25 @@ +fragment repoView on Repository { + name + pullRequests(first: 100, states: OPEN) { + nodes { title url } + } + issues(first: 100, states: OPEN) { + nodes { title url } + } +} + +query OpenItems($user: String!) { + user(login: $user) { + repositories(first: 100) { + nodes { ...repoView } + } + + organizations(first: 10) { + nodes { + repositories(first: 100) { + nodes { ...repoView } + } + } + } + } +} -- cgit v1.2.3