Shopify / shipit-engine

Deployment coordination
https://shopify.engineering/introducing-shipit
MIT License
1.42k stars 144 forks source link

Adding custom github webhook getting "NameError: uninitialized constant Handlers" #1084

Open stairsj opened 4 years ago

stairsj commented 4 years ago

Hello,

Newbie Ruby developer, I am trying to add a custom github webhook listener to shipit. I have modified by environments/development.rb and added the following lines

Shipit::Webhooks.register_handler('issue_comment') do |params| puts "Webhook Calling issue_comment" end

I end up with the following error message on start

rake aborted! NameError: uninitialized constant Handlers /usr/local/bundle/gems/shipit-engine-0.31.0/app/models/shipit/webhooks.rb:6:indefault_handlers' /usr/local/bundle/gems/shipit-engine-0.31.0/app/models/shipit/webhooks.rb:18:in reset_handlers!' /usr/local/bundle/gems/shipit-engine-0.31.0/app/models/shipit/webhooks.rb:14:inhandlers' /usr/local/bundle/gems/shipit-engine-0.31.0/app/models/shipit/webhooks.rb:22:in register_handler' /var/shipit/config/environments/development.rb:65:inblock in <top (required)>' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/railtie.rb:216:in instance_eval' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/railtie.rb:216:inconfigure' /var/shipit/config/environments/development.rb:1:in <top (required)>' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/engine.rb:612:inblock (2 levels) in ' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/engine.rb:611:in each' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/engine.rb:611:inblock in ' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/initializable.rb:32:in instance_exec' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/initializable.rb:32:inrun' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/initializable.rb:61:in block in run_initializers' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/initializable.rb:50:ineach' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/initializable.rb:50:in tsort_each_child' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/initializable.rb:60:inrun_initializers' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/application.rb:363:in initialize!' /var/shipit/config/environment.rb:5:in<top (required)>' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/application.rb:339:in require_environment!' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/application.rb:515:inblock in run_tasks_blocks' Tasks: TOP => railties:install:migrations => db:load_config => environment (See full trace by running task with --trace) ` Any assistance would be greatly appreciated.

casperisfine commented 4 years ago

Hi, this happens because the register happens too early in the application initialization cycle. To fix this the register_handler should be in a to_prepare callback:

https://guides.rubyonrails.org/configuring.html#initialization-events

The README.md isn't clear enough on this though, it should be improved.