Skip to content

Commit

Permalink
Refactor manageOverview.php API endpoint (#1881)
Browse files Browse the repository at this point in the history
This PR continues our ongoing effort to migrate our legacy API to
Laravel controllers, and to convert our protractor tests to Cypress.
Supersedes #1666. I also made Cypress query the value of APP_URL each
time a test is run in case the value changes from one test to another.
This PR also fixes #1889.
  • Loading branch information
williamjallen committed Dec 15, 2023
1 parent 4f0e423 commit b155445
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 240 deletions.
110 changes: 110 additions & 0 deletions app/Http/Controllers/ProjectOverviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
namespace App\Http\Controllers;

use App\Utils\PageTimer;
use CDash\Database;
use CDash\Model\Project;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;

final class ProjectOverviewController extends AbstractProjectController
{
Expand Down Expand Up @@ -749,4 +754,109 @@ public function manageOverview(): Response
{
return response()->angular_view('manageOverview');
}

public function apiManageOverview(): JsonResponse
{
$pageTimer = new PageTimer();
$response = begin_JSON_response();
$response['backurl'] = 'user.php';
$response['menutitle'] = 'CDash';
$response['menusubtitle'] = 'Overview';
$response['hidenav'] = 1;

// Make sure the user has admin rights to this project.
$this->setProjectById(intval(request()->input('projectid')));
Gate::authorize('edit-project', $this->project);

get_dashboard_JSON($this->project->GetName(), null, $response);

// Check if we are saving an overview layout.
if (Request::isMethod('post') && request()->has('saveLayout')) {
$inputRows = json_decode(request()->input('saveLayout'), true);
if (!is_null($inputRows)) {
// Remove any old overview layout from this project.
DB::delete('DELETE FROM overview_components WHERE projectid=?', [intval($this->project->Id)]);

if (count($inputRows) > 0) {
// Construct a query to insert the new layout.
$query = 'INSERT INTO overview_components (projectid, buildgroupid, position, type) VALUES ';
$params = [];
foreach ($inputRows as $inputRow) {
$query .= '(?, ?, ?, ?),';
$params[] = intval($this->project->Id);
$params[] = intval($inputRow['id']);
$params[] = intval($inputRow['position']);
$params[] = $inputRow['type'];
}

$query = rtrim($query, ',');
DB::insert($query, $params);
}
}

// Since this is called by AJAX we don't need to generate the JSON
// used to render this page.
return response()->json();
}

// Otherwise generate the JSON used to render this page.
// Get the groups that are already included in the overview.
$query = DB::select('
SELECT
bg.id,
bg.name,
obg.type
FROM overview_components AS obg
LEFT JOIN buildgroup AS bg ON (obg.buildgroupid = bg.id)
WHERE obg.projectid = ?
ORDER BY obg.position
', [intval($this->project->Id)]);

$build_response = [];
$static_response = [];
foreach ($query as $overviewgroup_row) {
$group_response = [
'id' => intval($overviewgroup_row->id),
'name' => $overviewgroup_row->name,
];
$type = $overviewgroup_row->type;
switch ($type) {
case 'build':
$build_response[] = $group_response;
break;
case 'static':
$static_response[] = $group_response;
break;
default:
Log::warning("Unrecognized overview group type: '$type'");
break;
}
}
$response['buildcolumns'] = $build_response;
$response['staticrows'] = $static_response;

// Get the buildgroups that aren't part of the overview yet.
$buildgroup_rows = DB::select('
SELECT
bg.id,
bg.name
FROM buildgroup AS bg
LEFT JOIN overview_components AS oc ON (bg.id = oc.buildgroupid)
WHERE
bg.projectid=?
AND oc.buildgroupid IS NULL
', [intval($this->project->Id)]);

$availablegroups_response = [];
foreach ($buildgroup_rows as $buildgroup_row) {
$availablegroups_response[] = [
'id' => intval($buildgroup_row->id),
'name' => $buildgroup_row->name,
];
}
$response['availablegroups'] = $availablegroups_response;

$pageTimer->end($response);
return response()->json(cast_data_for_JSON($response));
}
}
154 changes: 0 additions & 154 deletions app/cdash/public/api/v1/manageOverview.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/cdash/public/views/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Coverage
</a>
</div>
<table class="table-bordered table table-striped table-responsive">
<table class="table-bordered table table-striped table-responsive" data-cy="coverage-table">
<tr>
<th></th>
<th ng-repeat="buildgroup in cdash.coverage_buildgroups"
Expand Down
2 changes: 1 addition & 1 deletion app/cdash/tests/js/e2e_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function(add_protractor_test test_name)
)
endfunction()

add_cypress_test(manage-overview)
add_protractor_test(manageBuildGroup)
add_protractor_test(manageOverview)
add_protractor_test(manageSubProject)
add_protractor_test(viewBuildError)
add_protractor_test(viewTest)
Expand Down
24 changes: 0 additions & 24 deletions app/cdash/tests/js/e2e_tests/manageOverview.js

This file was deleted.

39 changes: 0 additions & 39 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17014,45 +17014,6 @@ parameters:
count: 1
path: app/cdash/public/api/v1/manageBuildGroup.php

-
message: "#^Argument of an invalid type array\\|false supplied for foreach, only iterables are supported\\.$#"
count: 2
path: app/cdash/public/api/v1/manageOverview.php

-
message: """
#^Call to deprecated function add_last_sql_error\\(\\)\\:
04/22/2023$#
"""
count: 3
path: app/cdash/public/api/v1/manageOverview.php

-
message: """
#^Call to deprecated function add_log\\(\\)\\:
04/04/2023 Use \\\\Illuminate\\\\Support\\\\Facades\\\\Log for logging instead$#
"""
count: 1
path: app/cdash/public/api/v1/manageOverview.php

-
message: """
#^Call to deprecated method executePrepared\\(\\) of class CDash\\\\Database\\:
04/22/2023 Use Laravel query builder or Eloquent instead$#
"""
count: 3
path: app/cdash/public/api/v1/manageOverview.php

-
message: "#^Magic constant __FUNCTION__ is always empty outside a function\\.$#"
count: 1
path: app/cdash/public/api/v1/manageOverview.php

-
message: "#^Parameter \\#1 \\$json of function json_decode expects string, string\\|false given\\.$#"
count: 1
path: app/cdash/public/api/v1/manageOverview.php

-
message: """
#^Call to deprecated function json_error_response\\(\\)\\:
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
Route::post('/v1/manageMeasurements.php', 'ManageMeasurementsController@apiPost');
Route::delete('/v1/manageMeasurements.php', 'ManageMeasurementsController@apiDelete');

Route::match(['get', 'post'], '/v1/manageOverview.php', 'ProjectOverviewController@apiManageOverview');

Route::middleware(['admin'])->group(function () {
Route::get('/authtokens/all', 'AuthTokenController@fetchAll');

Expand Down
Loading

0 comments on commit b155445

Please sign in to comment.