@@ -6,12 +6,11 @@ const _ = require('lodash')
6
6
const moment = require ( 'moment' )
7
7
const read = require ( 'read' )
8
8
const chalk = require ( 'chalk' )
9
- const sinon = require ( 'sinon' )
10
9
const argv = require ( 'yargs' )
11
10
. usage ( 'Usage: git-issues-downloader [options] URL \nType git-issues-downloader --help to see a list of all options.' )
12
11
. help ( 'h' )
13
12
14
- . version ( function ( ) {
13
+ . version ( function ( ) {
15
14
return `Version: ${ require ( './package.json' ) . version } `
16
15
} )
17
16
@@ -39,7 +38,7 @@ getAuth = function (auth, silent, callback) {
39
38
40
39
// callback function for getting requested options
41
40
42
- exports . getRequestedOptions = function ( username , password , url , callback ) {
41
+ const getRequestedOptions = exports . getRequestedOptions = function ( username , password , url , callback ) {
43
42
const requestOptions = {
44
43
headers : {
45
44
'User-Agent' : 'request'
@@ -89,35 +88,39 @@ exports.getRequestedOptions = function (username, password, url, callback) {
89
88
90
89
// main function for running program
91
90
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 )
96
95
97
96
// take body, parse it and add it to data
98
97
99
98
data = _ . concat ( data , body )
100
99
101
100
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 } ` ) )
104
102
requestedOptions . url = linkObject . nextPage . url
105
- this . main ( data , requestedOptions )
103
+ main ( data , requestedOptions )
106
104
} 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!` ) )
108
110
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
112
114
113
- this . writeData ( csvData , outputFileName )
115
+ logExceptOnTest ( chalk . yellow ( `\nIssues was downloaded, converted and saved to ${ outputFileName } ` ) )
116
+ } )
114
117
}
115
118
} )
116
119
}
117
120
118
121
// get page url and page number from link
119
122
120
- exports . getUrlAndNumber = function ( link ) {
123
+ const getUrlAndNumber = exports . getUrlAndNumber = function ( link ) {
121
124
return {
122
125
url : link . slice ( link . indexOf ( '<' ) + 1 , link . indexOf ( '>' ) ) ,
123
126
number : link . slice ( link . indexOf ( 'page' , link . indexOf ( 'state' ) ) + 5 , link . indexOf ( '>' ) )
@@ -126,25 +129,25 @@ exports.getUrlAndNumber = function (link) {
126
129
127
130
// create and return links info (page url and page number for all 4 possible links in response.headers.link) from whole response.hearders
128
131
129
- exports . responseToObject = function ( response ) {
132
+ const responseToObject = exports . responseToObject = function ( response ) {
130
133
const rawLink = response . link
131
134
132
135
if ( rawLink && rawLink . includes ( 'next' ) ) {
133
136
const links = rawLink . split ( ',' )
134
137
135
138
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
140
143
}
141
144
}
142
145
return false
143
146
}
144
147
145
148
// use url and request api
146
149
147
- exports . requestBody = function ( requestedOptions , callback ) {
150
+ const requestBody = exports . requestBody = function ( requestedOptions , callback ) {
148
151
request . get ( requestedOptions , function ( err , response , body ) {
149
152
const JSObject = JSON . parse ( body )
150
153
@@ -153,13 +156,13 @@ exports.requestBody = function (requestedOptions, callback) {
153
156
154
157
switch ( JSObject . message ) {
155
158
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' ) )
157
160
break
158
161
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' ) )
160
163
break
161
164
default :
162
- console . log ( chalk . red ( '\nRepository have 0 issues. Nothing to download' ) )
165
+ logExceptOnTest ( chalk . red ( '\nRepository have 0 issues. Nothing to download' ) )
163
166
}
164
167
} else {
165
168
callback ( err , response , JSObject )
@@ -169,46 +172,39 @@ exports.requestBody = function (requestedOptions, callback) {
169
172
170
173
// take JSON data, convert them into CSV format and return them
171
174
172
- exports . convertJSonToCsv = function ( jsData ) {
173
- const csvData = jsData . map ( object => {
175
+ const convertJSonToCsv = exports . convertJSonToCsv = function ( jsData ) {
176
+ return jsData . map ( object => {
174
177
const date = moment ( object . created_at ) . format ( 'L' )
175
178
const labels = object . labels
176
179
const stringLabels = labels . map ( label => label . name ) . toString ( )
177
180
return `"${ object . number } "; "${ object . title . replace ( / " / g, '\'' ) } "; "${ object . html_url } "; "${ stringLabels } "; "${ object . state } "; "${ date } "\n`
178
181
} ) . 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
- } )
192
182
}
193
183
194
184
// execute main function with requested options and condition for URL input
195
185
196
- exports . execute = function ( argvRepository ) {
186
+ const execute = exports . execute = function ( argvRepository ) {
197
187
if ( argvRepository ) {
198
188
const issuesPerPage = 100
199
189
const repoUserName = argvRepository . slice ( 19 , argvRepository . indexOf ( '/' , 19 ) )
200
190
const repoUrl = ( argvRepository . slice ( 20 + repoUserName . length , argvRepository . lastIndexOf ( '/' ) ) ) ? argvRepository . slice ( 20 + repoUserName . length , argvRepository . lastIndexOf ( '/' ) ) : argvRepository . slice ( 20 + repoUserName . length )
201
191
202
192
const startUrl = `https://api.github.com/repos/${ repoUserName } /${ repoUrl } /issues?per_page=${ issuesPerPage } &state=all&page=1`
203
193
204
- this . getRequestedOptions ( argv . username , argv . password , startUrl , ( requestedOptions ) => {
205
- this . main ( [ ] , requestedOptions )
194
+ getRequestedOptions ( argv . username , argv . password , startUrl , ( requestedOptions ) => {
195
+ main ( [ ] , requestedOptions )
206
196
} )
207
197
} else {
208
198
console . log ( 'Usage: git-issues-downloader [options] URL' )
209
199
}
210
200
}
211
201
202
+ function logExceptOnTest ( string ) {
203
+ if ( process . env . NODE_ENV !== 'test' ) {
204
+ console . log ( string )
205
+ }
206
+ }
207
+
212
208
const argvRepository = argv . _ [ argv . _ . length - 1 ]
213
209
214
210
this . execute ( argvRepository )
0 commit comments