"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.raiseCredentialsWarningIssue = exports.raiseConfigWarningIssue = void 0;
// TODO #22198
const global_1 = require("../../config/global");
const logger_1 = require("../../logger");
const platform_1 = require("../../modules/platform");
function raiseConfigWarningIssue(config, error) {
    logger_1.logger.debug('raiseConfigWarningIssue()');
    const title = `Action Required: Fix Renovate Configuration`;
    const body = `There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.\n\n`;
    const notificationName = 'configErrorIssue';
    return raiseWarningIssue(config, notificationName, title, body, error);
}
exports.raiseConfigWarningIssue = raiseConfigWarningIssue;
function raiseCredentialsWarningIssue(config, error) {
    logger_1.logger.debug('raiseCredentialsWarningIssue()');
    const title = `Action Required: Add missing credentials`;
    const body = `There are missing credentials for the authentication-required feature. As a precaution, Renovate will pause PRs until it is resolved.\n\n`;
    const notificationName = 'missingCredentialsError';
    return raiseWarningIssue(config, notificationName, title, body, error);
}
exports.raiseCredentialsWarningIssue = raiseCredentialsWarningIssue;
async function raiseWarningIssue(config, notificationName, title, initialBody, error) {
    let body = initialBody;
    if (error.validationSource) {
        body += `Location: \`${error.validationSource}\`\n`;
    }
    if (error.validationError) {
        body += `Error type: ${error.validationError}\n`;
    }
    if (error.validationMessage) {
        body += `Message: ${error.validationMessage}\n`;
    }
    const pr = await platform_1.platform.getBranchPr(config.onboardingBranch, config.baseBranch);
    if (pr?.state === 'open') {
        await handleOnboardingPr(pr, body);
        return;
    }
    if (global_1.GlobalConfig.get('dryRun')) {
        logger_1.logger.info({ configError: error }, 'DRY-RUN: Would ensure configuration error issue');
        return;
    }
    if (config.suppressNotifications?.includes(notificationName)) {
        logger_1.logger.info({ notificationName }, 'Configuration failure, issues will be suppressed');
        return;
    }
    const res = await platform_1.platform.ensureIssue({
        title,
        body,
        once: false,
        shouldReOpen: config.configWarningReuseIssue,
        confidential: config.confidential,
    });
    if (res === 'created') {
        logger_1.logger.warn({ configError: error, res }, 'Configuration Warning');
    }
}
async function handleOnboardingPr(pr, issueMessage) {
    logger_1.logger.debug('Updating onboarding PR with config error notice');
    if (global_1.GlobalConfig.get('dryRun')) {
        logger_1.logger.info(`DRY-RUN: Would update PR #${pr.number}`);
        return;
    }
    let prBody = `## Action Required: Fix Renovate Configuration\n\n${issueMessage}`;
    prBody += `\n\nOnce you have resolved this problem (in this onboarding branch), Renovate will return to providing you with a preview of your repository's configuration.`;
    try {
        await platform_1.platform.updatePr({
            number: pr.number,
            prTitle: pr.title,
            prBody,
        });
    }
    catch (err) /* istanbul ignore next */ {
        logger_1.logger.warn({ err }, 'Error updating onboarding PR');
    }
}
//# sourceMappingURL=error-config.js.map