<commit_before>function octane_job_configuration(target, progress, proxy) {

    if (typeof jQuery === 'undefined') {
        return {
            configure: function() {
                target.innerHTML = "JQuery plugin must be installed and enabled in <a href=" + rootURL + "/pluginManager>Plugin Manager</a>";
            }
        }
    }

    var originalUnload = window.onbeforeunload;

    var fieldSelectors = [];

    var $ = jQuery;

    function caseInsensitiveStringEquals(left, right) {
        // TODO: janotav: no easy way to do this in JS (?), need to implement this properly
        return left.toLowerCase() === right.toLowerCase();
    }

    function configure() {
        progressFunc("Retrieving configuration from server");
        proxy.loadJobConfigurationFromServer(function (t) {
            progressFunc();
            var response = t.responseObject();
            if (response.errors) {
                response.errors.forEach(function (error) {
                    var errorDiv = $("<div class='error'><font color='red'><b/></font></div>");
                    errorDiv.find("b").text(error);
                    $(target).append(errorDiv);
                });
            } else {
                renderConfiguration(response);
            }
        });
    }

    function progressFunc(msg) {
        if (typeof msg === 'string') {
            $(progress).find("h3").text(msg);
            $(progress).show();
            $(target).hide();
        } else {
            $(progress).hide();
            $(target).show();
        }
    }

    function renderConfiguration(jobConfiguration, pipelineId) {
        var result = $(target);
        result.empty();

        var pipelineDiv = $("<div class='mqm'>");
        result.append(pipelineDiv);

        var buttons = $("<div>");
        var status = $("<div>");

        var selectedWorkspaceId;
        var tagTypes = {};
        var allTags = {};
        var tagTypesByName = {};

        var validators = [];
        var apply = [];
        var dirtyFlag;

        function initialize() {
            validators.length = 0;
            apply.length = 0;
            clearDirty();

            pipelineDiv.empty();
        }

        function addApplyButton(caption, pipeline, func, callback) {
            buttons.empty();
            buttons.append(status);
            var applyButton = $("<button>");
            applyButton.text(caption);
            applyButton.unbind('click').click(function() {
                saveConfiguration(pipeline, func, callback);
            });
            buttons.append(applyButton);
            pipelineDiv.append(buttons);
        }

        function renderPipelineMetadata(pipeline, pipelineSelector) {
            var table = $("<table class='ui-block'><tbody><tr/></tbody></table>");
            pipelineDiv.append(table);
            var tbody = table.find("tbody");
            var tr = tbody.find("tr");

            if (pipeline.id === null) { //rendering workspace selector - only for creating new pipeline
                var trWorkspace = $("<tr><td class='setting-name'><label for='pipeline-workspace'>Workspace:</label></td></tr>");
                tbody.append(trWorkspace);

                var tdWorkspaceSelect = $("<td class='setting-main'>");
                trWorkspace.append(tdWorkspaceSelect);
                var workspaceSelect = $("<select>");
                tdWorkspaceSelect.append(workspaceSelect);
                workspaceSelect.prop('id', 'workspaceSelect');

                var area = $("<div class='validation-error-area' id='workspace-selector-validation-area'/>");
                tdWorkspaceSelect.append(area);

                workspaceSelect.change(function () {
                    selectedWorkspaceId = workspaceSelect.val();
                    $(jq("workspace-selector-validation-area")).hide();
                    trRelease.css('visibility', 'visible');
                });

                var isWorkspaceUndefined = function() {
                    if (!workspaceSelect.val()) {
                        return "Workspace must be selected";
                    }
                    return false;
                };

                enableInputValidation(workspaceSelect, "", area, {
                    check: isWorkspaceUndefined
                });
                apply.push(function () {
                    pipeline.workspaceId = workspaceSelect.val();
                });
            } else {
                pipelineSelector.renderIfNecessary(tbody, pipeline.id);
            }

            var tdPipeline = $("<td class='setting-name'><label for='pipeline-name'>Pipeline:</label></td>");
            tr.append(tdPipeline);

            if (pipeline.isRoot) {
                var tdPipelineInput = $("<td class='setting-main' colspan='2'><input id='pipeline-name' type='text' placeholder='Pipeline name' class='setting-input' maxlength='50'/><div class='validation-error-area'/></td>");
                tr.append(tdPipelineInput);

                var input = tdPipelineInput.find("input");
                input.attr("value", pipeline.name);
                var area = tdPipelineInput.find("div");

                var pipelineNameValidation = function() {
                    if (!input.val()) {
                        return "Pipeline name must be specified";
                    }
                    // based on latest discussions all characters should be allowed
                    //if (!input.val().match(/^[a-z0-9 _-]+$/i)) {
                    //    return "Pipeline name contains invalid characters";
                    //}
                    return false;
                };

                apply.push(function() {
                    pipeline.name = input.val();
                });
                enableDirtyInputCheck(input);
                enableInputValidation(input, "", area, {
                    check: pipelineNameValidation
                });
            } else {
                var tdPipelineName = $("<td class='setting-main' colspan='2'/>");
                tr.append(tdPipelineName);

                tdPipelineName.append(pipeline.name); // CODE REVIEW Johnny: Is this from XSS, shouldn't we use text instead of append?
            }

            if (pipeline.isRoot) {
                var trRelease = $("<tr><td class='setting-name'><label for='pipeline-release'>Release:</label>");
                tbody.append(trRelease);

                var tdReleaseSelect = $("<td class='setting-main'>");
                trRelease.append(tdReleaseSelect);

                var select = $("<select>");
                select.append($("<option>").text("-- Not specified --").val(-1).attr('selected', !isReleaseSpecified(pipeline)));
                if (pipeline.id !== null && isReleaseSpecified(pipeline)) {
                    select.append($("<option>").text(pipeline.releaseName).val(pipeline.releaseId).attr('selected', 'true'));
                }
                apply.push(function () {
                    pipeline.releaseId = Number(select.val());
                    pipeline.releaseName = select.find(":selected").text();
                });
                enableDirtyChangeCheck(select);
                tdReleaseSelect.append(select);
                select.prop('id', 'releaseSelect');
                trRelease.append($("<td class='setting-new'/>"));
                if (pipeline.id == null) {
                    trRelease.css('visibility', 'hidden');
                }
            }
        }

        function renderNewPipeline(pipeline) {

            function createPipelineFunc(pipeline, callback) {
                proxy.createPipelineOnServer(pipeline, callback);
            }

            function createPipelineCallback(pipeline, response) {
                jobConfiguration.currentPipeline = response.pipeline;
                jobConfiguration.fieldsMetadata = response.fieldsMetadata;

                //inserting newly created pipeline properly into workspaces object
                var workspacePipelines = {};
                workspacePipelines[jobConfiguration.currentPipeline.id] = jobConfiguration.currentPipeline;
<commit_msg>user story #6462: preventing XSS in configuration UI<commit_after>function octane_job_configuration(target, progress, proxy) {

    if (typeof jQuery === 'undefined') {
        return {
            configure: function() {
                target.innerHTML = "JQuery plugin must be installed and enabled in <a href=" + rootURL + "/pluginManager>Plugin Manager</a>";
            }
        }
    }

    var originalUnload = window.onbeforeunload;

    var fieldSelectors = [];

    var $ = jQuery;

    function caseInsensitiveStringEquals(left, right) {
        // TODO: janotav: no easy way to do this in JS (?), need to implement this properly
        return left.toLowerCase() === right.toLowerCase();
    }

    function configure() {
        progressFunc("Retrieving configuration from server");
        proxy.loadJobConfigurationFromServer(function (t) {
            progressFunc();
            var response = t.responseObject();
            if (response.errors) {
                response.errors.forEach(function (error) {
                    var errorDiv = $("<div class='error'><font color='red'><b/></font></div>");
                    errorDiv.find("b").text(error);
                    $(target).append(errorDiv);
                });
            } else {
                renderConfiguration(response);
            }
        });
    }

    function progressFunc(msg) {
        if (typeof msg === 'string') {
            $(progress).find("h3").text(msg);
            $(progress).show();
            $(target).hide();
        } else {
            $(progress).hide();
            $(target).show();
        }
    }

    function renderConfiguration(jobConfiguration, pipelineId) {
        var result = $(target);
        result.empty();

        var pipelineDiv = $("<div class='mqm'>");
        result.append(pipelineDiv);

        var buttons = $("<div>");
        var status = $("<div>");

        var selectedWorkspaceId;
        var tagTypes = {};
        var allTags = {};
        var tagTypesByName = {};

        var validators = [];
        var apply = [];
        var dirtyFlag;

        function initialize() {
            validators.length = 0;
            apply.length = 0;
            clearDirty();

            pipelineDiv.empty();
        }

        function addApplyButton(caption, pipeline, func, callback) {
            buttons.empty();
            buttons.append(status);
            var applyButton = $("<button>");
            applyButton.text(caption);
            applyButton.unbind('click').click(function() {
                saveConfiguration(pipeline, func, callback);
            });
            buttons.append(applyButton);
            pipelineDiv.append(buttons);
        }

        function renderPipelineMetadata(pipeline, pipelineSelector) {
            var table = $("<table class='ui-block'><tbody><tr/></tbody></table>");
            pipelineDiv.append(table);
            var tbody = table.find("tbody");
            var tr = tbody.find("tr");

            if (pipeline.id === null) { //rendering workspace selector - only for creating new pipeline
                var trWorkspace = $("<tr><td class='setting-name'><label for='pipeline-workspace'>Workspace:</label></td></tr>");
                tbody.append(trWorkspace);

                var tdWorkspaceSelect = $("<td class='setting-main'>");
                trWorkspace.append(tdWorkspaceSelect);
                var workspaceSelect = $("<select>");
                tdWorkspaceSelect.append(workspaceSelect);
                workspaceSelect.prop('id', 'workspaceSelect');

                var area = $("<div class='validation-error-area' id='workspace-selector-validation-area'/>");
                tdWorkspaceSelect.append(area);

                workspaceSelect.change(function () {
                    selectedWorkspaceId = workspaceSelect.val();
                    $(jq("workspace-selector-validation-area")).hide();
                    trRelease.css('visibility', 'visible');
                });

                var isWorkspaceUndefined = function() {
                    if (!workspaceSelect.val()) {
                        return "Workspace must be selected";
                    }
                    return false;
                };

                enableInputValidation(workspaceSelect, "", area, {
                    check: isWorkspaceUndefined
                });
                apply.push(function () {
                    pipeline.workspaceId = workspaceSelect.val();
                });
            } else {
                pipelineSelector.renderIfNecessary(tbody, pipeline.id);
            }

            var tdPipeline = $("<td class='setting-name'><label for='pipeline-name'>Pipeline:</label></td>");
            tr.append(tdPipeline);

            if (pipeline.isRoot) {
                var tdPipelineInput = $("<td class='setting-main' colspan='2'><input id='pipeline-name' type='text' placeholder='Pipeline name' class='setting-input' maxlength='50'/><div class='validation-error-area'/></td>");
                tr.append(tdPipelineInput);

                var input = tdPipelineInput.find("input");
                input.attr("value", pipeline.name);
                var area = tdPipelineInput.find("div");

                var pipelineNameValidation = function() {
                    if (!input.val()) {
                        return "Pipeline name must be specified";
                    }
                    // based on latest discussions all characters should be allowed
                    //if (!input.val().match(/^[a-z0-9 _-]+$/i)) {
                    //    return "Pipeline name contains invalid characters";
                    //}
                    return false;
                };

                apply.push(function() {
                    pipeline.name = input.val();
                });
                enableDirtyInputCheck(input);
                enableInputValidation(input, "", area, {
                    check: pipelineNameValidation
                });
            } else {
                var tdPipelineName = $("<td class='setting-main' colspan='2'/>");
                tr.append(tdPipelineName);

                tdPipelineName.text(pipeline.name);
            }

            if (pipeline.isRoot) {
                var trRelease = $("<tr><td class='setting-name'><label for='pipeline-release'>Release:</label>");
                tbody.append(trRelease);

                var tdReleaseSelect = $("<td class='setting-main'>");
                trRelease.append(tdReleaseSelect);

                var select = $("<select>");
                select.append($("<option>").text("-- Not specified --").val(-1).attr('selected', !isReleaseSpecified(pipeline)));
                if (pipeline.id !== null && isReleaseSpecified(pipeline)) {
                    select.append($("<option>").text(pipeline.releaseName).val(pipeline.releaseId).attr('selected', 'true'));
                }
                apply.push(function () {
                    pipeline.releaseId = Number(select.val());
                });
                enableDirtyChangeCheck(select);
                tdReleaseSelect.append(select);
                select.prop('id', 'releaseSelect');
                trRelease.append($("<td class='setting-new'/>"));
                if (pipeline.id == null) {
                    trRelease.css('visibility', 'hidden');
                }
            }
        }

        function renderNewPipeline(pipeline) {

            function createPipelineFunc(pipeline, callback) {
                proxy.createPipelineOnServer(pipeline, callback);
            }

            function createPipelineCallback(pipeline, response) {
                jobConfiguration.currentPipeline = response.pipeline;
                jobConfiguration.fieldsMetadata = response.fieldsMetadata;

                //inserting newly created pipeline properly into workspaces object
                var workspacePipelines = {};
                workspacePipelines[jobConfiguration.currentPipeline.id] = jobConfiguration.currentPipeline;

{'fraction_non_alphanumeric': 0.130217645970775, 'fraction_numerical': 0.0014797459769406252, 'mean_word_length': 1.7164629040361747, 'pattern_counts': {'":': 1, '<': 89, '<?xml version=': 0, '>': 89, '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': '4227082', 'n_tokens_mistral': 3952, 'n_tokens_neox': 3775, 'n_words': 755}