Changeset 14061 for sites/trunk/wordpress.org/public_html/wp-content/plugins/phpunit-test-reporter/src/class-restapi.php
- Timestamp:
- 09/18/2024 02:32:35 AM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/phpunit-test-reporter/src/class-restapi.php
r10290 r14061 114 114 } 115 115 116 $env = isset( $parameters['env'] ) ? json_decode( $parameters['env'], true ) : array(); 117 118 $php_version = ''; 119 if ( isset( $env['php_version'] ) ) { 120 $parts = explode( '.', $env['php_version'] ); 121 $php_version = $parts[0] . '.' . $parts[1]; 122 } 123 124 $db_version = ! empty( $parameters['mysql_version'] ) ? $parameters['mysql_version'] : 'Unknown'; 125 $env_name = ! empty( $env['label'] ) ? wp_kses( $env['label'], [] ) : ''; 126 116 127 $current_user = wp_get_current_user(); 117 118 $args = array( 128 $tax_query = [ 129 'relation' => 'AND', 130 ]; 131 132 if ( $php_version ) { 133 $tax_query[] = array( 134 'taxonomy' => 'php-version', 135 'terms' => [ $php_version ], 136 'field' => 'name', 137 ); 138 } 139 140 if ( $db_version ) { 141 $tax_query[] = array( 142 'taxonomy' => 'db-version', 143 'terms' => [ $db_version ], 144 'field' => 'name', 145 ); 146 } 147 148 $meta_query = []; 149 150 if ( $env_name ) { 151 $meta_query[] = array( 152 'key' => 'environment_name', 153 'value' => $env_name, 154 ); 155 } 156 157 // Check to see if the test result already exist. 158 $results = get_posts( array( 119 159 'post_parent' => $parent_id, 120 160 'post_type' => 'result', 121 161 'numberposts' => 1, 122 162 'author' => $current_user->ID, 123 );124 125 // Check to see if the test result already exist.126 $results = get_posts( $args ); 163 'tax_query' => $tax_query, 164 'meta_query' => $meta_query, 165 ) ); 166 127 167 if ( $results ) { 128 168 $post_id = $results[0]->ID; 129 169 } else { 130 $results = array( 131 'post_title' => $current_user->user_login . ' - ' . $slug, 170 $post_title = $current_user->user_login . ' - ' . $slug; 171 172 if ( $env_name ) { 173 $post_title .= ' - ' . $env_name; 174 } 175 176 if ( $php_version ) { 177 $post_title .= ' - ' . $php_version; 178 } 179 180 if ( $db_version ) { 181 $post_title .= ' - ' . $db_version; 182 } 183 184 $args = array( 185 'post_title' => $post_title, 132 186 'post_content' => '', 133 187 'post_status' => 'publish', … … 138 192 139 193 // Store the results. 140 $post_id = wp_insert_post( $ results, true );194 $post_id = wp_insert_post( $args, true ); 141 195 } 142 196 … … 145 199 } 146 200 201 wp_set_object_terms( $post_id, array( $php_version ), 'php-version' ); 202 wp_set_object_terms( $post_id, array( $db_version ), 'db-version' ); 203 147 204 $env = isset( $parameters['env'] ) ? json_decode( $parameters['env'], true ) : array(); 148 205 $results = isset( $parameters['results'] ) ? json_decode( $parameters['results'], true ) : array(); … … 150 207 update_post_meta( $post_id, 'env', $env ); 151 208 update_post_meta( $post_id, 'results', $results ); 209 update_post_meta( $post_id, 'environment_name', $env_name ); 210 211 $outcome = 'Unknown'; 212 213 if ( ! empty( $results['failures'] ) ) { 214 $outcome = 'Failed'; 215 } elseif ( ! empty( $results['errors'] ) ) { 216 $outcome = 'Errored'; 217 } else { 218 $outcome = 'Passed'; 219 } 220 221 wp_set_object_terms( $post_id, $outcome, 'report-result' ); 152 222 153 223 self::maybe_send_email_notifications( $parent_id ); … … 167 237 168 238 return $response; 239 } 240 241 private static function get_new_failures( $post_id ) { 242 $p = get_post( $post_id ); 243 $parent_id = $p->post_parent; 244 245 $post_terms = wp_get_object_terms( $post_id, array( 'php-version', 'db-version' ) ); 246 247 if ( is_wp_error( $post_terms ) ) { 248 return []; 249 } 250 251 $tax_query = []; 252 $meta_query = []; 253 254 foreach ( $post_terms as $term ) { 255 if ( 'php-version' === $term['taxonomy'] ) { 256 $tax_query[] = array( 257 'taxonomy' => 'php-version', 258 'terms' => [ $term['term_id'] ], 259 ); 260 } 261 262 if ( 'db-version' === $term['taxonomy'] ) { 263 $tax_query[] = array( 264 'taxonomy' => 'db-version', 265 'terms' => [ $term['term_id'] ], 266 ); 267 } 268 } 269 270 $env_name = get_post_meta( $post_id, 'environment_name', true ); 271 272 if ( ! empty( $env_name ) ) { 273 $meta_query [] = array( 274 'key' => 'environment_name', 275 'value' => $env_name, 276 ); 277 } 278 279 $previous_results = get_posts( array( 280 'post_parent__not_in' => [ $parent_id ], 281 'post_type' => 'result', 282 'numberposts' => 1, 283 'author' => $p->post_author, 284 'tax_query' => $tax_query, 285 'meta_query' => $meta_query, 286 ) ); 287 288 $new_failures = []; 289 $current_failures = self::get_failures( $post_id ); 290 $previous_failures = []; 291 292 if ( ! empty( $previous_results ) ) { 293 $previous_failures = self::get_failures( $previous_results[0]->ID ); 294 } 295 296 // Find new failures that didn't exist in the previous run. 297 298 foreach( $current_failures as $test_suite => $test_cases ) { 299 foreach( $test_cases as $test_case ) { 300 if ( 301 ! isset( $previous_failures[ $test_suite] ) || 302 ! in_array( $test_case, $previous_failures[ $test_suite ] ) 303 ) { 304 $new_failures[] = "$test_suite::$test_case"; 305 } 306 } 307 } 308 309 return $new_failures; 310 } 311 312 private static function get_failures( $post_id ) { 313 $results = get_post_meta( $post_id, 'results', true ); 314 if ( empty( $results['failures'] ) && empty( $results['errors'] ) ) { 315 return []; 316 } 317 318 $failures = []; 319 320 foreach ( $results['testsuites'] as $suite_name => $testsuite ) { 321 $failures[ $suite_name ] = array_keys( $testsuite['testcases'] ); 322 } 323 324 return $failures; 169 325 } 170 326 … … 205 361 continue; 206 362 } 363 207 364 // If the test result is failed and we haven't yet sent an 208 365 // email notification, then let the reporter know. 209 if ( self::is_failed_result( $result ) 210 && ! get_post_meta( $result->ID, 'ptr_reported_failure', true ) ) { 366 if ( 367 self::is_failed_result( $result ) && 368 ! get_post_meta( $result->ID, 'ptr_reported_failure', true ) 369 ) { 370 $new_failures = self::get_new_failures( $result->ID ); 371 372 if ( empty( $new_failures ) ) { 373 continue; 374 } 375 211 376 $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 ); 377 if ( ! $user ) { 378 continue; 221 379 } 380 381 $subject = '[Host Test Results] Test failure for ' . $result->post_name; 382 $body = 'Hi there,' . PHP_EOL . PHP_EOL 383 . "We've detected a new WordPress PHPUnit test failure on your hosting environment. Please review when you have a moment: " 384 . get_permalink( $result->ID ) . PHP_EOL . PHP_EOL 385 . 'New failures:' . PHP_EOL . PHP_EOL 386 . implode( PHP_EOL, $new_failures ) . PHP_EOL . PHP_EOL 387 . 'Thanks,' . PHP_EOL . PHP_EOL 388 . 'WordPress.org Hosting Community'; 389 390 wp_mail( $user->user_email, $subject, $body ); 391 update_post_meta( $result->ID, 'ptr_reported_failure', true ); 222 392 } 223 393 } … … 228 398 * Whether or not a given result is a failed result. 229 399 * 230 * @param WP_Post $post Result post object.400 * @param \WP_Post $post Result post object. 231 401 * @return boolean 232 402 */
Note: See TracChangeset
for help on using the changeset viewer.