var BUGZILLA_URL = 'https://bugs.gentoo.org'; function destroyDatatable(){ while (window.dataTables.length !== 0) { window.dataTables.pop().destroy(); } } function initDatatable(){ if (window.dataTables.length === 0 && $('.data-table').length !== 0) { $('.data-table').each((_, element) => { var table = $(element).DataTable( { "processing": true, "serverSide": true, "ajax": "/cve/data", "order": [[ 0, "desc" ]], "columnDefs": [ { "render": function ( data, type, row ) { return '' + data + '' }, "targets" : 'render-bold', }, { "render": function ( data, type, row ) { var bugs = ' no assigned bugs'; if(data!='' && JSON.parse(data) != null){ bugs = ""; JSON.parse(data).forEach(function(bug) { bugs = bugs + bug.id + ","; }); } return bugs; }, "targets" : 'render-bug', }, { "targets" : 'no-sort', "orderable": false, }, { "render": function ( data, type, row ) { return renderState(data, row[0]) }, "targets" : 'render-state', }, { "render": function ( data, type, row ) { if(data == "0.0"){ return 'None' }else if(parseFloat(data) < 4.0) { return '' + data + '' }else if(parseFloat(data) < 7.0) { return '' + data + '' }else if(parseFloat(data) < 9.0) { return '' + data + '' }else if(parseFloat(data) < 10.0) { return '' + data + '' } return }, "targets" : 'render-basescore', }, { "targets": 'hide', "visible": false }], buttons: [ { extend: 'colvis', columns: ':not(.noVis)', text: 'Columns', className: 'btn-sm btn-outline-secondary colvis-btn' }, { text: 'Fullscreen', className: 'btn-sm btn-outline-secondary colvis-btn fullscreen-btn', action: function ( e, dt, node, config ) { if(window.location.href.includes("fullscreen")){ Turbolinks.visit("/cve/tool"); } else { Turbolinks.visit("/cve/tool/fullscreen"); } } }, { text: 'New', className: 'btn-sm btn-outline-secondary float-left colvis-btn new-btn', action: function ( e, dt, node, config ) { Turbolinks.visit("/cve/new"); } }, { text: 'State', className: 'btn-sm btn-outline-secondary float-left colvis-btn mr-2 dropdown-toggle view-filter-state' } ], "initComplete": function( settings, json ) { $('#table_id_length').append( " Show " ); table.buttons().container() .appendTo( $('#table_id_length') ); $('.buttons-colvis').removeClass("btn-secondary"); $('#table_id_length').append( " Toggle " ); $('.fullscreen-btn').appendTo( $('#table_id_length') ); $('#table_id_filter').prepend( '' ); $('#table_id_filter').prepend( " Filter by " ); $('.view-filter-state').prependTo( $('#filterByStateDropdown') ); document.querySelector(".view-filter-state").setAttribute('data-toggle', 'dropdown'); $('#table_id_filter').prepend( $('.new-btn') ); $('#table_id_filter').prepend( " Create " ); $("#filterByStateNew").on('click', function () { $('.view-filter-state').text("New"); table.columns( 10 ).search( "New" ).draw(); }); $("#filterByStateAssigned").on('click', function () { $('.view-filter-state').text("Assigned"); table.columns( 10 ).search( "Assigned" ).draw(); }); $("#filterByStateNFU").on('click', function () { $('.view-filter-state').text("NFU"); table.columns( 10 ).search( "NFU" ).draw(); }); $("#filterByStateLater").on('click', function () { $('.view-filter-state').text("Later"); table.columns( 10 ).search( "Later" ).draw(); }); $("#filterByStateInvalid").on('click', function () { $('.view-filter-state').text("Invalid"); table.columns( 10 ).search( "Invalid" ).draw(); }); $("#filterByStateAll").on('click', function () { $('.view-filter-state').text("State"); table.columns( 10 ).search( "" ).draw(); }); }, }); window.dataTables.push(table); // Add event listener for opening and closing details $('#table_id tbody').on('click', 'td', function () { var tr = $(this).closest('tr'); var row = table.row( tr ); if ( row.child.isShown() ) { // This row is already open - close it row.child.hide(); tr.removeClass('shown'); } else { // Open this row row.child( format(row.data()) ).show(); tr.addClass('shown'); registerCommentListener(); registerAssignBugListener(); registerChangeStateListener(); } } ); }); } } function renderState( d, cveid ) { if(d == "New"){ return '' + d + '' }else if(d == "Assigned") { return '' + d + '' }else if(d == "NFU") { return '' + d + '' }else if(d == "Later") { return '' + d + '' }else if(d == "Invalid") { return '' + d + '' }else{ return '' + d + '' } return d } function format ( d ) { var bugs = ' no assigned bugs'; if(d[3]!='' && JSON.parse(d[3]) != null){ bugs = ""; JSON.parse(d[3]).forEach(function(bug) { bugs = bugs + bug.id + ","; }); } var packages = ' no assigned packages'; if(d[2]!=''){ packages = d[2]; } var comments = '
- no comments yet -
'; if(d[7]!='null') { console.log("CommentsObject"); console.log(d[7]); console.log(JSON.parse(d[7])); var commentsObjects = JSON.parse(d[7]); comments = ''; commentsObjects.forEach(function (comment, index) { var commentDate = '' + comment.Date.split("T")[0] + ' ' + comment.Date.split("T")[1].split(".")[0] + ' UTC'; comments = comments + '
' + comment.User.Name + '
' + commentDate + '
' + comment.Message + '
'; }); } var bugs_cards = '
- no assigned bugs yet -
'; if(d[3]!='' && JSON.parse(d[3]) != null) { bugs_cards = ''; JSON.parse(d[3]).forEach(function(bug) { var newBug = '
Bug ' + bug.id + '
' + escapeHtml(bug.summary) + '
Alias: ' + bug.alias.join(", ") + '
Status: ' + escapeHtml(bug.status) + '
Whiteboard: ' + escapeHtml(bug.whiteboard) + '
Created: ' + escapeHtml(bug.creation_time) + '
'; bugs_cards = bugs_cards + newBug; }); } var changes = 'no changes yet'; // `d` is the original data object for the row return '
' + '
' + '

Details of ' + d[0] + '

' + '
' + '' + d[1] + '' + '
' + bugs_cards + '
' + ''+ ''+ '
' + (window.userCanAssignBug ? '
' : '') + '
'+ // '
' + '
' + // '
Comments
' + comments + '
' + ''+ '
' + (window.userCanComment ? '
' : '') + '
'+ '
'+ '
' + ''+ ''+ '' + ''+ ''+ ''+ ''+ ''+ ''+ ''+ // ''+ // ''+ '
State:'+ renderState(d[10], d[0]) + ( (window.userCanChangeState && d[10] != 'Assigned') ? '' : '') + '
Last Modified:'+ d[8] + '
Published:'+ d[9] + '
Base Score:'+ d[4] + '
Impact:'+ d[5] + '
Bug(s):'+ bugs + '
Package(s):'+ packages + '
Reference(s):'+ d[6] + '
Comments:'+ d[7] + '
Changelog:'+ changes + '
'+ '
'+ '
'+ '
'; } $( "#disable-twofactor-notice" ).click(function() { $("#twofactor-notice").hide(); $.get( "/account/2fa/notice/disable", function( data ) { console.log("Disabled 2FA Notice.") }); }); function registerCommentListener(){ $( ".trigger-new-comment" ).click(function() { $('.new-comment-row[data-cveid="' + $(this).data( "cveid" ) + '"]').show(); $('.trigger-new-comment[data-cveid="' + $(this).data( "cveid" ) + '"]').hide(); }); $( ".btn-cancel-comment" ).click(function() { $('.new-comment-row[data-cveid="' + $(this).data( "cveid" ) + '"]').hide(); $('.trigger-new-comment[data-cveid="' + $(this).data( "cveid" ) + '"]').show(); }); $( ".save-new-comment" ).click(function() { var cveid = $(this).data( "cveid" ); var comment = $('textarea.new-comment[data-cveid="' + cveid + '"]').val(); $.post( '/cve/comment/add', { cveid: cveid, comment: comment, }, function(data) { if(data != "err") { console.log("hi"); console.log(data); var comment = JSON.parse(data); var commentDate = '' + comment.Date.split("T")[0] + ' ' + comment.Date.split("T")[1].split(".")[0] + ' UTC'; var newComment = '
' + comment.User.Name + '
' + commentDate + '
' + comment.Message + '
'; $('.comments-section[data-cveid="' + cveid + '"]').append(newComment); } return }); }); } function registerAssignBugListener(){ $( ".trigger-assign-bug" ).click(function() { var cveid = $(this).data( "cveid" ); showAssignBugForm(cveid); }); $( ".btn-cancel-assign-bug" ).click(function() { var cveid = $(this).data( "cveid" ); hideAssignBugForm(cveid); }); $( ".btn-save-assign-bug" ).click(function() { var cveid = $(this).data( "cveid" ); var bugid = $('input.assign-bug[data-cveid="' + cveid + '"]').val(); assignBug(cveid, bugid); hideAssignBugForm(cveid); }); } function showAssignBugForm(cveid){ $('.assign-bug-row[data-cveid="' + cveid + '"]').show(); $('.trigger-assign-bug[data-cveid="' + cveid + '"]').hide(); $('.trigger-new-bug[data-cveid="' + cveid + '"]').hide(); } function hideAssignBugForm(cveid) { $('.assign-bug-row[data-cveid="' + cveid + '"]').hide(); $('.trigger-assign-bug[data-cveid="' + cveid + '"]').show(); $('.trigger-new-bug[data-cveid="' + cveid + '"]').show(); } function assignBug(cveid, bugid){ $.post( '/cve/bug/assign', { cveid: cveid, bugid: bugid, }, function(data) { if(data != "err") { console.log("hi"); console.log(data); if(data == "ok"){ setStateToAssigned(cveid); var newBug = '
Bug ' + bugid + '
Loading...
Alias:
Status:
Resolution:
Whiteboard:
Created:
Last Update:
'; $('.bugs-section[data-cveid="' + cveid + '"]').append(newBug); updateBugInformation(cveid, bugid); } } }); } function registerChangeStateListener(){ $( ".change-state-nfu" ).click(function() { var cveid = $(this).data( "cveid" ); var reason = $('.change-state-reason[data-cveid="' + cveid + '"]').val(); if(reason != "") { changeState(cveid, reason, "NFU"); $("#collapseExample-" + cveid).removeClass('show'); }else{ $('.change-state-reason[data-cveid="' + cveid + '"]').addClass('is-invalid'); } }); $( ".change-state-invalid" ).click(function() { var cveid = $(this).data( "cveid" ); var reason = $('.change-state-reason[data-cveid="' + cveid + '"]').val(); if(reason != "") { changeState(cveid, reason, "Invalid"); $("#collapseExample-" + cveid).removeClass('show'); }else{ $('.change-state-reason[data-cveid="' + cveid + '"]').addClass('is-invalid'); } }); $( ".change-state-later" ).click(function() { var cveid = $(this).data( "cveid" ); var reason = $('.change-state-reason[data-cveid="' + cveid + '"]').val(); if(reason != ""){ changeState(cveid, reason, "Later"); $("#collapseExample-" + cveid).removeClass('show'); }else{ $('.change-state-reason[data-cveid="' + cveid + '"]').addClass('is-invalid'); } }); } function changeState(cveid, reason, newState){ $.post( '/cve/state/change', { cveid: cveid, newstate: newState, reason: reason, }, function(data) { if(data != "err") { console.log("hi"); console.log(data); // change state setStateTo(cveid, newState); // add comment var comment = JSON.parse(data); var commentDate = '' + comment.Date.split("T")[0] + ' ' + comment.Date.split("T")[1].split(".")[0] + ' UTC'; var newComment = '
' + comment.User + '
' + commentDate + '
' + escapeHtml(comment.Message) + '
'; $('.comments-section[data-cveid="' + cveid + '"]').append(newComment); } }); } function setStateToAssigned(cveid) { setStateTo(cveid, "Assigned"); } function setStateTo(cveid, newState) { $('.state[data-cveid="' + cveid + '"]').removeClass('badge-primary'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-secondary'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-success'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-danger'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-warning'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-info'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-light'); $('.state[data-cveid="' + cveid + '"]').removeClass('badge-dark'); if(newState == "New"){ $('.state[data-cveid="' + cveid + '"]').addClass('badge-danger'); } else if(newState == "Assigned"){ $('.state[data-cveid="' + cveid + '"]').addClass('badge-success'); } else if(newState == "NFU"){ $('.state[data-cveid="' + cveid + '"]').addClass('badge-info'); } else if(newState == "Later"){ $('.state[data-cveid="' + cveid + '"]').addClass('badge-warning'); } else if(newState == "Invalid"){ $('.state[data-cveid="' + cveid + '"]').addClass('badge-dark'); } else { $('.state[data-cveid="' + cveid + '"]').addClass('badge-primary'); } $('.state[data-cveid="' + cveid + '"]').html(newState); } function updateBugInformation(cveid, bugid){ $.getJSON( BUGZILLA_URL + "/rest/bug?id=" + bugid, function( data ) { console.log(data.bugs[0]); console.log(escapeHtml(data.bugs[0].alias.join(", "))); console.log(escapeHtml(data.bugs[0].status)); console.log(escapeHtml(data.bugs[0].resolution)); console.log(escapeHtml(data.bugs[0].whiteboard)); console.log(escapeHtml(data.bugs[0]['creation_time'])); console.log(escapeHtml(data.bugs[0]['last_change_time'])); $('.bug-title[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html(escapeHtml(data.bugs[0].summary)); $('.bug-alias[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html('' + escapeHtml(data.bugs[0].alias.join(", ")) + ''); $('.bug-status[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html('' + escapeHtml(data.bugs[0].status) + ''); $('.bug-resolution[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html('' + escapeHtml(data.bugs[0].resolution) + ''); $('.bug-whiteboard[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html('' + escapeHtml(data.bugs[0].whiteboard) + ''); $('.bug-created[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html('' + escapeHtml(data.bugs[0]['creation_time']) + ''); $('.bug-last-update[data-cveid="' + cveid + '"][data-bugid="' + bugid + '"]').html('' + escapeHtml(data.bugs[0]['last_change_time']) + ''); }); } function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } export default {initDatatable, destroyDatatable}