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

/app/controllers/bitbucket_service_controller.rb

https://bitbucket.org/yuji_developer/redmine_bitbucket
Ruby | 81 lines | 59 code | 18 blank | 4 comment | 7 complexity | e45ccc1adf94d510e56138292fde3b74 MD5 | raw file
  1. require 'json'
  2. class BitbucketServiceController < ApplicationController
  3. unloadable
  4. skip_before_filter :verify_authenticity_token, :check_if_login_required
  5. def index
  6. unless service_enabled? && valid_key?
  7. return render :nothing => true, :status => 404
  8. end
  9. repository = find_repository
  10. if repository.nil?
  11. logger.debug { "BitbucketPlugin: Invalid repository"}
  12. return render :nothing => true, :status => 500
  13. end
  14. # Fetch the new changesets into Redmine
  15. repository.fetch_changesets
  16. render :nothing => true, :status => 200
  17. rescue ActiveRecord::RecordNotFound
  18. logger.debug { "BitbucketPlugin: RecordNotFound"}
  19. render :nothing => true, :status => 404
  20. end
  21. private
  22. def service_enabled?
  23. Setting.plugin_redmine_bitbucket[:service_enabled]
  24. end
  25. def valid_key?
  26. setting_key = Setting.plugin_redmine_bitbucket[:service_key]
  27. return true if setting_key.to_s == ''
  28. return params[:key] == setting_key
  29. end
  30. # Finds the Redmine project in the database based on the given project identifier
  31. def find_project
  32. identifier = params[:project_id]
  33. scope = Project.active.has_module(:repository)
  34. project = scope.find_by_identifier(identifier.downcase)
  35. raise ActiveRecord::RecordNotFound unless project
  36. return project
  37. end
  38. # Returns the Redmine Repository object we are trying to update
  39. def find_repository
  40. project = find_project
  41. json, new_webhook = get_params()
  42. adapter = BitbucketAdapter.new(json, new_webhook)
  43. repository = project.repositories.find_by_identifier(adapter.identifier)
  44. if repository
  45. adapter.update_repository(repository)
  46. elsif Setting.plugin_redmine_bitbucket[:auto_create]
  47. # Clone the repository into Redmine
  48. repository = adapter.create_repository(project)
  49. else
  50. raise ActiveRecord::RecordNotFound
  51. end
  52. return repository
  53. end
  54. def get_params()
  55. if params[:payload]
  56. return JSON.parse(params[:payload])['repository'], false
  57. elsif params['repository']
  58. return params['repository'], true
  59. else
  60. raise "Provided POST parameters could not be recognized by Redmine Bitbucket plugin"
  61. end
  62. end
  63. end