# HG changeset patch # User Peter Sanchez # Date 1348535567 25200 # Mon Sep 24 18:12:47 2012 -0700 # Node ID cbed690b3191932c2632a82f1ce2e692612a9b0f # Parent c25b963dee4839316564a4c90bee2b05f1cd5978 Adding controller diff --git a/app/controllers/bitbucket_hook_controller.rb b/app/controllers/bitbucket_hook_controller.rb new file mode 100644 --- /dev/null +++ b/app/controllers/bitbucket_hook_controller.rb @@ -0,0 +1,40 @@ +require 'json' + +class BitbucketHookController < ApplicationController + + skip_before_filter :verify_authenticity_token, :check_if_login_required + + def index + payload = JSON.parse(params[:payload]) + logger.debug { "Received from Bitbucket: #{payload.inspect}" } + + # For now, we assume that the repository name is the same as the project identifier + identifier = payload['repository']['name'] + + project = Project.find_by_identifier(identifier) + raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil? + + repository = project.repository + raise TypeError, "Project '#{identifier}' has no repository" if repository.nil? + raise TypeError, "Repository for project '#{identifier}' is not a Mercurial repository" unless repository.is_a?(Repository::Mercurial) + + # Get updates from the bitbucket repository + command = "cd \"#{repository.url}\" && hg pull" + exec(command) + + # Fetch the new changesets into Redmine + repository.fetch_changesets + + render(:text => 'OK') + end + + private + + def exec(command) + logger.info { "BitbucketHook: Executing command: '#{command}'" } + #output = `#{command}` + output = system("#{command}") + logger.info { "BitbucketHook: Shell returned '#{output}'" } + end + +end