PageRenderTime 22ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/gitlab/gitlab_import/client.rb

https://gitlab.com/pauldibiase/gitlab-ce
Ruby | 82 lines | 68 code | 14 blank | 0 comment | 4 complexity | a0487c946015c8258ac60bc967ecb6ed MD5 | raw file
  1. module Gitlab
  2. module GitlabImport
  3. class Client
  4. attr_reader :client, :api
  5. PER_PAGE = 100
  6. def initialize(access_token)
  7. @client = ::OAuth2::Client.new(
  8. config.app_id,
  9. config.app_secret,
  10. gitlab_options
  11. )
  12. if access_token
  13. @api = OAuth2::AccessToken.from_hash(@client, access_token: access_token)
  14. end
  15. end
  16. def authorize_url(redirect_uri)
  17. client.auth_code.authorize_url({
  18. redirect_uri: redirect_uri,
  19. scope: "api"
  20. })
  21. end
  22. def get_token(code, redirect_uri)
  23. client.auth_code.get_token(code, redirect_uri: redirect_uri).token
  24. end
  25. def user
  26. api.get("/api/v3/user").parsed
  27. end
  28. def issues(project_identifier)
  29. lazy_page_iterator(PER_PAGE) do |page|
  30. api.get("/api/v3/projects/#{project_identifier}/issues?per_page=#{PER_PAGE}&page=#{page}").parsed
  31. end
  32. end
  33. def issue_comments(project_identifier, issue_id)
  34. lazy_page_iterator(PER_PAGE) do |page|
  35. api.get("/api/v3/projects/#{project_identifier}/issues/#{issue_id}/notes?per_page=#{PER_PAGE}&page=#{page}").parsed
  36. end
  37. end
  38. def project(id)
  39. api.get("/api/v3/projects/#{id}").parsed
  40. end
  41. def projects
  42. lazy_page_iterator(PER_PAGE) do |page|
  43. api.get("/api/v3/projects?per_page=#{PER_PAGE}&page=#{page}").parsed
  44. end
  45. end
  46. private
  47. def lazy_page_iterator(per_page)
  48. Enumerator.new do |y|
  49. page = 1
  50. loop do
  51. items = yield(page)
  52. items.each do |item|
  53. y << item
  54. end
  55. break if items.empty? || items.size < per_page
  56. page += 1
  57. end
  58. end
  59. end
  60. def config
  61. Gitlab.config.omniauth.providers.find{|provider| provider.name == "gitlab"}
  62. end
  63. def gitlab_options
  64. OmniAuth::Strategies::GitLab.default_options[:client_options].to_h.symbolize_keys
  65. end
  66. end
  67. end
  68. end