PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/bitbucketgit_hook_controller.rb

https://bitbucket.org/3100/redmine_bitbucketgit_hook
Ruby | 59 lines | 40 code | 13 blank | 6 comment | 6 complexity | 71641b25e27b4ff4e2b6666b07adc6ee 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. Rails.logger.info "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. owner = payload['repository']['owner']
  11. slug = payload['repository']['slug']
  12. is_private = payload['repository']['is_private']
  13. #project = Project.find_by_identifier(identifier)
  14. #raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil?
  15. searchPath = Dir.getwd + '/' + Setting.plugin_redmine_bitbucketgit_hook[:bitbucketgit_dir].to_s + '/' + owner + '_' + slug +'.git'
  16. Rails.logger.info searchPath
  17. repository = Repository.find_by_url(searchPath)
  18. raise TypeError, "Project '#{identifier}' has no repository" if repository.nil?
  19. raise TypeError, "Repository for project '#{identifier}' is not a Git repository" unless repository.is_a?(Repository::Git)
  20. repos = ''
  21. if is_private
  22. repos = "git@bitbucket.org:#{owner}/#{slug}.git"
  23. else
  24. repos = "https://bitbucket.org/#{owner}/#{slug}.git"
  25. end
  26. #command = "cd \"#{repository.url}\" && cd .. && rm -rf \"#{repository.url}\" && git clone --bare #{repos} \"#{repository.url}\""
  27. bFile = File.new(repository.url, "r")
  28. if bFile
  29. Rails.logger.info {"File exists"}
  30. command = "cd \"#{repository.url}\" && git fetch"
  31. else
  32. command = "cd \"#{repository.url}\" && git clone --mirror #{repos} \"#{repositry.url}\""
  33. end
  34. Rails.logger.info {command}
  35. exec(command)
  36. # Fetch the new changesets into Redmine
  37. repository.fetch_changesets
  38. render(:text => 'OK')
  39. end
  40. private
  41. def exec(command)
  42. logger.info { "BitbucketGitHook: Executing command: '#{command}'" }
  43. output = `#{command}`
  44. logger.info { "BitbucketGitHook: Shell returned '#{output}'" }
  45. end
  46. end