PageRenderTime 56ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/bitbucketgit_hook_controller.rb

https://bitbucket.org/sudocoders/redmine-bitbucket-git
Ruby | 56 lines | 38 code | 14 blank | 4 comment | 5 complexity | 71cb8258a3e024a9a7fe86cf92507698 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. searchPath = Dir.getwd + Setting.plugin_redmine_bitbucketgit_hook[:bitbucketgit_dir].to_s + '/' + payload['repository']['slug'] +'.git'
  11. repository = Repository.find_by_url(searchPath)
  12. raise TypeError, "Project '#{identifier}' has no repository" if repository.nil?
  13. raise TypeError, "Repository for project '#{identifier}' is not a Git repository" unless repository.is_a?(Repository::Git)
  14. # Get updates from the bitbucket repository
  15. aFile = File.new(repository.url + '/config', "r")
  16. if aFile
  17. content = aFile.sysread(2000)
  18. content = /^\surl\s=\s(.*)$/.match( content )
  19. logger.info{content[1]}
  20. else
  21. logger.info {"Unable to open file!"}
  22. end
  23. bFile = File.new(repository.url, "r")
  24. if bFile
  25. logger.info("File exists")
  26. command = "cd \"#{repository.url}\" && git fetch"
  27. else
  28. logger.info("File does not Exist")
  29. end
  30. logger.info {command}
  31. exec(command)
  32. # Fetch the new changesets into Redmine
  33. repository.fetch_changesets
  34. render(:text => 'OK')
  35. end
  36. private
  37. def exec(command)
  38. logger.info { "BitbucketGitHook: Executing command: '#{command}'" }
  39. output = `#{command}`
  40. logger.info { "BitbucketGitHook: Shell returned '#{output}'" }
  41. end
  42. end