2015-01-27 23:37:19 +00: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 15:59:50 +00:00
|
|
|
gitlab_options
|
2015-01-27 23:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if access_token
|
2015-02-03 01:01:07 +00:00
|
|
|
@api = OAuth2::AccessToken.from_hash(@client, access_token: access_token)
|
2015-01-27 23:37:19 +00: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 14:34:13 +00:00
|
|
|
def user
|
2018-05-15 13:39:33 +00:00
|
|
|
api.get("/api/v4/user").parsed
|
2015-03-31 14:34:13 +00:00
|
|
|
end
|
|
|
|
|
2015-01-27 23:37:19 +00:00
|
|
|
def issues(project_identifier)
|
|
|
|
lazy_page_iterator(PER_PAGE) do |page|
|
2018-05-15 13:39:33 +00:00
|
|
|
api.get("/api/v4/projects/#{project_identifier}/issues?per_page=#{PER_PAGE}&page=#{page}").parsed
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_comments(project_identifier, issue_id)
|
|
|
|
lazy_page_iterator(PER_PAGE) do |page|
|
2018-05-15 13:39:33 +00:00
|
|
|
api.get("/api/v4/projects/#{project_identifier}/issues/#{issue_id}/notes?per_page=#{PER_PAGE}&page=#{page}").parsed
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project(id)
|
2018-05-15 13:39:33 +00:00
|
|
|
api.get("/api/v4/projects/#{id}").parsed
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
lazy_page_iterator(PER_PAGE) do |page|
|
2018-05-15 13:39:33 +00:00
|
|
|
api.get("/api/v4/projects?per_page=#{PER_PAGE}&page=#{page}").parsed
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def lazy_page_iterator(per_page)
|
|
|
|
Enumerator.new do |y|
|
|
|
|
page = 1
|
|
|
|
loop do
|
|
|
|
items = yield(page)
|
|
|
|
items.each do |item|
|
|
|
|
y << item
|
|
|
|
end
|
|
|
|
break if items.empty? || items.size < per_page
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2015-01-27 23:37:19 +00:00
|
|
|
page += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
2018-03-12 21:01:43 +00:00
|
|
|
Gitlab::Auth::OAuth::Provider.config_for('gitlab')
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
|
2015-02-17 15:59:50 +00:00
|
|
|
def gitlab_options
|
2015-11-30 14:12:31 +00:00
|
|
|
OmniAuth::Strategies::GitLab.default_options[:client_options].to_h.symbolize_keys
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|