<commit_before>require('array.prototype.find');

var truncate = function(string) {
    var MAX_LENGTH = 80;

    if (string.length > MAX_LENGTH) {
        return string.substring(0, MAX_LENGTH) + ' [...]'
    }

    return string;
};

var getPossiblyUndefinedKeyValue = function (obj, keySequence) {
    var keys = keySequence.split('.');
    while (obj && keys.length) obj = obj[keys.shift()];

    return obj || undefined;
};

var extractPrData = function (pr) {
    // BitBucket api is not returning the PR url for all actions so far (2014-09-26, only create action)
    var getKey = getPossiblyUndefinedKeyValue.bind(this, pr);

    var data = {
        prAuthor            : getKey('author.display_name'),
        prUrl               : getKey('links.html.href'),
        prTitle             : getKey('title'),

        user                : getKey('user.display_name'),

        repoName            : getKey('source.repository.name'),
        repoSourceName      : getKey('source.branch.name'),
        repoDestinationName : getKey('destination.branch.name'),

        reason              : getKey('reason'),
        state               : getKey('state'),
        description         : getKey('description'),

        commentUrl          : getKey('links.html.href'),
        commentContentRaw   : getKey('content.raw')
    };

    return data;
};

module.exports = function () {
    var spice = require('spice');

    var COLORS = {
        red: '#ff0000',
        green: '#00ff00',
        blue: '#0000ff',
        yellow: '#ffff00'
    };

    var baseHandler = function (pr) {
        var data = extractPrData(pr);

        var result = {
            fields: [{
                title: data.prTitle,
                value: 'State: ' + data.state + '.',
                short: true
            }, {
                title: 'Project:',
                value: data.repoName + ' (' + data.repoSourceName + ' → ' + data.repoDestinationName + ')',
                short: true
            }]
        };

        if (data.reason) {
            result.fields.push({
                title: 'Reason:',
                value: data.reason
            });
        }

        return { result: result, data: data };

    };

    var messageHandlers = {
        created: function (pr) {
            var data = extractPrData(pr);

            var result = {
                fallback: 'PR *created* by ' + data.prAuthor + ' for ' + data.repoName + ':',
                color: COLORS.blue,
                fields: [{
                    title: '<' + data.prUrl + '|' + data.prTitle + '>',
                    value: data.repoSourceName + ' -> ' + data.repoDestinationName
                }, {
                    title: 'Project:',
                    value: data.repoName + ' (' + data.repoSourceName + ' → ' + data.repoDestinationName + ')',
                    short: true
                }]
            };
<commit_msg>Fixing html link for created action<commit_after>require('array.prototype.find');

var truncate = function(string) {
    var MAX_LENGTH = 80;

    if (string.length > MAX_LENGTH) {
        return string.substring(0, MAX_LENGTH) + ' [...]'
    }

    return string;
};

var getPossiblyUndefinedKeyValue = function (obj, keySequence) {
    var keys = keySequence.split('.');
    while (obj && keys.length) obj = obj[keys.shift()];

    return obj || undefined;
};

var extractPrData = function (pr) {
    // BitBucket api is not returning the PR url for all actions so far (2014-09-26, only create action)
    var getKey = getPossiblyUndefinedKeyValue.bind(this, pr);

    var data = {
        prAuthor            : getKey('author.display_name'),
        prUrl               : getKey('links.html.href'),
        prTitle             : getKey('title'),

        user                : getKey('user.display_name'),

        repoName            : getKey('source.repository.name'),
        repoSourceName      : getKey('source.branch.name'),
        repoDestinationName : getKey('destination.branch.name'),

        reason              : getKey('reason'),
        state               : getKey('state'),
        description         : getKey('description'),

        commentUrl          : getKey('links.html.href'),
        commentContentRaw   : getKey('content.raw')
    };

    return data;
};

module.exports = function () {
    var spice = require('spice');

    var COLORS = {
        red: '#ff0000',
        green: '#00ff00',
        blue: '#0000ff',
        yellow: '#ffff00'
    };

    var baseHandler = function (pr) {
        var data = extractPrData(pr);

        var result = {
            fields: [{
                title: data.prTitle,
                value: 'State: ' + data.state + '.',
                short: true
            }, {
                title: 'Project:',
                value: data.repoName + ' (' + data.repoSourceName + ' → ' + data.repoDestinationName + ')',
                short: true
            }]
        };

        if (data.reason) {
            result.fields.push({
                title: 'Reason:',
                value: data.reason
            });
        }

        return { result: result, data: data };

    };

    var messageHandlers = {
        created: function (pr) {
            var data = extractPrData(pr);

            var result = {
                fallback: '<' + data.prUrl + '|PR *created* by ' + data.prAuthor + '> for ' + data.repoName + ':',
                color: COLORS.blue,
                fields: [{
                    title: data.prTitle,
                    value: data.repoSourceName + ' -> ' + data.repoDestinationName
                }, {
                    title: 'Project:',
                    value: data.repoName + ' (' + data.repoSourceName + ' → ' + data.repoDestinationName + ')',
                    short: true
                }]
            };

{'fraction_non_alphanumeric': 0.13246396705559368, 'fraction_numerical': 0.008579272477693892, 'mean_word_length': 1.5521015761821366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3260422', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1559, 'n_words': 349}