Skip to content
Merged
3 changes: 2 additions & 1 deletion app/Http/Controllers/CTestConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Services\ProjectService;
use Illuminate\Http\Response;

final class CTestConfigurationController extends AbstractProjectController
Expand All @@ -11,7 +12,7 @@ public function get(int $id): Response
$this->setProjectById($id);

$view = $this->view('project.ctest-configuration', '')
->with('subprojects', $this->project->GetSubProjects());
->with('subprojects', ProjectService::getSubProjects((int) $this->project->Id));
return response($view, 200, ['Content-Type' => 'text/plain']);
}
}
5 changes: 3 additions & 2 deletions app/Http/Controllers/CoverageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Project as EloquentProject;
use App\Models\User;
use App\Services\ProjectService;
use App\Utils\PageTimer;
use App\Utils\TestingDay;
use CDash\Database;
Expand Down Expand Up @@ -1365,8 +1366,8 @@ public function apiCompareCoverage(): JsonResponse

// Are there any subproject groups?
$subproject_groups = [];
if ($this->project->GetNumberOfSubProjects($end_UTCDate) > 0) {
$subproject_groups = $this->project->GetSubProjectGroups();
if (ProjectService::getNumberOfSubProjects((int) $this->project->Id, $end_UTCDate) > 0) {
$subproject_groups = ProjectService::getSubProjectGroups((int) $this->project->Id);
}
foreach ($subproject_groups as $group) {
// Keep track of coverage info on a per-group basis.
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/ProjectOverviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Services\ProjectService;
use App\Utils\PageTimer;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
Expand All @@ -25,7 +26,7 @@ public function apiOverview(): JsonResponse
$pageTimer = new PageTimer();

// Check if this project has SubProjects.
$has_subprojects = $this->project->GetNumberOfSubProjects() > 0;
$has_subprojects = ProjectService::getNumberOfSubProjects((int) $this->project->Id) > 0;

// Handle optional date argument.
$date = htmlspecialchars($_GET['date'] ?? date(FMT_DATE));
Expand Down Expand Up @@ -108,7 +109,7 @@ public function apiOverview(): JsonResponse
$coverage_build_group_names = [];
if ($has_subprojects) {
// Detect if the subprojects are split up into groups.
$groups = $this->project->GetSubProjectGroups();
$groups = ProjectService::getSubProjectGroups((int) $this->project->Id);
if (count($groups) > 0) {
$has_subproject_groups = true;
foreach ($groups as $group) {
Expand Down
15 changes: 8 additions & 7 deletions app/Http/Controllers/SubProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\User;
use App\Services\ProjectService;
use App\Utils\PageTimer;
use CDash\Model\SubProject;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function apiManageSubProject(): JsonResponse
$response['threshold'] = $this->project->GetCoverageThreshold();

$subprojects_response = []; // JSON for subprojects
foreach ($this->project->GetSubProjects() as $subproject) {
foreach (ProjectService::getSubProjects((int) $this->project->Id) as $subproject) {
$subprojects_response[] = [
'id' => $subproject->id,
'name' => $subproject->name,
Expand All @@ -88,7 +89,7 @@ public function apiManageSubProject(): JsonResponse
$response['subprojects'] = $subprojects_response;

$groups = [];
foreach ($this->project->GetSubProjectGroups() as $subProjectGroup) {
foreach (ProjectService::getSubProjectGroups((int) $this->project->Id) as $subProjectGroup) {
$group = [
'id' => $subProjectGroup->GetId(),
'name' => $subProjectGroup->GetName(),
Expand Down Expand Up @@ -128,7 +129,7 @@ public function apiViewSubProjects(): JsonResponse
$date = htmlspecialchars(pdo_real_escape_string($_GET['date']));
$date_specified = true;
} else {
$last_start_timestamp = $this->project->GetLastSubmission();
$last_start_timestamp = ProjectService::getLastStartTimestamp((int) $this->project->Id);
$date = strlen($last_start_timestamp) > 0 ? $last_start_timestamp : null;
$date_specified = false;
}
Expand Down Expand Up @@ -192,7 +193,7 @@ public function apiViewSubProjects(): JsonResponse
$project_response['ntestpass'] = $this->project->GetNumberOfPassingTests($beginning_UTCDate, $end_UTCDate);
$project_response['ntestfail'] = $this->project->GetNumberOfFailingTests($beginning_UTCDate, $end_UTCDate);
$project_response['ntestnotrun'] = $this->project->GetNumberOfNotRunTests($beginning_UTCDate, $end_UTCDate);
$project_last_submission = $this->project->GetLastSubmission();
$project_last_submission = ProjectService::getLastStartTimestamp((int) $this->project->Id);
if (strlen($project_last_submission) === 0) {
$project_response['starttime'] = 'NA';
} else {
Expand All @@ -201,7 +202,7 @@ public function apiViewSubProjects(): JsonResponse
$response['project'] = $project_response;

// Look for the subproject
$subprojects = $this->project->GetSubProjects();
$subprojects = ProjectService::getSubProjects((int) $this->project->Id);
$subprojProp = [];
foreach ($subprojects as $subproject) {
$subprojProp[$subproject->id] = ['name' => $subproject->name];
Expand Down Expand Up @@ -286,10 +287,10 @@ public function apiDependenciesGraph(): JsonResponse

$date = isset($_GET['date']) ? Carbon::parse($_GET['date']) : null;

$subprojects = $this->project->GetSubProjects();
$subprojects = ProjectService::getSubProjects((int) $this->project->Id);

$subproject_groups = [];
$groups = $this->project->GetSubProjectGroups();
$groups = ProjectService::getSubProjectGroups((int) $this->project->Id);
foreach ($groups as $group) {
$subproject_groups[$group->GetId()] = $group;
}
Expand Down
10 changes: 9 additions & 1 deletion app/Http/Controllers/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\BadSubmissionException;
use App\Jobs\ProcessSubmission;
use App\Models\Site;
use App\Rules\ProjectNameRule;
use App\Utils\AuthTokenUtil;
use App\Utils\SubmissionUtils;
use App\Utils\UnparsedSubmissionProcessor;
Expand All @@ -21,6 +22,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
Expand Down Expand Up @@ -88,7 +90,13 @@ private function submitProcess(): Response
$this->failProcessing(null, Response::HTTP_BAD_REQUEST, 'No project name provided.');
}

if (!Project::validateProjectName($projectname)) {
$validator = Validator::make([
'name' => $projectname,
], [
'name' => new ProjectNameRule(),
]);

if ($validator->fails()) {
Log::info("Rejected submission with invalid project name: $projectname");
$this->failProcessing(null, Response::HTTP_BAD_REQUEST, "Invalid project name: $projectname");
}
Expand Down
30 changes: 29 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Illuminate\View\View;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function userPageContent(): JsonResponse
$project_response['name'] = $Project->Name;
$project_response['name_encoded'] = urlencode($Project->Name);
$project_response['nbuilds'] = $project_row->builds()->count();
$project_response['average_builds'] = round($Project->GetBuildsDailyAverage(gmdate(FMT_DATETIME, time() - (3600 * 24 * 7)), gmdate(FMT_DATETIME)), 2);
$project_response['average_builds'] = round(self::GetBuildsDailyAverage((int) $Project->Id, gmdate(FMT_DATETIME, time() - (3600 * 24 * 7)), gmdate(FMT_DATETIME)), 2);
$project_response['success'] = $Project->GetNumberOfPassingBuilds($start, gmdate(FMT_DATETIME));
$project_response['error'] = $Project->GetNumberOfErrorBuilds($start, gmdate(FMT_DATETIME));
$project_response['warning'] = $Project->GetNumberOfWarningBuilds($start, gmdate(FMT_DATETIME));
Expand Down Expand Up @@ -206,6 +207,33 @@ public function userPageContent(): JsonResponse
return response()->json(cast_data_for_JSON($response));
}

/** Get the number of builds given per day */
private static function GetBuildsDailyAverage(int $projectid, string $startUTCdate, string $endUTCdate): int
{
$project = DB::select('
SELECT starttime
FROM build
WHERE
projectid=?
AND starttime>?
AND starttime<=?
AND parentid IN (-1, 0)
ORDER BY starttime ASC
LIMIT 1
', [$projectid, $startUTCdate, $endUTCdate]);
if ($project === []) {
return 0;
}
$first_build = $project[0]->starttime;
$nb_days = strtotime($endUTCdate) - strtotime($first_build);
$nb_days = intval($nb_days / 86400) + 1;
$nbuilds = \App\Models\Project::findOrFail($projectid)
->builds()
->betweenDates(Carbon::parse($startUTCdate), Carbon::parse($endUTCdate))
->count();
return $nbuilds / $nb_days;
}

/** Report statistics about the last build */
private function ReportLastBuild(string $type, int $projectid, int $siteid, string $projectname, $nightlytime, PDO $PDO): array
{
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Submission/Handlers/BazelJSONHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
=========================================================================*/

use App\Models\Project as EloquentProject;
use App\Services\ProjectService;
use App\Utils\SubmissionUtils;
use App\Utils\TestCreator;
use CDash\Database;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function __construct(Build $build)
protected function HasSubProjects(): bool
{
if ($this->_HasSubProjects === null) {
$this->_HasSubProjects = $this->GetProject()->GetNumberOfSubProjects() > 0;
$this->_HasSubProjects = ProjectService::getNumberOfSubProjects((int) $this->GetProject()->Id) > 0;
}
return $this->_HasSubProjects;
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Submission/Handlers/CoverageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use App\Http\Submission\Traits\UpdatesSiteInformation;
use App\Models\Site;
use App\Models\SiteInformation;
use App\Services\ProjectService;
use App\Utils\SubmissionUtils;
use CDash\Model\Build;
use CDash\Model\Coverage;
Expand Down Expand Up @@ -133,7 +134,7 @@ public function endElement($parser, $name): void
$this->Build->UpdateBuild($this->Build->Id, -1, -1);
}

$hasSubProjects = $this->GetProject()->GetNumberOfSubProjects() > 0;
$hasSubProjects = ProjectService::getNumberOfSubProjects((int) $this->GetProject()->Id) > 0;

foreach ($this->Coverages as $coverageInfo) {
$coverage = $coverageInfo[0];
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Submission/Handlers/CoverageLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
=========================================================================*/

use App\Models\Site;
use App\Services\ProjectService;
use App\Utils\SubmissionUtils;
use CDash\Model\Build;
use CDash\Model\CoverageFile;
Expand Down Expand Up @@ -95,9 +96,7 @@ public function endElement($parser, $name): void
}

// Does this project have subprojects?
$project = new Project();
$project->Id = $this->GetProject()->Id;
$has_subprojects = $project->GetNumberOfSubProjects() > 0;
$has_subprojects = ProjectService::getNumberOfSubProjects((int) $this->GetProject()->Id) > 0;

// Record the coverage data that we parsed from this file.
foreach ($this->CoverageFiles as $coverageInfo) {
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Submission/Handlers/ProjectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PURPOSE. See the above copyright notices for more information.
=========================================================================*/

use App\Services\ProjectService;
use CDash\Model\Label;
use CDash\Model\Project;
use CDash\Model\SubProject;
Expand Down Expand Up @@ -139,7 +140,7 @@ public function endElement($parser, $name): void

if (config('cdash.delete_old_subprojects')) {
// Delete old subprojects that weren't included in this file.
$previousSubProjectIds = $this->GetProject()->GetSubProjects()->pluck('id')->toArray();
$previousSubProjectIds = ProjectService::getSubProjects((int) $this->GetProject()->Id)->pluck('id')->toArray();
foreach ($previousSubProjectIds as $previousId) {
$found = false;
foreach ($this->SubProjects as $subproject) {
Expand Down
2 changes: 1 addition & 1 deletion app/Models/BuildGroupPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class BuildGroupPosition extends Model
{
protected $table = 'buildgroup';
protected $table = 'buildgroupposition';

public $timestamps = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Translation\PotentiallyTranslatedString;

class ProjectAuthenticateSubmissions implements ValidationRule
class ProjectAuthenticateSubmissionsRule implements ValidationRule
{
/**
* Run the validation rule.
Expand Down
25 changes: 25 additions & 0 deletions app/Rules/ProjectNameRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\PotentiallyTranslatedString;

class ProjectNameRule implements ValidationRule
{
/**
* @param Closure(string): PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = (string) $value;

if (preg_match('/^[a-zA-Z0-9\ +.\-_]+$/', $value) !== 1) {
$fail('Project name may only contain letters, numbers, dashes, and underscores.');
}
if (str_contains($value, '_-_')) {
$fail('Project name must not contain string "_-_"');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Support\Str;
use Illuminate\Translation\PotentiallyTranslatedString;

class ProjectVisibilityAllowed implements ValidationRule
class ProjectVisibilityRule implements ValidationRule
{
/**
* Verify that the current user is able to create/edit a project with the requested visibility.
Expand Down
Loading