Commit 108794a4 authored by Jessica McKellar's avatar Jessica McKellar
Browse files

Switch to using the standard send_message API endpoint, with formatting plugin-side.

parent 5145753f
......@@ -7,11 +7,46 @@ class NotificationHook < Redmine::Hook::Listener
# We generate Zulips for creating and updating issues.
def controller_issues_new_after_save(context = {})
extract_and_send(context, "new")
issue = context[:issue]
project = issue.project
if !configured(project)
# Fail silently: the rest of the app needs to continue working.
return true
end
content = %Q{%s opened [issue %d: %s](%s) in %s
~~~ quote
%s
~~~
**Priority**: %s
**Status**: %s
**Assigned to**: %s} % [User.current.name, issue.id, issue.subject, url(issue),
project.name, issue.description, issue.priority.to_s,
issue.status.to_s, issue.assigned_to.to_s]
send_zulip_message(content, project)
end
def controller_issues_edit_after_save(context = {})
extract_and_send(context, "edit")
issue = context[:issue]
project = issue.project
if !configured(project)
# Fail silently: the rest of the app needs to continue working.
return true
end
content = %Q{%s updated [issue %d: %s](%s) in %s
~~~ quote
%s
~~~} % [User.current.name, issue.id, issue.subject, url(issue),
project.name, issue.notes]
send_zulip_message(content, project)
end
private
......@@ -56,19 +91,26 @@ class NotificationHook < Redmine::Hook::Listener
return Setting.plugin_redmine_zulip[:zulip_stream]
end
def send_message(data)
Rails.logger.info "Forwarding to Zulip: #{data[:payload]}"
def url(issue)
return "#{Setting[:protocol]}://#{Setting[:host_name]}/issues/#{issue.id}"
end
def send_zulip_message(content, project)
data = {"email" => zulip_email(project),
"api-key" => zulip_api_key(project),
"to" => zulip_stream(project),
"type" => "stream",
"subject" => project.name,
"content" => content}
Rails.logger.info "Forwarding to Zulip: #{data[:content]}"
http = Net::HTTP.new("api.zulip.com", 443)
http.use_ssl = true
req = Net::HTTP::Post.new("/api/v1/external/redmine")
req.set_form_data({
"email" => data[:zulip_email],
"api-key" => data[:zulip_api_key],
"stream" => data[:zulip_stream],
"payload" => data[:payload]
})
req = Net::HTTP::Post.new("/api/v1/send_message")
req.set_form_data(data)
begin
http.request(req)
......@@ -76,37 +118,4 @@ class NotificationHook < Redmine::Hook::Listener
Rails.logger.error "Error while POSTing to Zulip: #{e}"
end
end
def extract_and_send(context, type)
issue = context[:issue]
project = issue.project
if !configured(project)
# Fail silently: the rest of the app needs to continue working.
return true
end
author = CGI::escapeHTML(User.current.name)
subject = CGI::escapeHTML(issue.subject)
url = "#{Setting[:protocol]}://#{Setting[:host_name]}/issues/#{issue.id}"
payload = {:type => type,
:project => project.name,
:issue_id => issue.id,
:issue_subject => subject,
:issue_description => issue.description,
:priority => issue.priority.to_s,
:status => issue.status.to_s,
:assignee => issue.assigned_to.to_s,
:notes => issue.notes,
:author => author,
:url => "#{url}##{issue.id}"}.to_json
data = {:payload => payload,
:zulip_email => zulip_email(project),
:zulip_api_key => zulip_api_key(project),
:zulip_stream => zulip_stream(project)}
send_message(data)
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment