"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAsync = validateAsync;
exports.getEmailExampleLines = getEmailExampleLines;
const node_core_library_1 = require("@rushstack/node-core-library");
const terminal_1 = require("@rushstack/terminal");
const Utilities_1 = require("../../utilities/Utilities");
const Git_1 = require("../Git");
const RushConstants_1 = require("../RushConstants");
async function validateAsync(rushConfiguration, options) {
    const git = new Git_1.Git(rushConfiguration);
    if (!git.isGitPresent()) {
        // If Git isn't installed, or this Rush project is not under a Git working folder,
        // then we don't care about the Git email
        // eslint-disable-next-line no-console
        console.log(terminal_1.Colorize.cyan('Ignoring Git validation because the Git binary was not found in the shell path.') + '\n');
        return;
    }
    if (!git.isPathUnderGitWorkingTree()) {
        // If Git isn't installed, or this Rush project is not under a Git working folder,
        // then we don't care about the Git email
        // eslint-disable-next-line no-console
        console.log(terminal_1.Colorize.cyan('Ignoring Git validation because this is not a Git working folder.') + '\n');
        return;
    }
    let userEmail = await git.tryGetGitEmailAsync();
    // If there isn't a Git policy, then we don't care whether the person configured
    // a Git email address at all.
    if (rushConfiguration.gitAllowedEmailRegExps.length === 0) {
        if (userEmail === undefined) {
            return;
        }
        // Otherwise, if an email *is* configured at all, then we still perform the basic
        // sanity checks (e.g. no spaces in the address).
    }
    try {
        userEmail = git.validateGitEmail(userEmail);
        // sanity check; a valid email should not contain any whitespace
        // if this fails, then we have another issue to report
        if (!userEmail.match(/^\S+$/g)) {
            // eslint-disable-next-line no-console
            console.log([
                terminal_1.Colorize.red('Your Git email address is invalid: ' + JSON.stringify(userEmail)),
                '',
                `To configure your Git email address, try something like this:`,
                '',
                ...getEmailExampleLines(rushConfiguration),
                ''
            ].join('\n'));
            throw new node_core_library_1.AlreadyReportedError();
        }
    }
    catch (e) {
        if (e instanceof node_core_library_1.AlreadyReportedError) {
            let errorMessage = 'Aborting, so you can go fix your settings.';
            if (options.bypassPolicyAllowed) {
                errorMessage += ` (Or use "${RushConstants_1.RushConstants.bypassPolicyFlagLongName}" to skip.)`;
            }
            // eslint-disable-next-line no-console
            console.log(terminal_1.Colorize.red(errorMessage));
            throw e;
        }
        else {
            throw e;
        }
    }
    if (rushConfiguration.gitAllowedEmailRegExps.length === 0) {
        // If there is no policy, then we're good
        return;
    }
    // eslint-disable-next-line no-console
    console.log('Checking Git policy for this repository.\n');
    // If there is a policy, at least one of the RegExp's must match
    for (const pattern of rushConfiguration.gitAllowedEmailRegExps) {
        const regex = new RegExp(`^${pattern}$`, 'i');
        if (userEmail.match(regex)) {
            return;
        }
    }
    // Show the user's name as well.
    // Ex. "Example Name <name@example.com>"
    let fancyEmail = terminal_1.Colorize.cyan(userEmail);
    try {
        const userName = (await Utilities_1.Utilities.executeCommandAndCaptureOutputAsync({
            command: git.gitPath,
            args: ['config', 'user.name'],
            workingDirectory: '.'
        })).trim();
        if (userName) {
            fancyEmail = `${userName} <${fancyEmail}>`;
        }
    }
    catch (e) {
        // but if it fails, this isn't critical, so don't bother them about it
    }
    // eslint-disable-next-line no-console
    console.log([
        'Hey there!  To keep things tidy, this repo asks you to submit your Git commits using an email like ' +
            (rushConfiguration.gitAllowedEmailRegExps.length > 1 ? 'one of these patterns:' : 'this pattern:'),
        '',
        ...rushConfiguration.gitAllowedEmailRegExps.map((pattern) => '    ' + terminal_1.Colorize.cyan(pattern)),
        '',
        '...but yours is configured like this:',
        '',
        `    ${fancyEmail}`,
        '',
        'To fix it, you can use commands like this:',
        '',
        ...getEmailExampleLines(rushConfiguration),
        ''
    ].join('\n'));
    let errorMessage = 'Aborting, so you can go fix your settings.';
    if (options.bypassPolicyAllowed) {
        errorMessage += ` (Or use "${RushConstants_1.RushConstants.bypassPolicyFlagLongName}" to skip.)`;
    }
    // eslint-disable-next-line no-console
    console.log(terminal_1.Colorize.red(errorMessage));
    throw new node_core_library_1.AlreadyReportedError();
}
function getEmailExampleLines(rushConfiguration) {
    return [
        terminal_1.Colorize.cyan('    git config --local user.name "Example Name"'),
        terminal_1.Colorize.cyan(`    git config --local user.email "${rushConfiguration.gitSampleEmail || 'name@example.com'}"`)
    ];
}
//# sourceMappingURL=GitEmailPolicy.js.map