Making WordPress.org


Ignore:
Timestamp:
01/11/2015 11:00:02 AM (9 years ago)
Author:
nacin
Message:

Update Slack libraries.

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/commit-handler.php

    r1080 r1121  
    11<?php
    22
    3 namespace Dotorg\SlackTracHooks;
     3namespace Dotorg\Slack\Trac;
     4use Dotorg\Slack\Send;
    45
    5 class Commits {
     6class Commit_Handler {
    67    protected $trac;
    78    protected $repo;
    89    protected $rev;
    9     protected $slack_hook;
     10    protected $send;
    1011
    1112    protected $test_mode = false;
    1213    protected $svnlook = '/usr/bin/svnlook';
    1314
    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 );
    1618        $this->repo = $repo;
    17         $this->rev = $rev;
    18         $this->slack_hook = $slack_hook;
     19        $this->rev  = $rev;
    1920    }
    2021
    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 );
    2331    }
    2432
     
    2735    }
    2836
    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 
    4137    protected function generate_payload() {
    4238        $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' ) );
    4440
    4541        $url = $this->trac->get_commit_template( $this->rev );
     
    4743
    4844        $username = $this->trac->get_commit_username();
    49         $emoji    = $this->trac->get_emoji();
     45        $icon     = $this->trac->get_icon();
    5046        $color    = $this->trac->get_color();
    5147
     
    5349        $fallback = "$revision by $author: $log";
    5450
    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        ) );
    7460    }
    7561
     
    8167        );
    8268        $args = array_map( 'escapeshellarg', $args );
    83         $command = escapeshellcmd( $this->svnlook ) . ' ' . implode( ' ', $args );
     69        $command = escapeshellcmd( $this->svnlook . ' ' . implode( ' ', $args ) );
    8470
    8571        if ( $subcommand === 'changed' ) {
     
    9884        exit( 1 );
    9985    }
    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     }
    11986}
Note: See TracChangeset for help on using the changeset viewer.