Commit 7c9bbb0f authored by Lobster's avatar Lobster
Browse files

Improve subject settings

parent 815e9f15
......@@ -2,5 +2,3 @@ Redmine Zulip plugin
====================
Get notifications about new and updated issues directly in Zulip!
Please visit https://zulipchat.com/integrations/#redmine for detailed installation instructions.
<p>
<%= form.text_field :zulip_email %>
This should be a bot address for most uses.
<%= form.text_field :zulip_email %>
This should be a bot address for most uses.
</p>
<p>
<%= form.text_field :zulip_api_key %>
The API key for the bot.
<%= form.text_field :zulip_api_key %>
The API key for the bot.
</p>
<p>
<%= form.text_field :zulip_stream %>
Which Zulip stream will receive updates. The message topic will be the project name.
<%= form.text_field :zulip_stream %>
Which Zulip stream will receive updates.
</p>
<p>
<%= form.check_box :zulip_subject_project %>
Messages will be posted on project subject.
</p>
<p>
<%= form.check_box :zulip_subject_issue %>
Messages will be posted on issue subject.
</p>
<p>
<%= form.check_box :zulip_subject_version %>
Messages will be posted on version subject.
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_url)) %>
<%= text_field_tag 'settings[zulip_url]', @settings["zulip_url"] %>
<%= content_tag(:label, l(:zulip_settings_label_url)) %>
<%= text_field_tag 'settings[zulip_url]', @settings["zulip_url"] %>
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_email)) %>
<%= text_field_tag 'settings[zulip_email]', @settings["zulip_email"] %>
<%= content_tag(:label, l(:zulip_settings_label_email)) %>
<%= text_field_tag 'settings[zulip_email]', @settings["zulip_email"] %>
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_api_key)) %>
<%= text_field_tag 'settings[zulip_api_key]', @settings["zulip_api_key"] %>
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_stream)) %>
<%= text_field_tag 'settings[zulip_stream]', @settings["zulip_stream"] %>
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_projects)) %>
<%= select_tag 'settings[projects][]', project_tree_options_for_select(Project.active, :selected => Project.find_by_id(@settings["projects"])), :multiple => true, :size => Project.active.size %>
<%= content_tag(:label, l(:zulip_settings_label_api_key)) %>
<%= text_field_tag 'settings[zulip_api_key]', @settings["zulip_api_key"] %>
</p>
......@@ -8,3 +8,6 @@ en:
field_zulip_email: Zulip Email
field_zulip_api_key: Zulip API key
field_zulip_stream: Zulip stream
field_zulip_subject_issue: Issue subject
field_zulip_subject_project: Project subject
field_zulip_subject_version: Version subject
class AddZulipTopicSettingsToProject < ActiveRecord::Migration
def change
add_column :projects, :zulip_subject_project, :boolean, null: true
add_column :projects, :zulip_subject_issue, :boolean, null: true
add_column :projects, :zulip_subject_version, :boolean, null: true
reversible do |dir|
dir.up do
execute <<-SQL
UPDATE projects
SET zulip_subject_version = FALSE,
zulip_subject_issue = FALSE,
zulip_subject_project = FALSE
SQL
end
end
change_column_null :projects, :zulip_subject_project, false
change_column_null :projects, :zulip_subject_issue, false
change_column_null :projects, :zulip_subject_version, false
end
end
......@@ -16,9 +16,11 @@ Redmine::Plugin.register :redmine_zulip do
end
settings :partial => 'settings/redmine_zulip', :default => {
:zulip_email => "",
:zulip_api_key => "",
:zulip_stream => "",
:zulip_url => ""
"zulip_email" => "",
"zulip_api_key" => "",
"zulip_url" => "",
"project_stream" => false,
"version_stream" => false,
"issue_stream" => false
}
end
......@@ -3,7 +3,13 @@ module RedmineZulip
module ProjectPatch
def self.included(base)
base.class_eval do
safe_attributes 'zulip_email', 'zulip_api_key', 'zulip_stream'
safe_attributes(
"zulip_email",
"zulip_api_key",
"zulip_subject_version",
"zulip_subject_issue",
"zulip_subject_project"
)
end
end
end
......
......@@ -15,7 +15,7 @@ class NotificationHook < Redmine::Hook::Listener
return true
end
content = %Q{%s opened [issue %d: %s](%s) in %s
content = %Q{%s opened [issue %d: %s](%s)
~~~ quote
%s
......@@ -24,10 +24,10 @@ class NotificationHook < Redmine::Hook::Listener
**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]
issue.description, issue.priority.to_s,
issue.status.to_s, issue.assigned_to.to_s]
send_zulip_message(content, project)
send_zulip_message(content, issue)
end
def controller_issues_edit_after_save(context = {})
......@@ -39,14 +39,14 @@ class NotificationHook < Redmine::Hook::Listener
return true
end
content = %Q{%s updated [issue %d: %s](%s) in %s
content = %Q{%s updated [issue %d: %s](%s)
~~~ quote
%s
~~~} % [User.current.name, issue.id, issue.subject, url(issue),
project.name, issue.notes]
~~~} % [User.current.name, issue.id, issue.subject,
url(issue), issue.notes]
send_zulip_message(content, project)
send_zulip_message(content, issue)
end
private
......@@ -104,31 +104,46 @@ class NotificationHook < Redmine::Hook::Listener
"#{Setting[:protocol]}://#{Setting[:host_name]}/issues/#{issue.id}"
end
def send_zulip_message(content, project)
data = {
"to" => zulip_stream(project),
"type" => "stream",
"subject" => project.name,
"content" => content
}
def send_zulip_message(content, issue)
project = issue.project
Rails.logger.info "Forwarding to Zulip: #{data['content']}"
subjects = []
if project.zulip_subject_project
subjects << project.name
end
if project.zulip_subject_issue
subjects << "Issue #{issue.id}"
end
if project.zulip_subject_version && issue.fixed_version.present?
subjects << "Version #{issue.fixed_version}"
end
uri = URI("#{zulip_url}/v1/messages")
subjects.each do |subject|
data = {
"to" => zulip_stream(project),
"type" => "stream",
"subject" => subject,
"content" => content
}
req = Net::HTTP::Post.new(uri)
req.basic_auth(zulip_email(project), zulip_api_key(project))
req["User-Agent"] = "ZulipRedmine/#{RedmineZulip::VERSION}"
req.set_form_data(data)
Rails.logger.info "Forwarding to Zulip: #{data['content']}"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
uri = URI("#{zulip_url}/v1/messages")
req = Net::HTTP::Post.new(uri)
req.basic_auth(zulip_email(project), zulip_api_key(project))
req["User-Agent"] = "ZulipRedmine/#{RedmineZulip::VERSION}"
req.set_form_data(data)
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
if res.code == "200"
Rails.logger.info "Zulip message sent!"
else
Rails.logger.error "Error while POSTing to Zulip: #{res.body}"
if res.code == "200"
Rails.logger.info "Zulip message sent!"
else
Rails.logger.error "Error while POSTing to Zulip: #{res.body}"
end
end
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