Commit 7b0cd969e05995b3792aed76ed15b482ed4381a3

Authored by Dmitriy Zaporozhets
2 parents a3efa430 473445c7

Merge branch 'web_hooks_scaffold'

.foreman 0 → 100644
... ... @@ -0,0 +1 @@
  1 +port: 3000
... ...
Procfile 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +web: bundle exec rails s -p $PORT
  2 +worker: bundle exec rake environment resque:work QUEUE=*
... ...
app/assets/stylesheets/projects.css.scss
... ... @@ -181,6 +181,13 @@ input.ssh_project_url {
181 181 }
182 182 }
183 183  
  184 +.text_field {
  185 + width:400px;
  186 + padding:8px;
  187 + font-size:14px;
  188 + @include round-borders-all(4px);
  189 +}
  190 +
184 191 .input_button {
185 192 padding:8px;
186 193 font-size:14px;
... ...
app/controllers/hooks_controller.rb 0 → 100644
... ... @@ -0,0 +1,51 @@
  1 +class HooksController < ApplicationController
  2 + before_filter :authenticate_user!
  3 + before_filter :project
  4 + layout "project"
  5 +
  6 + # Authorize
  7 + before_filter :add_project_abilities
  8 + before_filter :authorize_read_project!
  9 + before_filter :authorize_admin_project!, :only => [:new, :create, :destroy]
  10 +
  11 + respond_to :html
  12 +
  13 + def index
  14 + @hooks = @project.web_hooks
  15 + end
  16 +
  17 + def new
  18 + @hook = @project.web_hooks.new
  19 + end
  20 +
  21 + def create
  22 + @hook = @project.web_hooks.new(params[:hook])
  23 + @hook.save
  24 +
  25 + if @hook.valid?
  26 + redirect_to project_hook_path(@project, @hook)
  27 + else
  28 + render :new
  29 + end
  30 + end
  31 +
  32 + def test
  33 + @hook = @project.web_hooks.find(params[:id])
  34 + commits = @project.commits(@project.default_branch, nil, 3)
  35 + data = @project.web_hook_data(commits.last.id, commits.first.id, "refs/heads/#{@project.default_branch}")
  36 + @hook.execute(data)
  37 +
  38 + redirect_to :back
  39 + end
  40 +
  41 + def show
  42 + @hook = @project.web_hooks.find(params[:id])
  43 + end
  44 +
  45 + def destroy
  46 + @hook = @project.web_hooks.find(params[:id])
  47 + @hook.destroy
  48 +
  49 + redirect_to project_hooks_path(@project)
  50 + end
  51 +end
... ...
app/helpers/projects_helper.rb
... ... @@ -35,7 +35,8 @@ module ProjectsHelper
35 35 end
36 36  
37 37 def repository_tab_class
38   - if controller.controller_name == "repositories"
  38 + if controller.controller_name == "repositories" ||
  39 + controller.controller_name == "hooks"
39 40 "current"
40 41 end
41 42 end
... ...
app/views/hooks/_data_ex.html.erb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +<% data_ex_str = <<eos
  2 +{
  3 + :before => "95790bf891e76fee5e1747ab589903a6a1f80f22",
  4 + :after => "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  5 + :ref => "refs/heads/master",
  6 + :repository => {
  7 + :name => "Diaspora",
  8 + :url => "localhost/diaspora",
  9 + :description => "",
  10 + :homepage => "localhost/diaspora",
  11 + :private => true
  12 + },
  13 + :commits => [
  14 + [0] {
  15 + :id => "450d0de7532f8b663b9c5cce183b...",
  16 + :message => "Update Catalan translation to e38cb41.",
  17 + :timestamp => "2011-12-12T14:27:31+02:00",
  18 + :url => "http://localhost/diaspora/commits/450d0de7532f...",
  19 + :author => {
  20 + :name => "Jordi Mallach",
  21 + :email => "jordi@softcatala.org"
  22 + }
  23 + },
  24 +
  25 + ....
  26 +
  27 + [3] {
  28 + :id => "da1560886d4f094c3e6c9ef40349...",
  29 + :message => "fixed readme",
  30 + :timestamp => "2012-01-03T23:36:29+02:00",
  31 + :url => "http://localhost/diaspora/commits/da1560886d...",
  32 + :author => {
  33 + :name => "gitlab dev user",
  34 + :email => "gitlabdev@dv6700.(none)"
  35 + }
  36 + }
  37 + ]
  38 +}
  39 +eos
  40 +%>
  41 +<% js_lexer = Pygments::Lexer[:js] %>
  42 +<%= raw js_lexer.highlight(data_ex_str) %>
... ...
app/views/hooks/index.html.haml 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 += render "repositories/head"
  2 +
  3 +
  4 +
  5 +
  6 +.right= link_to "Add new", new_project_hook_path(@project), :class => "grey-button append-bottom-10"
  7 +- unless @hooks.empty?
  8 + %div.update-data.ui-box.ui-box-small
  9 + .data
  10 + - @hooks.each do |hook|
  11 + %a.update-item{:href => project_hook_path(@project, hook)}
  12 + %span.update-title{:style => "margin-bottom:0px;"}
  13 + = hook.url
  14 + %span.update-author.right
  15 + Added
  16 + = time_ago_in_words(hook.created_at)
  17 + ago
  18 +- else
  19 + %h3 No hooks
  20 +
  21 +.clear
  22 +%h3 Help
  23 +%p
  24 + Post receive hooks. For now only POST request allowed. We send some data with request. Example below
  25 +
  26 +.view_file
  27 + .view_file_header
  28 + %strong POST data passed
  29 + .data.no-padding
  30 + = render "data_ex"
... ...
app/views/hooks/new.html.haml 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 += render "repositories/head"
  2 += form_for [@project, @hook], :as => :hook, :url => project_hooks_path(@project) do |f|
  3 + -if @hook.errors.any?
  4 + %ul
  5 + - @hook.errors.full_messages.each do |msg|
  6 + %li= msg
  7 + = f.label :url, "URL:"
  8 + = f.text_field :url, :class => "text_field"
  9 + .clear
  10 + %br
  11 + .merge-tabs
  12 + = f.submit "Save", :class => "grey-button"
  13 +
... ...
app/views/hooks/show.html.haml 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 += render "repositories/head"
  2 +%h3
  3 + %span.commit.tag POST
  4 + = @hook.url
  5 +
  6 +
  7 +- if can? current_user, :admin_project, @project
  8 + .merge-tabs
  9 + = link_to 'Test Hook', test_project_hook_path(@project, @hook), :class => "grey-button"
  10 + .right
  11 + = link_to 'Remove', project_hook_path(@project, @hook), :confirm => 'Are you sure?', :method => :delete, :class => "red-button"
... ...
app/views/repositories/_head.html.haml
... ... @@ -8,7 +8,7 @@
8 8 = link_to tags_project_repository_path(@project), :class => "tab #{'active' if current_page?(tags_project_repository_path(@project)) }" do
9 9 %span
10 10 Tags
11   - -#= link_to "#", :class => "tab" do
  11 + = link_to project_hooks_path, :class => "tab #{'active' if controller.controller_name == "hooks" }" do
12 12 %span
13 13 Hooks
14 14 -#= link_to "#", :class => "tab" do
... ...
app/workers/post_receive.rb
1 1 class PostReceive
  2 + @queue = :post_receive
  3 +
2 4 def self.perform(reponame, oldrev, newrev, ref)
3 5 project = Project.find_by_path(reponame)
4 6 return false if project.nil?
... ...
config/database.yml
... ... @@ -20,6 +20,6 @@ test:
20 20  
21 21 production:
22 22 adapter: sqlite3
23   - database: db/production.sqlite3
  23 + database: db/development.sqlite3
24 24 pool: 5
25 25 timeout: 5000
... ...
config/routes.rb
1 1 Gitlab::Application.routes.draw do
2 2  
3 3 # Optionally, enable Resque here
4   - # require 'resque/server'
5   - # mount Resque::Server.new, at: '/info/resque'
  4 + require 'resque/server'
  5 + mount Resque::Server.new, at: '/info/resque'
6 6  
7 7 get 'tags'=> 'tags#index'
8 8 get 'tags/:tag' => 'projects#index'
... ... @@ -83,7 +83,13 @@ Gitlab::Application.routes.draw do
83 83 get :commits
84 84 end
85 85 end
  86 +
86 87 resources :snippets
  88 + resources :hooks, :only => [:index, :new, :create, :destroy, :show] do
  89 + member do
  90 + get :test
  91 + end
  92 + end
87 93 resources :commits
88 94 resources :team_members
89 95 resources :issues do
... ...
lib/post-receive-hook
... ... @@ -8,5 +8,5 @@ do
8 8 # For every branch or tag that was pushed, create a Resque job in redis.
9 9 pwd=`pwd`
10 10 reponame=`basename "$pwd" | cut -d. -f1`
11   - env -i redis-cli rpush "resque:queue:post-receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\"]}" > /dev/null 2>&1
  11 + env -i redis-cli rpush "resque:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\"]}" > /dev/null 2>&1
12 12 done
... ...