PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/repository_observer.rb

https://bitbucket.org/3100/redmine_bitbucketgit_hook
Ruby | 40 lines | 32 code | 3 blank | 5 comment | 5 complexity | bc0d3e70af203d318e0c2fe967047556 MD5 | raw file
  1. class RepositoryObserver < ActiveRecord::Observer
  2. # CREATE DIR Manually
  3. # KNOWN Problem: Changing BitBucket Git Dir in use
  4. # KNOWN Problem: Two Repositorys with the same name from different users
  5. def before_save(repository)
  6. Rails.logger.info("before save")
  7. flag = repository.type == 'Git' || repository.type == 'Repository::Git'
  8. flag &= repository.url.match('.*(bitbucket.org|github.com).*')
  9. if flag
  10. base_dir_name = repository.url[/[^\/]+.git/]
  11. url = repository.url
  12. user = /^\S*[:\/](.*)\/\S*$/.match( url )
  13. user = user[1]
  14. git_dir = Setting.plugin_redmine_bitbucketgit_hook[:bitbucketgit_dir].to_s
  15. git_dir = git_dir + '/' + user + '_' + base_dir_name
  16. Rails.logger.info git_dir
  17. redminedir = Dir.getwd + '/'
  18. Rails.logger.info redminedir+git_dir
  19. comm_str = ""
  20. unless Dir[redminedir+git_dir] == []
  21. #Rails.logger.info "Dir already in use..."
  22. comm_str = 'rm -rf "' + redminedir + git_dir + '" &&'
  23. #return false
  24. end
  25. comm_str += 'git clone --mirror '+ url + ' "'+ redminedir + git_dir +'"'
  26. b = system(comm_str)
  27. repository.url = redminedir + git_dir
  28. return false
  29. end
  30. end
  31. private
  32. def exec(command)
  33. Rails.logger.info("BitbucketGitHook: Executing command: '#{command}'")
  34. output = `#{command}`
  35. Rails.logger.info("BitbucketGitHook: Shell returned '#{output}'")
  36. end
  37. end