All files manifest-container-builds.js

100% Statements 19/19
100% Branches 15/15
100% Functions 6/6
100% Lines 18/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44    1x       8x 8x 8x 8x 8x 1x   7x 7x 7x 7x 7x         5x   2x 2x           2x           2x       7x    
import dockerParseImage from 'docker-parse-image';
 
module.exports = function({
    getContainers,
    getBuilds,
}) {
    return function manifestContainerBuilds(projectId, manifest) {
        const containers = getContainers(manifest);
        const builds = getBuilds(manifest);
        const accum = [];
        if (!builds) {
            return accum;
        }
        return containers.reduce((accum, {name, image}) => {
            const build = getBuild(builds, name);
            const parsedImage = dockerParseImage(image);
            const {registry, namespace, tag} = parsedImage;
            if (!(build &&
                registry === 'gcr.io' &&
                namespace === projectId &&
                tag !== null &&
                tag !== 'latest')) {
                    return accum;
            }
            const is = isBranch(tag);
            accum.push({
                image: parsedImage,
                build,
                ...(is && {branch: tag}),
                ...(!is && {commitSha: tag}),
            });
            return accum;
        }, accum);
    };
}
 
function isBranch(str) {
    return Buffer.from(str, 'hex').length !== 20;
}
 
function getBuild(builds, name) {
    return builds.filter(({container}) => container === name)[0] || null;
}