Merge pull request #12 from ffogarasi/master

Added privileges and possibility to add multiple webhook's
This commit is contained in:
Ryo SUETSUGU
2016-11-16 00:41:27 +09:00
committed by GitHub
7 changed files with 79 additions and 30 deletions

View File

@@ -12,7 +12,7 @@ Install
Type below commands:
$ cd $RAILS_ROOT/plugins
$ git clone git://github.com/suer/redmine_webhook.git
$ git clone git://github.com/ffogarasi/redmine_webhook.git
$ rake redmine:plugins:migrate RAILS_ENV=production
Then, restart your redmine.

View File

@@ -1,13 +1,34 @@
class WebhookSettingsController < ApplicationController
before_filter :find_project
def update
webhook = Webhook.where(:project_id => @project.id).first_or_create
before_filter :find_project, :authorize
def create
webhook = Webhook.new(:project_id => @project.id)
webhook.url = params[:url]
if webhook.save
flash[:notice] = l(:notice_successful_update)
flash[:notice] = l(:notice_successful_create_webhook)
else
flash[:error] = l(:notice_fail_to_save_settings)
flash[:error] = l(:notice_fail_create_webhook)
end
redirect_to settings_project_path(@project, :tab => 'webhook')
end
def update
id = params[:webhook_id]
webhook = Webhook.where(:project_id => @project.id).where(:id => id).first
webhook.url = params[:url]
if webhook.save
flash[:notice] = l(:notice_successful_update_webhook)
else
flash[:error] = l(:notice_fail_update_webhook)
end
redirect_to settings_project_path(@project, :tab => 'webhook')
end
def destroy
id = params[:webhook_id]
webhook = Webhook.where(:project_id => @project.id).where(:id => id).first
if webhook.destroy
flash[:notice] = l(:notice_successful_delete_webhook)
else
flash[:error] = l(:notice_fail_delete_webhook)
end
redirect_to settings_project_path(@project, :tab => 'webhook')
end

View File

@@ -1,12 +1,30 @@
<% webhook = Webhook.where(:project_id => @project.id).first || Webhook.new(:project_id => @project.id) %>
<%= form_tag(update_webhook_settings_path(@project), :method => :put, :class => "tabular") do %>
<% if User.current.allowed_to?( :manage_hook, @project) %>
<% if Webhook.where(:project_id => @project.id).first%>
<div class="box tabular">
<p>
<label>URL</label>
<% Webhook.where(:project_id => @project.id).each do |webhook|%>
<%= form_tag(update_webhook_path(@project, webhook.id), :method => :put, :class => "tabular") do %>
<span>
<span><strong>URL</strong></span>
<%= text_field_tag :url, webhook.url, :size => 80 %>
</p>
<%= submit_tag l(:button_update) %>
</span>
<% end %>
<%= link_to "Delete", delete_webhook_path(@project, webhook.id), :class => "icon icon-del", :method => :delete, :confirm => "Are you sure ?" %>
</br>
<% end %>
</div>
<% end %>
<%= form_tag(create_webhook_path(@project), :method => :post, :class => "tabular") do %>
<div class="box tabular">
<span>
<span><strong>URL</strong></span>
<%= text_field_tag :url, '', :size => 80 %>
<%= submit_tag l(:button_add) %>
</span>
</div>
<%= submit_tag l(:button_save) %>
<% end %>
<% end %>

View File

@@ -1,4 +1,10 @@
# English strings go here for Rails i18n
en:
webhook: WebHook
notice_fail_to_save_settings: Fail to save
notice_fail_create_webhook: Webhook creation failed.
notice_successful_create_webhook: Webhook created successfully.
notice_fail_update_webhook: Webhook update failed.
notice_successful_update_webhook : Webhook updated successfully.
notice_fail_delete_webhook: Webhook delete failed.
notice_successful_delete_webhook : Webhook deleted successfully.

View File

@@ -1,3 +1,4 @@
get 'projects/:id/webhook_settings/show', :controller => 'webhook_settings', :action => 'show', :as => :show_webhook_settings
put 'projects/:id/webhook_settings/update', :controller => 'webhook_settings', :action => 'update', :as => :update_webhook_settings
post 'projects/:id/webhook_settings/create', :controller => 'webhook_settings', :action => 'create', :as => :create_webhook
put 'projects/:id/webhook_settings/:webhook_id', :controller => 'webhook_settings', :action => 'update', :as => :update_webhook
delete 'projects/:id/webhook_settings/:webhook_id', :controller => 'webhook_settings', :action => 'destroy', :as => :delete_webhook

View File

@@ -13,4 +13,5 @@ Redmine::Plugin.register :redmine_webhook do
version '0.0.1'
url 'https://github.com/suer/redmine_webhook'
author_url 'http://d.hatena.ne.jp/suer'
permission :manage_hook, {:webhook_settings => [:show,:update,:create, :destroy]}, :require => :member
end

View File

@@ -5,9 +5,9 @@ module RedmineWebhook
issue = context[:issue]
controller = context[:controller]
project = issue.project
webhook = Webhook.where(:project_id => project.project.id).first
return unless webhook
post(webhook, issue_to_json(issue, controller))
webhooks = Webhook.where(:project_id => project.project.id)
return unless webhooks
post(webhooks, issue_to_json(issue, controller))
end
def controller_issues_edit_after_save(context = {})
@@ -15,9 +15,9 @@ module RedmineWebhook
controller = context[:controller]
issue = context[:issue]
project = issue.project
webhook = Webhook.where(:project_id => project.project.id).first
return unless webhook
post(webhook, journal_to_json(issue, journal, controller))
webhooks = Webhook.where(:project_id => project.project.id)
return unless webhooks
post(webhooks, journal_to_json(issue, journal, controller))
end
private
@@ -42,16 +42,18 @@ module RedmineWebhook
}.to_json
end
def post(webhook, request_body)
def post(webhooks, request_body)
Thread.start do
begin
Faraday.post do |req|
req.url webhook.url
req.headers['Content-Type'] = 'application/json'
req.body = request_body
webhooks.each do |webhook|
begin
Faraday.post do |req|
req.url webhook.url
req.headers['Content-Type'] = 'application/json'
req.body = request_body
end
rescue => e
Rails.logger.error e
end
rescue => e
Rails.logger.error e
end
end
end