1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Hook\Message\Action; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Console\IO; |
16
|
|
|
use CaptainHook\App\Exception\ActionFailed; |
17
|
|
|
use CaptainHook\App\Hook\Action; |
18
|
|
|
use CaptainHook\App\Hook\Constrained; |
19
|
|
|
use CaptainHook\App\Hook\Restriction; |
20
|
|
|
use CaptainHook\App\Hooks; |
21
|
|
|
use SebastianFeldmann\Git\Repository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Regex |
25
|
|
|
* |
26
|
|
|
* @package CaptainHook |
27
|
|
|
* @author Sebastian Feldmann <[email protected]> |
28
|
|
|
* @link https://github.com/captainhookphp/captainhook |
29
|
|
|
* @since Class available since Release 1.0.0 |
30
|
|
|
*/ |
31
|
|
|
class Regex implements Action, Constrained |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Return hook restriction |
35
|
|
|
* |
36
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
37
|
|
|
*/ |
38
|
5 |
|
public static function getRestriction(): Restriction |
39
|
|
|
{ |
40
|
5 |
|
return Restriction::fromArray([Hooks::COMMIT_MSG]); |
41
|
4 |
|
} |
42
|
4 |
|
|
43
|
4 |
|
/** |
44
|
|
|
* Execute the configured action |
45
|
4 |
|
* |
46
|
1 |
|
* @param \CaptainHook\App\Config $config |
47
|
|
|
* @param \CaptainHook\App\Console\IO $io |
48
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
49
|
3 |
|
* @param \CaptainHook\App\Config\Action $action |
50
|
1 |
|
* @return void |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
2 |
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void |
54
|
2 |
|
{ |
55
|
|
|
$regex = $this->getRegex($action->getOptions()); |
56
|
|
|
$errorMsg = $this->getErrorMessage($action->getOptions()); |
57
|
|
|
$successMsg = $this->getSuccessMessage($action->getOptions()); |
58
|
|
|
$matches = []; |
59
|
|
|
|
60
|
|
|
if ($repository->isMerging()) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
5 |
|
|
64
|
|
|
if (!preg_match($regex, $repository->getCommitMsg()->getContent(), $matches)) { |
65
|
5 |
|
throw new ActionFailed(sprintf($errorMsg, $regex)); |
66
|
5 |
|
} |
67
|
1 |
|
|
68
|
|
|
$io->write(sprintf($successMsg, $matches[0])); |
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Extract regex from options array |
73
|
|
|
* |
74
|
|
|
* @param \CaptainHook\App\Config\Options $options |
75
|
|
|
* @return string |
76
|
|
|
* @throws \CaptainHook\App\Exception\ActionFailed |
77
|
|
|
*/ |
78
|
4 |
|
protected function getRegex(Config\Options $options): string |
79
|
|
|
{ |
80
|
4 |
|
$regex = $options->get('regex'); |
81
|
|
|
if (empty($regex)) { |
82
|
|
|
throw new ActionFailed('No regex option'); |
83
|
|
|
} |
84
|
|
|
return $regex; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Determine the error message to use |
89
|
4 |
|
* |
90
|
|
|
* @param \CaptainHook\App\Config\Options $options |
91
|
4 |
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function getErrorMessage(Config\Options $options): string |
94
|
|
|
{ |
95
|
|
|
return $options->get('error') ?? 'Commit message did not match regex: %s'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Determine the error message to use |
100
|
|
|
* |
101
|
|
|
* @param \CaptainHook\App\Config\Options $options |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function getSuccessMessage(Config\Options $options): string |
105
|
|
|
{ |
106
|
|
|
return $options->get('success') ?? 'Found matching pattern: %s'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|