@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
70 lines (69 loc) • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubReleaseService = void 0;
const tslib_1 = require("tslib");
const properties = tslib_1.__importStar(require("../../properties.json"));
const json_utils_1 = require("../../utils/json-utils");
const string_utils_1 = require("../../utils/string-utils");
const https = tslib_1.__importStar(require("https"));
const github_exception_1 = require("../../exception/github-exception");
const git_exception_1 = require("../../exception/git-exception");
class GithubReleaseService {
constructor() { }
async getLatestRelease(ownerAndRepository, githubCredential) {
return new Promise((resolve, reject) => {
const url = new URL(`${properties.github["api-host"]}/repos/${ownerAndRepository}/releases/latest`);
const headers = {
Accept: "application/vnd.github+json",
"User-Agent": string_utils_1.StringUtils.getUserAgent(),
};
if (githubCredential) {
headers.Authorization = `token ${githubCredential.token}`;
}
const options = {
hostname: url.hostname,
port: 443,
path: `${url.pathname}${url.search}`,
method: "GET",
headers: headers,
};
const req = https.request(options, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", d => {
body += d;
});
res.on("end", () => {
const bodyMap = JSON.parse(json_utils_1.JsonUtils.isJsonString(body) ? body : "{}");
switch (res.statusCode) {
case 200:
resolve(bodyMap);
break;
case 404:
reject(new github_exception_1.GithubContentNotFoundException(bodyMap?.message || res.statusMessage));
break;
case 401:
reject(new git_exception_1.GitAccessDeniedException("Github Access Denied, check your token"));
break;
case 403:
reject(new github_exception_1.GithubContentConflictException(bodyMap?.message || res.statusMessage));
break;
default:
reject(new git_exception_1.GitException(`${res.statusCode}-${bodyMap?.message || res.statusMessage}`));
break;
}
});
});
req.on("error", error => {
reject(error);
});
req.end();
});
}
static get instance() {
if (!this._instance)
this._instance = new this();
return this._instance;
}
}
exports.GithubReleaseService = GithubReleaseService;