Skip to content

Commit 517da8a

Browse files
committed
New abstract base class for Git-based plugins
- Provides methods to validate a git branch name - Defines error() method for corresponding error message
1 parent 6409650 commit 517da8a

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
# Copyright (c) 2017 Damien Regad
4+
# Licensed under the MIT license
5+
6+
require_once( 'MantisSourcePlugin.class.php' );
7+
8+
/**
9+
* Class MantisSourceGitBasePlugin
10+
*
11+
* Base class providing common methods for all git-based Source Integration
12+
* Plugin classes.
13+
*
14+
*/
15+
abstract class MantisSourceGitBasePlugin extends MantisSourcePlugin
16+
{
17+
/**
18+
* Git branch name validation regex.
19+
* Based on rules defined in man page
20+
* http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
21+
*/
22+
private $valid_branch_regex = '%'
23+
# Must not start with '/'; cannot contain '/.', '//', '@{' or '\';
24+
# cannot be a single '@'.
25+
. '^(?!/|.*([/.]\.|//|@\{|\\\\)|@$)'
26+
# One or more chars, except the following: ASCII control, space,
27+
# tilde, caret, colon, question mark, asterisk, open bracket.
28+
. '[^\000-\037\177 ~^:?*[]+'
29+
# Must not end with '.lock', '/' or '.'
30+
. '(?<!\.lock|[/.])$'
31+
. '%';
32+
33+
/**
34+
* Error constants
35+
*/
36+
const ERROR_INVALID_BRANCH = 'invalid_branch';
37+
38+
/**
39+
* Define plugin's Error strings
40+
* @return array
41+
*/
42+
public function errors() {
43+
$t_errors_list = array(
44+
self::ERROR_INVALID_BRANCH,
45+
);
46+
47+
foreach( $t_errors_list as $t_error ) {
48+
$t_errors[$t_error] = plugin_lang_get( 'error_' . $t_error, 'Source' );
49+
}
50+
51+
return $t_errors;
52+
}
53+
54+
/**
55+
* Determines if given string name is a valid git branch name.
56+
* @param string $p_branch Branch name to validate
57+
* @return bool True if valid
58+
*/
59+
protected function is_branch_valid( $p_branch )
60+
{
61+
return (bool)preg_match( $this->valid_branch_regex, $p_branch );
62+
}
63+
64+
/**
65+
* Triggers an error if the branch is invalid
66+
* @param string $p_branch Branch name to validate
67+
* @return void
68+
*/
69+
protected function ensure_branch_valid( $p_branch )
70+
{
71+
if( !$this->is_branch_valid( $p_branch ) ) {
72+
error_parameters( $p_branch );
73+
plugin_error( self::ERROR_INVALID_BRANCH );
74+
}
75+
}
76+
77+
/**
78+
* Validates a comma-delimited list of git branches.
79+
* Triggers an ERROR_INVALID_BRANCH if one of the branches is invalid
80+
* @param string $p_list Comma-delimited list of branch names (or '*')
81+
* @return void
82+
*/
83+
protected function validate_branch_list( $p_list )
84+
{
85+
if( $p_list == '*' ) {
86+
return;
87+
}
88+
89+
foreach( explode( ',', $p_list ) as $t_branch ) {
90+
$this->ensure_branch_valid( trim( $t_branch ) );
91+
}
92+
}
93+
}

Source/lang/strings_english.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,4 @@ $s_plugin_Source_import_full_failed = 'Full repository data importing failed.';
147147

148148
$s_plugin_Source_changeset_column_title = 'C';
149149

150+
$s_plugin_Source_error_invalid_branch = 'Invalid characters in Branch name "%1$s".';

Source/lang/strings_german.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,5 @@ $s_plugin_Source_import_latest_failed = 'Partieller Import des Projektarchivs fe
144144
$s_plugin_Source_import_full_failed = 'Vollständiger Import des Projektarchivs fehlgeschlagen.';
145145

146146
$s_plugin_Source_changeset_column_title = 'C';
147+
148+
$s_plugin_Source_error_invalid_branch = 'Ungültige Zeichen in der Zweige "%1$s"';

0 commit comments

Comments
 (0)