2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-27 18:37:19 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitlabImport
|
|
|
|
class Client
|
|
|
|
attr_reader :client, :api
|
|
|
|
|
|
|
|
PER_PAGE = 100
|
|
|
|
|
|
|
|
def initialize(access_token)
|
|
|
|
@client = ::OAuth2::Client.new(
|
|
|
|
config.app_id,
|
|
|
|
config.app_secret,
|
2015-02-17 10:59:50 -05:00
|
|
|
gitlab_options
|
2015-01-27 18:37:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if access_token
|
2015-02-02 20:01:07 -05:00
|
|
|
@api = OAuth2::AccessToken.from_hash(@client, access_token: access_token)
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_url(redirect_uri)
|
|
|
|
client.auth_code.authorize_url({
|
|
|
|
redirect_uri: redirect_uri,
|
|
|
|
scope: "api"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(code, redirect_uri)
|
|
|
|
client.auth_code.get_token(code, redirect_uri: redirect_uri).token
|
|
|
|
end
|
|
|
|
|
2015-03-31 10:34:13 -04:00
|
|
|
def user
|
2018-05-15 09:39:33 -04:00
|
|
|
api.get("/api/v4/user").parsed
|
2015-03-31 10:34:13 -04:00
|
|
|
end
|
|
|
|
|
2018-07-23 03:52:15 -04:00
|
|
|
def issues(project_identifier, **kwargs)
|
|
|
|
lazy_page_iterator(**kwargs) do |page, per_page|
|
|
|
|
api.get("/api/v4/projects/#{project_identifier}/issues?per_page=#{per_page}&page=#{page}").parsed
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-23 03:52:15 -04:00
|
|
|
def issue_comments(project_identifier, issue_id, **kwargs)
|
|
|
|
lazy_page_iterator(**kwargs) do |page, per_page|
|
|
|
|
api.get("/api/v4/projects/#{project_identifier}/issues/#{issue_id}/notes?per_page=#{per_page}&page=#{page}").parsed
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project(id)
|
2018-05-15 09:39:33 -04:00
|
|
|
api.get("/api/v4/projects/#{id}").parsed
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
|
2018-07-23 03:52:15 -04:00
|
|
|
def projects(**kwargs)
|
|
|
|
lazy_page_iterator(**kwargs) do |page, per_page|
|
|
|
|
api.get("/api/v4/projects?per_page=#{per_page}&page=#{page}&simple=true&membership=true").parsed
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-07-23 03:52:15 -04:00
|
|
|
def lazy_page_iterator(starting_page: 1, page_limit: nil, per_page: PER_PAGE)
|
2015-01-27 18:37:19 -05:00
|
|
|
Enumerator.new do |y|
|
2018-07-23 03:52:15 -04:00
|
|
|
page = starting_page
|
|
|
|
page_limit = (starting_page - 1) + page_limit if page_limit
|
|
|
|
|
2015-01-27 18:37:19 -05:00
|
|
|
loop do
|
2018-07-23 03:52:15 -04:00
|
|
|
items = yield(page, per_page)
|
|
|
|
|
2015-01-27 18:37:19 -05:00
|
|
|
items.each do |item|
|
|
|
|
y << item
|
|
|
|
end
|
2018-07-23 03:52:15 -04:00
|
|
|
|
|
|
|
break if items.empty? || items.size < per_page || (page_limit && page >= page_limit)
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2015-01-27 18:37:19 -05:00
|
|
|
page += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
2018-03-12 17:01:43 -04:00
|
|
|
Gitlab::Auth::OAuth::Provider.config_for('gitlab')
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
def gitlab_options
|
2015-11-30 09:12:31 -05:00
|
|
|
OmniAuth::Strategies::GitLab.default_options[:client_options].to_h.symbolize_keys
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|