"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrationPrExists = exports.checkConfigMigrationBranch = void 0;
const global_1 = require("../../../../config/global");
const logger_1 = require("../../../../logger");
const platform_1 = require("../../../../modules/platform");
const comment_1 = require("../../../../modules/platform/comment");
const git_1 = require("../../../../util/git");
const common_1 = require("../common");
const commit_message_1 = require("./commit-message");
const create_1 = require("./create");
const rebase_1 = require("./rebase");
async function checkConfigMigrationBranch(config, migratedConfigData) {
    logger_1.logger.debug('checkConfigMigrationBranch()');
    if (!migratedConfigData) {
        logger_1.logger.debug('checkConfigMigrationBranch() Config does not need migration');
        return null;
    }
    const configMigrationBranch = (0, common_1.getMigrationBranchName)(config);
    const branchPr = await migrationPrExists(configMigrationBranch); // handles open/autoClosed PRs
    if (!branchPr) {
        const commitMessageFactory = new commit_message_1.ConfigMigrationCommitMessageFactory(config, migratedConfigData.filename);
        const prTitle = commitMessageFactory.getPrTitle();
        const closedPrConfig = {
            branchName: configMigrationBranch,
            prTitle,
            state: 'closed',
        };
        // handles closed PR
        const closedPr = await platform_1.platform.findPr(closedPrConfig);
        // found closed migration PR
        if (closedPr) {
            logger_1.logger.debug({ prTitle: closedPr.title }, 'Closed PR already exists. Skipping branch.');
            await handlepr(config, closedPr);
            return null;
        }
    }
    if (branchPr) {
        logger_1.logger.debug('Config Migration PR already exists');
        await (0, rebase_1.rebaseMigrationBranch)(config, migratedConfigData);
        if (platform_1.platform.refreshPr) {
            const configMigrationPr = await platform_1.platform.getBranchPr(configMigrationBranch);
            if (configMigrationPr) {
                await platform_1.platform.refreshPr(configMigrationPr.number);
            }
        }
    }
    else {
        logger_1.logger.debug('Config Migration PR does not exist');
        logger_1.logger.debug('Need to create migration PR');
        await (0, create_1.createConfigMigrationBranch)(config, migratedConfigData);
    }
    if (!global_1.GlobalConfig.get('dryRun')) {
        await (0, git_1.checkoutBranch)(configMigrationBranch);
    }
    return configMigrationBranch;
}
exports.checkConfigMigrationBranch = checkConfigMigrationBranch;
async function migrationPrExists(branchName) {
    return !!(await platform_1.platform.getBranchPr(branchName));
}
exports.migrationPrExists = migrationPrExists;
async function handlepr(config, pr) {
    if (pr.state === 'closed' &&
        !config.suppressNotifications.includes('prIgnoreNotification')) {
        if (global_1.GlobalConfig.get('dryRun')) {
            logger_1.logger.info(`DRY-RUN: Would ensure closed PR comment in PR #${pr.number}`);
        }
        else {
            const content = '\n\nIf this PR was closed by mistake or you changed your mind, you can simply rename this PR and you will soon get a fresh replacement PR opened.';
            await (0, comment_1.ensureComment)({
                number: pr.number,
                topic: 'Renovate Ignore Notification',
                content,
            });
        }
        if ((0, git_1.branchExists)(pr.sourceBranch)) {
            if (global_1.GlobalConfig.get('dryRun')) {
                logger_1.logger.info('DRY-RUN: Would delete branch ' + pr.sourceBranch);
            }
            else {
                await (0, git_1.deleteBranch)(pr.sourceBranch);
            }
        }
    }
}
//# sourceMappingURL=index.js.map