Skip to content

Commit

Permalink
first pass at DSL and at_exit running interface
Browse files Browse the repository at this point in the history
* Need to make a better decision about exactly when we want to start the server, but maybe a convention for file names isn't terrible for now
* The signature of the 'on' method feels sloppy, and well the DSL itself I feel could be a lot better...probably need to think
  outside the Probot library's use of on to see if there's some interesting grammar of sorts that Ruby could do well in a way that
  JavaScript can't always do based on its syntactical limitations
  • Loading branch information
bradshjg committed Feb 3, 2021
1 parent ec6845e commit 452c5c9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
28 changes: 28 additions & 0 deletions examples/demo/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
PRUBOT_ID=1
PRUBOT_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA40Ukj2IlvGpqxKWpXdHjUfjlfKCzczedkYSW2xWult0i+fzt
jr8KuBTnOvhPAlgpMAXNwBevdFzHQe/DF7hA9b1DK2bf9aN/WxPDUV4V6lLw3brO
sysCJMlbk8I4W6EWd97LaA4JHjsaU60vw1uHwMiBAWBmk+3NRCEWOTQ//tfkWwOn
2nuu/w10V8LCycMnnZhiz6zZqN7kREm5r2rWzQi3Pa+8LK86kFCe707DT2+lSTTH
o3qdHJ0x3H6hP4XVnfCFIG4ucR9yL7QFJe8rlzbcHj7RRalXXfZlqL2vJeTQUhRQ
dFnOpLn/mcNUe3z4Kn5fMdWeHwfeFyZF0WE6OQIDAQABAoIBAE7KzOcM7upItqVg
jbxiJP5pXR59FFnQoUcvAaII/VN+bT7gKnf/0baKQLRYHARDDlyA1plPXNvpwocg
mbGihprrt9rXOrQNr/yieGfAR7bKdIla14iUU5TvXll2z1UtHv4F9t0naI/bi8AQ
3etL01/J47fXdRi6LBz/n5XLgJhYF5TSglXmknCekuP4yNEQhni6U8Of0BLrnTFb
urxdcIpwtLH6vQgzN+asfPvtNWWVxWtI+RlQOd4d/XYTEyronTonH5Ca9gk5Ndim
S/tNAH3ecR1syp8il4vXA7SJ4F9gJqyrvucnoL+zsKSQ17hgUZNZzI59q7AWK8Oh
x6J8KpECgYEA+O8XYk7mpGmijKU6HufZDRoC7ErWmJbPPBhsrANVQHEhb1ETEF71
tS5h6qG4XIdgRZSyZE1EfxwKVB2oc8Wss9WV/QD73rcurHPL/enVGp5Y2hU41Cwq
L+K7e0nG9etJXMqfoZ1tWMbdotly8iEoc86PbZf77uOD6HLVUKhWGSUCgYEA6big
3tGBxU+yUk/ZSuKht7W5iOKENeKkM4MSYSvz6Tf7Knbdpf2P4GwI3tIKnjqwkoTm
wEmFXjWqebzkhJfsK/5GKfpZakfkXdSOo6B1SYMPHxg5SYw6Ljm6ZY8uMUnVn3Bm
B32DX4gRbS/i//dKtDP294v3eQSUIShcUrsOYoUCgYBYE6BedDQ+7/FHeh56WOX/
MLjgCgqrqpBgXvP0WKmNR3ng/PqVzEN7e5HBhr7IXk290MLdokkeADnXUTcM32Af
sO6Euhhj0MR+1+nRSS7RYD1nzCJzT70NfFeBY7U1xa4LWtXoRN8vjg1VVULKUNkT
QH6W9DN7bpx5XkgDZcww0QKBgQDnG23XZotPtDK8OcC/z8Hhioxf0ryxG98KIL13
4KJbJsPxhYA+trAiVjGfQ0rEk3wuJcSWtxxtv9tGbYX5ptAd4F04/RiVg2lJbvls
yymu+y1kretX0pVlSCgMbLxceIoIEniRR4c0W7P/p65rNPhloEzBE2IuBF0FPIeu
NtFOkQKBgQDMsV4UgyvoMpQm2BIv1ucUEE4qD0n3JYaA1R0FUDGktJqQjEEVQmLM
gemv4lrCnIlIE2MobN+/MzY2ycmcmXDxGsbArkIc1qpg3Hs5WJ1TL6wb0UjFsSwN
IBvt+fgG2en48cpljZ9UJHC/x+Mwlvs3ke1BQyJ/JI24/7KxL5MNGA==
-----END RSA PRIVATE KEY-----"
9 changes: 9 additions & 0 deletions examples/demo/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'prubot'

on 'test-event', 'say howdy' do
'howdy'
end

on 'test-action', 'test-event', 'say hello' do
'hello'
end
9 changes: 9 additions & 0 deletions examples/register/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'prubot'

on 'test-event', 'say howdy' do
'howdy'
end

on 'test-action', 'test-event', 'say hello' do
'hello'
end
2 changes: 1 addition & 1 deletion examples/welcomemat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'prubot'

on 'issues', 'opened', 'add a welcome message' do
on 'opened', 'issues', 'add a welcome message' do
# `payload` is a hash of the event payload.
discussions_link = payload['repository']['discussions_url']
welcome_message = 'Thanks for stopping by :wave:. While we work on getting back to you '\
Expand Down
31 changes: 31 additions & 0 deletions lib/prubot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def configured?
@config.configured?
end

def run!
Rack::Handler::WEBrick.run(app)
end

def run?
$0 == 'app.rb' # HACK HACK HACK need to decide how we want to handle this.
end

def register_event(name, event, action = nil, &block)
raise Error 'block required' unless block

Expand All @@ -47,4 +55,27 @@ def setup_routing
end
end
end

module DSL
@@app = Prubot::Application.new

##
# Supports both +on 'event' 'description' do...+ and +on 'action', 'event', 'description' do...+
# based on the number of arguments passed. We are careful to flip the arguments around based
# on the signature of the event registration method.
def on(*args, &block)
case args.length
when 2
@@app.register_event(args[1], args[0], &block)
when 3
@@app.register_event(args[2], args[1], args[0], &block)
else
raise Prubot::Error 'on supports either "on <event> <description> do..." or "on <action> <event> <description> do..."'
end
end

at_exit { @@app.run! if @@app.run? }
end
end

extend Prubot::DSL

0 comments on commit 452c5c9

Please sign in to comment.