Making WordPress.org

Changeset 6522


Ignore:
Timestamp:
02/02/2018 02:28:06 PM (7 years ago)
Author:
danielbachhuber
Message:

phpunit-test-reporter: Send an email notification to reporter when test fails

See https://github.com/WordPress/phpunit-test-reporter/pull/56

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/phpunit-test-reporter/src/class-restapi.php

    r6262 r6522  
    151151        update_post_meta( $post_id, 'results', $results );
    152152
     153        self::maybe_send_email_notifications( $parent_id );
     154
    153155        // Create the response object.
    154156        $response = new \WP_REST_Response(
     
    166168        return $response;
    167169    }
     170
     171    /**
     172     * Maybe send an email notification if 'wpdevbot' has reported
     173     * a success and others have reported failures.
     174     */
     175    private static function maybe_send_email_notifications( $parent_id ) {
     176
     177        // 'wpdevbot' doesn't exist on this system, so nothing to compare.
     178        $user = get_user_by( 'login', 'wpdevbot' );
     179        if ( ! $user ) {
     180            return;
     181        }
     182        $wporgbot_id = $user->ID;
     183
     184        $args             = array(
     185            'post_parent' => $parent_id,
     186            'post_type'   => 'result',
     187            'numberposts' => -1,
     188        );
     189        $results          = get_posts( $args );
     190        $wpdevbot_results = wp_filter_object_list( $results, array( 'post_author' => $wporgbot_id ) );
     191        // 'wpdevbot' hasn't reported yet
     192        if ( empty( $wpdevbot_results ) ) {
     193            return;
     194        }
     195        $wpdevbot_result = array_shift( $wpdevbot_results );
     196        // If 'wpdevbot' is failed, we already know the test failure
     197        // and don't need to report host testing bots failures.
     198        if ( self::is_failed_result( $wpdevbot_result ) ) {
     199            return;
     200        }
     201
     202        foreach ( $results as $result ) {
     203            // Doesn't make sense to report wpdevbot to itself
     204            if ( $wpdevbot_result->ID === $result->ID ) {
     205                continue;
     206            }
     207            // If the test result is failed and we haven't yet sent an
     208            // email notification, then let the reporter know.
     209            if ( self::is_failed_result( $result )
     210                && ! get_post_meta( $result->ID, 'ptr_reported_failure', true ) ) {
     211                $user = get_user_by( 'id', $result->post_author );
     212                if ( $user ) {
     213                    $subject = '[Host Test Results] Test failure for ' . $result->post_name;
     214                    $body    = 'Hi there,' . PHP_EOL . PHP_EOL
     215                        . "We've detected a WordPress PHPUnit test failure on your hosting environment. Please review when you have a moment: "
     216                        . get_permalink( $result->ID ) . PHP_EOL . PHP_EOL
     217                        . 'Thanks,' . PHP_EOL . PHP_EOL
     218                        . 'WordPress.org Hosting Community';
     219                    wp_mail( $user->user_email, $subject, $body );
     220                    update_post_meta( $result->ID, 'ptr_reported_failure', true );
     221                }
     222            }
     223        }
     224
     225    }
     226
     227    /**
     228     * Whether or not a given result is a failed result.
     229     *
     230     * @param WP_Post $post Result post object.
     231     * @return boolean
     232     */
     233    private static function is_failed_result( $post ) {
     234        $is_failed = false;
     235        $results   = get_post_meta( $post->ID, 'results', true );
     236        if ( isset( $results['failures'] ) ) {
     237            if ( 0 !== (int) $results['failures'] || 0 !== (int) $results['errors'] ) {
     238                $is_failed = true;
     239            }
     240        }
     241        return $is_failed;
     242    }
     243
    168244}
Note: See TracChangeset for help on using the changeset viewer.