@@ -6,12 +6,11 @@ const _ = require('lodash')
66const moment = require ( 'moment' )
77const read = require ( 'read' )
88const chalk = require ( 'chalk' )
9- const sinon = require ( 'sinon' )
109const argv = require ( 'yargs' )
1110 . usage ( 'Usage: git-issues-downloader [options] URL \nType git-issues-downloader --help to see a list of all options.' )
1211 . help ( 'h' )
1312
14- . version ( function ( ) {
13+ . version ( function ( ) {
1514 return `Version: ${ require ( './package.json' ) . version } `
1615 } )
1716
@@ -39,7 +38,7 @@ getAuth = function (auth, silent, callback) {
3938
4039// callback function for getting requested options
4140
42- exports . getRequestedOptions = function ( username , password , url , callback ) {
41+ const getRequestedOptions = exports . getRequestedOptions = function ( username , password , url , callback ) {
4342 const requestOptions = {
4443 headers : {
4544 'User-Agent' : 'request'
@@ -89,35 +88,39 @@ exports.getRequestedOptions = function (username, password, url, callback) {
8988
9089// main function for running program
9190
92- exports . main = function ( data , requestedOptions ) {
93- console . log ( 'Requesting API...' )
94- this . requestBody ( requestedOptions , ( error , response , body ) => {
95- const linkObject = this . responseToObject ( response . headers )
91+ const main = exports . main = function ( data , requestedOptions ) {
92+ logExceptOnTest ( 'Requesting API...' )
93+ requestBody ( requestedOptions , ( error , response , body ) => {
94+ linkObject = responseToObject ( response . headers )
9695
9796 // take body, parse it and add it to data
9897
9998 data = _ . concat ( data , body )
10099
101100 if ( linkObject . nextPage ) {
102- console . log ( chalk . green ( `Successfully requested ${ linkObject . nextPage . number - 1 } . page of ${ linkObject . lastPage . number } ` ) )
103-
101+ logExceptOnTest ( chalk . green ( `Successfully requested ${ linkObject . nextPage . number - 1 } . page of ${ linkObject . lastPage . number } ` ) )
104102 requestedOptions . url = linkObject . nextPage . url
105- this . main ( data , requestedOptions )
103+ main ( data , requestedOptions )
106104 } else {
107- console . log ( chalk . green ( 'Successfully requested last page' ) )
105+ logExceptOnTest ( chalk . green ( 'Successfully requested last page' ) )
106+
107+ logExceptOnTest ( '\nConverting issues...' )
108+ const csvData = convertJSonToCsv ( data )
109+ logExceptOnTest ( chalk . green ( `\nSuccessfully converted ${ data . length } issues!` ) )
108110
109- console . log ( '\nConverting issues... ' )
110- const csvData = this . convertJSonToCsv ( data )
111- console . log ( chalk . green ( `\nSuccessfully converted ${ data . length } issues!` ) )
111+ logExceptOnTest ( '\nWriting data to csv file ' )
112+ fs . writeFile ( outputFileName , csvData , ( err ) => {
113+ if ( err ) throw err
112114
113- this . writeData ( csvData , outputFileName )
115+ logExceptOnTest ( chalk . yellow ( `\nIssues was downloaded, converted and saved to ${ outputFileName } ` ) )
116+ } )
114117 }
115118 } )
116119}
117120
118121// get page url and page number from link
119122
120- exports . getUrlAndNumber = function ( link ) {
123+ const getUrlAndNumber = exports . getUrlAndNumber = function ( link ) {
121124 return {
122125 url : link . slice ( link . indexOf ( '<' ) + 1 , link . indexOf ( '>' ) ) ,
123126 number : link . slice ( link . indexOf ( 'page' , link . indexOf ( 'state' ) ) + 5 , link . indexOf ( '>' ) )
@@ -126,25 +129,25 @@ exports.getUrlAndNumber = function (link) {
126129
127130// create and return links info (page url and page number for all 4 possible links in response.headers.link) from whole response.hearders
128131
129- exports . responseToObject = function ( response ) {
132+ const responseToObject = exports . responseToObject = function ( response ) {
130133 const rawLink = response . link
131134
132135 if ( rawLink && rawLink . includes ( 'next' ) ) {
133136 const links = rawLink . split ( ',' )
134137
135138 return {
136- nextPage : ( links [ 0 ] ) ? this . getUrlAndNumber ( links [ 0 ] ) : false ,
137- lastPage : ( links [ 1 ] ) ? this . getUrlAndNumber ( links [ 1 ] ) : false ,
138- firstPage : ( links [ 2 ] ) ? this . getUrlAndNumber ( links [ 2 ] ) : false ,
139- prevPage : ( links [ 3 ] ) ? this . getUrlAndNumber ( links [ 3 ] ) : false
139+ nextPage : ( links [ 0 ] ) ? getUrlAndNumber ( links [ 0 ] ) : false ,
140+ lastPage : ( links [ 1 ] ) ? getUrlAndNumber ( links [ 1 ] ) : false ,
141+ firstPage : ( links [ 2 ] ) ? getUrlAndNumber ( links [ 2 ] ) : false ,
142+ prevPage : ( links [ 3 ] ) ? getUrlAndNumber ( links [ 3 ] ) : false
140143 }
141144 }
142145 return false
143146}
144147
145148// use url and request api
146149
147- exports . requestBody = function ( requestedOptions , callback ) {
150+ const requestBody = exports . requestBody = function ( requestedOptions , callback ) {
148151 request . get ( requestedOptions , function ( err , response , body ) {
149152 const JSObject = JSON . parse ( body )
150153
@@ -153,13 +156,13 @@ exports.requestBody = function (requestedOptions, callback) {
153156
154157 switch ( JSObject . message ) {
155158 case 'Not Found' :
156- console . log ( chalk . red ( '\nWe didn\'t find any repository on this URL, please check it' ) )
159+ logExceptOnTest ( chalk . red ( '\nWe didn\'t find any repository on this URL, please check it' ) )
157160 break
158161 case 'Bad credentials' :
159- console . log ( chalk . red ( '\nYour username or password is invalid, please check it' ) )
162+ logExceptOnTest ( chalk . red ( '\nYour username or password is invalid, please check it' ) )
160163 break
161164 default :
162- console . log ( chalk . red ( '\nRepository have 0 issues. Nothing to download' ) )
165+ logExceptOnTest ( chalk . red ( '\nRepository have 0 issues. Nothing to download' ) )
163166 }
164167 } else {
165168 callback ( err , response , JSObject )
@@ -169,46 +172,39 @@ exports.requestBody = function (requestedOptions, callback) {
169172
170173// take JSON data, convert them into CSV format and return them
171174
172- exports . convertJSonToCsv = function ( jsData ) {
173- const csvData = jsData . map ( object => {
175+ const convertJSonToCsv = exports . convertJSonToCsv = function ( jsData ) {
176+ return jsData . map ( object => {
174177 const date = moment ( object . created_at ) . format ( 'L' )
175178 const labels = object . labels
176179 const stringLabels = labels . map ( label => label . name ) . toString ( )
177180 return `"${ object . number } "; "${ object . title . replace ( / " / g, '\'' ) } "; "${ object . html_url } "; "${ stringLabels } "; "${ object . state } "; "${ date } "\n`
178181 } ) . join ( '' )
179-
180- return csvData
181- }
182-
183- // create a new file and write converted data on him
184-
185- exports . writeData = function ( data , outputFileName ) {
186- console . log ( '\nWriting data to csv file' )
187- fs . writeFile ( outputFileName , data , ( err ) => {
188- if ( err ) throw err
189-
190- console . log ( chalk . yellow ( `\nIssues was downloaded, converted and saved to ${ outputFileName } ` ) )
191- } )
192182}
193183
194184// execute main function with requested options and condition for URL input
195185
196- exports . execute = function ( argvRepository ) {
186+ const execute = exports . execute = function ( argvRepository ) {
197187 if ( argvRepository ) {
198188 const issuesPerPage = 100
199189 const repoUserName = argvRepository . slice ( 19 , argvRepository . indexOf ( '/' , 19 ) )
200190 const repoUrl = ( argvRepository . slice ( 20 + repoUserName . length , argvRepository . lastIndexOf ( '/' ) ) ) ? argvRepository . slice ( 20 + repoUserName . length , argvRepository . lastIndexOf ( '/' ) ) : argvRepository . slice ( 20 + repoUserName . length )
201191
202192 const startUrl = `https://api.github.com/repos/${ repoUserName } /${ repoUrl } /issues?per_page=${ issuesPerPage } &state=all&page=1`
203193
204- this . getRequestedOptions ( argv . username , argv . password , startUrl , ( requestedOptions ) => {
205- this . main ( [ ] , requestedOptions )
194+ getRequestedOptions ( argv . username , argv . password , startUrl , ( requestedOptions ) => {
195+ main ( [ ] , requestedOptions )
206196 } )
207197 } else {
208198 console . log ( 'Usage: git-issues-downloader [options] URL' )
209199 }
210200}
211201
202+ function logExceptOnTest ( string ) {
203+ if ( process . env . NODE_ENV !== 'test' ) {
204+ console . log ( string )
205+ }
206+ }
207+
212208const argvRepository = argv . _ [ argv . _ . length - 1 ]
213209
214210this . execute ( argvRepository )
0 commit comments