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+ }
0 commit comments