|
| 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 | +} |
0 commit comments