From 2dae0e18e09132c0db32c8646d8d11b30cfcb83f Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 4 Jan 2012 00:42:14 +0200 Subject: [PATCH 1/5] web hooks scaffold started --- app/controllers/hooks_controller.rb | 43 ++++++++++++++++++++++ app/controllers/projects_controller.rb | 2 +- app/controllers/repositories_controller.rb | 2 +- app/models/user.rb | 1 + app/models/web_hook.rb | 11 ++++++ app/views/hooks/index.html.haml | 10 +++++ app/workers/post_receive.rb | 2 + config/routes.rb | 6 ++- spec/models/user_spec.rb | 1 + spec/models/web_hook_spec.rb | 11 ++++++ 10 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 app/controllers/hooks_controller.rb create mode 100644 app/views/hooks/index.html.haml diff --git a/app/controllers/hooks_controller.rb b/app/controllers/hooks_controller.rb new file mode 100644 index 00000000000..70516dacf72 --- /dev/null +++ b/app/controllers/hooks_controller.rb @@ -0,0 +1,43 @@ +class HooksController < ApplicationController + before_filter :authenticate_user! + before_filter :project + layout "project" + + # Authorize + before_filter :add_project_abilities + before_filter :authorize_read_project! + before_filter :authorize_admin_project!, :only => [:new, :create, :destroy] + + respond_to :html + + def index + @hooks = @project.web_hooks + end + + def new + @hook = @project.web_hooks.new + end + + def create + @hook = @project.web_hooks.new(params[:hook]) + @hook.author = current_user + @hook.save + + if @hook.valid? + redirect_to [@project, @hook] + else + respond_with(@hook) + end + end + + def show + @hook = @project.web_hooks.find(params[:id]) + end + + def destroy + @hook = @project.web_hooks.find(params[:id]) + @hook.destroy + + redirect_to project_hooks_path(@project) + end +end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 1e859ceac31..29fcb52eb97 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -68,7 +68,7 @@ class ProjectsController < ApplicationController def show return render "projects/empty" unless @project.repo_exists? && @project.has_commits? - limit = (params[:limit] || 20).to_i + limit = (params[:limit] || 10).to_i @activities = @project.cached_updates(limit) end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 9a112f4674f..c0652cbeab9 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -9,7 +9,7 @@ class RepositoriesController < ApplicationController layout "project" def show - @activities = @project.fresh_commits(20) + @activities = @project.fresh_commits(10) end def branches diff --git a/app/models/user.rb b/app/models/user.rb index 8b136de90cf..ee9f0de11ec 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -82,5 +82,6 @@ end # linkedin :string(255) default(""), not null # twitter :string(255) default(""), not null # authentication_token :string(255) +# dark_scheme :boolean default(FALSE), not null # diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb index 0058bd57b91..40b930c3a98 100644 --- a/app/models/web_hook.rb +++ b/app/models/web_hook.rb @@ -18,3 +18,14 @@ class WebHook < ActiveRecord::Base # There was a problem calling this web hook, let's forget about it. end end +# == Schema Information +# +# Table name: web_hooks +# +# id :integer not null, primary key +# url :string(255) +# project_id :integer +# created_at :datetime +# updated_at :datetime +# + diff --git a/app/views/hooks/index.html.haml b/app/views/hooks/index.html.haml new file mode 100644 index 00000000000..956367393cb --- /dev/null +++ b/app/views/hooks/index.html.haml @@ -0,0 +1,10 @@ += render "repositories/head" +- unless @hooks.empty? + %div.update-data.ui-box.ui-box-small + .data + - @hooks.each do |hook| + %a.update-item{:href => project_hooks_path(@project, hook)} + %span.update-title{:style => "margin-bottom:0px;"} + = hook.url +- else + %h3 No hooks diff --git a/app/workers/post_receive.rb b/app/workers/post_receive.rb index d79f4599d80..922a66ebf86 100644 --- a/app/workers/post_receive.rb +++ b/app/workers/post_receive.rb @@ -1,4 +1,6 @@ class PostReceive + @queue = :post_receive + def self.perform(reponame, oldrev, newrev, ref) project = Project.find_by_path(reponame) return false if project.nil? diff --git a/config/routes.rb b/config/routes.rb index 90b391cd5a2..416ea6a9498 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,8 @@ Gitlab::Application.routes.draw do # Optionally, enable Resque here - # require 'resque/server' - # mount Resque::Server.new, at: '/info/resque' + require 'resque/server' + mount Resque::Server.new, at: '/info/resque' get 'tags'=> 'tags#index' get 'tags/:tag' => 'projects#index' @@ -83,6 +83,8 @@ Gitlab::Application.routes.draw do get :commits end end + + resources :hooks, :only => [:index, :new, :create, :destroy, :show] resources :snippets resources :commits resources :team_members diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3a3ac7c9c80..a62e56cdd30 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -65,5 +65,6 @@ end # linkedin :string(255) default(""), not null # twitter :string(255) default(""), not null # authentication_token :string(255) +# dark_scheme :boolean default(FALSE), not null # diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index e73e554adbb..309bfc0fd53 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -52,3 +52,14 @@ describe WebHook do end end end +# == Schema Information +# +# Table name: web_hooks +# +# id :integer not null, primary key +# url :string(255) +# project_id :integer +# created_at :datetime +# updated_at :datetime +# + From 495deea7eb5aa4c8b8e2365a351d1c2eedf1010a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 4 Jan 2012 01:09:48 +0200 Subject: [PATCH 2/5] fixed queue name --- lib/post-receive-hook | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/post-receive-hook b/lib/post-receive-hook index ec7c607f75d..d7354d6515b 100755 --- a/lib/post-receive-hook +++ b/lib/post-receive-hook @@ -8,5 +8,5 @@ do # For every branch or tag that was pushed, create a Resque job in redis. pwd=`pwd` reponame=`basename "$pwd" | cut -d. -f1` - env -i redis-cli rpush "resque:queue:post-receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\"]}" > /dev/null 2>&1 + env -i redis-cli rpush "resque:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\"]}" > /dev/null 2>&1 done From 2d3b6375f38ac6e3a14fad93f243c258d74b0331 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 4 Jan 2012 02:07:56 +0200 Subject: [PATCH 3/5] hooks scaffold --- app/assets/stylesheets/projects.css.scss | 7 +++++++ app/controllers/hooks_controller.rb | 5 ++--- app/helpers/projects_helper.rb | 3 ++- app/views/hooks/index.html.haml | 8 +++++++- app/views/hooks/new.html.haml | 13 +++++++++++++ app/views/hooks/show.html.haml | 7 +++++++ app/views/repositories/_head.html.haml | 2 +- config/routes.rb | 2 +- 8 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 app/views/hooks/new.html.haml create mode 100644 app/views/hooks/show.html.haml diff --git a/app/assets/stylesheets/projects.css.scss b/app/assets/stylesheets/projects.css.scss index 316ef033e28..63b15bae40f 100644 --- a/app/assets/stylesheets/projects.css.scss +++ b/app/assets/stylesheets/projects.css.scss @@ -181,6 +181,13 @@ input.ssh_project_url { } } +.text_field { + width:400px; + padding:8px; + font-size:14px; + @include round-borders-all(4px); +} + .input_button { padding:8px; font-size:14px; diff --git a/app/controllers/hooks_controller.rb b/app/controllers/hooks_controller.rb index 70516dacf72..9205a6a6f67 100644 --- a/app/controllers/hooks_controller.rb +++ b/app/controllers/hooks_controller.rb @@ -20,13 +20,12 @@ class HooksController < ApplicationController def create @hook = @project.web_hooks.new(params[:hook]) - @hook.author = current_user @hook.save if @hook.valid? - redirect_to [@project, @hook] + redirect_to project_hook_path(@project, @hook) else - respond_with(@hook) + render :new end end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 902d278019c..57786338ceb 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -35,7 +35,8 @@ module ProjectsHelper end def repository_tab_class - if controller.controller_name == "repositories" + if controller.controller_name == "repositories" || + controller.controller_name == "hooks" "current" end end diff --git a/app/views/hooks/index.html.haml b/app/views/hooks/index.html.haml index 956367393cb..b0795ad4b2f 100644 --- a/app/views/hooks/index.html.haml +++ b/app/views/hooks/index.html.haml @@ -1,10 +1,16 @@ = render "repositories/head" +.right= link_to "Add new", new_project_hook_path(@project), :class => "grey-button append-bottom-10" - unless @hooks.empty? %div.update-data.ui-box.ui-box-small .data - @hooks.each do |hook| - %a.update-item{:href => project_hooks_path(@project, hook)} + %a.update-item{:href => project_hook_path(@project, hook)} %span.update-title{:style => "margin-bottom:0px;"} = hook.url + %span.update-author.right + Added + = time_ago_in_words(hook.created_at) + ago - else %h3 No hooks + diff --git a/app/views/hooks/new.html.haml b/app/views/hooks/new.html.haml new file mode 100644 index 00000000000..8078aefa503 --- /dev/null +++ b/app/views/hooks/new.html.haml @@ -0,0 +1,13 @@ += render "repositories/head" += form_for [@project, @hook], :as => :hook, :url => project_hooks_path(@project) do |f| + -if @hook.errors.any? + %ul + - @hook.errors.full_messages.each do |msg| + %li= msg + = f.label :url, "URL:" + = f.text_field :url, :class => "text_field" + .clear + %br + .merge-tabs + = f.submit "Save", :class => "grey-button" + diff --git a/app/views/hooks/show.html.haml b/app/views/hooks/show.html.haml new file mode 100644 index 00000000000..56b5fa9d3a1 --- /dev/null +++ b/app/views/hooks/show.html.haml @@ -0,0 +1,7 @@ += render "repositories/head" += debug @hook + +- if can? current_user, :admin_project, @project + .merge-tabs + .right + = link_to 'Remove', project_hook_path(@project, @hook), :confirm => 'Are you sure?', :method => :delete, :class => "red-button" diff --git a/app/views/repositories/_head.html.haml b/app/views/repositories/_head.html.haml index c22286ec094..7ada9ff7798 100644 --- a/app/views/repositories/_head.html.haml +++ b/app/views/repositories/_head.html.haml @@ -8,7 +8,7 @@ = link_to tags_project_repository_path(@project), :class => "tab #{'active' if current_page?(tags_project_repository_path(@project)) }" do %span Tags - -#= link_to "#", :class => "tab" do + = link_to project_hooks_path, :class => "tab #{'active' if controller.controller_name == "hooks" }" do %span Hooks -#= link_to "#", :class => "tab" do diff --git a/config/routes.rb b/config/routes.rb index 416ea6a9498..5f129c904d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,8 +84,8 @@ Gitlab::Application.routes.draw do end end - resources :hooks, :only => [:index, :new, :create, :destroy, :show] resources :snippets + resources :hooks, :only => [:index, :new, :create, :destroy, :show] resources :commits resources :team_members resources :issues do From 57ac5fe99fd7c870f1b408ccebbd01960f514636 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 8 Jan 2012 12:20:24 +0200 Subject: [PATCH 4/5] Procfile added. use foreman start to run gitlabhq --- .foreman | 1 + Procfile | 2 ++ app/controllers/hooks_controller.rb | 9 +++++++++ app/views/hooks/show.html.haml | 3 ++- config/routes.rb | 6 +++++- 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .foreman create mode 100644 Procfile diff --git a/.foreman b/.foreman new file mode 100644 index 00000000000..549e85b1490 --- /dev/null +++ b/.foreman @@ -0,0 +1 @@ +port: 9999 diff --git a/Procfile b/Procfile new file mode 100644 index 00000000000..2f73f49416d --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +web: bundle exec rails s -p $PORT +worker: bundle exec rake environment resque:work QUEUE=* diff --git a/app/controllers/hooks_controller.rb b/app/controllers/hooks_controller.rb index 9205a6a6f67..7c5f7631f4e 100644 --- a/app/controllers/hooks_controller.rb +++ b/app/controllers/hooks_controller.rb @@ -29,6 +29,15 @@ class HooksController < ApplicationController end end + def test + @hook = @project.web_hooks.find(params[:id]) + commits = @project.commits(@project.default_branch, nil, 3) + data = @project.web_hook_data(commits.last.id, commits.first.id, "refs/heads/#{@project.default_branch}") + @hook.execute(data) + + redirect_to :back + end + def show @hook = @project.web_hooks.find(params[:id]) end diff --git a/app/views/hooks/show.html.haml b/app/views/hooks/show.html.haml index 56b5fa9d3a1..6bc1132de27 100644 --- a/app/views/hooks/show.html.haml +++ b/app/views/hooks/show.html.haml @@ -1,7 +1,8 @@ = render "repositories/head" -= debug @hook +%h3= @hook.url - if can? current_user, :admin_project, @project .merge-tabs + = link_to 'Test Hook', test_project_hook_path(@project, @hook), :class => "grey-button" .right = link_to 'Remove', project_hook_path(@project, @hook), :confirm => 'Are you sure?', :method => :delete, :class => "red-button" diff --git a/config/routes.rb b/config/routes.rb index 5f129c904d0..9d1e7089209 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,7 +85,11 @@ Gitlab::Application.routes.draw do end resources :snippets - resources :hooks, :only => [:index, :new, :create, :destroy, :show] + resources :hooks, :only => [:index, :new, :create, :destroy, :show] do + member do + get :test + end + end resources :commits resources :team_members resources :issues do From 473445c76fe6d99243a0e6b0247bc79cc47c3e44 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 8 Jan 2012 13:20:20 +0200 Subject: [PATCH 5/5] complete hooks for post receive --- .foreman | 2 +- app/controllers/projects_controller.rb | 2 +- app/controllers/repositories_controller.rb | 2 +- app/views/hooks/_data_ex.html.erb | 42 ++++++++++++++++++++++ app/views/hooks/index.html.haml | 14 ++++++++ app/views/hooks/show.html.haml | 5 ++- config/database.yml | 2 +- 7 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 app/views/hooks/_data_ex.html.erb diff --git a/.foreman b/.foreman index 549e85b1490..87c3f5a1c15 100644 --- a/.foreman +++ b/.foreman @@ -1 +1 @@ -port: 9999 +port: 3000 diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 29fcb52eb97..1e859ceac31 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -68,7 +68,7 @@ class ProjectsController < ApplicationController def show return render "projects/empty" unless @project.repo_exists? && @project.has_commits? - limit = (params[:limit] || 10).to_i + limit = (params[:limit] || 20).to_i @activities = @project.cached_updates(limit) end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index c0652cbeab9..9a112f4674f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -9,7 +9,7 @@ class RepositoriesController < ApplicationController layout "project" def show - @activities = @project.fresh_commits(10) + @activities = @project.fresh_commits(20) end def branches diff --git a/app/views/hooks/_data_ex.html.erb b/app/views/hooks/_data_ex.html.erb new file mode 100644 index 00000000000..f212bb2dc0b --- /dev/null +++ b/app/views/hooks/_data_ex.html.erb @@ -0,0 +1,42 @@ +<% data_ex_str = < "95790bf891e76fee5e1747ab589903a6a1f80f22", + :after => "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + :ref => "refs/heads/master", + :repository => { + :name => "Diaspora", + :url => "localhost/diaspora", + :description => "", + :homepage => "localhost/diaspora", + :private => true + }, + :commits => [ + [0] { + :id => "450d0de7532f8b663b9c5cce183b...", + :message => "Update Catalan translation to e38cb41.", + :timestamp => "2011-12-12T14:27:31+02:00", + :url => "http://localhost/diaspora/commits/450d0de7532f...", + :author => { + :name => "Jordi Mallach", + :email => "jordi@softcatala.org" + } + }, + + .... + + [3] { + :id => "da1560886d4f094c3e6c9ef40349...", + :message => "fixed readme", + :timestamp => "2012-01-03T23:36:29+02:00", + :url => "http://localhost/diaspora/commits/da1560886d...", + :author => { + :name => "gitlab dev user", + :email => "gitlabdev@dv6700.(none)" + } + } + ] +} +eos +%> +<% js_lexer = Pygments::Lexer[:js] %> +<%= raw js_lexer.highlight(data_ex_str) %> diff --git a/app/views/hooks/index.html.haml b/app/views/hooks/index.html.haml index b0795ad4b2f..e149f4fc767 100644 --- a/app/views/hooks/index.html.haml +++ b/app/views/hooks/index.html.haml @@ -1,4 +1,8 @@ = render "repositories/head" + + + + .right= link_to "Add new", new_project_hook_path(@project), :class => "grey-button append-bottom-10" - unless @hooks.empty? %div.update-data.ui-box.ui-box-small @@ -14,3 +18,13 @@ - else %h3 No hooks +.clear +%h3 Help +%p + Post receive hooks. For now only POST request allowed. We send some data with request. Example below + +.view_file + .view_file_header + %strong POST data passed + .data.no-padding + = render "data_ex" diff --git a/app/views/hooks/show.html.haml b/app/views/hooks/show.html.haml index 6bc1132de27..47c1ddeac40 100644 --- a/app/views/hooks/show.html.haml +++ b/app/views/hooks/show.html.haml @@ -1,5 +1,8 @@ = render "repositories/head" -%h3= @hook.url +%h3 + %span.commit.tag POST + = @hook.url + - if can? current_user, :admin_project, @project .merge-tabs diff --git a/config/database.yml b/config/database.yml index 51a4dd459dc..ff126d9ce70 100644 --- a/config/database.yml +++ b/config/database.yml @@ -20,6 +20,6 @@ test: production: adapter: sqlite3 - database: db/production.sqlite3 + database: db/development.sqlite3 pool: 5 timeout: 5000