1+ const _ = require ( 'underscore' ) ;
12const program = require ( 'commander' ) ;
23
34const Cli = require ( './include/cli' ) ;
45const Config = require ( './include/file-config' ) ;
5- const Report = require ( './model/report' ) ;
6+ const Report = require ( './models/report' ) ;
7+
8+ const Output = {
9+ table : require ( './output/table' )
10+ } ;
611
712// this collects options
813function collect ( val , arr ) {
@@ -21,19 +26,25 @@ program
2126 . option ( '-u --user <user>' , 'only query times from the given user' )
2227 . option ( '-m --milestone <milestone>' , 'include issues from the given milestone' )
2328 . option ( '-q --query <query>' , 'query the given data types: issues, merge_requests' , collect , null )
29+ . option ( '-r --report <report>' , 'include in the report: stats, issues, merge_requests, records' , collect , null )
2430 . option ( '-o --output <output>' , 'use the given output' , collect , null )
2531 . option ( '-l --file <file>' , 'save report to the given file' , collect , null )
26- . option ( '--columns <columns>' , 'include the given columns in the report' , collect , null )
2732 . option ( '--include_by_labels <labels>' , 'only include issues that have the given labels' , collect , null )
2833 . option ( '--exclude_by_labels <labels>' , 'exclude issues that have the given labels' , collect , null )
2934 . option ( '--include_labels <labels>' , 'only include the given labels in the report' , collect , null )
3035 . option ( '--exclude_labels <labels>' , 'exclude the given labels in the report' , collect , null )
3136 . option ( '--date_format <date>' , 'use the given date format in the report' , collect , null )
32- . option ( '--time_format <time>' , 'use the given time format in the report' , collect , null )
37+ . option ( '--time_format <time>' , 'use the given time format in the report' )
38+ . option ( '--no_headlines' , 'hide headlines in the report' )
39+ . option ( '--no_warnings' , 'hide warnings in the report' )
40+ . option ( '--record_columns <columns>' , 'include the given columns in the record part of the report' , collect , null )
41+ . option ( '--issue_columns <columns>' , 'include the given columns in the issue part of the report' , collect , null )
42+ . option ( '--merge_request_columns <columns>' , 'include the given columns in the merge request part of the report' , collect , null )
43+ . option ( '--user_columns' , 'include user columns in the report' )
3344 . parse ( process . argv ) ;
3445
3546// init helpers
36- let config = new Config ( __dirname ) ;
47+ let config = new Config ( process . cwd ( ) ) ;
3748let cli = new Cli ( program . args ) ;
3849
3950// overwrite config with args and opts
@@ -45,69 +56,119 @@ config
4556 . set ( 'closed' , program . closed )
4657 . set ( 'user' , program . user )
4758 . set ( 'milestone' , program . milestone )
48- . set ( 'columns' , program . columns )
4959 . set ( 'includeByLabels' , program . include_by_labels )
5060 . set ( 'excludeByLabels' , program . exclude_by_labels )
5161 . set ( 'includeLabels' , program . include_labels )
5262 . set ( 'excludeLabels' , program . exclude_labels )
5363 . set ( 'dateFormat' , program . date_format )
5464 . set ( 'timeFormat' , program . time_format )
5565 . set ( 'output' , program . output )
56- . set ( 'file' , program . file ) ;
66+ . set ( 'file' , program . file )
67+ . set ( 'query' , program . query )
68+ . set ( 'report' , program . report )
69+ . set ( 'recordColumns' , program . record_columns )
70+ . set ( 'issueColumns' , program . issue_columns )
71+ . set ( 'mergeRequestColumns' , program . merge_request_columns )
72+ . set ( 'noHeadlines' , program . no_headlines )
73+ . set ( 'noWarnings' , program . no_warnings )
74+ . set ( 'userColumns' , program . user_columns ) ;
5775
5876// warnings
5977if ( config . get ( 'iids' ) . length > 1 && config . get ( 'query' ) . length > 1 ) {
6078 Cli . warn ( `The ids argument is ignored when querying multiple data types` ) ;
6179}
80+ if ( ( config . get ( 'report' ) . includes ( 'issues' ) && ! config . get ( 'query' ) . includes ( 'issues' ) ) ) {
81+ Cli . warn ( `Issues are included in the report but not queried.` ) ;
82+ }
83+ if ( ( config . get ( 'report' ) . includes ( 'merge_requests' ) && ! config . get ( 'query' ) . includes ( 'merge_requests' ) ) ) {
84+ Cli . warn ( `Merge Requests are included in the report but not queried.` ) ;
85+ }
86+ if ( ! Output [ config . get ( 'output' ) ] ) {
87+ Cli . error ( `The output ${ config . get ( 'output' ) } doesn't exist` ) ;
88+ }
6289
63- let report = new Report ( config ) ;
90+ let report = new Report ( config ) , output ;
6491
65- // get project
66- Cli . list ( `🔍 Resolving project "${ config . get ( 'project' ) } "` ) ;
92+ Cli . list ( `${ Cli . look } Resolving project "${ config . get ( 'project' ) } "` ) ;
6793report
6894 . project ( )
95+ . then ( ( ) => new Promise ( ( resolve , reject ) => {
96+ if ( ! config . get ( 'userColumns' ) ) return resolve ( ) ;
97+
98+ report . project . members ( )
99+ . then ( ( ) => {
100+ let columns = report . project . users . map ( user => `time_${ user } ` ) ;
101+
102+ config . set ( 'issueColumns' , _ . uniq ( config . get ( 'issueColumns' ) . concat ( columns ) ) ) ;
103+ config . set ( 'mergeRequestColumns' , _ . uniq ( config . get ( 'mergeRequestColumns' ) . concat ( columns ) ) ) ;
104+ resolve ( ) ;
105+ } )
106+ . catch ( error => reject ( error ) ) ;
107+ } ) )
69108 . then ( ( ) => Cli . mark ( ) )
70109 . catch ( error => Cli . x ( `could not fetch project.` , error ) )
71110
72111 // get issues
73- . then ( ( ) => new Promise ( ( resolve ) => {
112+ . then ( ( ) => new Promise ( resolve => {
74113 if ( ! config . get ( 'query' ) . includes ( 'issues' ) ) return resolve ( ) ;
75- Cli . list ( `📦 Fetching issues` ) ;
114+
115+ Cli . list ( `${ Cli . fetch } Fetching issues` ) ;
76116 report . issues ( )
77117 . then ( ( ) => Cli . mark ( ) )
78118 . catch ( error => Cli . x ( `could not fetch issues.` , error ) )
79119 . then ( ( ) => resolve ( ) ) ;
80120 } ) )
81121
82122 // get merge requests
83- . then ( ( ) => new Promise ( ( resolve ) => {
123+ . then ( ( ) => new Promise ( resolve => {
84124 if ( ! config . get ( 'query' ) . includes ( 'merge_requests' ) ) return resolve ( ) ;
85- Cli . list ( `📦 Fetching merge requests` ) ;
125+
126+ Cli . list ( `${ Cli . fetch } Fetching merge requests` ) ;
86127 report . mergeRequests ( )
87128 . then ( ( ) => Cli . mark ( ) )
88129 . catch ( error => Cli . x ( `could not fetch merge requests.` , error ) )
89130 . then ( ( ) => resolve ( ) ) ;
90131 } ) )
91132
92133 // process issues
93- . then ( ( ) => new Promise ( ( resolve ) => {
94- if ( ! config . get ( 'query' ) . includes ( 'issues' ) ) return resolve ( ) ;
95- Cli . bar ( `⚙️ Processing issues` , report . issues . length ) ;
134+ . then ( ( ) => new Promise ( resolve => {
135+ if ( ! config . get ( 'query' ) . includes ( 'issues' ) || report . issues . length === 0 ) return resolve ( ) ;
136+
137+ Cli . bar ( `${ Cli . process } ️ Processing issues` , report . issues . length ) ;
96138 report . processIssues ( ( ) => Cli . advance ( ) )
97139 . then ( ( ) => Cli . mark ( ) )
98140 . catch ( error => Cli . x ( `could not process issues.` , error ) )
99141 . then ( ( ) => resolve ( ) ) ;
100142 } ) )
101143
102- //process merge requests
103- . then ( ( ) => new Promise ( ( resolve ) => {
104- if ( ! config . get ( 'query' ) . includes ( 'merge_requests' ) ) return resolve ( ) ;
105- Cli . bar ( `⚙️ Processing merge requests` , report . issues . length ) ;
144+ // process merge requests
145+ . then ( ( ) => new Promise ( resolve => {
146+ if ( ! config . get ( 'query' ) . includes ( 'merge_requests' ) || report . mergeRequests . length === 0 ) return resolve ( ) ;
147+
148+ Cli . bar ( `${ Cli . process } ️️ Processing merge requests` , report . mergeRequests . length ) ;
106149 report . processMergeRequests ( ( ) => Cli . advance ( ) )
107150 . then ( ( ) => Cli . mark ( ) )
108151 . catch ( error => Cli . x ( `could not process merge requests.` , error ) )
109152 . then ( ( ) => resolve ( ) ) ;
110153 } ) )
111154
155+ // make report
156+ . then ( ( ) => new Promise ( resolve => {
157+ Cli . list ( `${ Cli . output } Making report` ) ;
158+
159+ output = new Output [ config . get ( 'output' ) ] ( config , report ) ;
160+ output . make ( ) ;
161+ Cli . mark ( ) ;
162+ resolve ( ) ;
163+ } ) )
164+ . catch ( error => Cli . x ( `could not make report.` , error ) )
165+
166+ // print report
167+ . then ( ( ) => new Promise ( resolve => {
168+ output . toStdOut ( ) ;
169+ resolve ( ) ;
170+ } ) )
171+ . catch ( error => Cli . x ( `could not print report.` , error ) )
172+
112173 // time for a beer
113174 . then ( ( ) => Cli . done ( ) ) ;
0 commit comments