Skip to content

Commit

Permalink
feat(TestRuns): associate alerts with test runs
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Jun 13, 2024
1 parent e5485dd commit 8a98554
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
13 changes: 12 additions & 1 deletion includes/list-tables/class-alerts-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,20 @@ public function get_views() {
],
];

if ( isset( $_GET['test_run_id'] ) ) {
$test_run_id = intval( $_GET['test_run_id'] );
$links['test_run'] = [
'title' => esc_html__( 'For Test Run', 'visual-regression-tests' ) . ' #' . $test_run_id,
'link' => "{$base_link}&test_run_id=" . $test_run_id,
'count' => Alert::get_total_items( null, $test_run_id ),
];
$filter_status_query = 'test_run';
} else {
$filter_status_query = ( isset( $_REQUEST['status'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ) : 'all' );
}

$status_links = [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's status request.
$filter_status_query = ( isset( $_REQUEST['status'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ) : 'all' );
foreach ( $links as $key => $link ) {
$current_class = ( $filter_status_query === $key ? 'class="current" ' : '' );
$link = sprintf(
Expand Down
12 changes: 10 additions & 2 deletions includes/models/class-alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static function get_latest_alert_id_by_post_id( $post_id = 0, $alert_stat
*
* @return array
*/
public static function get_total_items( $filter_status_query = null ) {
public static function get_total_items( $filter_status_query = null, $test_run_id = null ) {
global $wpdb;

$alerts_table = Alerts_Table::get_table_name();
Expand All @@ -193,9 +193,17 @@ public static function get_total_items( $filter_status_query = null ) {
$alert_states = ( 'archived' === $filter_status_query ) ? [ 1, 2 ] : [ 0 ];
$alert_states_placeholders = implode( ', ', array_fill( 0, count( $alert_states ), '%d' ) );

$test_run_where = '';
if ( null !== $test_run_id ) {
$test_run_where = $wpdb->prepare(
' AND test_run_id = %d',
$test_run_id
);
}

$where = $wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- It's ok.
"WHERE alert_state IN ($alert_states_placeholders)",
"WHERE alert_state IN ($alert_states_placeholders)" . $test_run_where,
$alert_states
);

Expand Down
3 changes: 2 additions & 1 deletion includes/services/class-alert-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Alert_Service {
* @param int $test_id Test ID.
* @param array $comparison Comparison.
*/
public function create_alert_from_comparison( $post_id, $test_id, $comparison ) {
public function create_alert_from_comparison( $post_id, $test_id, $comparison, $test_run = null ) {
global $wpdb;
$table_alert = Alerts_Table::get_table_name();

Expand All @@ -27,6 +27,7 @@ public function create_alert_from_comparison( $post_id, $test_id, $comparison )
$prepare_alert['comparison_screenshot_url'] = $comparison['image_url'];
$prepare_alert['comparison_id'] = $comparison['id'];
$prepare_alert['differences'] = $comparison['pixels_diff'];
$prepare_alert['test_run_id'] = $test_run ? $test_run->id : null;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- It's ok.
if ( $wpdb->insert( $table_alert, $prepare_alert ) ) {
Expand Down
6 changes: 3 additions & 3 deletions includes/services/class-test-run-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function update_run_from_api_data( $data ) {

if ( $test_run && empty( $test_run->finished_at ) && ! empty( $data['finished_at'] ) ) {
$test_run_just_finished = true;
$alert_ids = $this->update_tests_and_create_alerts( $data['comparisons'] );
$alert_ids = $this->update_tests_and_create_alerts( $data['comparisons'], $test_run );
}

$test_ids = empty( $data['comparison_schedule_ids'] ) ? [] : array_map(function( $test ) {
Expand All @@ -53,7 +53,7 @@ public function update_run_from_api_data( $data ) {
return true;
}

protected function update_tests_and_create_alerts( $comparisons ) {
protected function update_tests_and_create_alerts( $comparisons, $test_run ) {
$alert_ids = [];

foreach ( $comparisons as $comparison ) {
Expand All @@ -63,7 +63,7 @@ protected function update_tests_and_create_alerts( $comparisons ) {
if ( $comparison['pixels_diff'] > 1 && ! $comparison['matches_false_positive'] ) {
$post_id = Test::get_post_id_by_service_test_id( $test_id );
$alert_service = new Alert_Service();
$alert_id = $alert_service->create_alert_from_comparison( $post_id, $test_id, $comparison );
$alert_id = $alert_service->create_alert_from_comparison( $post_id, $test_id, $comparison, $test_run );
$alert_ids[] = $alert_id;
}//end if

Expand Down

0 comments on commit 8a98554

Please sign in to comment.