@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
52 lines (51 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createSecurityAuditMergeRequest = exports.SECURITY_AUDIT_FILE_NAME = void 0;
const ts_results_es_1 = require("ts-results-es");
const auditDocument_1 = require("./auditDocument");
function makeDatedBranchName(branchName) {
const date = new Date().toISOString().slice(0, -5).replaceAll(/[:.T]/g, "-");
return `${branchName}-${date}`;
}
const MR_TITLE = "Draft: chore(security): add security audit document";
exports.SECURITY_AUDIT_FILE_NAME = "SECURITY.md";
async function createSecurityAuditMergeRequest({
projectId,
mainBranch,
userId,
api
}) {
const mrs = (await ts_results_es_1.Result.wrapAsync(() => api.MergeRequests.all({
state: "opened",
wip: "yes",
labels: "security-audit"
}))).mapErr(() => `could not search for existing merge requests`);
if (mrs.isErr()) return mrs;
const existingMr = mrs.value[0];
if (existingMr) return (0, ts_results_es_1.Err)(`open merge request with security audit already exists: ${existingMr.web_url}`);
const auditTemplate = ts_results_es_1.Result.wrap(() => (0, auditDocument_1.makeTemplate)()).mapErr(() => "could not make security audit template document");
if (auditTemplate.isErr()) return auditTemplate;
const branch = (await ts_results_es_1.Result.wrapAsync(() => api.Branches.create(projectId, makeDatedBranchName("chore/security-audit"), mainBranch))).mapErr(e => {
console.log(e);
return "could not create branch";
});
if (branch.isErr()) return branch;
const commit = (await ts_results_es_1.Result.wrapAsync(() => api.Commits.create(projectId, branch.value.name, "chore(security): add empty security audit document template", [{
action: "create",
filePath: exports.SECURITY_AUDIT_FILE_NAME,
content: auditTemplate.value,
encoding: "text"
}]))).mapErr(() => "could not create commit");
if (commit.isErr()) return commit;
const mr = (await ts_results_es_1.Result.wrapAsync(() => api.MergeRequests.create(projectId, branch.value.name, mainBranch, MR_TITLE, {
description: `Please follow and update security audit document in \`${exports.SECURITY_AUDIT_FILE_NAME}\`.`,
assigneeId: userId,
squash: true,
labels: "security-audit",
removeSourceBranch: true
}))).mapErr(() => "could not create merge request");
return mr;
}
exports.createSecurityAuditMergeRequest = createSecurityAuditMergeRequest;