@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
141 lines (140 loc) • 6.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubContentService = void 0;
const tslib_1 = require("tslib");
const string_utils_1 = require("../../utils/string-utils");
const properties = tslib_1.__importStar(require("../../properties.json"));
const https = tslib_1.__importStar(require("https"));
const json_utils_1 = require("../../utils/json-utils");
const git_exception_1 = require("../../exception/git-exception");
const github_exception_1 = require("../../exception/github-exception");
class GithubContentService {
constructor() { }
async uploadContent(params) {
return new Promise((resolve, reject) => {
const requestBody = json_utils_1.JsonUtils.stringify({
sha: params.remoteSHA ?? null,
message: params.commitMessage,
content: params.fileContentBase64,
branch: params.branch ?? null,
});
const url = new URL(`${properties.github["api-host"]}/repos/${params.ownerAndRepository}/contents/${params.remoteFilePath}`);
const options = {
hostname: url.hostname,
port: 443,
path: url.pathname,
method: "PUT",
headers: {
Authorization: `token ${params.githubCredential.token}`,
Accept: "application/vnd.github+json",
"User-Agent": string_utils_1.StringUtils.getUserAgent(),
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(requestBody),
},
};
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:
case 201:
resolve({
sha: bodyMap.content.sha,
url: bodyMap.content.html_url,
});
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 409:
reject(new github_exception_1.GithubContentConflictException(bodyMap?.message || res.statusMessage));
break;
case 422:
reject(new git_exception_1.GitException(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.write(requestBody);
req.end();
});
}
async getContent(githubCredential, ownerAndRepository, branch, remoteFilePath) {
return new Promise((resolve, reject) => {
const url = new URL(`${properties.github["api-host"]}/repos/${ownerAndRepository}/contents/${remoteFilePath}?ref=${branch}`);
const options = {
hostname: url.hostname,
port: 443,
path: `${url.pathname}${url.search}`,
method: "GET",
headers: {
Authorization: `token ${githubCredential.token}`,
Accept: "application/vnd.github+json",
"User-Agent": string_utils_1.StringUtils.getUserAgent(),
},
};
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:
case 302:
resolve({
content: bodyMap.content,
sha: bodyMap.sha,
downloadUrl: bodyMap.download_url,
encoding: bodyMap.encoding,
htmlUrl: bodyMap.html_url,
name: bodyMap.name,
path: bodyMap.path,
size: bodyMap.size,
type: bodyMap.type,
});
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.GithubContentService = GithubContentService;