Skip to content

Commit c65c017

Browse files
committed
#28: Added ability to export components to a DejaCode Product using the ProductComponent API. Added component summary table for components that will be uploaded to DejaCode.
1 parent 5915ca0 commit c65c017

File tree

6 files changed

+312
-11
lines changed

6 files changed

+312
-11
lines changed

assets/css/main.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,8 @@ div.dataTables_scrollHead th:first-child { /*For rounded table border*/
182182
width: 75%;
183183
height: 75%;
184184
padding: 10px 0;
185-
}
185+
}
186+
187+
/*////// Component Export //////*/
188+
189+
#export-input { width: 550px;}

assets/js/export-to-dejacode.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
#
3+
# Copyright (c) 2016 nexB Inc. and others. All rights reserved.
4+
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
5+
# The ScanCode software is licensed under the Apache License version 2.0.
6+
# AboutCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
*/
16+
17+
18+
// Converts array of components from AboutCode Manager to DejaCode component
19+
// format
20+
toDejaCodeFormat = function(components) {
21+
return $.map(components, function(component, index) {
22+
var name = component['name'];
23+
24+
if (!name) {
25+
throw new Error('Name required for component.');
26+
}
27+
28+
var owner = component['party']['name'];
29+
30+
if (!owner) {
31+
throw new Error('Owner required for component: ' + name);
32+
}
33+
34+
var license_expression = component['license_expression'];
35+
36+
if (!license_expression) {
37+
throw new Error('License expression required for component: '
38+
+ name);
39+
}
40+
41+
var newComponent = {
42+
name: name,
43+
version: component['version'],
44+
license_expression: license_expression,
45+
owner: owner,
46+
// DejaCode API expects a single copyright
47+
copyright: component['copyrights'].join('\n')
48+
};
49+
50+
if ('homepage_url' in component) {
51+
newComponent.homepage_url = component['homepage_url'];
52+
}
53+
54+
if ('programming_language' in component) {
55+
newComponent.primary_language = component['programming_language'];
56+
}
57+
58+
if ('notes' in component) {
59+
newComponent.reference_notes = component['notes'];
60+
}
61+
return newComponent;
62+
})
63+
}
64+
65+
module.exports = toDejaCodeFormat;
66+
67+
68+
// Uses DejaCode API to create a component
69+
function createComponent (productComponentUrl, component, apiKey) {
70+
var headers = {
71+
'Authorization': 'Token ' + apiKey,
72+
'Accept': 'application/json; indent=4'
73+
};
74+
75+
return $.ajax({
76+
type: 'POST',
77+
headers: headers,
78+
url: productComponentUrl,
79+
data: component
80+
});
81+
}
82+
83+
84+
// Upload created Components to a Product in DejaCode using the API
85+
function uploadComponents(host, components, apiKey, productNameVersion) {
86+
var dejaCodeComponents = toDejaCodeFormat(components);
87+
88+
$.each(dejaCodeComponents, function (index, component) {
89+
component['product'] = productNameVersion;
90+
})
91+
92+
$.each(dejaCodeComponents, function( index, component ) {
93+
createComponent(host, component, apiKey)
94+
.done(function (data) {
95+
console.log('Successfully exported: ' + JSON.stringify(data));
96+
})
97+
.fail(function(error) {
98+
console.log(error);
99+
});
100+
});
101+
}

assets/js/scancode.js

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ $(document).ready(function () {
340340
"scrollX": true,
341341
"scrollY": 700,
342342
"stateSave": true,
343-
deferRender: true,
343+
"deferRender": true,
344344
"buttons": [
345345
{ // Do not allow the first column to be hidden
346346
extend: 'colvis',
@@ -390,22 +390,83 @@ $(document).ready(function () {
390390
"<'row'<'col-sm-5'i><'col-sm-7'p>>"
391391
});
392392

393-
// Show DataTable and hide node view
393+
// Create a table with analyzed Components from node view
394+
var componentsTable = $('#components-table')
395+
.DataTable( {
396+
columns: [
397+
{
398+
data: 'name',
399+
title: 'Name',
400+
name: 'name'
401+
},
402+
{
403+
data: 'version',
404+
title: 'Version',
405+
name: 'version'
406+
},
407+
{
408+
data: 'party.name',
409+
title: 'Owner',
410+
name: 'party name',
411+
defaultContent: ""
412+
},
413+
{
414+
data: 'license_expression',
415+
title: 'License',
416+
name: 'license_expression',
417+
defaultContent: ""
418+
},
419+
{
420+
data: 'programming_language',
421+
title: 'Programming Language',
422+
name: 'programming_language',
423+
defaultContent: ""
424+
},
425+
{
426+
data: 'homepage_url',
427+
title: 'Homepage URL',
428+
name: 'homepage_url',
429+
defaultContent: ""
430+
},
431+
{
432+
data: 'notes',
433+
title: 'Notes',
434+
name: 'notes',
435+
defaultContent: ""
436+
}
437+
],
438+
"scrollX": true
439+
});
440+
441+
// Show DataTable. Hide node view and component summary table
394442
$( "#show-datatable" ).click(function() {
395-
$("table").show();
443+
$("#clues-table").show();
396444
$("#node-container").hide();
397445
$("#clues-table_wrapper").show();
446+
$("#component-container").hide();
398447
table.draw();
399448
});
400449

401-
// Show node view and hide DataTable
450+
// Show node view. Hide DataTable and component summary table
402451
$("#show-tree").click(function() {
403452
$("#node-container").show();
404-
$( "table" ).hide();
453+
$("#clues-table").hide();
405454
$("#clues-table_wrapper").hide();
455+
$("#component-container").hide();
406456
nodeview.redraw();
407457
});
408458

459+
// Show component summary table. Hide DataTable and node view
460+
$("#table-test").click(function() {
461+
$("#component-container").show();
462+
$("#clues-table").hide();
463+
$("#node-container").hide();
464+
$("#clues-table_wrapper").hide();
465+
componentsTable.clear();
466+
componentsTable.rows.add(scanData.toSaveFormat().components);
467+
componentsTable.draw();
468+
});
469+
409470
// Open a json file
410471
var dialog = require('electron').remote.dialog;
411472
$( "#open-file" ).click(function() {
@@ -432,6 +493,18 @@ $(document).ready(function () {
432493
});
433494
});
434495

496+
// Submit components to a DejaCode Product via ProductComponent API
497+
$('#componentSubmit').on('click', function () {
498+
var createdComponents = scanData.toSaveFormat().components;
499+
// Get product name and version
500+
var productNameVersion = $('#product-name').val()
501+
.concat(":", $('#product-version').val());
502+
var apiUrl = $('#api-url').val();
503+
var apiKey = $('#export-input').val();
504+
uploadComponents( apiUrl, createdComponents, apiKey, productNameVersion );
505+
$('#componentExportModal').modal('hide');
506+
});
507+
435508
// Make node view modal box draggable
436509
$("#nodeModal").draggable({ handle: ".modal-header" });
437510

assets/js/scandata.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// and handles the formatting for the jstree and the node view formats.
1919

2020

21-
function ScanData(json) { // Load json file and other options
21+
function ScanData(json) {
22+
// Load json file and other options
2223
// Save the scan data so it can be used elsewhere in the class
2324
this.json = json;
2425
this.jsTreeData = this.toJSTreeFormat(this.files());

index.html

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<script src="assets/DataTables/Scroller-1.4.1/js/dataTables.scroller.js"></script>
5050
<script src="assets/DataTables/Select-1.1.2/js/dataTables.select.js"></script>
5151
<script src="assets/js/jquery.jeditable.js"></script>
52+
<script src="assets/js/export-to-dejacode.js"></script>
5253
<script src="assets/js/nodeview.js"></script>
5354
<script src="assets/js/select2.js"></script>
5455
</head>
@@ -68,7 +69,13 @@
6869
<button class="btn btn-sidebar" id="show-datatable" data-toggle="tooltip" title="Table view"><i class="fa fa-table" aria-hidden="true"></i></button>
6970
</li>
7071
<li>
71-
<button class="btn btn-sidebar" id="show-tree" data-toggle="tooltip" title="Conclusions view"><i class="fa fa-check" aria-hidden="true"></i></button>
72+
<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>
73+
</li>
74+
<li>
75+
<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>
76+
</li>
77+
<li>
78+
<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>
7279
</li>
7380
<li>
7481
<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>
@@ -83,6 +90,10 @@
8390
<div id="tabbar" class="col-md-9">
8491
<table id="clues-table" class="display table table-striped table-bordered dataTable no-footer" cellspacing="0" width="100%" >
8592
</table>
93+
<div id="component-container">
94+
<table id="components-table" class="display table table-striped table-bordered dataTable no-footer" cellspacing="0" width="100%" >
95+
</table>
96+
</div>
8697
<div class="node-hide" id="node-container">
8798
<select id="node-drop-down" style="border-color: #eee; border-radius: 5px; padding: 5px;" multiple>
8899
<option value="filename" selected>File Name</option>
@@ -151,6 +162,40 @@ <h3 class="modal-title" id="nodeModalLabel">Modal title</h3>
151162
</div>
152163
</div>
153164
</div>
165+
166+
<!--Component Modal -->
167+
<div class="modal fade" id="componentExportModal" tabindex="-1" role="dialog" aria-labelledby="componentExportModalLabel">
168+
<div class="modal-dialog" role="document">
169+
<div class="modal-content">
170+
<div class="modal-header">
171+
<h4 class="modal-title" id="myModalLabel">Upload Components to DejaCode</h4>
172+
</div>
173+
<div class="modal-body">
174+
<form class="form-inline">
175+
<div class="form-group">
176+
<h4>Product Information</h4>
177+
<p> Enter the Product Name and Version you want the components to be added to</p>
178+
<input type="text" class="form-control" id="product-name" placeholder="Product Name" required>
179+
<input type="text" class="form-control" id="product-version" placeholder="Product Version" required>
180+
181+
<h4>DejaCode API URL</h4>
182+
<p> Enter the URL (For example, https://enterprise.dejacode.com)</p>
183+
<input type="text" class="form-control" id="api-url" placeholder="DejaCode API URL" required>
184+
185+
<h4>Please provide your API key</h4>
186+
<p> Your API key can be found in your DejaCode Profile Settings</p>
187+
<input type="password" class="form-control" id="export-input" placeholder="DejaCode API Key" required>
188+
</div>
189+
</form>
190+
</div>
191+
<div class="modal-footer">
192+
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
193+
<button type="submit" class="btn btn-primary" id="componentSubmit">Submit</button>
194+
</div>
195+
</div>
196+
</div>
197+
</div>
198+
154199
<!-- Help Modal -->
155200
<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel">
156201
<div class="modal-dialog modal-lg" role="document">

0 commit comments

Comments
 (0)