"use strict";
/* istanbul ignore file */
Object.defineProperty(exports, "__esModule", { value: true });
exports.setBranchCache = void 0;
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 parent_sha_cache_1 = require("../../util/git/parent-sha-cache");
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);
        let prNo = null;
        let parentSha = null;
        let isModified = false;
        let isBehindBase = false;
        let isConflicted = false;
        if (sha) {
            parentSha = (0, parent_sha_cache_1.getCachedBranchParentShaResult)(branchName, 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,
            parentSha,
            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;
        }
        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