Skip to content

Commit

Permalink
#28: Added ability to export components to a DejaCode Product using t…
Browse files Browse the repository at this point in the history
…he ProductComponent API. Added component summary table for components that will be uploaded to DejaCode.
  • Loading branch information
jdaguil committed Dec 16, 2016
1 parent 5915ca0 commit c65c017
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 11 deletions.
6 changes: 5 additions & 1 deletion assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,8 @@ div.dataTables_scrollHead th:first-child { /*For rounded table border*/
width: 75%;
height: 75%;
padding: 10px 0;
}
}

/*////// Component Export //////*/

#export-input { width: 550px;}
101 changes: 101 additions & 0 deletions assets/js/export-to-dejacode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
#
# Copyright (c) 2016 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# AboutCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
*/


// Converts array of components from AboutCode Manager to DejaCode component
// format
toDejaCodeFormat = function(components) {
return $.map(components, function(component, index) {
var name = component['name'];

if (!name) {
throw new Error('Name required for component.');
}

var owner = component['party']['name'];

if (!owner) {
throw new Error('Owner required for component: ' + name);
}

var license_expression = component['license_expression'];

if (!license_expression) {
throw new Error('License expression required for component: '
+ name);
}

var newComponent = {
name: name,
version: component['version'],
license_expression: license_expression,
owner: owner,
// DejaCode API expects a single copyright
copyright: component['copyrights'].join('\n')
};

if ('homepage_url' in component) {
newComponent.homepage_url = component['homepage_url'];
}

if ('programming_language' in component) {
newComponent.primary_language = component['programming_language'];
}

if ('notes' in component) {
newComponent.reference_notes = component['notes'];
}
return newComponent;
})
}

module.exports = toDejaCodeFormat;


// Uses DejaCode API to create a component
function createComponent (productComponentUrl, component, apiKey) {
var headers = {
'Authorization': 'Token ' + apiKey,
'Accept': 'application/json; indent=4'
};

return $.ajax({
type: 'POST',
headers: headers,
url: productComponentUrl,
data: component
});
}


// Upload created Components to a Product in DejaCode using the API
function uploadComponents(host, components, apiKey, productNameVersion) {
var dejaCodeComponents = toDejaCodeFormat(components);

$.each(dejaCodeComponents, function (index, component) {
component['product'] = productNameVersion;
})

$.each(dejaCodeComponents, function( index, component ) {
createComponent(host, component, apiKey)
.done(function (data) {
console.log('Successfully exported: ' + JSON.stringify(data));
})
.fail(function(error) {
console.log(error);
});
});
}
83 changes: 78 additions & 5 deletions assets/js/scancode.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ $(document).ready(function () {
"scrollX": true,
"scrollY": 700,
"stateSave": true,
deferRender: true,
"deferRender": true,
"buttons": [
{ // Do not allow the first column to be hidden
extend: 'colvis',
Expand Down Expand Up @@ -390,22 +390,83 @@ $(document).ready(function () {
"<'row'<'col-sm-5'i><'col-sm-7'p>>"
});

// Show DataTable and hide node view
// Create a table with analyzed Components from node view
var componentsTable = $('#components-table')
.DataTable( {
columns: [
{
data: 'name',
title: 'Name',
name: 'name'
},
{
data: 'version',
title: 'Version',
name: 'version'
},
{
data: 'party.name',
title: 'Owner',
name: 'party name',
defaultContent: ""
},
{
data: 'license_expression',
title: 'License',
name: 'license_expression',
defaultContent: ""
},
{
data: 'programming_language',
title: 'Programming Language',
name: 'programming_language',
defaultContent: ""
},
{
data: 'homepage_url',
title: 'Homepage URL',
name: 'homepage_url',
defaultContent: ""
},
{
data: 'notes',
title: 'Notes',
name: 'notes',
defaultContent: ""
}
],
"scrollX": true
});

// Show DataTable. Hide node view and component summary table
$( "#show-datatable" ).click(function() {
$("table").show();
$("#clues-table").show();
$("#node-container").hide();
$("#clues-table_wrapper").show();
$("#component-container").hide();
table.draw();
});

// Show node view and hide DataTable
// Show node view. Hide DataTable and component summary table
$("#show-tree").click(function() {
$("#node-container").show();
$( "table" ).hide();
$("#clues-table").hide();
$("#clues-table_wrapper").hide();
$("#component-container").hide();
nodeview.redraw();
});

// Show component summary table. Hide DataTable and node view
$("#table-test").click(function() {
$("#component-container").show();
$("#clues-table").hide();
$("#node-container").hide();
$("#clues-table_wrapper").hide();
componentsTable.clear();
componentsTable.rows.add(scanData.toSaveFormat().components);
componentsTable.draw();
});

// Open a json file
var dialog = require('electron').remote.dialog;
$( "#open-file" ).click(function() {
Expand All @@ -432,6 +493,18 @@ $(document).ready(function () {
});
});

// Submit components to a DejaCode Product via ProductComponent API
$('#componentSubmit').on('click', function () {
var createdComponents = scanData.toSaveFormat().components;
// Get product name and version
var productNameVersion = $('#product-name').val()
.concat(":", $('#product-version').val());
var apiUrl = $('#api-url').val();
var apiKey = $('#export-input').val();
uploadComponents( apiUrl, createdComponents, apiKey, productNameVersion );
$('#componentExportModal').modal('hide');
});

// Make node view modal box draggable
$("#nodeModal").draggable({ handle: ".modal-header" });

Expand Down
3 changes: 2 additions & 1 deletion assets/js/scandata.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// and handles the formatting for the jstree and the node view formats.


function ScanData(json) { // Load json file and other options
function ScanData(json) {
// Load json file and other options
// Save the scan data so it can be used elsewhere in the class
this.json = json;
this.jsTreeData = this.toJSTreeFormat(this.files());
Expand Down
47 changes: 46 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<script src="assets/DataTables/Scroller-1.4.1/js/dataTables.scroller.js"></script>
<script src="assets/DataTables/Select-1.1.2/js/dataTables.select.js"></script>
<script src="assets/js/jquery.jeditable.js"></script>
<script src="assets/js/export-to-dejacode.js"></script>
<script src="assets/js/nodeview.js"></script>
<script src="assets/js/select2.js"></script>
</head>
Expand All @@ -68,7 +69,13 @@
<button class="btn btn-sidebar" id="show-datatable" data-toggle="tooltip" title="Table view"><i class="fa fa-table" aria-hidden="true"></i></button>
</li>
<li>
<button class="btn btn-sidebar" id="show-tree" data-toggle="tooltip" title="Conclusions view"><i class="fa fa-check" aria-hidden="true"></i></button>
<button class="btn btn-sidebar" id="show-tree" data-toggle="tooltip" title="Conclusions view"><i class="fa fa-code-fork" aria-hidden="true"></i></button>
</li>
<li>
<button class="btn btn-sidebar" id="table-test" data-toggle="modal" data-placement="right" title="Import Components to DejaCode"><i class="fa fa-list-ol" aria-hidden="true"></i></button>
</li>
<li>
<button class="btn btn-sidebar" id="show-components" data-toggle="modal" data-placement="right" title="Components Summary" data-target="#componentExportModal"><i class="fa fa-cloud-upload" aria-hidden="true"></i></button>
</li>
<li>
<button class="btn btn-sidebar" id="show-help" data-toggle="modal" data-placement="right" title="Help with Application" data-target="#helpModal"><i class="fa fa-question" aria-hidden="true"></i></button>
Expand All @@ -83,6 +90,10 @@
<div id="tabbar" class="col-md-9">
<table id="clues-table" class="display table table-striped table-bordered dataTable no-footer" cellspacing="0" width="100%" >
</table>
<div id="component-container">
<table id="components-table" class="display table table-striped table-bordered dataTable no-footer" cellspacing="0" width="100%" >
</table>
</div>
<div class="node-hide" id="node-container">
<select id="node-drop-down" style="border-color: #eee; border-radius: 5px; padding: 5px;" multiple>
<option value="filename" selected>File Name</option>
Expand Down Expand Up @@ -151,6 +162,40 @@ <h3 class="modal-title" id="nodeModalLabel">Modal title</h3>
</div>
</div>
</div>

<!--Component Modal -->
<div class="modal fade" id="componentExportModal" tabindex="-1" role="dialog" aria-labelledby="componentExportModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Upload Components to DejaCode</h4>
</div>
<div class="modal-body">
<form class="form-inline">
<div class="form-group">
<h4>Product Information</h4>
<p> Enter the Product Name and Version you want the components to be added to</p>
<input type="text" class="form-control" id="product-name" placeholder="Product Name" required>
<input type="text" class="form-control" id="product-version" placeholder="Product Version" required>

<h4>DejaCode API URL</h4>
<p> Enter the URL (For example, https://enterprise.dejacode.com)</p>
<input type="text" class="form-control" id="api-url" placeholder="DejaCode API URL" required>

<h4>Please provide your API key</h4>
<p> Your API key can be found in your DejaCode Profile Settings</p>
<input type="password" class="form-control" id="export-input" placeholder="DejaCode API Key" required>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="componentSubmit">Submit</button>
</div>
</div>
</div>
</div>

<!-- Help Modal -->
<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel">
<div class="modal-dialog modal-lg" role="document">
Expand Down
Loading

0 comments on commit c65c017

Please sign in to comment.