aliaksei_tatarynchykepam.com / Security Impact Score Calculator

// ==UserScript==
// @name         Security Impact Score Calculator
// @namespace    http://tampermonkey.net/
// @include      https://*.atlassian.net/browse/*
// @version      0.3
// @description  This tool calculates security impact score automaticaly based on selected "Security - Affected Areas". Please note that you should first add 2 fields to Jira: "Security - Affected Areas" and "Security Impact Score". Also this version of script works only with Cloud version of Jira
// @author       Aliaksei Tatarynchyk
// @license MIT
// @copyright 2019, aliaksei_tatarynchykepam.com (https://openuserjs.org/users/aliaksei_tatarynchykepam.com)
// @grant        none
// ==/UserScript==

function initSecurityImpactScoreCalculator($) {
    const selectors = {
        affectedAreas: "#edit-issue-dialog label:contains(Security - Affected Areas) + select option, .jira-dialog-content label:contains(Security - Affected Areas) + select option",
        securityScore: "#edit-issue-dialog label:contains(Security Impact Score) + input, .jira-dialog-content label:contains(Security Impact Score) + input"
    }
    const impactScoreMapping = {
        "None":  -100,
        "No impact":  0,
        "Authentication / Authorization": 5,
        "Auditing, monitoring and alerting": 1,
        "Cryptography": 5,
        "New API": 2,
        "New pages": 1,
        "New 3rd party dependency": 1,
        "New data storage": 3,
        "New data flow": 3,
        "Handling sensitive data": 5,
        "Handling new type of data": 2
    };

    $('#edit-issue').on('click', function() {
        checkThatContentIsLoadedAndRunCallback("#edit-issue-dialog .content", function makeSecurityBlockNavigationEasier() {
            $(selectors.affectedAreas).prev().attr('id', 'security-area');
            $('#edit-issue-dialog .content').prepend("<a href=\"javascript:void(0)\" onclick=\"$('#security-area').get(0).scrollIntoView()\"><strong>Scroll to Security section</strong></a>");
        });
    });

    $('#jira').on('click', selectors.affectedAreas, function() {
        var $affectedAreasDropdown = $(this).parent();
        var $selectedAffectedAreas = $affectedAreasDropdown.find("option:selected");

        // validation that if 'None' is selected it is the only selected option
        var noAffectedAreasAreSelected = false;
        var isError = false;
        $selectedAffectedAreas.each(function(){
            if ($(this).text().trim() === 'None') {
                noAffectedAreasAreSelected = true;

                if ($selectedAffectedAreas.size() > 1) {
                    isError = true;
                }
            }
        });

        if (isError) {
            if ($affectedAreasDropdown.next('.errortext').size() == 0) {
                $affectedAreasDropdown.css('border', '2px solid red').after('<span class="errortext" style="color: red">"None" option can\'t be selected together with another option</span>')
            }
        } else {
            $affectedAreasDropdown.css('border', 'none').next('.errortext').remove();
        }
        if (noAffectedAreasAreSelected) {
            $(selectors.securityScore).val('');
            return;
        }

        var impactScore = $selectedAffectedAreas.map(function(i, e) {
            return $(e).text().trim();
        }).get().reduce(function(acc, val) {
            return acc + impactScoreMapping[val];
        }, 0);

        $(selectors.securityScore).val(impactScore);
    });

    var runsCount = 0;
    function checkThatContentIsLoadedAndRunCallback(contentSelector, callback) {
        if (runsCount++ > 15) {
            return;
        }

        if ($(contentSelector).size() > 0) {
            runsCount = 0;
            callback();
        } else {
            setTimeout(checkThatContentIsLoadedAndRunCallback, 500, contentSelector, callback);
        }
    }
}

(function() {
    if (typeof(jQuery) !== "undefined") {
        initSecurityImpactScoreCalculator(jQuery);
    } else {
        getScript('https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js', function() {
            initSecurityImpactScoreCalculator(jQuery);
        });
    }

    function getScript(url, success) {
        var script = document.createElement('script');
        script.src = url;
        var head = document.getElementsByTagName('head')[0],
            done = false;
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function() {
            if (!done && (!this.readyState
                          || this.readyState == 'loaded'
                          || this.readyState == 'complete')) {
                done = true;
                success();
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);
            }
        };
        head.appendChild(script);
    }
})();