Skip to content

Commit

Permalink
Added ability to fetch_repositories by web hook from bitbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
nodecarter committed Jun 6, 2014
1 parent 1416b46 commit 901d852
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/controllers/undev_git_hooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def github_push
head :bad_request
end

def bitbucket_push
urls = RedmineUndevGit::Services::Bitbucket.git_urls_from_request(request)
fetch_repositories(urls)
head :ok
rescue RedmineUndevGit::Services::ServiceError
head :bad_request
end

private

def fetch_repositories(urls)
Expand Down
6 changes: 6 additions & 0 deletions app/views/settings/_web_hooks_description.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
github_hooks_url(:host => Setting.host_name, :protocol => Setting.protocol),
:size => 60,
:readonly => true,
:class => 'web-hook-url' %><br/>
Bitbucket webhook url:
<%= text_field_tag 'bitbucket-web-hook',
bitbucket_hooks_url(:host => Setting.host_name, :protocol => Setting.protocol),
:size => 60,
:readonly => true,
:class => 'web-hook-url' %>
</em>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

match 'gitlab_hooks' => 'undev_git_hooks#gitlab_push', :via => :post, :as => :gitlab_hooks
match 'github_hooks' => 'undev_git_hooks#github_push', :via => :post, :as => :github_hooks
match 'bitbucket_hooks' => 'undev_git_hooks#bitbucket_push', :via => :post, :as => :bitbucket_hooks
end
5 changes: 4 additions & 1 deletion lib/redmine_undev_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def self.fetch_by_web_hook=(value)

require 'redmine_undev_git/hooks/view_hooks'

require 'redmine_undev_git/services/errors'
require 'redmine_undev_git/services/migration'
require 'redmine_undev_git/services/errors'
require 'redmine_undev_git/services/ext_repo'
require 'redmine_undev_git/services/gitlab'
require 'redmine_undev_git/services/github'
require 'redmine_undev_git/services/bitbucket'

require 'redmine_undev_git/patches/redmine_scm_base_patch'
require 'redmine_undev_git/patches/custom_field_patch'
Expand Down
42 changes: 42 additions & 0 deletions lib/redmine_undev_git/services/bitbucket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module RedmineUndevGit::Services
class BitbucketRepo < ExtRepo
attr_reader :repo_owner
def initialize(absolute_url, canon_url, repo_owner)
if m = /\Ahttps?:\/\/(?<host>.+?)\z/.match(canon_url)
@host = m[:host]
else
raise RedmineUndevGit::Services::WrongRepoUrl
end
if m = /\A\/(?<path_to_repo>.+)\/\z/.match(absolute_url)
@path_to_repo = m[:path_to_repo]
else
raise RedmineUndevGit::Services::WrongRepoUrl
end
@user = 'git'
@repo_owner = repo_owner
end

def git_urls
[ssh_url, https_url, https_with_owner_url]
end

def https_with_owner_url
"https://#{repo_owner}@#{host}/#{path_to_repo}.git"
end
end

class Bitbucket
class << self
def git_urls_from_request(request)
web_hook = web_hook_from_request(request)
repo = RedmineUndevGit::Services::BitbucketRepo.new(
web_hook['repository']['absolute_url'], web_hook['canon_url'], web_hook['user'])
repo.git_urls
end

def web_hook_from_request(request)
JSON.parse(request.params[:payload])
end
end
end
end
44 changes: 44 additions & 0 deletions test/integration/receive_external_hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def test_success_on_github_ping_hook_when_login_requred
assert_response :success
end

def test_fetch_after_bitbucket_push_hook
repository = create_test_repository(:project => @project, :url => 'https://bitbucket.org/test/dt_fetch.git')
assert repository
Workers::RepositoryFetcher.expects(:defer).with(repository.id).at_least_once
post '/bitbucket_hooks', :payload => bitbucket_payload.to_json
assert_response :success
end

def gitlab_payload
{
before: '95790bf891e76fee5e1747ab589903a6a1f80f22',
Expand Down Expand Up @@ -259,4 +267,40 @@ def github_ping_headers
'HTTP_X_GITHUB_EVENT' => 'ping'
}
end

def bitbucket_payload
{
repository: {
website: '',
fork: false,
name: 'dt_fetch',
scm: 'git',
owner: 'test',
absolute_url: '/test/dt_fetch/',
slug: 'dt_fetch',
is_private: false
},
truncated: false,
commits: [
{
node: '81c83664d120',
files: [
{type: 'added', file: 'update5.txt'}
],
raw_author: 'Test <[email protected]>',
utctimestamp: '2014-06-06 07:04:38+00:00',
author: 'test',
timestamp: '2014-06-06 09:04:38',
raw_node: '81c83664d120ce05c91b7259b70c02ebc64edd52',
parents: ['75d5e5aef45a'],
branch: 'master',
message: 'update 5',
revision: nil,
size: -1
}
],
canon_url: 'https://bitbucket.org',
user: 'test'
}
end
end

0 comments on commit 901d852

Please sign in to comment.