<commit_before>/**
 * Created by <NAME>
 * <EMAIL>
 * 14/07/2015.
 */

var pullRequestsURL = '/rest/inbox/latest/pull-requests?role=reviewer&start=0&limit=10&avatarSize=64&state=OPEN&order=oldest';
var myPullRequestsURL = '/rest/inbox/latest/pull-requests?role=author&start=0&limit=10&avatarSize=64&state=OPEN&order=oldest';
var whoAmI = '/plugins/servlet/applinks/whoami';

function host(uri) {

    if (!uri) return localStorage["_server"];
    if (uri.indexOf('http') === 0) return uri;

    var splitRegExp = /\/(\b|$)/g;
    return (_.compact(host().split(splitRegExp).concat(uri.split(splitRegExp)))).join('/');
}

var _listeners = {};

function me() {
    if (localStorage["_username"].length) {
        return localStorage["_username"];
    } else {
        return najax(host(whoAmI))
            .then(function (data) {
                localStorage["_username"] = data;
                return data;
            });
    }
}

function getTasks(pr) {
    var uri = host("/rest/api/latest" + pr["link"]["url"] + "/tasks?start=0&limit=5");
    var idx = 0;
    return najax(uri)
        .then(function (data) {
            return JSON.parse(data).values.map(function (task) {
                return ++idx + ". " + task.anchor.text.split(/\n/)[0];
            }).join('\n');
        });
}

function filterResult(data) {

    // Open tasks:
    if (localStorage["_hide_pr_with_tasks"] == "true") {
        data.values = _.filter(data.values, function (pr) {
            return !(
                pr['attributes']['openTaskCount']
                && (pr['attributes']['openTaskCount'] != "0")
            );
        });
    }

    // Scrum master:
    if (localStorage["_scrum_master"] == "true"
        && me()
    ) {
        data.values = _.filter(data.values, function (pr) {
            // If I'm the only reviewer, display:
            if (pr["reviewers"].length == 1) return true;
            // Else, if at least one other had reviewed:
            return _.some(pr["reviewers"], function (rev) {
                return (
                    rev['approved'] == true
                    && rev['user']['name'] != me()
                );
            });
        });
    }
    data.size = data.values.length;
    //return data;
    return data;
}

function getPullRequestData() {

    // TODO: Improve
    var uri = host(pullRequestsURL);

    return najax(uri)
        .then(JSON.parse)
        .then(filterResult)
        .then(function (res) {
            if (res.size == 0) clearBadge();
            else setBadge(res.size, null, "#ff0000");
            localStorage.prData = JSON.stringify(res);
            return res;
        })
        .catch(errHandle);
}

function getMyRequestsData() {

    var uri = host(myPullRequestsURL);

    return najax(uri)
        .then(JSON.parse)
        .then(function (res) {
            localStorage.prDataMine = JSON.stringify(res);
            return res;
        })
        .catch(errHandle);
}

var notifyPullRequests = _.throttle(function (PRData) {
    if (localStorage["_notifyPRs"] == "true" && !(localStorage['snooze_all'] > Date.now())) {
        PRData.values.forEach(notifyPullRequest);
    }
    return Promise.resolve();
}, localStorage["_notifyInterval"], {});

function notifyPullRequest(pr) {

    var prID = pr.links.self[0].href;

    // Quit if snoozed:
    if (localStorage['snooze.' + prID] > Date.now()) return;

    // Don't show if no repeat:
    if (localStorage["_repeatUntilNoticed"] == "once"
        && localStorage["notif." + prID])
        return;

    // Don't show if clicked:
    if (localStorage["_repeatUntilNoticed"] == "click"
        && localStorage["click." + prID])
        return;

    // Prepare buttons:
    var buttons = [];
    if (!(localStorage["_snooze_this_btn"] == 'true')) {
        buttons.push({title: "Snooze this Pull Request", iconUrl: "/assets/snooze.svg"});
    }
    if (!(localStorage["_snooze_all_btn"] == 'true')) {
        buttons.push({title: "Snooze all", iconUrl: "/assets/zzz.svg"});
    }

    chrome.notifications.clear(prID, null);
    chrome.notifications.create(prID, {
        type: "basic",
        message: pr.title,
        title: pr['author']['user']['displayName'],
        iconUrl: host(pr['author']['user']['avatarUrl'].split("?")[0]),
        buttons: buttons
    }, function () {
        // Mark as shown:
        localStorage["notif." + prID] = 1;
        if (!(_listeners[prID] == 'true')) {
            _listeners[prID] = true;
            chrome.notifications.onClicked.addListener(function (prID) {
                window.open(prID);
                localStorage["click." + prID] = 1;
            });

            chrome.notifications.onButtonClicked.addListener(function (prID, buttonIndex) {
                    switch (buttonIndex) {
                        case 0: // Snooze THIS!
                            localStorage['snooze.' + prID] = Date.now() + Number(localStorage["_snooze_duration"].replace(/"/g, ''));
                            break;
                        case 1: // Snooze ALL!
                            localStorage['snooze_all'] = Date.now() + Number(localStorage["_snooze_duration"].replace(/"/g, ''));
                            break;
                    }
                }
            );
        }
    });
}<commit_msg>Promisified stash_model and added tasks<commit_after>/**
 * Created by <NAME>
 * <EMAIL>
 * 14/07/2015.
 */

var pullRequestsURL = '/rest/inbox/latest/pull-requests?role=reviewer&start=0&limit=10&avatarSize=64&state=OPEN&order=oldest';
var myPullRequestsURL = '/rest/inbox/latest/pull-requests?role=author&start=0&limit=10&avatarSize=64&state=OPEN&order=oldest';
var whoAmI = '/plugins/servlet/applinks/whoami';

function host(uri) {

    if (!uri) return localStorage["_server"];
    if (uri.indexOf('http') === 0) return uri;

    var splitRegExp = /\/(\b|$)/g;
    return (_.compact(host().split(splitRegExp).concat(uri.split(splitRegExp)))).join('/');
}

var _listeners = {};

function me() {
    if (localStorage["_username"].length) {
        return localStorage["_username"];
    } else {
        return najax(host(whoAmI))
            .then(function (data) {
                localStorage["_username"] = data;
                return data;
            });
    }
}

function getTasksByPR(pr, limit) {
    var uri = host("/rest/api/latest" + pr["link"]["url"] + "/tasks?start=0&limit=" + (limit || 5));
    var idx = 0;
    return najax(uri)
        .then(function (data) {
            return JSON.parse(data).values.map(function (t) {
                debugger;
                return {text: t.text, author: t.author.displayName, state: t.state, created: t.createdDate}
            });
        })
        .catch(function (e) {
            debugger;
        });
}

function getTasks(data) {
    return Promise.map(data.values, function (pr) {
        return getTasksByPR(pr)
            .then(function (tasks) {
                pr.tasks = tasks;
                return pr;
            });
    })
        .then(function (res) {
            data.values = res;
            return data;
        });
}

function filterResult(data) {

    // Open tasks:
    if (localStorage["_hide_pr_with_tasks"] == "true") {
        data.values = _.filter(data.values, function (pr) {
            return !(
                pr['attributes']['openTaskCount']
                && (pr['attributes']['openTaskCount'] != "0")
            );
        });
    }

    // Scrum master:
    if (localStorage["_scrum_master"] == "true"
        && me()
    ) {
        data.values = _.filter(data.values, function (pr) {
            // If I'm the only reviewer, display:
            if (pr["reviewers"].length == 1) return true;
            // Else, if at least one other had reviewed:
            return _.some(pr["reviewers"], function (rev) {
                return (
                    rev['approved'] == true
                    && rev['user']['name'] != me()
                );
            });
        });
    }
    data.size = data.values.length;
    //return data;
    return data;
}

function getPullRequestData() {

    // TODO: Improve
    var uri = host(pullRequestsURL);

    return najax(uri)
        .then(JSON.parse)
        .then(filterResult)
        .then(getTasks)
        .then(function (res) {
            if (res.size == 0) clearBadge();
            else setBadge(res.size, null, "#ff0000");
            localStorage.prData = JSON.stringify(res);
            return res;
        })
        .catch(errHandle);
}

function getMyRequestsData() {

    var uri = host(myPullRequestsURL);

    return najax(uri)
        .then(JSON.parse)
        .then(function (res) {
            localStorage.prDataMine = JSON.stringify(res);
            return res;
        })
        .catch(errHandle);
}

var notifyPullRequests = _.throttle(function (PRData) {
    if (localStorage["_notifyPRs"] == "true" && !(localStorage['snooze_all'] > Date.now())) {
        PRData.values.forEach(notifyPullRequest);
    }
    return Promise.resolve();
}, localStorage["_notifyInterval"], {});

function notifyPullRequest(pr) {

    var prID = pr.links.self[0].href;

    // Quit if snoozed:
    if (localStorage['snooze.' + prID] > Date.now()) return;

    // Don't show if no repeat:
    if (localStorage["_repeatUntilNoticed"] == "once"
        && localStorage["notif." + prID])
        return;

    // Don't show if clicked:
    if (localStorage["_repeatUntilNoticed"] == "click"
        && localStorage["click." + prID])
        return;

    // Prepare buttons:
    var buttons = [];
    if (!(localStorage["_snooze_this_btn"] == 'true')) {
        buttons.push({title: "Snooze this Pull Request", iconUrl: "/assets/snooze.svg"});
    }
    if (!(localStorage["_snooze_all_btn"] == 'true')) {
        buttons.push({title: "Snooze all", iconUrl: "/assets/zzz.svg"});
    }

    chrome.notifications.clear(prID, null);
    chrome.notifications.create(prID, {
        type: "basic",
        message: pr.title,
        title: pr['author']['user']['displayName'],
        iconUrl: host(pr['author']['user']['avatarUrl'].split("?")[0]),
        buttons: buttons
    }, function () {
        // Mark as shown:
        localStorage["notif." + prID] = 1;
        if (!(_listeners[prID] == 'true')) {
            _listeners[prID] = true;
            chrome.notifications.onClicked.addListener(function (prID) {
                window.open(prID);
                localStorage["click." + prID] = 1;
            });

            chrome.notifications.onButtonClicked.addListener(function (prID, buttonIndex) {
                    switch (buttonIndex) {
                        case 0: // Snooze THIS!
                            localStorage['snooze.' + prID] = Date.now() + Number(localStorage["_snooze_duration"].replace(/"/g, ''));
                            break;
                        case 1: // Snooze ALL!
                            localStorage['snooze_all'] = Date.now() + Number(localStorage["_snooze_duration"].replace(/"/g, ''));
                            break;
                    }
                }
            );
        }
    });
}
{'fraction_non_alphanumeric': 0.1613804289061092, 'fraction_numerical': 0.006397549107947378, 'mean_word_length': 2.0258996728462377, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '2453988', 'n_tokens_mistral': 3498, 'n_tokens_neox': 3365, 'n_words': 619}