# encoding: utf-8 module LocalPac module App class GitHookController < ApplicationController set :observers, [LocalPac::App::FileServeController, LocalPac::App::LookupController] helpers Sinatra::Param helpers Sinatra::JSON error do handler = ErrorHandler.find(StandardError) halt 500, handler.to_json end error Exceptions::GivenUrlInvalid do handler = ErrorHandler.find(Exceptions::GivenUrlInvalid) handler.use(url: env['sinatra.error'].message) halt 401, handler.to_json end not_found do handler = ErrorHandler.find(Exceptions::GitHookUnknown) handler.use(name: env['PATH_INFO']) halt 404, handler.to_json end set :raise_sinatra_param_exceptions, true error Sinatra::Param::InvalidParameterError do handler = ErrorHandler.find(Sinatra::Param::InvalidParameterError) handler.use(JSON.dump(parameter: env['sinatra.error'].param)) halt 401, handler.to_json end error Exceptions::PacFileInvalid do handler = ErrorHandler.find(Exceptions::PacFileInvalid) handler.use(JSON.dump(name: env['sinatra.error'])) halt 403, handler.to_json end error Exceptions::ApiKeyInvalid do handler = ErrorHandler.find(Exceptions::ApiKeyInvalid) halt 403, handler.to_json end error Exceptions::ReloadOfLocalStorageFailed do handler = ErrorHandler.find(Exceptions::ReloadOfLocalStorageFailed) halt 500, handler.to_json end before do fail Exceptions::ApiKeyInvalid unless params['api_key'] == LocalPac.config.api_key end post '/pre-receive' do param :old_commit_id, String, required: true param :new_commit_id, String, required: true param :reference, String, required: true param :api_key, String, required: true validator = PacFileValidator.new repo = GitStorage.new(LocalPac.config.local_storage) repo.each_added_pac_file(params[:old_commit_id], params[:new_commit_id]) do |f| fail Exceptions::PacFileInvalid, name: f.path unless validator.valid?(f) end LocalPac::Actions::ReloadLocalStorage.new(settings.observers).run json result: :success end end end end