Commit 4ef28813 authored by Lobster's avatar Lobster
Browse files

Simplify settings

parent e196d2c9
<p>
<%= 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"] %>
......@@ -13,16 +18,6 @@
<%= text_field_tag 'settings[zulip_stream]', @settings["zulip_stream"] %>
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_server)) %>
<%= text_field_tag 'settings[zulip_server]', @settings["zulip_server"] %>
</p>
<p>
<%= content_tag(:label, l(:zulip_settings_label_port)) %>
<%= text_field_tag 'settings[zulip_port]', @settings["zulip_port"] %>
</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 %>
......
en:
zulip_settings_header: Zulip Plugin Configuration
zulip_settings_label_stream: Stream name
zulip_settings_label_server: Server URL
zulip_settings_label_port: Server Port
zulip_settings_label_stream: Zulip stream
zulip_settings_label_url: Zulip URL
zulip_settings_label_email: Zulip Email
zulip_settings_label_api_key: Zulip API key
zulip_settings_label_projects: Projects
field_zulip_email: Zulip Email
field_zulip_api_key: Zulip API key
field_zulip_stream: Stream name
field_zulip_stream: Zulip stream
......@@ -20,6 +20,5 @@ Redmine::Plugin.register :redmine_zulip do
:zulip_email => "",
:zulip_api_key => "",
:zulip_stream => "",
:zulip_server => "",
:zulip_port => ""}
:zulip_url => ""}
end
......@@ -54,99 +54,79 @@ class NotificationHook < Redmine::Hook::Listener
def configured(project)
# The plugin can be configured as a system setting or per-project.
if !project.zulip_email.empty? && !project.zulip_api_key.empty? &&
!project.zulip_stream.empty? && Setting.plugin_redmine_zulip["projects"] &&
Setting.plugin_redmine_zulip["zulip_server"]
if project.zulip_email.present? &&
project.zulip_api_key.present? &&
project.zulip_stream.present? &&
Setting.plugin_redmine_zulip["projects"] &&
Setting.plugin_redmine_zulip["zulip_url"].present?
# We have full per-project settings.
return true
elsif Setting.plugin_redmine_zulip["projects"] &&
Setting.plugin_redmine_zulip["zulip_email"] &&
Setting.plugin_redmine_zulip["zulip_api_key"] &&
Setting.plugin_redmine_zulip["zulip_stream"] &&
Setting.plugin_redmine_zulip["zulip_server"]
end
if Setting.plugin_redmine_zulip["projects"] &&
Setting.plugin_redmine_zulip["zulip_email"].present? &&
Setting.plugin_redmine_zulip["zulip_api_key"].present? &&
Setting.plugin_redmine_zulip["zulip_stream"].present? &&
Setting.plugin_redmine_zulip["zulip_url"].present?
# We have full global settings.
return true
end
Rails.logger.info "Missing config, can't sent to Zulip!"
return false
false
end
def zulip_email(project)
if !project.zulip_email.empty?
if project.zulip_email.present?
return project.zulip_email
end
return Setting.plugin_redmine_zulip["zulip_email"]
Setting.plugin_redmine_zulip["zulip_email"]
end
def zulip_api_key(project)
if !project.zulip_api_key.empty?
if project.zulip_api_key.present?
return project.zulip_api_key
end
return Setting.plugin_redmine_zulip["zulip_api_key"]
Setting.plugin_redmine_zulip["zulip_api_key"]
end
def zulip_stream(project)
if !project.zulip_stream.empty?
if project.zulip_stream.present?
return project.zulip_stream
end
return Setting.plugin_redmine_zulip["zulip_stream"]
Setting.plugin_redmine_zulip["zulip_stream"]
end
def zulip_server()
server = Setting.plugin_redmine_zulip["zulip_server"].sub(
%r{^https?:(//|\\\\)}i, ""
)
if server.end_with?("/api")
server[0..-5]
else
server
end
end
def zulip_port()
if Setting.plugin_redmine_zulip["zulip_port"].present?
return Setting.plugin_redmine_zulip["zulip_port"]
end
return 443
def zulip_url()
Setting.plugin_redmine_zulip["zulip_url"]
end
def zulip_api_basename()
basename = Setting.plugin_redmine_zulip["zulip_server"]
unless basename.end_with?("/api")
basename << "/api"
end
if zulip_port == 443 && !basename.start_with?("https://")
"https://#{basename}"
else
basename
end
def url(issue)
"#{Setting[:protocol]}://#{Setting[:host_name]}/issues/#{issue.id}"
end
def url(issue)
return "#{Setting[:protocol]}://#{Setting[:host_name]}/issues/#{issue.id}"
end
def send_zulip_message(content, project)
def send_zulip_message(content, project)
data = {"to" => zulip_stream(project),
"type" => "stream",
"subject" => project.name,
"content" => content}
data = {"to" => zulip_stream(project),
"type" => "stream",
"subject" => project.name,
"content" => content}
Rails.logger.info "Forwarding to Zulip: #{data['content']}"
Rails.logger.info "Forwarding to Zulip: #{data['content']}"
uri = URI("#{zulip_url}/v1/messages")
http = Net::HTTP.new(zulip_server(), zulip_port())
http.use_ssl = true
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)
req = Net::HTTP::Post.new("#{zulip_api_basename()}/v1/messages")
req.basic_auth(zulip_email(project), zulip_api_key(project))
req.add_field('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
res = http.request(req)
unless res.code == "200"
Rails.logger.error "Error while POSTing to Zulip: #{res.body}"
end
end
if res.code == "200"
Rails.logger.info "Zulip message sent!"
else
Rails.logger.error "Error while POSTing to Zulip: #{res.body}"
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