require 'notification_services/helpers' require 'notification_services/models' require 'notification_services/triggers_helper' require 'notification_services/workers/webhook_notifier' require 'p4_web_services_auth' require 'rack/parser' require 'resque' require 'sinatra' require 'sinatra/json' module NotificationServices class App < Sinatra::Base # Automatically parse content as JSON use Rack::Parser, content_types: { 'application/json' => proc { |body| ::MultiJson.decode body } } use P4WebServicesAuth::AuthMiddleware, settings: settings helpers NotificationServices::Helpers before do content_type 'application/json' end configure do set(:p4, 'port' => '1666') set(:token_path, '/tmp/p4_phoenix_services/tokens') set(:p4_web_api_url, 'http://localhost:4567') set(:p4_web_api_prefix, nil) set(:p4_notification_services_url, 'http://localhost:4567') set(:p4_notification_services_prefix, '/v1') set :raise_errors, :environment == :test set :dump_errors, :environment == :development set :show_exceptions, :environment == :development enable :logging end error do err = env['sinatra.error'] msg = err.message if ENV['RACK_ENV'] == 'development' msg += "\n#{err.backtrace.join('\n')}" end e = P4WebServicesAuth::P4Error.default_error(msg) return { MessageCode: e.message_code, MessageSeverity: e.message_severity, MessageText: e.message_text }.to_json end post '/v1/triggers/change-commit' do variables = params['variables'] notification = { 'trigger' => 'change-commit', 'variables' => variables } Resque.enqueue(Workers::WebhookNotifier, notification) end post '/v1/triggers/shelve-commit' do end post '/v1/triggers/shelve-delete' do end post '/v1/triggers/form-commit' do end post '/v1/triggers/form-delete' do end # Creates triggers in the system. post '/v1/triggers/install' do triggers = nil client_app = :curl trigger_user = params[:trigger_user] trigger_ticket = params[:trigger_ticket] url = settings.p4_notification_services_url unless settings.p4_notification_services_prefix.nil? url += "/#{settings.p4_notification_services_prefix}" end open_p4_web_api_client do |c| helper = TriggersHelper.new( client: c, url: url, user: trigger_user, ticket: trigger_ticket, client_app: client_app ) triggers = helper.execute(triggers) end triggers.to_json end get '/v1/webhooks' do hooks = Models::Webhooks.dataset.all hooks.to_json end post '/v1/webhooks' do hook = Models::Webhooks.new hook.id = params['id'] hook.url = params['url'] hook.save trigger = Models::Triggers.new trigger.trigger = params['trigger'] hook.add_trigger(trigger) hook.save hook.to_json end get '/v1/webhooks/:id' do |id| hook = Models::Webhooks.dataset.where(id: id).first halt 404 if hook.nil? hook.to_json end put '/v1/webhooks/:id' do |id| hook = Models::Webhooks.dataset.where(id: id).first hook.url = params['url'] if params.key?('url') if params.key?('trigger') hook.remove_triggers trigger = Models::Triggers.new trigger.trigger = params['trigger'] hook.add_trigger(trigger) hook.save end hook.to_json end delete '/v1/webhooks/:id' do |id| Models::Webhooks.dataset.where(id: id).delete end end end
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#7 | 13972 | tjuricek |
Removing old microservice implementations. The system is now mostly a monolith. Eventually there will be a websocket service. |
||
#6 | 13689 | tjuricek | Switch from Resque to Sidekiq. | ||
#5 | 13677 | tjuricek | Fixed issues with Sequel saving and displaying webhooks in notification_services. | ||
#4 | 13675 | tjuricek |
Add notification_services initialization Removing the 'online setup' mode in lieu of doing things during the salt process. Mostly this removes the trigger setup from the main web application. |
||
#3 | 13469 | tjuricek |
Initial implementation of change-commit notification services. Set up a new database just for storing webhook configuration for the notification services. It uses the stable version of Resque, which needs to be evaluated in more detail. This should be easier for an admin to monitor than something like Sidekiq, but performance evaluation needs to happen before production release. No automated tests yet, that will rely upon a Phoenix notification mechanism existing first. |
||
#2 | 13468 | tjuricek | Set up resque worker that will trigger web hooks in the background. | ||
#1 | 13467 | tjuricek | Add a basic notification_services endpoint that can setup a trigger entry and accept basic POST requests. |