git-documentdb
Version:
Offline-first database that syncs with Git
184 lines • 7.18 kB
JavaScript
;
/**
* GitDocumentDB
* Copyright (c) Hidekazu Kubota
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoteRepository = void 0;
const rest_1 = require("@octokit/rest");
const error_1 = require("../error");
const const_1 = require("../const");
const utils_1 = require("../utils");
/**
* Remote repository class
*
* @public
*/
class RemoteRepository {
/**
* Constructor
*
* @public
*/
constructor(options) {
var _a, _b;
var _c, _d;
if (options.remoteUrl === undefined || options.remoteUrl === '') {
throw new error_1.Err.UndefinedRemoteURLError();
}
this._options = JSON.parse(JSON.stringify(options));
(_a = (_c = this._options).connection) !== null && _a !== void 0 ? _a : (_c.connection = {
type: 'none',
});
if (this._options.connection.type === 'github') {
(_b = (_d = this._options.connection).private) !== null && _b !== void 0 ? _b : (_d.private = true);
this._octokit = new rest_1.Octokit({
auth: this._options.connection.personalAccessToken,
});
}
else if (this._options.connection.type === 'none') {
// nop
}
}
/**
* Create a repository on a remote site
*
* @remarks
* connection.type must be 'github'
*
* @throws {@link Err.UndefinedPersonalAccessTokenError}
* @throws {@link Err.PersonalAccessTokenForAnotherAccountError}
* @throws {@link Err.CannotConnectRemoteRepositoryError}
*
* may include the following errors:
*
* - HttpError
*
* - Authentication error
*
* - Permission error
*
* - Other network errors
*
* @throws {@link Err.AuthenticationTypeNotAllowCreateRepositoryError}
*
* @public
*/
async create() {
var _a, _b;
if (((_a = this._options.connection) === null || _a === void 0 ? void 0 : _a.type) === 'github') {
// @ts-ignore
if (this._options.connection.personalAccessToken === undefined) {
throw new error_1.Err.UndefinedPersonalAccessTokenError();
}
const urlArray = this._options.remoteUrl.split('/');
const owner = urlArray[urlArray.length - 2];
let repo = urlArray[urlArray.length - 1];
if (repo.endsWith('.git')) {
repo = repo.replace(/\.git$/, '');
}
let result;
let retry = 0;
for (; retry < const_1.NETWORK_RETRY; retry++) {
// @ts-ignore
// eslint-disable-next-line no-await-in-loop
result = await this._octokit.repos.createForAuthenticatedUser({
name: repo,
private: this._options.connection.private,
}).catch((err) => {
// May throw HttpError if the repository which has the same name already exists.
// HttpError: Repository creation failed.:
// {"resource":"Repository","code":"custom","field":"name","message":"name already exists on this account
return err;
});
if (result instanceof Error) {
// console.log(`NetworkError in creating remote repository: ${this._options.remoteUrl}, ` + result);
}
else {
// Check owner name because personal access token does not check owner
if (result.data.full_name === `${owner}/${repo}`) {
break;
}
throw new error_1.Err.PersonalAccessTokenForAnotherAccountError();
}
// eslint-disable-next-line no-await-in-loop
await (0, utils_1.sleep)(const_1.NETWORK_RETRY_INTERVAL);
}
if (result instanceof Error) {
throw new error_1.Err.CannotConnectRemoteRepositoryError(retry, this._options.remoteUrl, result.message);
}
}
else {
throw new error_1.Err.AuthenticationTypeNotAllowCreateRepositoryError((_b = this._options.connection) === null || _b === void 0 ? void 0 : _b.type);
}
}
/**
* Delete a repository on a remote site
*
* @remarks
* connection.type must be 'github'
*
* @throws {@link Err.UndefinedPersonalAccessTokenError}
* @throws {@link Err.CannotConnectRemoteRepositoryError}
*
* may include the following errors:
*
* - HttpError
*
* - Authentication error
*
* - Permission for private repository error
*
* - Other network errors
*
* @throws {@link Err.AuthenticationTypeNotAllowCreateRepositoryError}
*
* @public
*/
async destroy() {
var _a, _b, _c;
if (((_a = this._options.connection) === null || _a === void 0 ? void 0 : _a.type) === 'github') {
// @ts-ignore
if (((_b = this._options.connection) === null || _b === void 0 ? void 0 : _b.personalAccessToken) === undefined) {
throw new error_1.Err.UndefinedPersonalAccessTokenError();
}
const urlArray = this._options.remoteUrl.split('/');
const owner = urlArray[urlArray.length - 2];
let repo = urlArray[urlArray.length - 1];
if (repo.endsWith('.git')) {
repo = repo.replace(/\.git$/, '');
}
let result;
let retry = 0;
for (; retry < const_1.NETWORK_RETRY; retry++) {
// @ts-ignore
// eslint-disable-next-line no-await-in-loop
result = await this._octokit.repos.delete({
owner,
repo,
}).catch((err) => {
return err;
});
if (result instanceof Error) {
// console.log(`NetworkError in creating remote repository: ${this._options.remoteUrl}, ` + result);
}
else {
break;
}
// eslint-disable-next-line no-await-in-loop
await (0, utils_1.sleep)(const_1.NETWORK_RETRY_INTERVAL);
}
if (result instanceof Error) {
throw new error_1.Err.CannotConnectRemoteRepositoryError(retry, this._options.remoteUrl, result.message);
}
}
else {
throw new error_1.Err.AuthenticationTypeNotAllowCreateRepositoryError((_c = this._options.connection) === null || _c === void 0 ? void 0 : _c.type);
}
}
}
exports.RemoteRepository = RemoteRepository;
//# sourceMappingURL=remote_repository.js.map