Commit c38578428b19b6375f24266ea5f4a98ca290eb58

Authored by Valeriy Sizov
1 parent 65dc68b3

System Hooks: CRUD has done

app/controllers/admin/hooks_controller.rb 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +class Admin::HooksController < ApplicationController
  2 + layout "admin"
  3 + before_filter :authenticate_user!
  4 + before_filter :authenticate_admin!
  5 +
  6 + def index
  7 + @hooks = SystemHook.all
  8 + @hook = SystemHook.new
  9 + end
  10 +
  11 + def create
  12 + @hook = SystemHook.new(params[:hook])
  13 +
  14 + respond_to do |format|
  15 + if @hook.save
  16 + format.html { redirect_to admin_hooks_path, notice: 'Hook was successfully created.' }
  17 + else
  18 + format.html { render :index }
  19 + end
  20 + end
  21 + end
  22 +
  23 + def destroy
  24 + @hook = SystemHook.find(params[:id])
  25 + @hook.destroy
  26 +
  27 + redirect_to admin_hooks_path
  28 + end
  29 +
  30 +
  31 + def test
  32 + @hook = @project.hooks.find(params[:id])
  33 + commits = @project.commits(@project.default_branch, nil, 3)
  34 + data = @project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{@project.default_branch}", current_user)
  35 + @hook.execute(data)
  36 +
  37 + redirect_to :back
  38 + end
  39 +end
app/controllers/admin/mailer_controller.rb
@@ -1,45 +0,0 @@ @@ -1,45 +0,0 @@
1 -class Admin::MailerController < ApplicationController  
2 - layout "admin"  
3 - before_filter :authenticate_user!  
4 - before_filter :authenticate_admin!  
5 -  
6 - def preview  
7 -  
8 - end  
9 -  
10 - def preview_note  
11 - @note = Note.first  
12 - @user = @note.author  
13 - @project = @note.project  
14 - case params[:type]  
15 - when "Commit" then  
16 - @commit = @project.commit  
17 - render :file => 'notify/note_commit_email', :layout => 'notify'  
18 - when "Issue" then  
19 - @issue = Issue.first  
20 - render :file => 'notify/note_issue_email', :layout => 'notify'  
21 - else  
22 - render :file => 'notify/note_wall_email', :layout => 'notify'  
23 - end  
24 - rescue  
25 - render :text => "Preview not available"  
26 - end  
27 -  
28 - def preview_user_new  
29 - @user = User.first  
30 - @password = "DHasJKDHAS!"  
31 -  
32 - render :file => 'notify/new_user_email', :layout => 'notify'  
33 - rescue  
34 - render :text => "Preview not available"  
35 - end  
36 -  
37 - def preview_issue_new  
38 - @issue = Issue.first  
39 - @user = @issue.assignee  
40 - @project = @issue.project  
41 - render :file => 'notify/new_issue_email', :layout => 'notify'  
42 - rescue  
43 - render :text => "Preview not available"  
44 - end  
45 -end  
app/models/web_hook.rb
@@ -12,8 +12,6 @@ class WebHook &lt; ActiveRecord::Base @@ -12,8 +12,6 @@ class WebHook &lt; ActiveRecord::Base
12 12
13 def execute(data) 13 def execute(data)
14 WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" }) 14 WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" })
15 - rescue  
16 - # There was a problem calling this web hook, let's forget about it.  
17 end 15 end
18 end 16 end
19 # == Schema Information 17 # == Schema Information
app/views/admin/hooks/_data_ex.html.erb 0 → 100644
@@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
  1 +<% data_ex_str = <<eos
  2 +{
  3 + :before => "95790bf891e76fee5e1747ab589903a6a1f80f22",
  4 + :after => "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  5 + :ref => "refs/heads/master",
  6 + :user_id => 4,
  7 + :user_name => "John Smith",
  8 + :repository => {
  9 + :name => "Diaspora",
  10 + :url => "localhost/diaspora",
  11 + :description => "",
  12 + :homepage => "localhost/diaspora",
  13 + :private => true
  14 + },
  15 + :commits => [
  16 + [0] {
  17 + :id => "450d0de7532f8b663b9c5cce183b...",
  18 + :message => "Update Catalan translation to e38cb41.",
  19 + :timestamp => "2011-12-12T14:27:31+02:00",
  20 + :url => "http://localhost/diaspora/commits/450d0de7532f...",
  21 + :author => {
  22 + :name => "Jordi Mallach",
  23 + :email => "jordi@softcatala.org"
  24 + }
  25 + },
  26 +
  27 + ....
  28 +
  29 + [3] {
  30 + :id => "da1560886d4f094c3e6c9ef40349...",
  31 + :message => "fixed readme",
  32 + :timestamp => "2012-01-03T23:36:29+02:00",
  33 + :url => "http://localhost/diaspora/commits/da1560886d...",
  34 + :author => {
  35 + :name => "gitlab dev user",
  36 + :email => "gitlabdev@dv6700.(none)"
  37 + }
  38 + }
  39 + ],
  40 + total_commits_count => 3
  41 +}
  42 +eos
  43 +%>
  44 +<% js_lexer = Pygments::Lexer[:js] %>
  45 +<%= raw js_lexer.highlight(data_ex_str) %>
app/views/admin/hooks/index.html.haml 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +.alert.alert-info
  2 + %span
  3 + Post receive hooks for binding events.
  4 + %br
  5 + Read more about system hooks
  6 + %strong #{link_to "here", help_system_hooks_path, :class => "vlink"}
  7 +
  8 += form_for @hook, :as => :hook, :url => admin_hooks_path do |f|
  9 + -if @hook.errors.any?
  10 + .alert-message.block-message.error
  11 + - @hook.errors.full_messages.each do |msg|
  12 + %p= msg
  13 + .clearfix
  14 + = f.label :url, "URL:"
  15 + .input
  16 + = f.text_field :url, :class => "text_field xxlarge"
  17 + &nbsp;
  18 + = f.submit "Add System Hook", :class => "btn primary"
  19 +%hr
  20 +
  21 +-if @hooks.any?
  22 + %h3
  23 + Hooks
  24 + %small (#{@hooks.count})
  25 + %br
  26 + %table.admin-table
  27 + %tr
  28 + %th URL
  29 + %th Method
  30 + %th
  31 + - @hooks.each do |hook|
  32 + %tr
  33 + %td
  34 + = link_to admin_hook_path(hook) do
  35 + %strong= hook.url
  36 + = link_to 'Test Hook', admin_hook_test_path(hook), :class => "btn small right"
  37 + %td POST
  38 + %td
  39 + = link_to 'Remove', admin_hook_path(hook), :confirm => 'Are you sure?', :method => :delete, :class => "danger btn small right"
app/views/admin/mailer/preview.html.haml
@@ -1,28 +0,0 @@ @@ -1,28 +0,0 @@
1 -%p This is page with preview for all system emails that are sent to user  
2 -%p Email previews built based on existing Project/Commit/Issue base - so some preview maybe unavailable unless object appear in system  
3 -  
4 -#accordion  
5 - %h3  
6 - %a New user  
7 - %div  
8 - %iframe{ :src=> admin_mailer_preview_user_new_path, :width=>"100%", :height=>"350"}  
9 - %h3  
10 - %a New issue  
11 - %div  
12 - %iframe{ :src=> admin_mailer_preview_issue_new_path, :width=>"100%", :height=>"350"}  
13 - %h3  
14 - %a Commit note  
15 - %div  
16 - %iframe{ :src=> admin_mailer_preview_note_path(:type => "Commit"), :width=>"100%", :height=>"350"}  
17 - %h3  
18 - %a Issue note  
19 - %div  
20 - %iframe{ :src=> admin_mailer_preview_note_path(:type => "Issue"), :width=>"100%", :height=>"350"}  
21 - %h3  
22 - %a Wall note  
23 - %div  
24 - %iframe{ :src=> admin_mailer_preview_note_path(:type => "Wall"), :width=>"100%", :height=>"350"}  
25 -  
26 -:javascript  
27 - $(function() {  
28 - $("#accordion").accordion(); });  
app/views/help/system_hooks.html.haml 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +%h3 System hooks
  2 +.back_link
  3 + = link_to :back do
  4 + &larr; back
  5 +%hr
  6 +
  7 +%p.slead
  8 + Your Gitlab instance can perform HTTP POST request on next event: create_project, delete_project, create_user, delete_user, change_team_member.
  9 + %br
  10 + System Hooks can be used for logging or change information in LDAP server.
  11 + %br
  12 +%h5 Hooks request example:
  13 += render "admin/hooks/data_ex"
app/views/layouts/admin.html.haml
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 %li{:class => tab_class(:admin_logs)} 15 %li{:class => tab_class(:admin_logs)}
16 = link_to "Logs", admin_logs_path 16 = link_to "Logs", admin_logs_path
17 %li{:class => tab_class(:admin_emails)} 17 %li{:class => tab_class(:admin_emails)}
18 - = link_to "Emails", admin_emails_path 18 + = link_to "Hooks", admin_hooks_path
19 %li{:class => tab_class(:admin_resque)} 19 %li{:class => tab_class(:admin_resque)}
20 = link_to "Resque", admin_resque_path 20 = link_to "Resque", admin_resque_path
21 21
config/routes.rb
@@ -28,6 +28,7 @@ Gitlab::Application.routes.draw do @@ -28,6 +28,7 @@ Gitlab::Application.routes.draw do
28 get 'help/workflow' => 'help#workflow' 28 get 'help/workflow' => 'help#workflow'
29 get 'help/api' => 'help#api' 29 get 'help/api' => 'help#api'
30 get 'help/web_hooks' => 'help#web_hooks' 30 get 'help/web_hooks' => 'help#web_hooks'
  31 + get 'help/system_hooks' => 'help#system_hooks'
31 32
32 # 33 #
33 # Admin Area 34 # Admin Area
@@ -47,11 +48,13 @@ Gitlab::Application.routes.draw do @@ -47,11 +48,13 @@ Gitlab::Application.routes.draw do
47 end 48 end
48 end 49 end
49 resources :team_members, :only => [:edit, :update, :destroy] 50 resources :team_members, :only => [:edit, :update, :destroy]
50 - get 'emails', :to => 'mailer#preview'  
51 get 'mailer/preview_note' 51 get 'mailer/preview_note'
52 get 'mailer/preview_user_new' 52 get 'mailer/preview_user_new'
53 get 'mailer/preview_issue_new' 53 get 'mailer/preview_issue_new'
54 54
  55 + resources :hooks, :only => [:index, :create, :destroy] do
  56 + get :test
  57 + end
55 resource :logs 58 resource :logs
56 resource :resque, :controller => 'resque' 59 resource :resque, :controller => 'resque'
57 root :to => "dashboard#index" 60 root :to => "dashboard#index"