Changeset 3054 for sites/trunk/svn.wordpress.org/bin/slack-trac-hook.php
- Timestamp:
- 05/01/2016 09:24:19 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/svn.wordpress.org/bin/slack-trac-hook.php
r1079 r3054 1 #!/usr/bin/env php2 1 <?php 3 2 /** 4 3 * This script receives mail directly from Trac and sends tickets and commits to Slack. 5 *6 * New tickets are merely identified here. It is then passed off to the API.7 * That will change when the libraries are merged.8 *9 * New comments are processed here and sent to Slack.10 4 */ 11 5 12 define( 'INC', dirname( __DIR__ ) . '/includes/slack-trac-hooks' );13 // This is a shared secret so the Trac server can ping the API server for processing tickets. 14 define( ' NEW_TICKET_API', 'https://api.wordpress.org/dotorg/slack/newticket.php?token=...' );15 // Comments are processed and posted directly from here. 16 define( ' SLACK_SENDING_HOOK', 'https://hooks.slack.com/services/...' );6 require '/home/svn/bin/includes/slack/autoload.php'; 7 8 define( 'WEBHOOK', 'https://hooks.slack.com/services/...' ); 9 define( 'MENTIONS_API_HANDLER', 'https://api.wordpress.org/dotorg/trac/mentions-handler.php' ); 10 define( 'MENTIONS_API_KEY', '...' ); 17 11 18 12 $lines = array(); … … 37 31 38 32 if ( empty( $type ) ) { 39 exit( 1 );33 return; 40 34 } 41 35 42 36 if ( $type === 'ticket' ) { 43 slack_ticket_hook( $trac, $ticket ); 37 $trac = \Dotorg\Slack\Trac\Trac::get( $trac ); 38 if ( ! $trac->is_public() ) { 39 // Comment emails are parsed, but ticket emails are 40 // only used as a trigger for an HTTP request to the 41 // ticket to retrieve a CSV. Bail for non-public tickets. 42 return; 43 } 44 45 $new_ticket = new \Dotorg\Slack\Trac\New_Ticket( $trac, $ticket ); 46 47 foreach ( $trac->get_ticket_channels( $new_ticket ) as $channel ) { 48 $send = new \Dotorg\Slack\Send( WEBHOOK ); 49 $send->set_user( $new_ticket ); 50 51 if ( 'title' === $trac->get_ticket_format( $channel ) ) { 52 $send->set_text( $new_ticket->get_text() ); 53 } else { 54 $send->add_attachment( $new_ticket->get_attachment() ); 55 } 56 57 $send->send( $channel ); 58 } 59 $payload = (array) $new_ticket->fetch(); 60 array_shift( $payload ); 61 $payload['ticket_id'] = $new_ticket->id; 62 $payload['ticket_url'] = $new_ticket->get_url(); 63 $payload['trac'] = $trac->get_slug(); 64 maybe_add_ticket_cc_to_payload( $payload ); 65 send_mentions_payload( $payload ); 44 66 } else { 45 require INC . '/comments.php'; 46 require INC . '/trac.php'; 47 require INC . '/config.php'; 48 $args = Dotorg\SlackTracHooks\Comments\process_message( $lines ); 49 $args['trac'] = $trac; 50 $args['ticket'] = $ticket; 51 $args['ticket_url'] = $ticket_url; 52 $args['comment_url'] = $comment_url; 53 Dotorg\SlackTracHooks\Comments\send( SLACK_SENDING_HOOK, $args ); 67 $send = new \Dotorg\Slack\Send( WEBHOOK ); 68 $handler = new \Dotorg\Slack\Trac\Comment_Handler( $send, $lines ); 69 $handler->run(); 70 71 if ( false !== strpos( $handler->comment, '#!CommitTicketReference' ) ) { 72 return; 73 } 74 75 $payload = compact( 'type', 'trac' ); 76 $properties = array( 'author', 'comment', 'changes', 'ticket_id', 'ticket_url', 'comment_id', 'comment_url' ); 77 foreach ( $properties as $property ) { 78 $payload[ $property ] = $handler->$property; 79 } 80 $payload['summary'] = $handler->title; 81 maybe_add_ticket_cc_to_payload( $payload ); 82 send_mentions_payload( $payload ); 54 83 } 55 84 56 function slack_ticket_hook( $trac, $ticket ) { 57 $payload = array( 58 'trac' => $trac, 59 'ticket' => $ticket, 60 ); 85 function send_mentions_payload( $payload ) { 86 $payload = json_encode( $payload ); 87 $secret = MENTIONS_API_KEY; 88 $content = http_build_query( compact( 'payload', 'secret' ) ); 61 89 62 90 $context = stream_context_create( array( … … 64 92 'method' => 'POST', 65 93 'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL, 66 'content' => http_build_query( $payload ),94 'content' => $content, 67 95 ), 68 96 ) ); 69 97 70 file_get_contents( NEW_TICKET_API, false, $context );98 return file_get_contents( MENTIONS_API_HANDLER, false, $context ); 71 99 } 72 100 101 function maybe_add_ticket_cc_to_payload( &$payload ) { 102 if ( $payload['trac'] === 'security' && file_exists( __DIR__ . '/security-trac-cc.sh' ) ) { 103 $cc = trim( shell_exec( escapeshellcmd( __DIR__ . '/security-trac-cc.sh ' . escapeshellarg( (int) $payload['ticket_id'] ) ) ) ); 104 if ( $cc ) { 105 $payload['cc'] = $cc; 106 } 107 } 108 }
Note: See TracChangeset
for help on using the changeset viewer.