|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace sebastianfeldmann\CaptainHook\Hook\Message\Check; |
|
11
|
|
|
|
|
12
|
|
|
use sebastianfeldmann\CaptainHook\Config; |
|
13
|
|
|
use sebastianfeldmann\CaptainHook\Console\IO; |
|
14
|
|
|
use sebastianfeldmann\CaptainHook\Exception\ActionExecution; |
|
15
|
|
|
use sebastianfeldmann\CaptainHook\Git\Repository; |
|
16
|
|
|
use sebastianfeldmann\CaptainHook\Hook\Action; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Regex |
|
20
|
|
|
* |
|
21
|
|
|
* @package CaptainHook |
|
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
23
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
24
|
|
|
* @since Class available since Release 1.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Regex implements Action |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Execute the configured action. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \sebastianfeldmann\CaptainHook\Config $config |
|
32
|
|
|
* @param \sebastianfeldmann\CaptainHook\Console\IO $io |
|
33
|
|
|
* @param \sebastianfeldmann\CaptainHook\Git\Repository $repository |
|
34
|
|
|
* @param \sebastianfeldmann\CaptainHook\Config\Action $action |
|
35
|
|
|
* @throws \sebastianfeldmann\CaptainHook\Exception\ActionExecution |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action) |
|
38
|
|
|
{ |
|
39
|
3 |
|
$regex = $this->getRegex($action->getOptions()); |
|
40
|
|
|
|
|
41
|
2 |
|
if (!preg_match($regex, $repository->getCommitMsg()->getContent())) { |
|
42
|
1 |
|
throw new ActionExecution('Commit message did not match regex: ' . $regex); |
|
43
|
|
|
} |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Extract regex from options array. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $options |
|
50
|
|
|
* @return string |
|
51
|
|
|
* @throws \sebastianfeldmann\CaptainHook\Exception\ActionExecution |
|
52
|
|
|
*/ |
|
53
|
3 |
|
protected function getRegex(array $options) |
|
54
|
|
|
{ |
|
55
|
3 |
|
if (!isset($options['regex'])) { |
|
56
|
1 |
|
throw new ActionExecution('No regex option'); |
|
57
|
|
|
} |
|
58
|
2 |
|
return $options['regex']; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|