"use strict";
/* istanbul ignore file */
Object.defineProperty(exports, "__esModule", { value: true });
exports.setBranchCache = void 0;
const error_messages_1 = require("../../constants/error-messages");
const logger_1 = require("../../logger");
const platform_1 = require("../../modules/platform");
const repository_1 = require("../../util/cache/repository");
const git_1 = require("../../util/git");
const pristine_1 = require("../../util/git/pristine");
function generateBranchUpgradeCache(upgrade) {
    const { datasource, depName, packageName, fixedVersion, currentVersion, newVersion, currentDigest, newDigest, sourceUrl, } = upgrade;
    const result = {
        datasource,
        depName,
        fixedVersion,
        currentVersion,
        newVersion,
        currentDigest,
        newDigest,
        sourceUrl,
    };
    if (packageName) {
        result.packageName = packageName;
    }
    return result;
}
async function generateBranchCache(branch) {
    const { baseBranch, branchName } = branch;
    try {
        const sha = (0, git_1.getBranchCommit)(branchName) ?? null;
        const baseBranchSha = (0, git_1.getBranchCommit)(baseBranch);
        const pristine = (0, pristine_1.getCachedPristineResult)(branchName);
        let prNo = null;
        let isModified = false;
        let isBehindBase = false;
        let isConflicted = false;
        if (sha) {
            const branchPr = await platform_1.platform.getBranchPr(branchName);
            if (branchPr) {
                prNo = branchPr.number;
            }
            isModified = await (0, git_1.isBranchModified)(branchName);
            isBehindBase = await (0, git_1.isBranchBehindBase)(branchName, baseBranch);
            isConflicted = await (0, git_1.isBranchConflicted)(baseBranch, branchName);
        }
        const automerge = !!branch.automerge;
        const upgrades = branch.upgrades
            ? branch.upgrades.map(generateBranchUpgradeCache)
            : [];
        const branchFingerprint = branch.branchFingerprint;
        return {
            automerge,
            baseBranchSha,
            baseBranch,
            branchFingerprint,
            branchName,
            isBehindBase,
            isConflicted,
            isModified,
            pristine,
            prNo,
            sha,
            upgrades,
        };
    }
    catch (error) {
        const err = error.err || error; // external host error nests err
        const errCodes = [401, 404];
        // istanbul ignore if
        if (errCodes.includes(err.response?.statusCode)) {
            logger_1.logger.warn({ err, branchName }, 'HTTP error generating branch cache');
            return null;
        }
        if (err.message === error_messages_1.REPOSITORY_CHANGED) {
            throw err;
        }
        logger_1.logger.error({ err, branchName }, 'Error generating branch cache');
        return null;
    }
}
async function setBranchCache(branches) {
    const branchCaches = [];
    for (const branch of branches) {
        const branchCache = await generateBranchCache(branch);
        if (branchCache) {
            branchCaches.push(branchCache);
        }
    }
    (0, repository_1.getCache)().branches = branchCaches;
}
exports.setBranchCache = setBranchCache;
//# sourceMappingURL=cache.js.map