all files / lib/ data.js

90% Statements 27/30
75% Branches 6/8
80% Functions 8/10
90% Lines 27/30
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101×         131× 131× 131× 131× 131× 131× 131×         55×   55×       55×                       123×                 24× 24×       24× 1×     23× 23×   23×           123×         123×   123×                                 23× 23×       23×                       23×       1×      
const s3 = require('./s3-helpers'),
      config = require('./config');
 
class Build {
    constructor (opts) {
        this.project = opts.project;
        this.channel = opts.channel;
        this.version = opts.version;
        this.number = opts.number;
        this.arch = opts.arch;
        this.metadata = opts.metadata || {};
        this.buildDate = new Date(opts.buildDate);
    }
 
    static fromListEntry (entry) {
        // (-([a-fA-F0-9]{7}))?
        let found = entry.Key.match(/([^\/]+)\/([^\/]+)\/([^_]+)_([^_-]+)-(\d+)_([^_]+)\.tar\.gz/);
 
        Iif (!found) {
            return null;
        }
 
        return new Build({
            project: found[1],
            channel: found[2],
            version: found[4],
            number: parseInt(found[5]),
            arch: found[6],
            metadata: {},
            buildDate: new Date(entry.LastModified)
        });
    }
 
    get path() {
        return `${this.project}/${this.channel}/` +
               `${this.project}_${this.buildVersion}_${this.arch}.tar.gz`;
    }
 
    get publicUrl() {
        return `https://${config.local.rootBucket.name}.s3.amazonaws.com/${this.path}`;
    }
 
    fetchMetadata() {
        return new Promise((resolve, reject) => {
            s3.getInstance().headObject({
                Bucket: config.local.rootBucket.name,
                Key: this.path
            }, (err, data) => {
                if (err) {
                    return reject(err);
                }
 
                Eif (data.Metadata) {
                    this.metadata = data.Metadata;
                }
                resolve(this.metadata);
            });
        });
    }
 
    get buildVersion () {
        let buildTag = '';
 
        // if (this.revision) {
        //     buildTag = this.revision + '-';
        // }
        buildTag += this.number;
 
        return this.version + '-' + buildTag;
    }
 
    toJSON () {
        return JSON.stringify({
            project: this.project,
            channel: this.channel,
            version: this.version,
            number: this.number,
            buildDate: this.buildDate,
            metadata: this.metadata
        });
    }
}
 
class Release extends Build {
    constructor(opts) {
        super(opts);
        this.releaseDate = new Date(opts.releaseDate);
    }
 
    toJSON() {
        return JSON.stringify({
            project: this.project,
            channel: this.channel,
            version: this.version,
            number: this.number,
            releaseDate: this.releaseDate,
            buildDate: this.buildDate,
            metadata: this.metadata
        });
    }
 
    updateReleaseDate () {
        this.releaseDate = new Date();
    }
}
 
module.exports = {
    Build,
    Release
}