forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportProject.php
More file actions
302 lines (256 loc) · 8.91 KB
/
ReportProject.php
File metadata and controls
302 lines (256 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
namespace kriskbx\gtt\Commands;
use Carbon\Carbon;
use Gitlab\Api\Issues;
use Illuminate\Support\Collection;
use kriskbx\gtt\Issue;
use kriskbx\gtt\Output\Table;
use kriskbx\gtt\Project;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class ReportProject
* @package kriskbx\gtt\Commands
*/
class ReportProject extends BaseCommand
{
/**
* Check mark
* @var string
*/
protected $check = '<fg=green>✔</>';
/**
* Configure the command.
*/
protected function configure()
{
$this
->setName('report')
->addArgument('project', InputArgument::OPTIONAL,
'Id or project namespace. Defaults to project defined in config.')
->addArgument('issue', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Id of a specific issue.')
->addOption('from', null, InputOption::VALUE_REQUIRED, 'Date from. Defaults to 01-01-1977')
->addOption('to', null, InputOption::VALUE_REQUIRED, 'Date to. Defaults to now')
->setDescription('Get metrics');
parent::configure();
}
/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
list($from,
$to,
$closed,
$projectName,
$columns,
$user,
$questionHelper,
$file,
$milestone,
$outputOption,
$includeLabels,
$excludeLabels
) = $this->getConfiguration($input);
// Get project
$project = $this->getProject($output, $projectName);
// Get params (for filtering via milestone/label/closed)
$params = $this->getParams($closed, $milestone);
// Get issues
$issues = $this->getIssues($output, $project, $params, $includeLabels, $excludeLabels);
$issues = $this->filterIssuesByArgument($issues, $input);
if ($issues->count() == 0) {
throw new \Exception('No issues found that match your criteria!');
}
// Set time records in issues
$issues = $this->setTimesInIssues($output, $issues, $from, $to, $user);
// Choose output
if ( ! $outputOption) {
$outputInstance = new Table($input, $output, $questionHelper, $columns, $file);
} elseif (array_key_exists($outputOption, $this->outputs)) {
$outputClassName = $this->outputs[$outputOption];
$outputInstance = new $outputClassName($input, $output, $questionHelper, $columns, $file);
} else {
throw new \Exception("Output '{$output}' not found!");
}
// Put everything out there!
$outputInstance->render($issues, $this->config);
$output->writeln("<fg=green>Done!</> 🍺");
}
/**
* Get Project by the given name.
*
* @param OutputInterface $output
* @param string $projectName
*
* @return Project
* @throws \Exception
*/
protected function getProject(OutputInterface $output, $projectName)
{
$output->write("* Making sure project '{$projectName}' exists... ");
try {
$project = (new Project($projectName, $this->client))->show();
} catch (\Exception $e) {
throw new \Exception("Project '{$projectName}' not found!", 1, $e);
}
$output->writeln($this->check);
return $project;
}
/**
* Get all Issues by the given project and params.
*
* @param OutputInterface $output
* @param Project $project
* @param array $params
*
* @param null $includeLabels
* @param array $excludeLabels
*
* @return Collection
*/
protected function getIssues(
OutputInterface $output,
Project $project,
array $params,
$includeLabels = null,
$excludeLabels = []
) {
$output->write("* Getting issues... ");
$issues = collect([]);
$page = 1;
$perPage = 100;
// Loop through all pages
while (true) {
$response = collect((new Issues($this->client))->all($project->id, $page, $perPage, $params));
$issues = $issues->merge($response);
$page++;
if ($response->count() < $perPage) {
break;
}
}
// Create Issue objects
$issues = $issues->map(function ($data) use ($project, $includeLabels, $excludeLabels) {
$issue = Issue::fromArray($this->client, $project, $data);
$issue->setIncludeLabels($includeLabels);
$issue->setExcludeLabels($excludeLabels);
return $issue;
});
$output->writeln($this->check);
return $issues;
}
/**
* Set times in the given issues.
*
* @param OutputInterface $output
* @param Collection $issues
* @param string $user
* @param Carbon $from
* @param Carbon $to
*
* @return Collection
*
* @throws \Exception
*/
protected function setTimesInIssues(OutputInterface $output, Collection $issues, Carbon $from, Carbon $to, $user)
{
$progress = new ProgressBar($output, $issues->count());
$progress->setFormat('* Processing issues... %current%/%max% [%bar%] %percent:3s%% | <fg=green>%remaining%</>');
// Add times to each Issue
$issues = $issues->map(function (Issue $issue) use ($from, $to, $user, &$progress) {
$issue->setTimes($from, $to, $user);
$progress->advance();
return $issue;
});
// Filter issues with empty times
$issues = $issues->filter(function (Issue $issue) {
return $issue->getTimes()->count() > 0;
});
$progress->finish();
return $issues;
}
/**
* Get configuration based upon the passed config, options and arguments.
*
* @param InputInterface $input
*
* @return array
*/
protected function getConfiguration(InputInterface $input)
{
$from = Carbon::parse($input->getOption('from') ?: '01-01-1977');
$to = $input->getOption('to') ? Carbon::parse($input->getOption('to')) : Carbon::now();
$closed = $input->getOption('closed') === null ? $this->config['closed'] : $input->getOption('closed');
$milestone = $input->getOption('milestone') === null ? $this->config['milestone'] : $input->getOption('milestone');
$projectName = $input->getArgument('project') ?: $this->config['project'];
$columns = array_merge($input->getOption('columns') ?: $this->config['columns'], ['times']);
$user = $input->getOption('user') ?: false;
$questionHelper = $this->getHelper('question');
$file = $input->getOption('file');
$outputOption = $input->getOption('output') === null ? $this->config['output'] : $input->getOption('output');
$includeLabels = $input->getOption('include_labels') === null ? $this->config['includeLabels'] : $input->getOption('include_labels');
$excludeLabels = $input->getOption('exclude_labels') === null ? $this->config['excludeLabels'] : $input->getOption('exclude_labels');
return [
$from,
$to,
$closed,
$projectName,
$columns,
$user,
$questionHelper,
$file,
$milestone,
$outputOption,
$includeLabels,
$excludeLabels
];
}
/**
* Filter issues by the 'issue' argument.
*
* @param Collection $issues
* @param InputInterface $input
*
* @return Collection
*/
protected function filterIssuesByArgument(Collection $issues, InputInterface $input)
{
if ($input->getArgument('issue')) {
$issues = $issues->filter(function ($issue) use ($input) {
return in_array($issue->id, $input->getArgument('issue'))
|| in_array($issue->iid, $input->getArgument('issue'));
});
}
return $issues;
}
/**
* Get params for querying issues. Is sued to filter out issues in the first place.
*
* @param bool $closed
* @param string $milestone
*
* @return array
*/
protected function getParams($closed, $milestone)
{
$params = [];
if ($closed == false) {
$params['state'] = 'opened';
}
if ($milestone) {
$params['milestone'] = $milestone;
}
return $params;
}
}