PageRenderTime 83ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/bitbucketgit_hook_controller.rb

https://bitbucket.org/bluevox/redmine-bitbucket-git
Ruby | 52 lines | 32 code | 14 blank | 6 comment | 6 complexity | c32411424e89112110c3f0cfb1d340a5 MD5 | raw file
  1. require 'json'
  2. # Update by Bastian Bringenberg <typo3@bastian-bringenberg.de> 2012
  3. class BitbucketgitHookController < ApplicationController
  4. skip_before_filter :verify_authenticity_token, :check_if_login_required
  5. def index
  6. payload = JSON.parse(params[:payload])
  7. logger.debug { "Received from Bitbucket: #{payload.inspect}" }
  8. # For now, we assume that the repository name is the same as the project identifier
  9. identifier = payload['repository']['name']
  10. #project = Project.find_by_identifier(identifier)
  11. #raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil?
  12. searchPath = Dir.getwd + '/' + Setting.plugin_redmine_bitbucketgit_hook[:bitbucketgit_dir].to_s + '/' + payload['repository']['owner'] + '_' + payload['repository']['name'] +'.git'
  13. repository = Repository.find_by_url(searchPath)
  14. raise TypeError, "Project '#{identifier}' has no repository" if repository.nil?
  15. raise TypeError, "Repository for project '#{identifier}' is not a Git repository" unless repository.is_a?(Repository::Git)
  16. # Get updates from the bitbucket repository
  17. aFile = File.new(repository.url + '/config', "r")
  18. if aFile
  19. content = aFile.sysread(2000)
  20. content = /^\surl\s=\s(.*)$/.match( content )
  21. logger.info{content[1]}
  22. else
  23. logger.info {"Unable to open file!"}
  24. end
  25. command = "cd \"#{repository.url}\" && cd .. && rm -rf \"#{repository.url}\" && git clone --bare #{content[1]} \"#{repository.url}\""
  26. logger.info {command}
  27. exec(command)
  28. # Fetch the new changesets into Redmine
  29. repository.fetch_changesets
  30. render(:text => 'OK')
  31. end
  32. private
  33. def exec(command)
  34. logger.info { "BitbucketGitHook: Executing command: '#{command}'" }
  35. output = `#{command}`
  36. logger.info { "BitbucketGitHook: Shell returned '#{output}'" }
  37. end
  38. end