|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of forecast.it.fill project. |
|
7
|
|
|
* (c) Patrick Jaja <[email protected]> |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ForecastAutomation\GitlabClient\Shared\Plugin; |
|
13
|
|
|
|
|
14
|
|
|
use ForecastAutomation\Activity\Shared\Dto\ActivityDto; |
|
15
|
|
|
use ForecastAutomation\Activity\Shared\Dto\ActivityDtoCollection; |
|
16
|
|
|
use ForecastAutomation\Activity\Shared\Plugin\ActivityPluginInterface; |
|
17
|
|
|
use ForecastAutomation\GitlabClient\Shared\Dto\GitlabQueryDto; |
|
18
|
|
|
use ForecastAutomation\Kernel\Shared\Plugin\AbstractPlugin; |
|
19
|
|
|
use GuzzleHttp\Promise\Promise; |
|
20
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @method \ForecastAutomation\GitlabClient\GitlabClientFacade getFacade() |
|
24
|
|
|
*/ |
|
25
|
|
|
class GitlabActivityPlugin extends AbstractPlugin implements ActivityPluginInterface |
|
26
|
|
|
{ |
|
27
|
|
|
public const ALLOWED_ACTION_NAMES = ['commented on', 'approved']; |
|
28
|
|
|
public const ACTIVITY_SUFFIX = 'Entwicklungsprozess'; |
|
29
|
|
|
public const ACTIVITY_DURATION = 15; |
|
30
|
|
|
|
|
31
|
1 |
|
public function collect(): PromiseInterface |
|
32
|
|
|
{ |
|
33
|
1 |
|
$wrapPromise = new Promise( |
|
34
|
1 |
|
function () use (&$wrapPromise) { |
|
35
|
1 |
|
$wrapPromise->resolve( |
|
36
|
1 |
|
$this->mapEventsToActivity( |
|
37
|
1 |
|
$this->getFacade()->getEvents(new GitlabQueryDto(date('Y-m-d', strtotime('-1 day')))) |
|
38
|
|
|
) |
|
39
|
|
|
); |
|
40
|
1 |
|
} |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
1 |
|
return $wrapPromise; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
private function mapEventsToActivity(array $events): ActivityDtoCollection |
|
47
|
|
|
{ |
|
48
|
1 |
|
$activityDtoArray = []; |
|
49
|
1 |
|
foreach ($events as $event) { |
|
50
|
1 |
|
if (\in_array($event->action_name, self::ALLOWED_ACTION_NAMES, true)) { |
|
51
|
1 |
|
$duration = self::ACTIVITY_DURATION; |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
1 |
|
$ticketNr = $this->getNeedle($event->target_title); |
|
55
|
1 |
|
if (isset($activityDtoArray[$ticketNr])) { |
|
56
|
|
|
$duration = self::ACTIVITY_DURATION + $activityDtoArray[$ticketNr]->duration; |
|
57
|
|
|
} |
|
58
|
1 |
|
$activityDtoArray[$ticketNr] = new ActivityDto( |
|
59
|
1 |
|
$ticketNr, |
|
60
|
1 |
|
sprintf('%s: %s (%s)', self::ACTIVITY_SUFFIX, $event->target_title, $event->action_name), |
|
61
|
1 |
|
new \DateTime($event->created_at), |
|
62
|
|
|
$duration |
|
63
|
|
|
); |
|
64
|
|
|
} catch (\Exception $e) { |
|
65
|
|
|
// needle not found. no activity will be created, its ok. |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return new ActivityDtoCollection(...array_values($activityDtoArray)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
private function getNeedle(string $target_title): string |
|
74
|
|
|
{ |
|
75
|
1 |
|
$matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['GITLAB_PATTERN']); |
|
76
|
1 |
|
$resultMatch = preg_match($matchPattern, $target_title, $match); |
|
77
|
1 |
|
if (0 === $resultMatch || ! isset($match[0])) { |
|
78
|
|
|
throw new \Exception('gitlab needle not found for target_title: '.$target_title); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return strtoupper($match[0]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|