-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathMantisSourceGitBasePlugin.class.php
More file actions
204 lines (184 loc) · 5.58 KB
/
MantisSourceGitBasePlugin.class.php
File metadata and controls
204 lines (184 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
# Copyright (c) 2017 Damien Regad
# Licensed under the MIT license
require_once( 'MantisSourcePlugin.class.php' );
/**
* Class MantisSourceGitBasePlugin
*
* Base class providing common methods for all git-based Source Integration
* Plugin classes.
*
*/
abstract class MantisSourceGitBasePlugin extends MantisSourcePlugin
{
/**
* Git branch name validation regex.
* Based on rules defined in man page
* http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
* - Must not start with '/'; cannot contain '/.', '//', '@{' or '\';
* cannot be a single '@': `^(?!/|.*([/.]\.|//|@\{|\\\\)|@$)`
* - One or more chars, except the following: ASCII control, space,
* tilde, caret, colon, question mark, asterisk, open bracket:
* `[^\000-\037\177 ~^:?*[]+`
* - Must not end with '.lock', '/' or '.': `(?<!\.lock|[/.])$`
*/
private $valid_branch_regex = '%^(?!/|.*([/.]\.|//|@\{|\\\\)|@$)[^\000-\037\177 ~^:?*[]+(?<!\.lock|[/.])$%';
/**
* @var bool Parent class includes global configuratino
*/
public $configuration = true;
/**
* @var bool Prevent more than one Git-based plugin from processing form
*/
private static $config_form_handled = false;
/**
*
*/
const CFG_DEFAULT_PRIMARY_BRANCH = 'git_default_primary_branch';
/**
* Error constants
*/
const ERROR_INVALID_BRANCH = 'invalid_branch';
const ERROR_INVALID_DATE = 'invalid_date';
/**
* Define plugin's Error strings
* @return array
*/
public function errors() {
$t_errors_list = array(
self::ERROR_INVALID_BRANCH,
self::ERROR_INVALID_DATE,
);
foreach( $t_errors_list as $t_error ) {
$t_errors[$t_error] = plugin_lang_get( 'error_' . $t_error, 'Source' );
}
return array_merge( parent::errors(), $t_errors );
}
/**
* Determines if given string name is a valid git branch name.
* @param string $p_branch Branch name to validate
* @return bool True if valid
*/
protected function is_branch_valid( $p_branch )
{
return (bool)preg_match( $this->valid_branch_regex, $p_branch );
}
/**
* Determines if given string name is a valid regex.
* @param string $p_regex regex to validate
* @return bool True if valid
*/
protected function is_regex_branch_valid( $p_regex )
{
# Trick to compile the regex
# see ReturnValues of the official doc https://www.php.net/manual/en/function.preg-match.php
return (0 === preg_match( $p_regex, "" ));
}
/**
* Triggers an error if the branch is invalid
* @param string $p_branch Branch name to validate
* @return void
*/
protected function ensure_branch_valid( $p_branch )
{
if( !$this->is_branch_valid( $p_branch ) ) {
error_parameters( $p_branch );
plugin_error( self::ERROR_INVALID_BRANCH );
}
}
/**
* Validates a comma-delimited list of git branches.
* Triggers an ERROR_INVALID_BRANCH if one of the branches is invalid
* @param string $p_list Comma-delimited list of branch names, or a regex, or '*'
* @return void
*/
protected function validate_branch_list( $p_list )
{
#Case '*'
if( $p_list == '*' ) {
return;
}
#Case regex
if( preg_match( "/^\/.+\/[a-z]*$/i", $p_list ) ) {
return;
}
#Case list of validi git branches
foreach( explode( ',', $p_list ) as $t_branch ) {
$this->ensure_branch_valid( trim( $t_branch ) );
}
}
/**
* Retrieves the default primary branches from Source plugin's config
* @return string
*/
protected function get_default_primary_branches() {
plugin_push_current( 'Source' );
$t_value = plugin_config_get( self::CFG_DEFAULT_PRIMARY_BRANCH, 'master' );
plugin_pop_current();
return $t_value;
}
/**
* Validates a date
* Triggers an ERROR_INVALID_DATE if date is not valid
* @return void
*/
protected function validate_date($p_date) {
if (empty($p_date)) {
return;
}
if (! (bool)strtotime($p_date)) {
error_parameters( $p_date );
plugin_error( self::ERROR_INVALID_DATE );
}
}
/**
* Output form elements for configuration options.
*/
public function update_config_form() {
# Prevent more than one Git-based class from outputting form elements.
if( !MantisSourceGitBasePlugin::$config_form_handled ) {
plugin_push_current( 'Source' );
MantisSourceGitBasePlugin::$config_form_handled = true;
?>
<tr class="spacer"></tr>
<tr>
<td colspan="2"><h4><?php echo plugin_lang_get( 'git_title' ) ?></h4></td>
</tr>
<tr>
<td class="category">
<label for="<?php echo self::CFG_DEFAULT_PRIMARY_BRANCH ?>">
<?php echo plugin_lang_get( self::CFG_DEFAULT_PRIMARY_BRANCH ) ?>
</label>
</td>
<td>
<input id="<?php echo self::CFG_DEFAULT_PRIMARY_BRANCH ?>"
name="<?php echo self::CFG_DEFAULT_PRIMARY_BRANCH ?>"
type="text" class="input-sm" size="50"
value="<?php echo string_attribute( plugin_config_get( self::CFG_DEFAULT_PRIMARY_BRANCH, 'master' ) ) ?>"
/>
<br>
<span class="small"><?php echo plugin_lang_get( 'git_default_primary_branch_info' ) ?></span>
</td>
</tr>
<tr></tr>
<?php
plugin_pop_current();
}
}
/**
* Process form elements for configuration options.
*/
public function update_config() {
# Prevent more than one SVN class from handling form elements.
if( !MantisSourceGitBasePlugin::$config_form_handled ) {
MantisSourceGitBasePlugin::$config_form_handled = true;
plugin_push_current( 'Source' );
$f_default_branch = trim( gpc_get_string( self::CFG_DEFAULT_PRIMARY_BRANCH ) ) ?: 'master';
$t_default_branch = plugin_config_get( self::CFG_DEFAULT_PRIMARY_BRANCH, 'master' );
if ( $f_default_branch != $t_default_branch ) {
plugin_config_set( self::CFG_DEFAULT_PRIMARY_BRANCH, $f_default_branch );
}
plugin_pop_current();
}
}
}