shrub/ape/gh.hoon

214 lines
6.0 KiB
Plaintext
Raw Normal View History

2016-01-28 00:50:06 +03:00
:: This is a driver for the Github API v3.
::
:: You can interact with this in a few different ways:
::
:: - .^(%gx /=gh=/read-unauth{/endpoint}) or subscribe to
:: /scry/x/read-unauth{/endpoint} for unauthenticated reads.
::
:: - .^(%gx /=gh=/read-auth{/endpoint}) or subscribe to
:: /scry/x/read-auth{/endpoint} for authenticated reads.
::
:: - subscribe to /scry/x/listen/{owner}/{repo}/{events...}
:: for webhook-powered event notifications. For event list,
:: see https://developer.github.com/webhooks/.
::
:: See the %github app for example usage.
2016-01-21 02:06:50 +03:00
/? 314
2016-01-26 04:13:08 +03:00
/- gh
2016-01-21 02:06:50 +03:00
=> |%
++ move (pair bone card)
++ sub-result
$% [%json json]
2016-01-28 00:50:06 +03:00
[%gh-issues issues:gh]
[%gh-issue-comment issue-comment:gh]
2016-01-21 02:06:50 +03:00
==
++ card
$% [%diff sub-result]
[%them wire (unit hiss)]
[%hiss wire [~ ~] %httr [%hiss hiss]]
2016-01-21 02:06:50 +03:00
==
2016-01-28 00:50:06 +03:00
++ hook-response
$% [%gh-issues issues:gh]
[%gh-issue-comment issue-comment:gh]
==
2016-01-21 02:06:50 +03:00
--
2016-01-26 04:13:08 +03:00
|_ [hid=bowl cnt=@ hook=(map ,@t ,[id=@t listeners=(set bone)])]
::++ prep ,_`.
2016-01-28 00:50:06 +03:00
::
:: This core manages everything related to a particular request.
::
:: Each request has a particular 'style', which is currently
:: one of 'read-auth', 'read-unauth', or 'listen'. ++scry
:: handles all three types of requests.
2016-01-26 04:13:08 +03:00
++ help
|= [style=@tas pax=path]
=| mow=(list move)
|%
2016-01-28 00:50:06 +03:00
:: Resolve core.
2016-01-26 04:13:08 +03:00
++ abet
^- [(list move) _+>.$]
[(flop mow) +>.$]
2016-01-28 00:50:06 +03:00
::
:: Append path to api.github.com and parse to a purl.
2016-01-21 02:06:50 +03:00
++ real-pax (scan "https://api.github.com{<`path`pax>}" auri:epur)
2016-01-28 00:50:06 +03:00
::
:: Send a hiss with the correct authentication
2016-01-26 04:13:08 +03:00
++ send-hiss
|= [aut=? hiz=hiss]
2016-01-26 04:13:08 +03:00
^+ +>
=+ wir=`wire`[%x (scot %ud cnt) pax]
=+ ^= new-move
?. aut [ost.hid %them wir ~ hiz]
[ost.hid %hiss wir `~ %httr [%hiss hiz]]
+>.$(mow [new-move mow])
2016-01-28 00:50:06 +03:00
::
:: Decide how to handle a request based on its style.
2016-01-21 02:06:50 +03:00
++ scry
2016-01-26 04:13:08 +03:00
^+ .
2016-01-21 02:06:50 +03:00
=- ~& [%requesting -] -
?+ style ~|(%invalid-style !!)
%read-unauth read-unauth
%read-auth read-auth
%listen listen
==
2016-01-28 00:50:06 +03:00
::
++ read-unauth (send-hiss | real-pax %get ~ ~)
++ read-auth (send-hiss & real-pax %get ~ ~)
2016-01-28 00:50:06 +03:00
::
:: Create or update a webhook to listen for a set of events.
2016-01-21 02:06:50 +03:00
++ listen
2016-01-26 04:13:08 +03:00
^+ .
?> ?=([@ @ *] pax)
2016-01-28 00:50:06 +03:00
=+ events=t.t.pax
2016-01-26 04:13:08 +03:00
|- ^+ +>.$
2016-01-28 00:50:06 +03:00
?~ events
2016-01-26 04:13:08 +03:00
+>.$
2016-01-28 00:50:06 +03:00
?: (~(has by hook) i.events)
=. +>.$ =>((update-hook i.events) ?>(?=([@ @ *] pax) .))
$(events t.events)
=. +>.$ =>((create-hook i.events) ?>(?=([@ @ *] pax) .))
$(events t.events)
::
:: Set up a webhook.
2016-01-21 02:06:50 +03:00
++ create-hook
2016-01-26 04:13:08 +03:00
|= event=@t
^+ +>
?> ?=([@ @ *] pax)
2016-01-28 00:50:06 +03:00
=+ clean-event=`tape`(turn (trip event) |=(a=@tD ?:(=('_' a) '-' a)))
=. hook
%+ ~(put by hook) (crip clean-event)
=+ %+ fall
(~(get by hook) (crip clean-event))
*[id=@t listeners=(set bone)]
[id (~(put in listeners) ost.hid)]
2016-01-26 04:13:08 +03:00
%- send-hiss
:* &
%+ scan
2016-01-21 02:06:50 +03:00
=+ [(trip i.pax) (trip i.t.pax)]
"https://api.github.com/repos/{-<}/{->}/hooks"
auri:epur
2016-01-26 22:03:31 +03:00
%post ~ ~
2016-01-21 02:06:50 +03:00
%- taco %- crip %- pojo %- jobe :~
name/s/%web
active/b/&
2016-01-26 04:13:08 +03:00
events/a/~[s/event] ::(turn `(list ,@t)`t.t.pax |=(a=@t s/a))
2016-01-21 02:06:50 +03:00
:- %config
%- jobe :~
2016-01-28 00:50:06 +03:00
=+ =+ clean-event
"http://107.170.195.5:8443/~/to/gh/gh-{-}.json?anon&wire=/"
2016-01-26 04:13:08 +03:00
[%url s/(crip -)]
2016-01-21 02:06:50 +03:00
[%'content_type' s/%json]
==
==
==
2016-01-28 00:50:06 +03:00
::
:: Add current bone to the list of subscribers for this event.
2016-01-21 02:06:50 +03:00
++ update-hook
2016-01-26 04:13:08 +03:00
|= event=@t
^+ +>
2016-01-21 02:06:50 +03:00
?> ?=([@ @ @ *] pax)
2016-01-26 04:13:08 +03:00
=+ hok=(~(got by hook) event)
%_ +>.$
hook
%+ ~(put by hook) event
hok(listeners (~(put in listeners.hok) ost.hid))
2016-01-21 02:06:50 +03:00
==
--
::
2016-01-28 00:50:06 +03:00
:: Pokes that haven't already been caught are handled here.
:: These should be only from webhooks firing, so if we get any
:: mark that we shouldn't get from a webhook, we reject it.
:: Otherwise, we spam out the event to everyone who's listening
:: for that event.
++ poke
|= response=hook-response
2016-01-21 03:49:13 +03:00
^- [(list move) _+>.$]
2016-01-28 00:50:06 +03:00
=+ hook-data=(~(get by hook) (rsh 3 3 -.response))
?~ hook-data
~& [%strange-hook hook response]
`+>.$
~& response=response
:_ +>.$
%+ turn (~(tap in listeners.u.hook-data))
|= ost=bone
[ost %diff response]
2016-01-21 03:49:13 +03:00
::
2016-01-28 00:50:06 +03:00
:: When a peek on a path blocks, ford turns it into a peer on
:: /scry/{care}/{path}. You can also just peer to this
:: directly.
::
:: After some sanity checking we hand control to ++scry in
:: ++help.
2016-01-21 02:06:50 +03:00
++ peer-scry-x
|= pax=path
^- [(list move) _+>.$]
?> ?=(^ pax)
=- ~& [%peered -] -
2016-01-26 04:13:08 +03:00
[abet(cnt now.hid)]:scry:(help i.pax t.pax)
::
2016-01-28 00:50:06 +03:00
:: HTTP response. We make sure the response is good, then
:: produce the result (as JSON) to whoever sent the request.
2016-01-22 00:20:55 +03:00
++ sigh-httr-x thou-x
2016-01-21 02:06:50 +03:00
++ thou-x
|= [way=wire res=httr]
^- [(list move) _+>.$]
?> ?=([@ *] way)
:_ +>.$ :_ ~
2016-01-22 00:20:55 +03:00
:^ ost.hid %diff %json
2016-01-21 02:06:50 +03:00
?~ r.res
2016-01-22 00:20:55 +03:00
(jobe err/s/%empty-response code/(jone p.res) ~)
2016-01-21 02:06:50 +03:00
=+ (rush q.u.r.res apex:poja)
?~ -
2016-01-22 00:20:55 +03:00
(jobe err/s/%bad-json code/(jone p.res) body/s/q.u.r.res ~)
?. =(2 (div p.res 100))
(jobe err/s/%request-rejected code/(jone p.res) msg/u.- ~)
2016-01-21 02:06:50 +03:00
u.-
::
2016-01-28 00:50:06 +03:00
:: We can't actually give the response to pretty much anything
:: without blocking, so we just block unconditionally.
2016-01-21 02:06:50 +03:00
++ peek
|= [ren=@tas tyl=path]
^- (unit (unit (pair mark ,*)))
2016-01-21 03:49:13 +03:00
~ ::``noun/[ren tyl]
2016-01-21 02:06:50 +03:00
--
2016-01-28 00:50:06 +03:00
::
::++ poke-json
:: |= jon=json
:: ^- [(list move) _+>.$]
:: =+ ^- [repo=json sender=json hok=(list ,@t) hook-id=@t zen=json]
:: %- need
:: %. jon
:: => jo
:: (ot repository/some sender/some hook/(ot events/(ar so) ~) 'hook_id'^no zen/some ~)
:: ?. ?=([@ ~] hok)
:: ~& [%weird-hook hook-id hok]
:: [~ +>.$]
:: ~& [%id hook-id hok]
:: =+ old-bones=`(set bone)`(biff (~(get by hook) i.hok) tail)
:: [~ +>.$(hook (~(put by hook) i.hok [hook-id (~(put in old-bones) ost.hid)]))]
.