|
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\Branch\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\Restriction; |
|
19
|
|
|
use CaptainHook\App\Hooks; |
|
20
|
|
|
use SebastianFeldmann\Git\Repository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class EnsureNaming |
|
24
|
|
|
* |
|
25
|
|
|
* @package CaptainHook |
|
26
|
|
|
* @author Felix Edelmann <[email protected]> |
|
27
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
28
|
|
|
* @since TODO |
|
29
|
|
|
*/ |
|
30
|
|
|
class EnsureNaming implements Action |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Return hook restriction |
|
34
|
|
|
* |
|
35
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function getRestriction(): Restriction |
|
38
|
|
|
{ |
|
39
|
|
|
return Restriction::fromArray([Hooks::PRE_COMMIT, Hooks::PRE_PUSH, Hooks::POST_CHECKOUT]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Execute the configured action |
|
44
|
|
|
* |
|
45
|
|
|
* @param \CaptainHook\App\Config $config |
|
46
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
47
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
48
|
|
|
* @param \CaptainHook\App\Config\Action $action |
|
49
|
|
|
* @return void |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void |
|
53
|
|
|
{ |
|
54
|
|
|
$regex = $this->getRegex($action->getOptions()); |
|
55
|
|
|
$errorMsg = $this->getErrorMessage($action->getOptions()); |
|
56
|
|
|
$successMsg = $this->getSuccessMessage($action->getOptions()); |
|
57
|
|
|
|
|
58
|
|
|
$branch = $repository->getInfoOperator()->getCurrentBranch(); |
|
59
|
|
|
if (!preg_match($regex, $branch)) { |
|
60
|
|
|
throw new ActionFailed(sprintf($errorMsg, $regex)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$io->write(sprintf($successMsg, $regex)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Extract regex from options array |
|
68
|
|
|
* |
|
69
|
|
|
* @param \CaptainHook\App\Config\Options $options |
|
70
|
|
|
* @return string |
|
71
|
|
|
* @throws \CaptainHook\App\Exception\ActionFailed |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getRegex(Config\Options $options): string |
|
74
|
|
|
{ |
|
75
|
|
|
$regex = $options->get('regex'); |
|
76
|
|
|
if (empty($regex)) { |
|
77
|
|
|
throw new ActionFailed('No regex option'); |
|
78
|
|
|
} |
|
79
|
|
|
return $regex; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Determine the error message to use |
|
84
|
|
|
* |
|
85
|
|
|
* @param \CaptainHook\App\Config\Options $options |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getErrorMessage(Config\Options $options): string |
|
89
|
|
|
{ |
|
90
|
|
|
return $options->get('error') ?? '<error>FAIL</error> Branch name does not match regex: %s'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Determine the error message to use |
|
95
|
|
|
* |
|
96
|
|
|
* @param \CaptainHook\App\Config\Options $options |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getSuccessMessage(Config\Options $options): string |
|
100
|
|
|
{ |
|
101
|
|
|
return $options->get('success') ?? '<info>OK</info> Branch name does match regex: %s'; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|