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\Action; |
11
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Config; |
13
|
|
|
use SebastianFeldmann\CaptainHook\Console\IO; |
14
|
|
|
use SebastianFeldmann\CaptainHook\Exception\ActionFailed; |
15
|
|
|
use SebastianFeldmann\CaptainHook\Hook\Action; |
16
|
|
|
use SebastianFeldmann\Git\Repository; |
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
|
|
|
const MESSAGE_ERROR = 'error'; |
29
|
|
|
const MESSAGE_SUCCESS = 'success'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the configured action. |
33
|
|
|
* |
34
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config $config |
35
|
|
|
* @param \SebastianFeldmann\CaptainHook\Console\IO $io |
36
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
37
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Action $action |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
3 |
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action) |
41
|
|
|
{ |
42
|
3 |
|
$regex = $this->getRegex($action->getOptions()); |
43
|
2 |
|
$messageSuccess = $this->getMessage($action->getOptions(), self::MESSAGE_SUCCESS); |
44
|
2 |
|
$messageError = $this->getMessage($action->getOptions(), self::MESSAGE_ERROR); |
45
|
|
|
|
46
|
2 |
|
if (!preg_match($regex, $repository->getCommitMsg()->getContent())) { |
47
|
1 |
|
throw ActionFailed::withMessage(sprintf( |
48
|
1 |
|
$messageError ?? 'Commit message did not match regex: %s', |
49
|
1 |
|
$regex |
50
|
|
|
)); |
51
|
1 |
|
} else if (!empty($messageSuccess)) { |
52
|
1 |
|
$io->write(sprintf($messageSuccess, $regex)); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Extract regex from options array. |
58
|
|
|
* |
59
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Options $options |
60
|
|
|
* @return string |
61
|
|
|
* @throws \SebastianFeldmann\CaptainHook\Exception\ActionFailed |
62
|
|
|
*/ |
63
|
3 |
|
protected function getRegex(Config\Options $options) |
64
|
|
|
{ |
65
|
3 |
|
$regex = $options->get('regex'); |
66
|
3 |
|
if (empty($regex)) { |
67
|
1 |
|
throw ActionFailed::withMessage('No regex option'); |
68
|
|
|
} |
69
|
2 |
|
return $regex; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Extract success/error message from options array. |
74
|
|
|
* |
75
|
|
|
* @param Config\Options $options |
76
|
|
|
* @param string $type |
77
|
|
|
* @return string|null |
78
|
|
|
*/ |
79
|
2 |
|
protected function getMessage(Config\Options $options, string $type) |
80
|
|
|
{ |
81
|
2 |
|
$message = $options->get($type); |
82
|
2 |
|
if (empty($message)) { |
83
|
2 |
|
return null; |
84
|
|
|
} |
85
|
2 |
|
return $message; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|