|
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\Rule; |
|
11
|
|
|
|
|
12
|
|
|
use sebastianfeldmann\CaptainHook\Git\CommitMessage; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class UseImperativeMood |
|
16
|
|
|
* |
|
17
|
|
|
* @package CaptainHook |
|
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
19
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
20
|
|
|
* @since Class available since Release 0.9.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Blacklist extends Base |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Case sensitivity |
|
26
|
|
|
* |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $isCaseSensitive; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Blacklisted words |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $blacklist = [ |
|
37
|
|
|
'subject' => [], |
|
38
|
|
|
'body' => [], |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param bool $caseSensitive |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($caseSensitive = false) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->isCaseSensitive = $caseSensitive; |
|
49
|
|
|
$this->hint = 'Commit message should not contain blacklisted words'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set body blacklist. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $list |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setBodyBlacklist(array $list) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->setBlacklist($list, 'body'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set subject blacklist. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $list |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function setSubjectBlacklist(array $list) |
|
68
|
|
|
{ |
|
69
|
5 |
|
$this->setBlacklist($list, 'subject'); |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Blacklist setter. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $list |
|
76
|
|
|
* @param string $type |
|
77
|
|
|
*/ |
|
78
|
5 |
|
protected function setBlacklist(array $list, $type) |
|
79
|
|
|
{ |
|
80
|
5 |
|
$this->blacklist[$type] = $list; |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check if the message contains blacklisted words. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function pass(CommitMessage $msg) |
|
90
|
|
|
{ |
|
91
|
4 |
|
return $this->isSubjectValid($msg) && $this->isBodyValid($msg); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Check commit message subject for blacklisted words. |
|
96
|
|
|
* |
|
97
|
|
|
* @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
4 |
|
protected function isSubjectValid(CommitMessage $msg) |
|
101
|
|
|
{ |
|
102
|
4 |
|
return !$this->containsBlacklistedWord($this->blacklist['subject'], $msg->getSubject()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Check commit message body for blacklisted words. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
3 |
|
protected function isBodyValid(CommitMessage $msg) |
|
112
|
|
|
{ |
|
113
|
3 |
|
return !$this->containsBlacklistedWord($this->blacklist['subject'], $msg->getBody()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Contains blacklisted word. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $list |
|
120
|
|
|
* @param string $content |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
4 |
|
protected function containsBlacklistedWord(array $list, $content) |
|
124
|
|
|
{ |
|
125
|
4 |
|
if (!$this->isCaseSensitive) { |
|
126
|
4 |
|
$content = strtolower($content); |
|
127
|
4 |
|
$list = array_map('strtolower', $list); |
|
128
|
|
|
} |
|
129
|
4 |
|
foreach ($list as $term) { |
|
130
|
4 |
|
if (strpos($content, $term) !== false) { |
|
131
|
1 |
|
$this->hint .= PHP_EOL . 'Invalid use of \'' . $term . '\''; |
|
132
|
4 |
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
3 |
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|