const childProcess = require('child_process');
const packageVersion = require('../package').version;

/* eslint-disable consistent-return */
const getGitCommitHash = () => {
    if (process.env.GIT_COMMIT) {
        return process.env.GIT_COMMIT;
    }
    try {
        return childProcess.execSync('git rev-parse HEAD').toString();
    } catch (e) {
        console.log('Failed to get git commit hash');
    }
};

const getBranchInfo = () => {
    if (process.env.BRANCH_NAME) {
        return process.env.BRANCH_NAME;
    } if (process.env.CHANGE_BRANCH) {
        return process.env.CHANGE_BRANCH;
    }
    try {
        return childProcess.execSync('git branch --show-current').toString();
    } catch (e) {
        console.log('Failed to get git branch');
    }
};


/* eslint-disable arrow-body-style */
const getBuildInfo = () => {
    return `${packageVersion} | ${getBranchInfo()} | ${getGitCommitHash()} | ${new Date()}`;
};


module.exports = { getBuildInfo };
