"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.rebaseOnboardingBranch = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const global_1 = require("../../../../config/global");
const logger_1 = require("../../../../logger");
const commit_1 = require("../../../../modules/platform/commit");
const git_1 = require("../../../../util/git");
const hasha_1 = require("../../../../util/hasha");
const common_1 = require("../common");
const commit_message_1 = require("./commit-message");
const config_1 = require("./config");
async function rebaseOnboardingBranch(config, previousConfigHash) {
    logger_1.logger.debug('Checking if onboarding branch needs rebasing');
    const configFile = (0, common_1.defaultConfigFile)(config);
    const existingContents = (await (0, git_1.getFile)(configFile, config.onboardingBranch)) ?? '';
    const currentConfigHash = (0, hasha_1.toSha256)(existingContents);
    const contents = await (0, config_1.getOnboardingConfigContents)(config, configFile);
    if (config.onboardingRebaseCheckbox) {
        handleOnboardingManualRebase(previousConfigHash, currentConfigHash);
    }
    // TODO #7154
    if (await (0, git_1.isBranchModified)(config.onboardingBranch)) {
        logger_1.logger.debug('Onboarding branch has been edited and cannot be rebased');
        return null;
    }
    // TODO: fix types (#7154)
    if (contents === existingContents &&
        !(await (0, git_1.isBranchBehindBase)(config.onboardingBranch, config.defaultBranch))) {
        logger_1.logger.debug('Onboarding branch is up to date');
        return null;
    }
    logger_1.logger.debug('Rebasing onboarding branch');
    if (config.onboardingRebaseCheckbox) {
        common_1.OnboardingState.prUpdateRequested = true;
    }
    // istanbul ignore next
    const commitMessageFactory = new commit_message_1.OnboardingCommitMessageFactory(config, configFile);
    const commitMessage = commitMessageFactory.create();
    // istanbul ignore if
    if (global_1.GlobalConfig.get('dryRun')) {
        logger_1.logger.info('DRY-RUN: Would rebase files in onboarding branch');
        return null;
    }
    // TODO #7154
    return (0, commit_1.commitAndPush)({
        baseBranch: config.baseBranch,
        branchName: config.onboardingBranch,
        files: [
            {
                type: 'addition',
                path: configFile,
                contents,
            },
        ],
        message: commitMessage.toString(),
        platformCommit: !!config.platformCommit,
    });
}
exports.rebaseOnboardingBranch = rebaseOnboardingBranch;
function handleOnboardingManualRebase(previousConfigHash, currentConfigHash) {
    if (is_1.default.nullOrUndefined(previousConfigHash)) {
        logger_1.logger.debug('Missing previousConfigHash bodyStruct prop in onboarding PR');
        common_1.OnboardingState.prUpdateRequested = true;
    }
    else if (previousConfigHash !== currentConfigHash) {
        logger_1.logger.debug('Onboarding config has been modified by the user');
        common_1.OnboardingState.prUpdateRequested = true;
    }
}
//# sourceMappingURL=rebase.js.map