OSDN Git Service

Merge branch 'master' into deploy_keys
authormiks <miks@cubesystems.lv>
Thu, 29 Dec 2011 19:46:21 +0000 (21:46 +0200)
committermiks <miks@cubesystems.lv>
Thu, 29 Dec 2011 19:46:21 +0000 (21:46 +0200)
Conflicts:
app/models/project.rb

35 files changed:
.travis.yml
Gemfile
Gemfile.lock
README.md
app/assets/images/favicon.png [deleted file]
app/assets/stylesheets/notes.css.scss
app/assets/stylesheets/style.scss
app/helpers/commits_helper.rb
app/models/project.rb
app/models/repository.rb
app/models/web_hook.rb [new file with mode: 0644]
app/views/commits/show.html.haml
app/views/layouts/_head_panel.html.erb
app/views/layouts/admin.html.haml
app/views/layouts/devise.html.haml
app/views/layouts/profile.html.haml
app/views/layouts/project.html.haml
app/views/notes/_form.html.haml
app/workers/post_receive.rb [new file with mode: 0644]
config/routes.rb
db/migrate/20111214091851_create_web_hooks.rb [new file with mode: 0644]
db/schema.rb
lib/gitlabhq/gitolite.rb
lib/post-receive-hook [new file with mode: 0755]
lib/tasks/resque.rake [new file with mode: 0644]
lib/utils.rb
public/favicon.ico
spec/factories.rb
spec/helpers/commit_helper_spec.rb [new file with mode: 0644]
spec/models/project_spec.rb
spec/models/web_hook_spec.rb [new file with mode: 0644]
spec/requests/dashboard_spec.rb
spec/requests/projects_tree_perfomance_spec.rb
spec/spec_helper.rb
spec/workers/post_receive_spec.rb [new file with mode: 0644]

index 62a5dac..7a450b7 100644 (file)
@@ -1,3 +1,4 @@
+before_install: sudo apt-get install libicu-dev -y
 branches:
   only:
     - 'master'
diff --git a/Gemfile b/Gemfile
index 01b14cc..e240fa6 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -22,7 +22,9 @@ gem "acts_as_list"
 gem "rdiscount"
 gem "acts-as-taggable-on", "~> 2.1.0"
 gem "drapper"
-gem "rchardet19", "~> 1.3.5"
+gem "resque"
+gem "httparty"
+gem "charlock_holmes"
 
 group :assets do
   gem "sass-rails",   "~> 3.1.0"
@@ -47,6 +49,7 @@ group :development, :test do
   gem "awesome_print"
   gem "database_cleaner"
   gem "launchy"
+  gem "webmock"
 end
 
 group :test do
index 54b1e25..86a6998 100644 (file)
@@ -77,6 +77,7 @@ GEM
       xpath (~> 0.1.4)
     carrierwave (0.5.8)
       activesupport (~> 3.0)
+    charlock_holmes (0.6.8)
     childprocess (0.2.2)
       ffi (~> 1.0.6)
     coffee-rails (3.1.1)
@@ -87,6 +88,7 @@ GEM
       execjs
     coffee-script-source (1.1.3)
     columnize (0.3.4)
+    crack (0.3.1)
     daemons (1.1.4)
     database_cleaner (0.7.0)
     devise (1.5.0)
@@ -111,6 +113,9 @@ GEM
       railties (~> 3.0)
     hashery (1.4.0)
     hike (1.2.1)
+    httparty (0.8.1)
+      multi_json
+      multi_xml
     i18n (0.6.0)
     jquery-rails (1.0.17)
       railties (~> 3.0)
@@ -132,6 +137,7 @@ GEM
       treetop (~> 1.4.8)
     mime-types (1.17.2)
     multi_json (1.0.3)
+    multi_xml (0.4.1)
     nokogiri (1.5.0)
     orm_adapter (0.0.5)
     polyglot (0.3.3)
@@ -143,6 +149,8 @@ GEM
       rack (>= 0.4)
     rack-mount (0.8.3)
       rack (>= 1.0.0)
+    rack-protection (1.1.4)
+      rack
     rack-ssl (1.3.2)
       rack
     rack-test (0.6.1)
@@ -165,10 +173,17 @@ GEM
       rdoc (~> 3.4)
       thor (~> 0.14.6)
     rake (0.9.2.2)
-    rchardet19 (1.3.5)
     rdiscount (1.6.8)
     rdoc (3.11)
       json (~> 1.4)
+    redis (2.2.2)
+    redis-namespace (1.0.3)
+      redis (< 3.0.0)
+    resque (1.19.0)
+      multi_json (~> 1.0)
+      redis-namespace (~> 1.0.2)
+      sinatra (>= 0.9.2)
+      vegas (~> 0.1.2)
     rspec (2.7.0)
       rspec-core (~> 2.7.0)
       rspec-expectations (~> 2.7.0)
@@ -220,6 +235,10 @@ GEM
       multi_json (~> 1.0.3)
       simplecov-html (~> 0.5.3)
     simplecov-html (0.5.3)
+    sinatra (1.3.1)
+      rack (~> 1.3, >= 1.3.4)
+      rack-protection (~> 1.1, >= 1.1.2)
+      tilt (~> 1.3, >= 1.3.3)
     six (0.2.0)
     sprockets (2.0.3)
       hike (~> 1.2)
@@ -244,8 +263,13 @@ GEM
     uglifier (1.1.0)
       execjs (>= 0.3.0)
       multi_json (>= 1.0.2)
+    vegas (0.1.8)
+      rack (>= 1.0.0)
     warden (1.1.0)
       rack (>= 1.0)
+    webmock (1.7.8)
+      addressable (~> 2.2, > 2.2.5)
+      crack (>= 0.1.7)
     xpath (0.1.4)
       nokogiri (~> 1.3)
 
@@ -261,6 +285,7 @@ DEPENDENCIES
   awesome_print
   capybara
   carrierwave
+  charlock_holmes
   coffee-rails (~> 3.1.0)
   database_cleaner
   devise (= 1.5.0)
@@ -270,6 +295,7 @@ DEPENDENCIES
   gitolite!
   grit!
   haml-rails
+  httparty
   jquery-rails
   kaminari
   launchy
@@ -277,8 +303,8 @@ DEPENDENCIES
   pygments.rb (= 0.2.3)
   rails (= 3.1.1)
   rails-footnotes (~> 3.7.5)
-  rchardet19 (~> 1.3.5)
   rdiscount
+  resque
   rspec-rails
   ruby-debug19
   sass-rails (~> 3.1.0)
@@ -292,3 +318,4 @@ DEPENDENCIES
   thin
   turn
   uglifier
+  webmock
index 2287d48..2da2d3c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -5,9 +5,9 @@ GitLab is a free Project/Repository management application
 
 ## Application details
 
-rails 3.1
-works only with gitolite
-sqlite as default a database
+rails 3.1
+works only with gitolite
+sqlite as default a database
 
 ## Requirements
 
diff --git a/app/assets/images/favicon.png b/app/assets/images/favicon.png
deleted file mode 100644 (file)
index 310003b..0000000
Binary files a/app/assets/images/favicon.png and /dev/null differ
index c3ee843..4aba53c 100644 (file)
@@ -42,3 +42,11 @@ body.project-page #notes-list .note span.note-author strong{font-weight: bold; f
 
 
 .note .note-title { margin-left:55px; }
+
+p.notify_controls input{
+  margin: 5px;
+}
+
+p.notify_controls span{
+  font-weight: 700;
+}
index 6982440..72c1e20 100644 (file)
@@ -359,6 +359,9 @@ header nav{border-radius: 4px; box-shadow: 0 1px 2px black; width: 294px; margin
   margin-top: 2px;
   height:30px
 }
+header nav.shorter_nav{
+  width: 207px;
+}
 header nav a{padding: 8px 12px 8px 34px; display: inline-block; color: #D6DADF; border-right: 1px solid #31363E; position: relative; box-shadow: 1px 0 0 rgba(255,255,255,.1); margin: 0}
 header nav a span{width: 20px; height: 20px; display: inline-block; background: red; position: absolute; left: 8px; top: 6px;}
 header nav a:last-child {border: 0; box-shadow: none}
@@ -382,7 +385,7 @@ header nav a.dashboard {
   border-bottom-left-radius: 4px;
 }
 
-header nav a.admin{
+header nav a.last_elem{
   -webkit-border-top-right-radius: 4px;
   -webkit-border-bottom-right-radius: 4px;
   -moz-border-radius-topright: 4px;
index 98ec993..33975e8 100644 (file)
@@ -23,4 +23,23 @@ module CommitsHelper
     link_to "More", project_commits_path(@project, :offset =>  offset.to_i + limit.to_i, :limit => limit),
       :remote => true, :class => "lite_button vm", :style => "text-align:center; width:930px; ", :id => "more-commits-link"
   end
+
+  def commit_msg_with_link_to_issues(project, message)
+    return '' unless message
+    out = ''
+    message.split(/(#[0-9]+)/m).each do |m|
+      if m =~ /(#([0-9]+))/m
+        begin
+          issue = project.issues.find($2)
+          out += link_to($1, project_issue_path(project, $2))
+        rescue
+          out += $1
+        end
+      else
+        out += m
+      end
+    end
+    preserve out
+  end
+
 end
index 67320f6..06b9e71 100644 (file)
@@ -15,6 +15,7 @@ class Project < ActiveRecord::Base
   has_many :notes, :dependent => :destroy
   has_many :snippets, :dependent => :destroy
   has_many :deploy_keys, :dependent => :destroy
+  has_many :web_hooks, :dependent => :destroy
 
   acts_as_taggable
 
@@ -83,12 +84,58 @@ class Project < ActiveRecord::Base
     :heads,
     :commits_since,
     :fresh_commits,
+    :commits_between,
     :to => :repository, :prefix => nil
 
   def to_param
     code
   end
 
+  def web_url
+    [GIT_HOST['host'], code].join("/")
+  end
+
+  def execute_web_hooks(oldrev, newrev, ref)
+    ref_parts = ref.split('/')
+
+    # Return if this is not a push to a branch (e.g. new commits)
+    return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
+
+    data = web_hook_data(oldrev, newrev, ref)
+    web_hooks.each { |web_hook| web_hook.execute(data) }
+  end
+
+  def web_hook_data(oldrev, newrev, ref)
+    data = {
+      before: oldrev,
+      after: newrev,
+      ref: ref,
+      repository: {
+        name: name,
+        url: web_url,
+        description: description,
+        homepage: web_url,
+        private: private?
+      },
+      commits: []
+    }
+
+    commits_between(oldrev, newrev).each do |commit|
+      data[:commits] << {
+        id: commit.id,
+        message: commit.safe_message,
+        timestamp: commit.date.xmlschema,
+        url: "http://#{GIT_HOST['host']}/#{code}/commits/#{commit.id}",
+        author: {
+          name: commit.author_name,
+          email: commit.author_email
+        }
+      }
+    end
+
+    data
+  end
+
   def team_member_by_name_or_email(email = nil, name = nil)
     user = users.where("email like ? or name like ?", email, name).first
     users_projects.find_by_user_id(user.id) if user
index 0e6f0e9..6f90fd0 100644 (file)
@@ -31,6 +31,22 @@ class Repository
     project.id
   end
 
+  def write_hooks
+    %w(post-receive).each do |hook|
+      write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook")))
+    end
+  end
+
+  def write_hook(name, content)
+    hook_file = File.join(project.path_to_repo, 'hooks', name)
+
+    File.open(hook_file, 'w') do |f|
+      f.write(content)
+    end
+
+    File.chmod(0775, hook_file)
+  end
+
   def repo
     @repo ||= Grit::Repo.new(project.path_to_repo)
   end
@@ -47,6 +63,8 @@ class Repository
     Gitlabhq::GitHost.system.new.configure do |c|
       c.update_project(path, project)
     end
+
+    write_hooks if File.exists?(project.path_to_repo)
   end
 
   def destroy_repository
@@ -115,4 +133,8 @@ class Repository
       repo.commits(ref)
     end.map{ |c| Commit.new(c) } 
   end
+
+  def commits_between(from, to)
+    repo.commits_between(from, to).map { |c| Commit.new(c) }
+  end
 end
diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb
new file mode 100644 (file)
index 0000000..0058bd5
--- /dev/null
@@ -0,0 +1,20 @@
+class WebHook < ActiveRecord::Base
+  include HTTParty
+
+  # HTTParty timeout
+  default_timeout 10
+
+  belongs_to :project
+
+  validates :url,
+            presence: true,
+            format: {
+              with: URI::regexp(%w(http https)),
+              message: "should be a valid url" }
+
+  def execute(data)
+    WebHook.post(url, body: data.to_json)
+  rescue
+    # There was a problem calling this web hook, let's forget about it.
+  end
+end
index 57ada2d..44b7b8f 100644 (file)
@@ -18,8 +18,7 @@
 
 %hr
 %pre.commit_message
-  = preserve @commit.safe_message
-
+  = commit_msg_with_link_to_issues(@project, @commit.safe_message)
 .clear
 %br
 
index 70484df..5b3c2a6 100644 (file)
     <%= text_field_tag "search", nil, :placeholder => "Search", :class => "search-input"  %>
   </div>
   <!-- .login-top -->
-  <nav>
-    <%= link_to dashboard_path, :class => current_page?(root_path) ? "current dashboard" : "dashboard" do %>
+  <nav class="<%= 'shorter_nav' unless current_user.is_admin? %>">
+    <%= link_to dashboard_path, :class => "#{'current' if current_page?(root_path)} dashboard" do %>
       <span></span>Dashboard
     <% end %>
-    <%= link_to projects_path, :class => current_page?(projects_path) ? "current project" : "project" do %>
+    <%= link_to projects_path, :class =>"#{'current' if current_page?(projects_path)} project #{'last_elem' unless current_user.is_admin?}" do %>
       <span></span>Projects
     <% end %>
-    <%= link_to((current_user.is_admin? ? admin_root_path : "#"), :class => (admin_namespace? ? "current admin" : "admin")) do %>
-      <span></span>Admin
-    <% end  %>
+    <% if current_user.is_admin? %>
+      <%= link_to((current_user.is_admin? ? admin_root_path : "#"), :class => "#{'current' if admin_namespace?} admin last_elem") do %>
+        <span></span>Admin
+      <% end  %>
+    <% end %>
   </nav>
 
 </header>
index f013f12..cb0d6b9 100644 (file)
@@ -3,6 +3,7 @@
   %head
     %title
       GitLab #{" - #{@project.name}" if @project && !@project.new_record?}
+    = favicon_link_tag 'favicon.ico'
     = stylesheet_link_tag    "application"
     = javascript_include_tag "application"
     = csrf_meta_tags
index 3296466..882195a 100644 (file)
@@ -3,6 +3,7 @@
   %head
     %title
       GitLab #{" - #{@project.name}" if @project && !@project.new_record?}
+    = favicon_link_tag 'favicon.ico'
     = stylesheet_link_tag    "application"
     = javascript_include_tag "application"
     = csrf_meta_tags
index 5c4f59d..6eeecb2 100644 (file)
@@ -3,6 +3,7 @@
   %head
     %title
       GitLab #{" - #{@project.name}" if @project && !@project.new_record?}
+    = favicon_link_tag 'favicon.ico'
     = stylesheet_link_tag    "application"
     = javascript_include_tag "application"
     = csrf_meta_tags
index a5ea76d..ef33df8 100644 (file)
@@ -3,6 +3,7 @@
   %head
     %title
       GitLab #{" - #{@project.name}" if @project && !@project.new_record?}
+    = favicon_link_tag 'favicon.ico'
     = stylesheet_link_tag    "application"
     = javascript_include_tag "application"
     - if current_page?(tree_project_ref_path(@project, @project.root_ref)) || current_page?(project_commits_path(@project))
index 23c9553..65af82b 100644 (file)
       %br
       %br
       = f.file_field :attachment
+      
+      %p.notify_controls
+        %span Notify:
+        = check_box_tag :notify, 1, @note.noteable_type != "Commit"
+        = label_tag :notify, "Project team"
 
-      = check_box_tag :notify, 1, @note.noteable_type != "Commit"
-      = label_tag :notify, "Notify project team about your note"
-
-      -if @note.noteable_type == "Commit"
-        = check_box_tag :notify_author, 1 , @note.noteable_type == "Commit"
-        = label_tag :notify_author, "Notify commit author about your note"
+        -if @note.noteable_type == "Commit"
+          = check_box_tag :notify_author, 1 , @note.noteable_type == "Commit"
+          = label_tag :notify_author, "Commit author"
 
     .clear
     %br
diff --git a/app/workers/post_receive.rb b/app/workers/post_receive.rb
new file mode 100644 (file)
index 0000000..d79f459
--- /dev/null
@@ -0,0 +1,8 @@
+class PostReceive
+  def self.perform(reponame, oldrev, newrev, ref)
+    project = Project.find_by_path(reponame)
+    return false if project.nil?
+
+    project.execute_web_hooks(oldrev, newrev, ref)
+  end
+end
index 20890b7..bf5eaf7 100644 (file)
@@ -1,5 +1,8 @@
 Gitlab::Application.routes.draw do
 
+  # Optionally, enable Resque here
+  # require 'resque/server'
+  # mount Resque::Server.new, at: '/info/resque'
 
   get 'tags'=> 'tags#index'
   get 'tags/:tag' => 'projects#index'
diff --git a/db/migrate/20111214091851_create_web_hooks.rb b/db/migrate/20111214091851_create_web_hooks.rb
new file mode 100644 (file)
index 0000000..c6ba89c
--- /dev/null
@@ -0,0 +1,9 @@
+class CreateWebHooks < ActiveRecord::Migration
+  def change
+    create_table :web_hooks do |t|
+      t.string :url
+      t.integer :project_id
+      t.timestamps
+    end
+  end
+end
index 18a3486..3014579 100644 (file)
 
 ActiveRecord::Schema.define(:version => 20111220190817) do
 
-  create_table "features", :force => true do |t|
-    t.string   "name"
-    t.string   "branch_name"
-    t.integer  "assignee_id"
-    t.integer  "author_id"
-    t.integer  "project_id"
-    t.datetime "created_at"
-    t.datetime "updated_at"
-    t.string   "version"
-    t.integer  "status",      :default => 0, :null => false
-  end
-
   create_table "issues", :force => true do |t|
     t.string   "title"
     t.integer  "assignee_id"
@@ -145,4 +133,11 @@ ActiveRecord::Schema.define(:version => 20111220190817) do
     t.integer  "project_access", :default => 0, :null => false
   end
 
+  create_table "web_hooks", :force => true do |t|
+    t.string   "url"
+    t.integer  "project_id"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
+
 end
index 9300087..397d9fb 100644 (file)
@@ -43,14 +43,14 @@ module Gitlabhq
 
     def destroy_project(project)
       FileUtils.rm_rf(project.path_to_repo)
-      
+
       ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite'))
       conf = ga_repo.config
       conf.rm_repo(project.path)
       ga_repo.save
     end
 
-     #update or create
+    #update or create
     def update_keys(user, key)
       File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) }
     end
diff --git a/lib/post-receive-hook b/lib/post-receive-hook
new file mode 100755 (executable)
index 0000000..ec7c607
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# This file was placed here by Gitlab. It makes sure that your pushed commits
+# will be processed properly.
+
+while read oldrev newrev ref
+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
+done
diff --git a/lib/tasks/resque.rake b/lib/tasks/resque.rake
new file mode 100644 (file)
index 0000000..9b30bb0
--- /dev/null
@@ -0,0 +1 @@
+require 'resque/tasks'
index 2de1476..8e5d469 100644 (file)
@@ -17,15 +17,13 @@ module Utils
   end
 
   module CharEncode
-    def encode(string)
-      return '' unless string
-      cd = CharDet.detect(string)
-      if cd.confidence > 0.6
-        string.force_encoding(cd.encoding)
+    def encode(content)
+      content ||= ''
+      detection = CharlockHolmes::EncodingDetector.detect(content)
+      if hash = detection
+        content = CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding]
       end
-      string.encode("utf-8", :undef => :replace, :replace => "?", :invalid => :replace)
-    rescue
-      "Invalid Encoding"
+      content
     end
   end
 
index e69de29..057f74a 100644 (file)
Binary files a/public/favicon.ico and b/public/favicon.ico differ
index 4b0b032..15e54ed 100644 (file)
@@ -55,3 +55,7 @@ Factory.add(:key, Key) do |obj|
   obj.title = "Example key"
   obj.key = File.read(File.join(Rails.root, "db", "pkey.example"))
 end
+
+Factory.add(:web_hook, WebHook) do |obj|
+  obj.url = Faker::Internet.url
+end
diff --git a/spec/helpers/commit_helper_spec.rb b/spec/helpers/commit_helper_spec.rb
new file mode 100644 (file)
index 0000000..747a28a
--- /dev/null
@@ -0,0 +1,67 @@
+require "spec_helper"
+include Haml::Helpers
+
+describe CommitsHelper do
+
+  before do
+    @project = Factory :project
+    @other_project = Factory :project, :path => "OtherPath", :code => "OtherCode"
+    @fake_user = Factory :user
+    @valid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project
+    @invalid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @other_project
+  end
+
+  it "should provides return message untouched if no issue number present" do
+    message = "Dummy message without issue number"
+
+    commit_msg_with_link_to_issues(@project, message).should eql message
+  end
+
+  it "should returns message handled by preserve" do
+    message = "My brand new
+    Commit on multiple
+    lines !"
+
+    #\n are converted to &#x000A as specified in preserve_rspec
+    expected = "My brand new&#x000A;    Commit on multiple&#x000A;    lines !"
+
+    commit_msg_with_link_to_issues(@project, message).should eql expected
+  end
+
+  it "should returns empty string if message undefined" do
+    commit_msg_with_link_to_issues(@project, nil).should eql ''
+  end
+
+  it "should returns link_to issue for one valid issue in message" do
+    issue_id = @valid_issue.id
+    message = "One commit message ##{issue_id}"
+    expected = "One commit message <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>"
+
+    commit_msg_with_link_to_issues(@project, message).should eql expected
+  end
+
+  it "should returns message untouched for one invalid issue in message" do
+    issue_id = @invalid_issue.id
+    message = "One commit message ##{issue_id}"
+
+    commit_msg_with_link_to_issues(@project, message).should eql message
+  end
+
+  it "should handle multiple issue references in commit message" do
+    issue_id = @valid_issue.id
+    invalid_issue_id = @invalid_issue.id
+
+    message = "One big commit message with a valid issue ##{issue_id} and an invalid one ##{invalid_issue_id}.
+    We reference valid ##{issue_id} multiple times (##{issue_id}) as the invalid ##{invalid_issue_id} is also
+    referenced another time (##{invalid_issue_id})"
+
+    expected = "One big commit message with a valid issue <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>"+
+        " and an invalid one ##{invalid_issue_id}.&#x000A;    "+
+        "We reference valid <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a> multiple times "+
+        "(<a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>) "+
+        "as the invalid ##{invalid_issue_id} is also&#x000A;    referenced another time (##{invalid_issue_id})"
+
+    commit_msg_with_link_to_issues(@project, message).should eql expected
+  end
+
+end
\ No newline at end of file
index a5edc40..68bc82d 100644 (file)
@@ -7,6 +7,7 @@ describe Project do
     it { should have_many(:issues) }
     it { should have_many(:notes) }
     it { should have_many(:snippets) }
+    it { should have_many(:web_hooks).dependent(:destroy) }
   end
 
   describe "Validation" do
@@ -33,6 +34,7 @@ describe Project do
     it { should respond_to(:repo) }
     it { should respond_to(:tags) }
     it { should respond_to(:commit) }
+    it { should respond_to(:commits_between) }
   end
 
   it "should not allow 'gitolite-admin' as repo name" do
@@ -50,6 +52,11 @@ describe Project do
     project.path_to_repo.should == File.join(Rails.root, "tmp", "tests", "somewhere")
   end
 
+  it "returns the full web URL for this repo" do
+    project = Project.new(:code => "somewhere")
+    project.web_url.should == "#{GIT_HOST['host']}/somewhere"
+  end
+
   describe :valid_repo? do
     it "should be valid repo" do
       project = Factory :project
@@ -62,6 +69,106 @@ describe Project do
     end
   end
 
+  describe "web hooks" do
+    let(:project) { Factory :project }
+
+    context "with no web hooks" do
+      it "raises no errors" do
+        lambda {
+          project.execute_web_hooks('oldrev', 'newrev', 'ref')
+        }.should_not raise_error
+      end
+    end
+
+    context "with web hooks" do
+      before do
+        @webhook = Factory(:web_hook)
+        @webhook_2 = Factory(:web_hook)
+        project.web_hooks << [@webhook, @webhook_2]
+      end
+
+      it "executes multiple web hook" do
+        @webhook.should_receive(:execute).once
+        @webhook_2.should_receive(:execute).once
+
+        project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master')
+      end
+    end
+
+    context "does not execute web hooks" do
+      before do
+        @webhook = Factory(:web_hook)
+        project.web_hooks << [@webhook]
+      end
+
+      it "when pushing a branch for the first time" do
+        @webhook.should_not_receive(:execute)
+        project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master')
+      end
+
+      it "when pushing tags" do
+        @webhook.should_not_receive(:execute)
+        project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0')
+      end
+    end
+
+    context "when pushing new branches" do
+
+    end
+
+    context "when gathering commit data" do
+      before do
+        @oldrev, @newrev, @ref = project.fresh_commits(2).last.sha, project.fresh_commits(2).first.sha, 'refs/heads/master'
+        @commit = project.fresh_commits(2).first
+
+        # Fill nil/empty attributes
+        project.description = "This is a description"
+
+        @data = project.web_hook_data(@oldrev, @newrev, @ref)
+      end
+
+      subject { @data }
+
+      it { should include(before: @oldrev) }
+      it { should include(after: @newrev) }
+      it { should include(ref: @ref) }
+
+      context "with repository data" do
+        subject { @data[:repository] }
+
+        it { should include(name: project.name) }
+        it { should include(url: project.web_url) }
+        it { should include(description: project.description) }
+        it { should include(homepage: project.web_url) }
+        it { should include(private: project.private?) }
+      end
+
+      context "with commits" do
+        subject { @data[:commits] }
+
+        it { should be_an(Array) }
+        it { should have(1).element }
+
+        context "the commit" do
+          subject { @data[:commits].first }
+
+          it { should include(id: @commit.id) }
+          it { should include(message: @commit.safe_message) }
+          it { should include(timestamp: @commit.date.xmlschema) }
+          it { should include(url: "http://localhost/#{project.code}/commits/#{@commit.id}") }
+
+          context "with a author" do
+            subject { @data[:commits].first[:author] }
+
+            it { should include(name: @commit.author_name) }
+            it { should include(email: @commit.author_email) }
+          end
+        end
+      end
+
+    end
+  end
+
   describe "updates" do
     let(:project) { Factory :project }
 
@@ -107,6 +214,21 @@ describe Project do
     it { project.fresh_commits.last.id.should == "0dac878dbfe0b9c6104a87d65fe999149a8d862c" }
   end
 
+  describe "commits_between" do
+    let(:project) { Factory :project }
+
+    subject do
+      commits = project.commits_between("a6d1d4aca0c85816ddfd27d93773f43a31395033",
+                                        "2fb376f61875b58bceee0492e270e9c805294b1a")
+      commits.map { |c| c.id }
+    end
+
+    it { should have(2).elements }
+    it { should include("2fb376f61875b58bceee0492e270e9c805294b1a") }
+    it { should include("4571e226fbcd7be1af16e9fa1e13b7ac003bebdf") }
+    it { should_not include("a6d1d4aca0c85816ddfd27d93773f43a31395033") }
+  end
+
   describe "Git methods" do
     let(:project) { Factory :project }
 
diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb
new file mode 100644 (file)
index 0000000..e73e554
--- /dev/null
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe WebHook do
+  describe "Associations" do
+    it { should belong_to :project }
+  end
+
+  describe "Validations" do
+    it { should validate_presence_of(:url) }
+
+    context "url format" do
+      it { should allow_value("http://example.com").for(:url) }
+      it { should allow_value("https://excample.com").for(:url) }
+      it { should allow_value("http://test.com/api").for(:url) }
+      it { should allow_value("http://test.com/api?key=abc").for(:url) }
+      it { should allow_value("http://test.com/api?key=abc&type=def").for(:url) }
+
+      it { should_not allow_value("example.com").for(:url) }
+      it { should_not allow_value("ftp://example.com").for(:url) }
+      it { should_not allow_value("herp-and-derp").for(:url) }
+    end
+  end
+
+  describe "execute" do
+    before(:each) do
+      @webhook = Factory :web_hook
+      @project = Factory :project
+      @project.web_hooks << [@webhook]
+      @data = { before: 'oldrev', after: 'newrev', ref: 'ref'}
+
+      WebMock.stub_request(:post, @webhook.url)
+    end
+
+    it "POSTs to the web hook URL" do
+      @webhook.execute(@data)
+      WebMock.should have_requested(:post, @webhook.url).once
+    end
+
+    it "POSTs the data as JSON" do
+      json = @data.to_json
+
+      @webhook.execute(@data)
+      WebMock.should have_requested(:post, @webhook.url).with(body: json).once
+    end
+
+    it "catches exceptions" do
+      WebHook.should_receive(:post).and_raise("Some HTTP Post error")
+
+      lambda {
+        @webhook.execute(@data)
+      }.should_not raise_error
+    end
+  end
+end
index 331aeeb..69ddd17 100644 (file)
@@ -1,12 +1,18 @@
 require 'spec_helper'
 
 describe "Dashboard" do
-  before { login_as :user }
+  before do 
+    @project = Factory :project
+    @user = User.create(:email => "test917@mail.com",
+                        :name => "John Smith",
+                        :password => "123456",
+                        :password_confirmation => "123456")
+    @project.add_access(@user, :read, :write)
+    login_with(@user)
+  end
 
   describe "GET /dashboard" do
     before do
-      @project = Factory :project
-      @project.add_access(@user, :read, :write)
       visit dashboard_path
     end
 
@@ -20,12 +26,14 @@ describe "Dashboard" do
       end
     end
 
-    it "should have news feed" do
-      within "#news-feed"  do
-        page.should have_content("commit")
-        page.should have_content(@project.commit.author.name)
-        page.should have_content(@project.commit.safe_message)
-      end
-    end
+    # Temporary disabled cause of travis
+    # TODO: fix or rewrite
+    #it "should have news feed" do
+      #within "#news-feed"  do
+        #page.should have_content("commit")
+        #page.should have_content(@project.commit.author.name)
+        #page.should have_content(@project.commit.safe_message)
+      #end
+    #end
   end
 end
index 5cf93bf..a97b0b6 100644 (file)
@@ -1,37 +1,36 @@
-require 'spec_helper'
-require 'benchmark'
-
-describe "Projects" do
-  before { login_as :user }
-
-  describe "GET /projects/tree" do
-    describe "head" do
-      before do
-        @project = Factory :project
-        @project.add_access(@user, :read)
-
-      end
-
-      it "should be fast" do
-        time = Benchmark.realtime do
-          visit tree_project_ref_path(@project, @project.root_ref)
-        end
-        (time < 1.0).should be_true
-      end
-    end
-
-    describe ValidCommit::ID do
-      before do
-        @project = Factory :project
-        @project.add_access(@user, :read)
-      end
-
-      it "should be fast" do
-        time = Benchmark.realtime do
-          visit tree_project_ref_path(@project, ValidCommit::ID)
-        end
-        (time < 1.0).should be_true
-      end
-    end
-  end
-end
+#require 'spec_helper'
+#require 'benchmark'
+#
+#describe "Projects" do
+#  before { login_as :user }
+#
+#  describe "GET /projects/tree" do
+#    describe "head" do
+#      before do
+#        @project = Factory :project
+#        @project.add_access(@user, :read)
+#      end
+#
+#      it "should be fast" do
+#        time = Benchmark.realtime do
+#          visit tree_project_ref_path(@project, @project.root_ref)
+#        end
+#        (time < 1.0).should be_true
+#      end
+#    end
+#
+#    describe ValidCommit::ID do
+#      before do
+#        @project = Factory :project
+#        @project.add_access(@user, :read)
+#      end
+#
+#      it "should be fast" do
+#        time = Benchmark.realtime do
+#          visit tree_project_ref_path(@project, ValidCommit::ID)
+#        end
+#        (time < 1.0).should be_true
+#      end
+#    end
+#  end
+#end
index 05fd6ca..f24496e 100644 (file)
@@ -8,6 +8,7 @@ require 'rspec/rails'
 require 'capybara/rails'
 require 'capybara/rspec'
 require 'capybara/dsl'
+require 'webmock/rspec'
 require 'factories'
 require 'monkeypatch'
 
@@ -48,6 +49,8 @@ RSpec.configure do |config|
     end
 
     DatabaseCleaner.start
+
+    WebMock.disable_net_connect!(allow_localhost: true)
   end
 
   config.after do
diff --git a/spec/workers/post_receive_spec.rb b/spec/workers/post_receive_spec.rb
new file mode 100644 (file)
index 0000000..500a699
--- /dev/null
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe PostReceive do
+
+  context "as a resque worker" do
+    it "reponds to #perform" do
+      PostReceive.should respond_to(:perform)
+    end
+  end
+
+  context "web hooks" do
+    let(:project) { Factory :project }
+
+    it "it retrieves the correct project" do
+      Project.should_receive(:find_by_path).with(project.path)
+      PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master')
+    end
+
+    it "asks the project to execute web hooks" do
+      Project.stub(find_by_path: project)
+      project.should_receive(:execute_web_hooks).with('sha-old', 'sha-new', 'refs/heads/master')
+
+      PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master')
+    end
+  end
+end