diff --git a/README.md b/README.md index e10164a..af26ec7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,18 @@ HipChat Plugin for Redmine ========================== +Supports HipChat Server and HipChat API V2 This plugin sends messages to your HipChat room when issues are created or updated. Setup ----- +``` +cd REDMINE_ROOT +git clone https://github.com/wreiske/redmine_hipchat plugins/redmine_hipchat +rake redmine:plugins:migrate RAILS_ENV=production +``` -1. Install this plugin following the standard Redmine [plugin installation guide](http://www.redmine.org/wiki/redmine/Plugins). -1. In Redmine, go to the Plugin page in the Adminstration area. -1. Select 'Configure' next to the HipChat plugin and enter the required details. + +1. Clone this repo to Redmine's plugin directory (see above) +2. In Redmine, go to the Plugin page in the Adminstration area. +3. Select 'Configure' next to the HipChat plugin and enter the required details. diff --git a/app/views/projects/_redmine_hipchat.html.erb b/app/views/projects/_redmine_hipchat.html.erb index 19953af..1cd3734 100644 --- a/app/views/projects/_redmine_hipchat.html.erb +++ b/app/views/projects/_redmine_hipchat.html.erb @@ -8,6 +8,11 @@ Target room's ID or name. Leave empty to use <%= link_to 'global settings', plugin_settings_path(:redmine_hipchat) %>.
++ <%= form.text_field :hipchat_endpoint %> + Using Hipchat server? Leave empty to use <%= link_to 'global settings', plugin_settings_path(:redmine_hipchat) %>. +
+<%= form.check_box :hipchat_notify %> Notify room members? Will trigger sound/popup based on user preferences. diff --git a/app/views/settings/_redmine_hipchat.html.erb b/app/views/settings/_redmine_hipchat.html.erb index 836b1ea..356342d 100644 --- a/app/views/settings/_redmine_hipchat.html.erb +++ b/app/views/settings/_redmine_hipchat.html.erb @@ -5,11 +5,17 @@
- <%= content_tag(:label, l(:hipchat_settings_label_room_id)) %> - <%= text_field_tag 'settings[room_id]', @settings[:room_id] %> + <%= content_tag(:label, l(:hipchat_settings_label_room_name)) %> + <%= text_field_tag 'settings[room_name]', @settings[:room_name] %> Target room's ID or name.
++ <%= content_tag(:label, l(:hipchat_settings_label_endpoint)) %> + <%= text_field_tag 'settings[endpoint]', @settings[:endpoint] %> + Using Hipchat server? Put it here. If you leave this empty it will default to api.hipchat.com. +
+<%= content_tag(:label, l(:hipchat_settings_label_notify)) %> <%= check_box_tag 'settings[notify]', 1, @settings[:notify] %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 0cf5c3a..6d12a96 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,9 +1,11 @@ en: hipchat_settings_header: HipChat Plugin Configuration - hipchat_settings_label_room_id: Room ID + hipchat_settings_label_room_name: Room ID + hipchat_settings_label_endpoint: Endpoint hipchat_settings_label_auth_token: Room token hipchat_settings_label_notify: Notify hipchat_settings_label_projects: Projects field_hipchat_auth_token: HipChat Room token field_hipchat_room_name: HipChat Room ID + field_hipchat_endpoint: Endpoint field_hipchat_notify: HipChat notify diff --git a/db/migrate/20150107044321_hipchat_api_v2_changes.rb b/db/migrate/20150107044321_hipchat_api_v2_changes.rb new file mode 100644 index 0000000..24f72e7 --- /dev/null +++ b/db/migrate/20150107044321_hipchat_api_v2_changes.rb @@ -0,0 +1,5 @@ +class HipchatApiV2Changes < ActiveRecord::Migration + def change + add_column :projects, :hipchat_endpoint, :string, :default => "", :null => false + end +end diff --git a/init.rb b/init.rb index eaf0848..3ff8550 100644 --- a/init.rb +++ b/init.rb @@ -15,7 +15,8 @@ settings :partial => 'settings/redmine_hipchat', :default => { - :room_id => "", + :room_name => "", :auth_token => "", + :endpoint => "" } end diff --git a/lib/hipchat_hooks.rb b/lib/hipchat_hooks.rb index 73d52a7..cefffc9 100644 --- a/lib/hipchat_hooks.rb +++ b/lib/hipchat_hooks.rb @@ -1,4 +1,6 @@ # encoding: utf-8 +require 'uri' +require 'json' class NotificationHook < Redmine::Hook::Listener @@ -16,8 +18,9 @@ def controller_issues_new_after_save(context = {}) data = {} data[:text] = text data[:token] = hipchat_auth_token(project) - data[:room] = hipchat_room_name(project) + data[:room_name] = hipchat_room_name(project) data[:notify] = hipchat_notify(project) + data[:endpoint] = hipchat_endpoint(project) send_message(data) end @@ -38,8 +41,9 @@ def controller_issues_edit_after_save(context = {}) data = {} data[:text] = text data[:token] = hipchat_auth_token(project) - data[:room] = hipchat_room_name(project) + data[:room_name] = hipchat_room_name(project) data[:notify] = hipchat_notify(project) + data[:endpoint] = hipchat_endpoint(project) send_message(data) end @@ -58,8 +62,9 @@ def controller_wiki_edit_after_save(context = {}) data = {} data[:text] = text data[:token] = hipchat_auth_token(project) - data[:room] = hipchat_room_name(project) + data[:room_name] = hipchat_room_name(project) data[:notify] = hipchat_notify(project) + data[:endpoint] = hipchat_endpoint(project) send_message(data) end @@ -72,7 +77,8 @@ def hipchat_configured?(project) elsif Setting.plugin_redmine_hipchat[:projects] && Setting.plugin_redmine_hipchat[:projects].include?(project.id.to_s) && Setting.plugin_redmine_hipchat[:auth_token] && - Setting.plugin_redmine_hipchat[:room_id] + Setting.plugin_redmine_hipchat[:room_name] && + Setting.plugin_redmine_hipchat[:endpoint] return true else Rails.logger.info "Not sending HipChat message - missing config" @@ -87,7 +93,12 @@ def hipchat_auth_token(project) def hipchat_room_name(project) return project.hipchat_room_name if !project.hipchat_room_name.empty? - return Setting.plugin_redmine_hipchat[:room_id] + return Setting.plugin_redmine_hipchat[:room_name] + end + + def hipchat_endpoint(project) + return project.hipchat_endpoint if !project.hipchat_endpoint.empty? + return Setting.plugin_redmine_hipchat[:endpoint] end def hipchat_notify(project) @@ -106,26 +117,32 @@ def get_url(object) def send_message(data) Rails.logger.info "Sending message to HipChat: #{data[:text]}" - req = Net::HTTP::Post.new("/v1/rooms/message") - req.set_form_data({ - :auth_token => data[:token], - :room_id => data[:room], - :notify => data[:notify] ? 1 : 0, - :from => 'Redmine', - :message => data[:text] - }) - req["Content-Type"] = 'application/x-www-form-urlencoded' - - http = Net::HTTP.new("api.hipchat.com", 443) + endpoint = data[:endpoint] || 'api.hipchat.com' + room_name = data[:room_name] + room_token = data[:token] + uri = URI.parse("https://#{endpoint}/v2/room/#{CGI::escape(room_name)}/notification?auth_token=#{room_token}") + http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE + + req = Net::HTTP::Post.new(uri.request_uri) + req.body = { + "color" => 'random', + "message" => data[:text], + "message_format" => 'html', + "notify" => data[:notify] ? true : false + }.to_json + + req['Content-Type'] = 'application/json' + Rails.logger.info "Before HipChat Begin Http.. #{req.body} (#{uri.request_uri}" begin - http.start do |connection| + res = http.start do |connection| connection.request(req) end rescue Net::HTTPBadResponse => e Rails.logger.error "Error hitting HipChat API: #{e}" end + Rails.logger.info "HipChat Result: #{res.body}" end def truncate(text, length = 20, end_string = '…') diff --git a/lib/project_patch.rb b/lib/project_patch.rb index 94b64de..eb669a2 100644 --- a/lib/project_patch.rb +++ b/lib/project_patch.rb @@ -3,7 +3,7 @@ module Patches module ProjectPatch def self.included(base) base.class_eval do - safe_attributes 'hipchat_auth_token', 'hipchat_room_name', 'hipchat_notify' + safe_attributes 'hipchat_endpoint', 'hipchat_auth_token', 'hipchat_room_name', 'hipchat_notify' end end end