"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = exports.parseAzurePipelines = exports.extractAzurePipelinesTasks = exports.extractContainer = exports.extractRepository = void 0;
const js_yaml_1 = require("js-yaml");
const logger_1 = require("../../../logger");
const array_1 = require("../../../util/array");
const regex_1 = require("../../../util/regex");
const azure_pipelines_tasks_1 = require("../../datasource/azure-pipelines-tasks");
const git_tags_1 = require("../../datasource/git-tags");
const extract_1 = require("../dockerfile/extract");
const AzurePipelinesTaskRegex = (0, regex_1.regEx)(/^(?<name>[^@]+)@(?<version>.*)$/);
function extractRepository(repository) {
    if (repository.type !== 'github') {
        return null;
    }
    if (!repository.ref?.startsWith('refs/tags/')) {
        return null;
    }
    return {
        autoReplaceStringTemplate: 'refs/tags/{{newValue}}',
        currentValue: repository.ref.replace('refs/tags/', ''),
        datasource: git_tags_1.GitTagsDatasource.id,
        depName: repository.name,
        depType: 'gitTags',
        packageName: `https://github.com/${repository.name}.git`,
        replaceString: repository.ref,
    };
}
exports.extractRepository = extractRepository;
function extractContainer(container) {
    if (!container.image) {
        return null;
    }
    const dep = (0, extract_1.getDep)(container.image);
    logger_1.logger.debug({
        depName: dep.depName,
        currentValue: dep.currentValue,
        currentDigest: dep.currentDigest,
    }, 'Azure pipelines docker image');
    dep.depType = 'docker';
    return dep;
}
exports.extractContainer = extractContainer;
function extractAzurePipelinesTasks(task) {
    const match = AzurePipelinesTaskRegex.exec(task);
    if (match?.groups) {
        return {
            depName: match.groups.name,
            currentValue: match.groups.version,
            datasource: azure_pipelines_tasks_1.AzurePipelinesTasksDatasource.id,
        };
    }
    return null;
}
exports.extractAzurePipelinesTasks = extractAzurePipelinesTasks;
function parseAzurePipelines(content, filename) {
    let pkg = null;
    try {
        pkg = (0, js_yaml_1.load)(content, { json: true });
    }
    catch (err) /* istanbul ignore next */ {
        logger_1.logger.info({ filename, err }, 'Error parsing azure-pipelines content');
        return null;
    }
    return pkg;
}
exports.parseAzurePipelines = parseAzurePipelines;
function extractPackageFile(content, filename) {
    logger_1.logger.trace(`azurePipelines.extractPackageFile(${filename})`);
    const deps = [];
    const pkg = parseAzurePipelines(content, filename);
    if (!pkg) {
        return null;
    }
    for (const repository of (0, array_1.coerceArray)(pkg.resources?.repositories)) {
        const dep = extractRepository(repository);
        if (dep) {
            deps.push(dep);
        }
    }
    for (const container of (0, array_1.coerceArray)(pkg.resources?.containers)) {
        const dep = extractContainer(container);
        if (dep) {
            deps.push(dep);
        }
    }
    for (const { jobs } of (0, array_1.coerceArray)(pkg.stages)) {
        for (const { steps } of (0, array_1.coerceArray)(jobs)) {
            for (const step of (0, array_1.coerceArray)(steps)) {
                const task = extractAzurePipelinesTasks(step.task);
                if (task) {
                    deps.push(task);
                }
            }
        }
    }
    for (const { steps } of (0, array_1.coerceArray)(pkg.jobs)) {
        for (const step of (0, array_1.coerceArray)(steps)) {
            const task = extractAzurePipelinesTasks(step.task);
            if (task) {
                deps.push(task);
            }
        }
    }
    for (const step of (0, array_1.coerceArray)(pkg.steps)) {
        const task = extractAzurePipelinesTasks(step.task);
        if (task) {
            deps.push(task);
        }
    }
    if (!deps.length) {
        return null;
    }
    return { deps };
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map