#!/usr/bin/env node
"use strict";
/**
 * The script allows you to correct the name of the git branch,
 * incorrectly computed by jenkins.
 *
 * See issue on jenkins board: https://is.gd/vxh37N
 *
 * Parameters:
 *  --branch      (required) You must set an automatically calculated branch name.
 *
 *  @type {string}
 */
Object.defineProperty(exports, "__esModule", { value: true });
const childProcess = require("child_process");
const argument_parser_1 = require("@eigenspace/argument-parser");
const exec = childProcess.execSync;
const argv = new argument_parser_1.ArgumentParser().get(process.argv.slice(2));
const autoCalculatedBranchName = argv.get('branch');
if (!Boolean(autoCalculatedBranchName)) {
    throwError('Must pass auto calculated branch name as run param with key "branch"');
}
const verifiedBranchName = autoCalculatedBranchName;
const lastCommit = run('git log -1 --oneline');
// Different commit messages for different merge cases
const localBranchIntoMaster = /Merge branch '.+'$/g;
const remoteBranchIntoMaster = /Merge remote-tracking branch '.+'$/g;
const pullRequestIntoMaster = /Merge pull request .+ from .+$/g;
const remoteBranchIntoAnyBranch = /Merge remote-tracking branch 'origin\/.+' into (.+)$/g;
const localBranchIntoAnyBranch = /Merge branch '.+' into (.+)$/g;
const intoMaster = [localBranchIntoMaster, remoteBranchIntoMaster, pullRequestIntoMaster];
const intoAnyBranch = [remoteBranchIntoAnyBranch, localBranchIntoAnyBranch];
if (![...intoMaster, ...intoAnyBranch].some(regx => regx.test(lastCommit))) {
    returnResult(verifiedBranchName);
}
if (intoMaster.some(regx => regx.test(lastCommit))) {
    returnResult('origin/master');
}
const intoBranches = intoAnyBranch.map(matcher => lastCommit.match(matcher))
    .filter(match => Boolean(match))
    // @ts-ignore: Object is possibly 'null'
    .map(match => match[0]);
if (intoBranches.length !== 1) {
    throwError('Have problem with matchers!');
}
const branch = intoBranches[0];
returnResult(branch.startsWith('origin/') ? branch : `origin/${branch}`);
function run(command) {
    // eslint-disable-next-line no-console
    console.log('run command:', command);
    const stdout = exec(command, { encoding: 'utf8' });
    // eslint-disable-next-line no-console
    console.log(stdout);
    return stdout;
}
function returnResult(result) {
    // eslint-disable-next-line no-console
    console.log(result);
    process.exit(0);
}
function throwError(error) {
    // eslint-disable-next-line no-console
    console.error(error);
    process.exit(1);
}
//# sourceMappingURL=correct-git-branch-name.js.map