- Timestamp:
- 01/11/2015 11:00:02 AM (11 years ago)
- Location:
- sites/trunk/svn.wordpress.org/includes/slack-trac-hooks/trac
- Files:
-
- 1 added
- 1 moved
-
. (added)
-
commit-handler.php (moved) (moved from sites/trunk/svn.wordpress.org/includes/slack-trac-hooks/commits.php) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/svn.wordpress.org/includes/slack-trac-hooks/trac/commit-handler.php
r1080 r1121 1 1 <?php 2 2 3 namespace Dotorg\SlackTracHooks; 3 namespace Dotorg\Slack\Trac; 4 use Dotorg\Slack\Send; 4 5 5 class Commit s{6 class Commit_Handler { 6 7 protected $trac; 7 8 protected $repo; 8 9 protected $rev; 9 protected $s lack_hook;10 protected $send; 10 11 11 12 protected $test_mode = false; 12 13 protected $svnlook = '/usr/bin/svnlook'; 13 14 14 public function __construct( $trac, $repo, $rev, $slack_hook ) { 15 $this->set_trac( $trac ); 15 public function __construct( Send $send, $trac, $repo, $rev ) { 16 $this->send = $send; 17 $this->trac = Trac::get( $trac ); 16 18 $this->repo = $repo; 17 $this->rev = $rev; 18 $this->slack_hook = $slack_hook; 19 $this->rev = $rev; 19 20 } 20 21 21 public function use_test_channel( $enabled ) { 22 $this->test_mode = (bool) $enabled; 22 public function run() { 23 $this->generate_payload(); 24 foreach ( $this->trac->get_commit_channels( $this->svnlook( 'changed' ) ) as $channel ) { 25 $this->send->send( $channel ); 26 } 27 } 28 29 public function testing( $enabled ) { 30 $this->send->testing( (bool) $enabled ); 23 31 } 24 32 … … 27 35 } 28 36 29 public function run() {30 $payload = $this->generate_payload();31 foreach ( $this->trac->get_commit_channels( $this->svnlook( 'changed' ) ) as $channel ) {32 $this->send( $channel, $payload );33 }34 }35 36 protected function set_trac( $trac ) {37 $class = __NAMESPACE__ . '\\' . $trac . '_Trac';38 $this->trac = new $class;39 }40 41 37 protected function generate_payload() { 42 38 $author = $this->svnlook( 'author' ); 43 $log = $this->format_log_for_slack($this->svnlook( 'log' ) );39 $log = Commit::format_commit_for_slack( $this->trac, $this->svnlook( 'log' ) ); 44 40 45 41 $url = $this->trac->get_commit_template( $this->rev ); … … 47 43 48 44 $username = $this->trac->get_commit_username(); 49 $ emoji = $this->trac->get_emoji();45 $icon = $this->trac->get_icon(); 50 46 $color = $this->trac->get_color(); 51 47 … … 53 49 $fallback = "$revision by $author: $log"; 54 50 55 return array( 56 'channel' => '#test', 57 'username' => $username, 58 'icon_emoji' => $emoji, 59 'attachments' => array( array( 60 'color' => $color, 61 'pretext' => $pretext, 62 'text' => $log, 63 'fallback' => $fallback, 64 'mrkdwn_in' => array( 'text', 'pretext' ), 65 ) ), 66 ); 67 } 68 69 protected function format_log_for_slack( $log ) { 70 foreach ( $this->trac->get_log_replacements() as $find => $replace ) { 71 $log = preg_replace( $find, '<' . $replace . '|$0>', $log ); 72 } 73 return $log; 51 $this->send->set_username( $username ); 52 $this->send->set_icon( $icon ); 53 $this->send->add_attachment( array( 54 'color' => $color, 55 'pretext' => $pretext, 56 'text' => $log, 57 'fallback' => $fallback, 58 'mrkdwn_in' => array( 'text', 'pretext', 'fallback' ), 59 ) ); 74 60 } 75 61 … … 81 67 ); 82 68 $args = array_map( 'escapeshellarg', $args ); 83 $command = escapeshellcmd( $this->svnlook ) . ' ' . implode( ' ', $args);69 $command = escapeshellcmd( $this->svnlook . ' ' . implode( ' ', $args ) ); 84 70 85 71 if ( $subcommand === 'changed' ) { … … 98 84 exit( 1 ); 99 85 } 100 101 protected function send( $channel, $payload ) {102 if ( $this->test_mode ) {103 $payload['attachments'][0]['pretext'] = "[$channel] " . $payload['attachments'][0]['pretext'];104 $payload['attachments'][0]['fallback'] = "[$channel] " . $payload['attachments'][0]['fallback'];105 } else {106 $payload['channel'] = $channel;107 }108 109 $context = stream_context_create( array(110 'http' => array(111 'method' => 'POST',112 'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,113 'content' => http_build_query( array( 'payload' => json_encode( $payload ) ) ),114 ),115 ) );116 117 file_get_contents( $this->slack_hook, false, $context );118 }119 86 }
Note: See TracChangeset
for help on using the changeset viewer.