2018-07-25 08:22:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
module BitbucketServer
|
|
|
|
class Client
|
|
|
|
attr_reader :connection
|
|
|
|
|
2018-07-27 02:05:22 -04:00
|
|
|
ServerError = Class.new(StandardError)
|
|
|
|
|
|
|
|
SERVER_ERRORS = [SocketError,
|
|
|
|
OpenSSL::SSL::SSLError,
|
|
|
|
Errno::ECONNRESET,
|
|
|
|
Errno::ECONNREFUSED,
|
|
|
|
Errno::EHOSTUNREACH,
|
|
|
|
Net::OpenTimeout,
|
|
|
|
Net::ReadTimeout,
|
|
|
|
Gitlab::HTTP::BlockedUrlError,
|
|
|
|
BitbucketServer::Connection::ConnectionError].freeze
|
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
def initialize(options = {})
|
|
|
|
@connection = Connection.new(options)
|
|
|
|
end
|
|
|
|
|
2018-06-26 02:13:32 -04:00
|
|
|
def pull_requests(project_key, repo)
|
|
|
|
path = "/projects/#{project_key}/repos/#{repo}/pull-requests?state=ALL"
|
2018-06-25 16:06:10 -04:00
|
|
|
get_collection(path, :pull_request)
|
|
|
|
end
|
|
|
|
|
2018-07-27 02:05:22 -04:00
|
|
|
def activities(project_key, repo, pull_request_id)
|
|
|
|
path = "/projects/#{project_key}/repos/#{repo}/pull-requests/#{pull_request_id}/activities"
|
2018-06-29 01:26:33 -04:00
|
|
|
get_collection(path, :activity)
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo(project, repo_name)
|
|
|
|
parsed_response = connection.get("/projects/#{project}/repos/#{repo_name}")
|
|
|
|
BitbucketServer::Representation::Repo.new(parsed_response)
|
|
|
|
end
|
|
|
|
|
2018-11-05 18:37:21 -05:00
|
|
|
def repos(page_offset: 0, limit: nil)
|
2018-06-25 16:06:10 -04:00
|
|
|
path = "/repos"
|
2018-11-05 18:37:21 -05:00
|
|
|
get_collection(path, :repo, page_offset: page_offset, limit: limit)
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-03 19:37:17 -04:00
|
|
|
def create_branch(project_key, repo, branch_name, sha)
|
|
|
|
payload = {
|
|
|
|
name: branch_name,
|
|
|
|
startPoint: sha,
|
|
|
|
message: 'GitLab temporary branch for import'
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.post("/projects/#{project_key}/repos/#{repo}/branches", payload.to_json)
|
|
|
|
end
|
|
|
|
|
2018-07-13 18:39:24 -04:00
|
|
|
def delete_branch(project_key, repo, branch_name, sha)
|
|
|
|
payload = {
|
|
|
|
name: Gitlab::Git::BRANCH_REF_PREFIX + branch_name,
|
|
|
|
dryRun: false
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.delete(:branches, "/projects/#{project_key}/repos/#{repo}/branches", payload.to_json)
|
|
|
|
end
|
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
private
|
|
|
|
|
2018-11-05 18:37:21 -05:00
|
|
|
def get_collection(path, type, page_offset: 0, limit: nil)
|
|
|
|
paginator = BitbucketServer::Paginator.new(connection, Addressable::URI.escape(path), type, page_offset: page_offset, limit: limit)
|
2018-06-25 16:06:10 -04:00
|
|
|
BitbucketServer::Collection.new(paginator)
|
2018-07-27 02:05:22 -04:00
|
|
|
rescue *SERVER_ERRORS => e
|
|
|
|
raise ServerError, e
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|