PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/bitbucket_hook_controller.rb

https://bitbucket.org/nolith/redmine-bitbucket/
Ruby | 39 lines | 24 code | 12 blank | 3 comment | 4 complexity | 623327e95441a753ca4e4c2d1f6e2f79 MD5 | raw file
  1. require 'json'
  2. class BitbucketHookController < ApplicationController
  3. skip_before_filter :verify_authenticity_token, :check_if_login_required
  4. def index
  5. payload = JSON.parse(params[:payload])
  6. logger.debug { "Received from Bitbucket: #{payload.inspect}" }
  7. # For now, we assume that the repository name is the same as the project identifier
  8. identifier = payload['repository']['name']
  9. project = Project.find_by_identifier(identifier)
  10. raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil?
  11. repository = project.repository
  12. raise TypeError, "Project '#{identifier}' has no repository" if repository.nil?
  13. raise TypeError, "Repository for project '#{identifier}' is not a Mercurial repository" unless repository.is_a?(Repository::Mercurial)
  14. # Get updates from the bitbucket repository
  15. command = "cd \"#{repository.url}\" && hg pull"
  16. exec(command)
  17. # Fetch the new changesets into Redmine
  18. repository.fetch_changesets
  19. render(:text => 'OK')
  20. end
  21. private
  22. def exec(command)
  23. logger.info { "BitbucketHook: Executing command: '#{command}'" }
  24. output = `#{command}`
  25. logger.info { "BitbucketHook: Shell returned '#{output}'" }
  26. end
  27. end