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