@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
112 lines (111 loc) • 4.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPullRequest = void 0;
const chalk_1 = __importDefault(require("chalk"));
// import { auth } from "./index.js";
const log_1 = require("diginext-utils/dist/xconsole/log");
const simple_git_1 = require("simple-git");
const utils_1 = require("../../plugins/utils");
const index_1 = require("./index");
let git;
let auth = { username: "" };
async function processCreatingPullRequest(repoSlug, fromBranch, destBranch, options) {
if (!git)
git = (0, simple_git_1.simpleGit)(options.targetDirectory || "./", { binary: "git" });
// merge origin/DEST_BRANCH with FROM_BRANCH
try {
await git.add("./*");
await git.commit("Commit all files to create PR");
await git.push();
if (fromBranch != "master" || fromBranch != "staging" || fromBranch != "prod") {
await git.mergeFromTo("origin/" + destBranch, fromBranch);
}
}
catch (e) {
(0, log_1.log)(e.toString());
}
let message = `${auth.username} is requesting to merge "${fromBranch}" -> "${destBranch}"`;
(0, log_1.log)(message);
let params = {
title: message,
source: {
branch: {
name: fromBranch,
},
},
destination: {
branch: {
name: destBranch,
},
},
};
// TODO: get workspace name from git provider
const workspace = "digitopvn";
try {
let res = await index_1.bitbucket.repositories.createPullRequest({
workspace: workspace,
repo_slug: repoSlug,
_body: params,
});
// log(res.data);
let prID = res.data.id;
// AUTO MERGE if NOT CONFLICTED
if (prID && options && options.shouldMerge == true) {
try {
const _res = await index_1.bitbucket.repositories.mergePullRequest({
pull_request_id: prID,
repo_slug: repoSlug,
workspace: workspace,
});
if (_res && _res.data && _res.data.links) {
(0, log_1.logSuccess)(`Approved PR: ${chalk_1.default.blue(_res.data.links.html.href)}`);
}
}
catch (e) {
(0, log_1.logWarn)(e);
}
}
else {
(0, log_1.logSuccess)(`Review here: ${chalk_1.default.blue(res.data.links.html.href)}`);
}
}
catch (e) {
// console.log('e', e)
(0, log_1.logError)(`[CREATE PR]`, e);
}
}
const createPullRequest = async (options) => {
auth.username = options.username;
// diginext git pr master
if (!git)
git = (0, simple_git_1.simpleGit)(options.targetDirectory || "./", { binary: "git" });
const gitStatus = await git.status(["-s"]);
// get repo URL & repo slug
const commands = ["config", "--get", "remote.origin.url"];
const repoURL = await git.raw(commands);
const repoSlug = (0, utils_1.parseRepoSlugFromUrl)(repoURL);
if (!gitStatus.current) {
await (0, log_1.logError)("Can't fetch current git repository status");
}
let fromBranch = decodeURIComponent(gitStatus.current);
let destBranch = options.thirdAction || "main";
if (options.fourAction) {
fromBranch = decodeURIComponent(options.thirdAction);
destBranch = decodeURIComponent(options.fourAction);
}
// check for multiple destinations
if (destBranch.indexOf(",") > -1) {
const destBranches = destBranch.split(",");
for (let i = 0; i < destBranches.length; i++) {
const dest = destBranches[i];
await processCreatingPullRequest(repoSlug, fromBranch, dest, options);
}
}
else {
await processCreatingPullRequest(repoSlug, fromBranch, destBranch, options);
}
};
exports.createPullRequest = createPullRequest;