{"size": 3734, "ext": "js", "max_stars_repo_path": ".github/.test/auto-labeler.js", "max_stars_repo_name": "MalcolmScoffable/openapi-generator", "max_stars_repo_stars_event_min_datetime": "2018-05-12T02:58:07.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T21:19:39.000Z", "max_issues_repo_path": ".github/.test/auto-labeler.js", "max_issues_repo_name": "MalcolmScoffable/openapi-generator", "max_issues_repo_issues_event_min_datetime": "2018-05-12T14:25:43.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T23:59:30.000Z", "max_forks_repo_path": ".github/.test/auto-labeler.js", "max_forks_repo_name": "MalcolmScoffable/openapi-generator", "max_forks_repo_forks_event_min_datetime": "2018-05-12T12:06:08.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T19:52:51.000Z"}
{"max_stars_count": 11868, "max_issues_count": 9672, "max_forks_count": 4776, "avg_line_length": 31.6440677966, "max_line_length": 133, "alphanum_fraction": 0.5942688806}
let fs   = require('fs');
let path = require('path');
let util = require('util');
let yaml = require('./js-yaml.js');
let samples = require('./samples.json');

class LabelMatch {
    constructor (match, label) {
        this.match = match;
        this.label = label;
    }
}

class FileError {
    constructor (file, actualLabels, expectedLabels) {
        this.file = file;
        this.actual = actualLabels;
        this.expected = expectedLabels;
    }
}

class TextError {
    constructor (text, actualLabels, expectedLabels) {
        this.text = text;
        this.actual = actualLabels;
        this.expected = expectedLabels;
    }
}

let labels = [];

function labelsForFile(file) {
    let body = fs.readFileSync(file);
    return labelsForText(body)
}

function labelsForText(text) {
    let addLabels = new Set();
    let body = text;
    for (const v of labels) {
        if (v.match.test(body)) {
            addLabels.add(v.label)
        }
        // reset regex state
        v.match.lastIndex = 0
    }
    return addLabels;
}

try {
    let config = yaml.safeLoad(fs.readFileSync('../auto-labeler.yml', 'utf8'));

    if (config && config.labels && Object.keys(config.labels).length > 0) {
        for (const labelName in config.labels) {
            if (config.labels.hasOwnProperty(labelName)) {
                let matchAgainst = config.labels[labelName];
                if (Array.isArray(matchAgainst)) {
                    matchAgainst.forEach(regex => {
                        // noinspection JSCheckFunctionSignatures
                        labels.push(new LabelMatch(new RegExp(regex, 'g'), labelName));
                    })
                }
            }
        }
    }

    if (labels.length === 0) {
        // noinspection ExceptionCaughtLocallyJS
        throw new Error("Expected to parse config.labels, but failed.")
    }

    let fileErrors = [];
    samples.files.forEach(function(tester){
        let file = path.normalize(path.join('..', '..', 'bin', tester.input));
        let expectedLabels = new Set(tester.matches);
        let actualLabels = labelsForFile(file);
        let difference = new Set([...actualLabels].filter(x => !expectedLabels.has(x)));
        if (difference.size > 0) {
            fileErrors.push(new FileError(file, actualLabels, expectedLabels));
        }
    });

    let textErrors = [];
    samples.text.forEach(function(tester){
        let expectedLabels = new Set(tester.matches);
        let actualLabels = labelsForText(tester.input);
        let difference = new Set([...actualLabels].filter(x => !expectedLabels.has(x)));
        if (difference.size > 0) {
            textErrors.push(new TextError(tester.input, actualLabels, expectedLabels));
        }
    });

    // These are separate (file vs text) in case we want to preview where these would fail in the file. not priority at the moment.
    if (fileErrors.length > 0) {
        console.warn('There were %d file tester errors', fileErrors.length);
        fileErrors.forEach(function(errs) {
            console.log("file: %j\n  actual: %j\n  expected: %j", errs.file, util.inspect(errs.actual), util.inspect(errs.expected))
        });
    }

    if (textErrors.length > 0) {
        console.warn('There were %d text tester errors', textErrors.length);
        textErrors.forEach(function(errs){
            console.log("input: %j\n  actual: %j\n  expected: %j", errs.text, util.inspect(errs.actual), util.inspect(errs.expected))
        })
    }

    let totalErrors = fileErrors.length + textErrors.length;
    if (totalErrors === 0) {
        console.log('Success!');
    } else {
        console.log('Failure: %d total errors', totalErrors);
    }
} catch (e) {
    console.log(e);
}

