Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to support HipChat Server and API V2 #26

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions app/views/projects/_redmine_hipchat.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
Target room's ID or name. Leave empty to use <%= link_to 'global settings', plugin_settings_path(:redmine_hipchat) %>.
</p>

<p>
<%= form.text_field :hipchat_endpoint %>
Using Hipchat server? Leave empty to use <%= link_to 'global settings', plugin_settings_path(:redmine_hipchat) %>.
</p>

<p>
<%= form.check_box :hipchat_notify %>
Notify room members? Will trigger sound/popup based on user preferences.
Expand Down
10 changes: 8 additions & 2 deletions app/views/settings/_redmine_hipchat.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
</p>

<p>
<%= 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.
</p>

<p>
<%= 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.
</p>

<p>
<%= content_tag(:label, l(:hipchat_settings_label_notify)) %>
<%= check_box_tag 'settings[notify]', 1, @settings[:notify] %>
Expand Down
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20150107044321_hipchat_api_v2_changes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HipchatApiV2Changes < ActiveRecord::Migration
def change
add_column :projects, :hipchat_endpoint, :string, :default => "", :null => false
end
end
3 changes: 2 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

settings :partial => 'settings/redmine_hipchat',
:default => {
:room_id => "",
:room_name => "",
:auth_token => "",
:endpoint => ""
}
end
51 changes: 34 additions & 17 deletions lib/hipchat_hooks.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# encoding: utf-8
require 'uri'
require 'json'

class NotificationHook < Redmine::Hook::Listener

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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 = '…')
Expand Down
2 changes: 1 addition & 1 deletion lib/project_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down