From 89c9cb022f94013c9cafbc24d08bc15f11b079fa Mon Sep 17 00:00:00 2001 From: Jan Schulz-Hofen Date: Fri, 3 Oct 2008 15:58:08 +0200 Subject: [PATCH] first commit --- README | 27 ++++++++++++ app/controllers/notices_controller.rb | 51 ++++++++++++++++++++++ app/helpers/notices_helper.rb | 2 + init.rb | 8 ++++ lang/en.yml | 2 + test/functional/notices_controller_test.rb | 8 ++++ test/test_helper.rb | 5 +++ 7 files changed, 103 insertions(+) create mode 100644 README create mode 100644 app/controllers/notices_controller.rb create mode 100644 app/helpers/notices_helper.rb create mode 100644 init.rb create mode 100644 lang/en.yml create mode 100644 test/functional/notices_controller_test.rb create mode 100644 test/test_helper.rb diff --git a/README b/README new file mode 100644 index 0000000..b5bb0c0 --- /dev/null +++ b/README @@ -0,0 +1,27 @@ += hoptoad_server + +This is a simple Redmine plugin that makes Redmine act like a Hoptoad server. All exceptions caught and sent by HoptoadNotifier will create or update an Issue in Redmine. + += Installation & Configuration + +Just install the Plugin following the general Redmine plugin installation instructions at http://www.redmine.org/wiki/redmine/Plugins. + +Then, go to Administration -> Settings -> Incoming emails in your Redmine and generate an API key. + +Now, download and install the HoptoadNotifier following the excellent instructions on their github page http://github.com/thoughtbot/hoptoad_notifier. + +When it comes to creating your config/initializers/hoptoad.rb file, deviate from the standard and put in something like this: + +HoptoadNotifier.configure do |config| + config.api_key = {:project => 'my_redmine_project_identifier', # the one you specified for your project in Redmine + :tracker => 'Bug', # the name of your Tracker of choice in Redmine + :api_key => 'my_redmine_api_key', # the key you generated before in Redmine (NOT YOUR HOPTOAD API KEY!) + :category => 'Development', # the name of a ticket category (optional.) + :assigned_to => 'admin', # the login of a user the ticket should get assigned to by default (optional.) + :priority => 5 # the default priority (use a number, not a name. optional.) + }.to_yaml + config.host = 'my_redmine_host.com' # the hostname your Redmine runs at + config.port = 443 # the port your Redmine runs at +end + +You're done. You can start receiving your Exceptions in Redmine! diff --git a/app/controllers/notices_controller.rb b/app/controllers/notices_controller.rb new file mode 100644 index 0000000..f7dec3e --- /dev/null +++ b/app/controllers/notices_controller.rb @@ -0,0 +1,51 @@ +class NoticesController < ApplicationController +unloadable + def index + notice = YAML.load(request.raw_post)['notice'] + redmine_params = YAML.load(notice['api_key']) + + if authorized = Setting.mail_handler_api_key == redmine_params[:api_key] + + project = Project.find_by_name(redmine_params[:project]) + tracker = project.trackers.find_by_name(redmine_params[:tracker]) + author = User.anonymous + + issue = Issue.find_or_initialize_by_subject_and_project_id_and_tracker_id_and_author_id_and_description(notice['error_message'], + project.id, + tracker.id, + author.id, + 'Hoptoad Issue') + + if issue.new_record? + issue.category = IssueCategory.find_by_name(redmine_params[:category]) unless redmine_params[:category].blank? + issue.assigned_to = User.find_by_login(redmine_params[:assigned_to]) unless redmine_params[:assigned_to].blank? + issue.priority_id = redmine_params[:priority] unless redmine_params[:priority].blank? + end + + issue.save! + + journal = issue.init_journal(author, "h4. Backtrace\n\n
#{notice['back'].to_yaml}
\n\n + h4. Request\n\n
#{notice['request'].to_yaml}
\n\n + h4. Session\n\n
#{notice['session'].to_yaml}
\n\n + h4. Environment\n\n
#{notice['environment'].to_yaml}
") + + if issue.status.blank? or issue.status.is_closed? + issue.status = IssueStatus.find(:first, :conditions => {:is_default => true}, :order => 'position ASC') + end + + + issue.save! + + if issue.new_record? + Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added') + else + Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') + end + + render :status => 200, :text => "Received bug report. Created/updated issue #{issue.id}." + else + logger.info 'Unauthorized Hoptoad API request.' + render :status => 403, :text => 'You provided a wrong or no Redmine API key.' + end + end +end diff --git a/app/helpers/notices_helper.rb b/app/helpers/notices_helper.rb new file mode 100644 index 0000000..7adf7a6 --- /dev/null +++ b/app/helpers/notices_helper.rb @@ -0,0 +1,2 @@ +module NoticesHelper +end diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..ef5c0d5 --- /dev/null +++ b/init.rb @@ -0,0 +1,8 @@ +require 'redmine' + +Redmine::Plugin.register :redmine_hoptoad_server do + name 'Redmine Hoptoad Server plugin' + author 'Jan Schulz-Hofen' + description 'This plugin turns Redmin into a Hoptoad server, i.e. an API provider which can be used with the hoptoad_notifier which is available at: http://www.hoptoadapp.com/' + version '0.0.1' +end diff --git a/lang/en.yml b/lang/en.yml new file mode 100644 index 0000000..e338591 --- /dev/null +++ b/lang/en.yml @@ -0,0 +1,2 @@ +# English strings go here +my_label: "My label" diff --git a/test/functional/notices_controller_test.rb b/test/functional/notices_controller_test.rb new file mode 100644 index 0000000..0bd665e --- /dev/null +++ b/test/functional/notices_controller_test.rb @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class NoticesControllerTest < ActionController::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..bd1ed0c --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,5 @@ +# Load the normal Rails helper +require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper') + +# Ensure that we are using the temporary fixture path +Engines::Testing.set_fixture_path