debian-mirror-gitlab/lib/gitlab/gitlab_import/client.rb

88 lines
2.3 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
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,
gitlab_options
)
if access_token
@api = OAuth2::AccessToken.from_hash(@client, access_token: access_token)
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
def user
2018-11-08 19:23:39 +05:30
api.get("/api/v4/user").parsed
2015-04-26 12:48:37 +05:30
end
2018-11-18 11:00:15 +05:30
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-04-26 12:48:37 +05:30
end
end
2018-11-18 11:00:15 +05:30
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-04-26 12:48:37 +05:30
end
end
def project(id)
2018-11-08 19:23:39 +05:30
api.get("/api/v4/projects/#{id}").parsed
2015-04-26 12:48:37 +05:30
end
2018-11-18 11:00:15 +05:30
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-04-26 12:48:37 +05:30
end
end
private
2018-11-18 11:00:15 +05:30
def lazy_page_iterator(starting_page: 1, page_limit: nil, per_page: PER_PAGE)
2015-04-26 12:48:37 +05:30
Enumerator.new do |y|
2018-11-18 11:00:15 +05:30
page = starting_page
page_limit = (starting_page - 1) + page_limit if page_limit
2015-04-26 12:48:37 +05:30
loop do
2018-11-18 11:00:15 +05:30
items = yield(page, per_page)
2015-04-26 12:48:37 +05:30
items.each do |item|
y << item
end
2018-11-18 11:00:15 +05:30
break if items.empty? || items.size < per_page || (page_limit && page >= page_limit)
2018-03-17 18:26:18 +05:30
2015-04-26 12:48:37 +05:30
page += 1
end
end
end
def config
2018-05-09 12:01:36 +05:30
Gitlab::Auth::OAuth::Provider.config_for('gitlab')
2015-04-26 12:48:37 +05:30
end
def gitlab_options
2015-12-23 02:04:40 +05:30
OmniAuth::Strategies::GitLab.default_options[:client_options].to_h.symbolize_keys
2015-04-26 12:48:37 +05:30
end
end
end
end