<commit_before>"use strict";
const PR = require("./models/pr");
const ViewBody = require("./views/body");
const Config = require("./models/config");
const github = require("simple-github");

class Controller {
    constructor() {
        this.currently_running = new Set();
        this.previewer_cache = new Map();
        this.renderer = new ViewBody();
    }

    setCache(params, pr) {
        let k = this.cacheKey(params);
        let c = require("urlencode")(params.config);
        let f = ".pr-preview.json";
        let pr_url = `https://github.com/${pr.owner}/${pr.repo}/new/${pr.base.branch}?filename=${f}&value=${c}`;
        console.log("cache:set", k, pr_url);
        this.previewer_cache.set(k, pr_url);
    }

    cacheKey(params) {
        return `${params.owner}/${params.repo}/${params.config}`;
    }

    getCache(params) {
        let k = this.cacheKey(params);
        console.log("cache:get", k);
        return this.previewer_cache.get(k);
    }

    getUrl(params) {
        try {
            let regex = /^[a-z0-9-_]+$/i;
            let owner = params.owner.trim();
            let repo = params.repo.trim();
            if (!regex.test(owner) || !regex.test(repo)) {
                throw new TypeError("Invalid repo or owner name");
            }
            let config = JSON.parse(params.config);
            Config.validate(config);
            return require("./auth")(false).then(headers => {
                return github({
                    headers: headers
                }).request("GET /repos/:owner/:repo/pulls?state=all&per_page=1", {
                    repo: repo,
                    owner: owner,
                    limit: 1
                }).then(prs => {
                    if (!prs.length) {
                        throw new Error(`No pull requests to test with on ${owner}/${repo}. Sorry!`);
                    }
                    let pr = new PR({ pull_request: prs[0] });
                    pr.config = config;
                    return this.testUrl(pr.head.github_url)
                        .then(_ => pr.head.getUrl())
                        .then(url => {
                            // cache stuff to make pr request faster
                            this.setCache(params, pr);
                            return url;
                        });
                });
            });
        } catch(err) {
            return Promise.reject(err);
        }
    }<commit_msg>Fix config preview tool<commit_after>"use strict";
const PR = require("./models/pr");
const ViewBody = require("./views/body");
const Config = require("./models/config");
const github = require("simple-github");

class Controller {
    constructor() {
        this.currently_running = new Set();
        this.previewer_cache = new Map();
        this.renderer = new ViewBody();
    }

    setCache(params, pr) {
        let k = this.cacheKey(params);
        let c = require("urlencode")(params.config);
        let f = ".pr-preview.json";
        let pr_url = `https://github.com/${pr.owner}/${pr.repo}/new/${pr.base.branch}?filename=${f}&value=${c}`;
        console.log("cache:set", k, pr_url);
        this.previewer_cache.set(k, pr_url);
    }

    cacheKey(params) {
        return `${params.owner}/${params.repo}/${params.config}`;
    }

    getCache(params) {
        let k = this.cacheKey(params);
        console.log("cache:get", k);
        return this.previewer_cache.get(k);
    }

    getUrl(params) {
        try {
            let regex = /^[a-z0-9-_]+$/i;
            let owner = params.owner.trim();
            let repo = params.repo.trim();
            if (!regex.test(owner) || !regex.test(repo)) {
                throw new TypeError("Invalid repo or owner name");
            }
            let config = JSON.parse(params.config);
            Config.validate(config);
            return require("./auth")(false).then(headers => {
                return github({
                    headers: headers
                }).request("GET /repos/:owner/:repo/pulls?state=all&per_page=1", {
                    repo: repo,
                    owner: owner,
                    limit: 1
                }).then(prs => {
                    if (!prs.length) {
                        throw new Error(`No pull requests to test with on ${owner}/${repo}. Sorry!`);
                    }
                    let pr = new PR(`${ prs[0].base.repo.full_name }/${ prs[0].id }`, { id: -1 });
                    pr.payload = prs[0];
                    pr.config = config;
                    return this.testUrl(pr.head.github_url)
                        .then(_ => pr.head.getUrl())
                        .then(url => {
                            // cache stuff to make pr request faster
                            this.setCache(params, pr);
                            return url;
                        });
                });
            });
        } catch(err) {
            return Promise.reject(err);
        }
    }
{'fraction_non_alphanumeric': 0.14902590881703154, 'fraction_numerical': 0.0026109660574412533, 'mean_word_length': 1.4471744471744472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '461884', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1355, 'n_words': 279}