Commit 815e9f15 authored by Lobster's avatar Lobster
Browse files

Fix codestyle

parent 4ef28813
require_relative "lib/version"
Redmine::Plugin.register :redmine_zulip do
name 'Zulip'
author 'Zulip, Inc.'
description 'Sends notifications to Zulip.'
version RedmineZulip::VERSION
url 'https://github.com/zulip/zulip-redmine-plugin'
author_url 'https://www.zulip.org/'
name 'Zulip'
author 'Zulip, Inc.'
description 'Sends notifications to Zulip.'
version RedmineZulip::VERSION
url 'https://github.com/zulip/zulip-redmine-plugin'
author_url 'https://www.zulip.org/'
Rails.configuration.to_prepare do
require_dependency 'zulip_hooks'
require_dependency 'zulip_view_hooks'
require_dependency 'project_patch'
Project.send(:include, RedmineZulip::Patches::ProjectPatch)
end
Rails.configuration.to_prepare do
require_dependency 'zulip_hooks'
require_dependency 'zulip_view_hooks'
require_dependency 'project_patch'
Project.send(:include, RedmineZulip::Patches::ProjectPatch)
end
settings :partial => 'settings/redmine_zulip',
:default => {
:zulip_email => "",
:zulip_api_key => "",
:zulip_stream => "",
:zulip_url => ""}
settings :partial => 'settings/redmine_zulip', :default => {
:zulip_email => "",
:zulip_api_key => "",
:zulip_stream => "",
:zulip_url => ""
}
end
module RedmineZulip
module Patches
module ProjectPatch
def self.included(base)
base.class_eval do
safe_attributes 'zulip_email', 'zulip_api_key', 'zulip_stream'
end
end
module Patches
module ProjectPatch
def self.included(base)
base.class_eval do
safe_attributes 'zulip_email', 'zulip_api_key', 'zulip_stream'
end
end
end
end
end
module RedmineZulip
VERSION = "0.91"
VERSION = "1.0"
end
......@@ -4,18 +4,18 @@ require 'json'
class NotificationHook < Redmine::Hook::Listener
# We generate Zulips for creating and updating issues.
# We generate Zulips for creating and updating issues.
def controller_issues_new_after_save(context = {})
issue = context[:issue]
project = issue.project
def controller_issues_new_after_save(context = {})
issue = context[:issue]
project = issue.project
if !configured(project)
# Fail silently: the rest of the app needs to continue working.
return true
end
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
content = %Q{%s opened [issue %d: %s](%s) in %s
~~~ quote
%s
......@@ -24,109 +24,111 @@ 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]
project.name, issue.description, issue.priority.to_s,
issue.status.to_s, issue.assigned_to.to_s]
send_zulip_message(content, project)
end
send_zulip_message(content, project)
end
def controller_issues_edit_after_save(context = {})
issue = context[:issue]
project = issue.project
def controller_issues_edit_after_save(context = {})
issue = context[:issue]
project = issue.project
if !configured(project)
# Fail silently: the rest of the app needs to continue working.
return true
end
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
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]
project.name, issue.notes]
send_zulip_message(content, project)
end
send_zulip_message(content, project)
end
private
private
def configured(project)
# The plugin can be configured as a system setting or per-project.
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
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!"
false
def configured(project)
# The plugin can be configured as a system setting or per-project.
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
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
def zulip_email(project)
if project.zulip_email.present?
return project.zulip_email
end
Setting.plugin_redmine_zulip["zulip_email"]
Rails.logger.info "Missing config, can't sent to Zulip!"
false
end
def zulip_email(project)
if project.zulip_email.present?
return project.zulip_email
end
Setting.plugin_redmine_zulip["zulip_email"]
end
def zulip_api_key(project)
if project.zulip_api_key.present?
return project.zulip_api_key
end
Setting.plugin_redmine_zulip["zulip_api_key"]
def zulip_api_key(project)
if project.zulip_api_key.present?
return project.zulip_api_key
end
Setting.plugin_redmine_zulip["zulip_api_key"]
end
def zulip_stream(project)
if project.zulip_stream.present?
return project.zulip_stream
end
Setting.plugin_redmine_zulip["zulip_stream"]
def zulip_stream(project)
if project.zulip_stream.present?
return project.zulip_stream
end
Setting.plugin_redmine_zulip["zulip_stream"]
end
def zulip_url()
Setting.plugin_redmine_zulip["zulip_url"]
end
def zulip_url()
Setting.plugin_redmine_zulip["zulip_url"]
end
def url(issue)
"#{Setting[:protocol]}://#{Setting[:host_name]}/issues/#{issue.id}"
end
def url(issue)
"#{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, project)
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")
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)
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
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}"
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
class NotificationViewHook < Redmine::Hook::ViewListener
render_on(:view_projects_form, :partial => 'projects/redmine_zulip', :layout => false)
render_on(:view_projects_form, :partial => 'projects/redmine_zulip', :layout => false)
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