- Timestamp:
- 01/11/2015 11:00:02 AM (10 years ago)
- Location:
- sites/trunk/svn.wordpress.org/includes/slack-trac-hooks/trac
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/svn.wordpress.org/includes/slack-trac-hooks/trac/trac.php
r1080 r1121 1 1 <?php 2 2 3 namespace Dotorg\SlackTracHooks; 4 5 abstract class Trac { 6 protected $commit_channels = '#test'; 7 protected $commit_username = 'Commit'; 8 protected $ticket_channels = '#test'; 3 namespace Dotorg\Slack\Trac; 4 use Dotorg\Slack\User; 5 6 class Trac implements User { 7 protected $public = true; 8 protected $commits = true; 9 protected $tickets = true; 10 11 protected $primary_channel = '#test'; 12 protected $tickets_channel = false; 13 protected $commits_channel = false; 14 protected $firehose_channel = false; 15 protected $commit_username; 9 16 protected $ticket_username; 10 17 11 protected $firehose_channel = false; 12 13 protected $color = '#21759b'; 14 protected $emoji = ':wordpress:'; 18 // 'title', 'fields', 'description' 19 protected $primary_channel_ticket_format = 'description'; 20 21 protected $bypass_primary_channel_for_commit_filter_matches = false; 22 protected $bypass_primary_channel_for_ticket_filter_matches = false; 23 24 protected $commit_path_filters = array(); 25 protected $ticket_component_filters = array(); 26 27 protected $color = '#21759b'; 28 protected $icon = ':wordpress:'; 15 29 16 30 protected $ticket_template = 'https://%s.trac.wordpress.org/ticket/%s'; 17 31 protected $commit_template = 'https://%s.trac.wordpress.org/changeset/%s'; 18 protected $ticket_range = array( 1, 5 ); 19 protected $commit_range = array( 1, 5 ); 20 21 function __construct() { 22 // 'Dotorg\SlackTracHooks\Core_Trac' => 'Core_Trac' 23 $class = str_replace( __NAMESPACE__ . '\\', '', get_class( $this ) ); 24 25 // 'Core_Trac' => 'core' 26 $this->trac = strtolower( str_replace( '_Trac', '', $class ) ); 32 protected $commit_info_template = 'https://%s.trac.wordpress.org/log/?rev=%s&format=changelog&limit=1&verbose=on'; 33 34 const min_digits = 2; 35 const max_digits = 5; 36 37 static protected $shorthands = array( 38 'wp' => 'core', 39 'wordpress' => 'core', 40 'develop' => 'core', 41 'bb' => 'bbpress', 42 ); 43 44 protected function __construct() { 45 if ( isset( $this->trac, $this->name ) ) { 46 return; 47 } 48 49 // 'Dotorg\Slack\Trac\Tracs\Core' => 'Core' 50 $class = str_replace( __NAMESPACE__ . '\\Tracs\\', '', get_class( $this ) ); 51 52 if ( ! isset( $this->trac ) ) { 53 $this->trac = strtolower( $class ); 54 } 55 56 if ( ! isset( $this->name ) ) { 57 $this->name = str_replace( '_', ' ', $class ); 58 } 27 59 28 60 if ( ! isset( $this->ticket_username ) ) { 29 // 'Core_Trac' => 'Core Trac' 30 $this->ticket_username = str_replace( '_', ' ', $class ); 31 } 61 $this->ticket_username = $this->name . ' Trac'; 62 } 63 64 if ( ! isset( $this->commit_username ) ) { 65 $this->commit_username = $this->name . ' commit'; 66 } 67 68 foreach ( $this->commit_path_filters as $path => $channel ) { 69 if ( is_string( $channel ) ) { 70 $this->commit_path_filters[ $path ] = array( $channel => true ); 71 } 72 if ( $this->bypass_primary_channel_for_commit_filter_matches && empty( $this->commit_path_filters[ $path ][ $this->primary_channel ] ) ) { 73 $this->commit_path_filters[ $path ][ $this->primary_channel ] = false; 74 } 75 } 76 77 foreach ( $this->ticket_component_filters as $component => $channel ) { 78 if ( is_string( $channel ) ) { 79 $this->ticket_component_filters[ $component ] = array( $channel => true ); 80 } 81 if ( $this->bypass_primary_channel_for_ticket_filter_matches && empty( $this->ticket_component_filters[ $path ][ $this->primary_channel ] ) ) { 82 $this->ticket_component_filters[ $path ][ $this->primary_channel ] = false; 83 } 84 } 85 } 86 87 static function get( $trac ) { 88 require_once __DIR__ . '/config.php'; 89 if ( $trac instanceof Trac ) { 90 return $trac; 91 } 92 93 $ns = __NAMESPACE__ . '\\Tracs\\'; 94 95 $class = $ns . $trac; 96 if ( class_exists( $class ) ) { 97 return new $class; 98 } 99 100 if ( isset( self::$shorthands[ $trac ] ) ) { 101 $class = $ns . self::$shorthands[ $trac ]; 102 return new $class; 103 } 104 105 if ( preg_match( '~([a-z]+).trac.wordpress.org~', $url, $match ) ) { 106 $class = $ns . $match[1]; 107 if ( class_exists( $class ) ) { 108 return new $class; 109 } 110 } 111 112 return false; 113 } 114 115 static function get_regex() { 116 return implode( '|', array_merge( self::get_registered_tracs(), array_keys( self::$shorthands ) ) ); 117 } 118 119 static function get_registered_tracs() { 120 require_once __DIR__ . '/config.php'; 121 $classes = get_declared_classes(); 122 $tracs = array(); 123 124 foreach ( $classes as $key => $class ) { 125 if ( 0 !== strpos( $class, __NAMESPACE__ . '\\Tracs\\' ) ) { 126 continue; 127 } 128 129 $tracs[] = strtolower( str_replace( array( __NAMESPACE__ . '\\Tracs\\', '_' ), array( '', '-' ), $class ) ); 130 } 131 132 return $tracs; 133 } 134 135 function get_slug() { 136 return $this->trac; 32 137 } 33 138 … … 44 149 } 45 150 46 function get_emoji() { 47 return $this->emoji; 151 function get_name() { 152 return $this->name . ' Trac'; 153 } 154 155 function get_icon() { 156 return $this->icon; 157 } 158 159 function get_ticket_url( $ticket ) { 160 return $this->get_ticket_template( $ticket ); 48 161 } 49 162 … … 52 165 } 53 166 167 function get_commit_url( $commit ) { 168 return $this->get_commit_template( $commit ); 169 } 170 54 171 function get_commit_template( $template = '%s' ) { 55 172 return sprintf( $this->commit_template, $this->trac, $template ); 56 173 } 57 174 58 function get_digit_capture( $range ) { 59 $min = $range[0] - 1; 60 $max = $range[1] - 1; 61 return '[0-9]\d{' . $min . ',' . $max . '}'; 175 function get_commit_info_url( $commit ) { 176 return sprintf( $this->commit_info_template, $this->trac, $commit ); 177 } 178 179 function is_public() { 180 return (bool) $this->public; 181 } 182 183 function has_tickets() { 184 return (bool) $this->tickets; 185 } 186 187 function has_commits() { 188 return (bool) $this->commits; 189 } 190 191 static function get_digit_capture() { 192 $min = self::min_digits - 1; 193 $max = self::max_digits - 1; 194 return '[1-9][0-9]{' . $min . ',' . $max . '}'; 62 195 } 63 196 64 197 function get_log_replacements() { 65 198 $commit_template = $this->get_commit_template( '$1' ); 66 $ticket_digits = $this->get_digit_capture( $this->ticket_range ); 67 $commit_digits = $this->get_digit_capture( $this->commit_range ); 199 $digits = self::get_digit_capture(); 68 200 69 201 return array( 70 "/#($ ticket_digits)\b/" => $this->get_ticket_template( '$1' ),71 "/\[($ commit_digits)\]/" => $commit_template,72 "/\br($ commit_digits)\b/" => $commit_template,202 "/#($digits)\b/" => $this->get_ticket_template( '$1' ), 203 "/\[($digits)\]/" => $commit_template, 204 "/\br($digits)\b/" => $commit_template, 73 205 ); 74 206 } 75 207 76 208 function get_commit_channels( $changed_files = null ) { 77 $channels = (array) $this->commit_channels;78 79 if ( ! empty( $this->firehose_channel )) {80 $channels[ ] = $this->firehose_channel;81 } 82 83 if ( empty( $this->commit_path_filters ) || empty( $changed_files )) {84 return $channels;209 $channels = array(); 210 211 if ( $this->primary_channel ) { 212 $channels[ $this->primary_channel ] = true; 213 } 214 215 if ( $this->commits_channel ) { 216 $channels[ $this->commits_channel ] = true; 85 217 } 86 218 … … 95 227 } 96 228 } 97 98 return $channels; 229 230 if ( $this->firehose_channel ) { 231 $channels[ $this->firehose_channel ] = true; 232 } 233 234 return array_keys( array_filter( $channels ) ); 99 235 } 100 236 101 237 function get_ticket_channels( $ticket = null ) { 102 $channels = (array) $this->ticket_channels;103 104 if ( ! empty( $this->firehose_channel )) {105 $channels[ ] = $this->firehose_channel;106 } 107 108 if ( empty( $this->ticket_component_filters ) || empty( $ticket )) {109 return $channels;238 $channels = array(); 239 240 if ( $this->primary_channel ) { 241 $channels[ $this->primary_channel ] = true; 242 } 243 244 if ( $this->tickets_channel ) { 245 $channels[ $this->tickets_channel ] = true; 110 246 } 111 247 … … 121 257 } 122 258 } 123 return $channels; 259 260 if ( $this->firehose_channel ) { 261 $channels[ $this->firehose_channel ] = true; 262 } 263 264 return array_keys( array_filter( $channels ) ); 124 265 } 125 266 … … 127 268 return $this->firehose_channel; 128 269 } 270 271 function get_ticket_format( $channel ) { 272 if ( $channel === $this->primary_channel ) { 273 return $this->primary_channel_ticket_format; 274 } 275 return 'description'; 276 } 277 278 function format_for_slack( $text ) { 279 $text = str_replace( "\r\n", "\n", $text ); 280 $text = trim( str_replace( 281 array( "\n{{{\n", "\n}}}\n", '{{{', '}}}' ), 282 array( "\n```\n", "\n```\n", '`', '`' ), 283 "\n$text\n" 284 ), "\n" ); 285 286 // TODO: bold, italic, links 287 288 return $text; 289 } 129 290 } 130
Note: See TracChangeset
for help on using the changeset viewer.