diff --git a/app/Http/Controllers/BuildController.php b/app/Http/Controllers/BuildController.php index 466f4ab7b0..f2c64786ce 100644 --- a/app/Http/Controllers/BuildController.php +++ b/app/Http/Controllers/BuildController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Build as EloquentBuild; +use App\Models\BuildGroup; use App\Models\BuildUpdateFile; use App\Models\Comment; use App\Models\Project; @@ -1421,14 +1422,16 @@ private function restApiPost(): JsonResponse $expected = request()->input('expected'); $newgroupid = request()->input('newgroupid'); - // Remove the build from its previous group. - DB::delete('DELETE FROM build2group WHERE buildid = ?', [$this->build->Id]); + $eloquent_build = EloquentBuild::findOrFail((int) $this->build->Id); + + if (BuildGroup::findOrFail((int) $newgroupid)->project()->isNot($eloquent_build->project)) { + abort(403, 'Requested build group is not associated with this project.'); + } + // Remove the build from its previous group. + $eloquent_build->buildGroups()->detach(); // Insert it into the new group. - DB::insert(' - INSERT INTO build2group(groupid, buildid) - VALUES (?, ?) - ', [$newgroupid, $this->build->Id]); + $eloquent_build->buildGroups()->attach((int) $newgroupid); // Mark any previous buildgroup rule as finished as of this time. $now = gmdate(FMT_DATETIME); diff --git a/app/Models/Build.php b/app/Models/Build.php index 4c9066d539..26f72fe3e8 100644 --- a/app/Models/Build.php +++ b/app/Models/Build.php @@ -224,7 +224,7 @@ public function comments(): HasMany */ public function buildGroups(): BelongsToMany { - return $this->belongsToMany(BuildGroup::class, 'build2group', 'groupid', 'buildid'); + return $this->belongsToMany(BuildGroup::class, 'build2group', 'buildid', 'groupid'); } /** diff --git a/app/Models/BuildGroup.php b/app/Models/BuildGroup.php index 1308fc4e0d..a79ab78471 100644 --- a/app/Models/BuildGroup.php +++ b/app/Models/BuildGroup.php @@ -59,7 +59,7 @@ class BuildGroup extends Model */ public function project(): BelongsTo { - return $this->belongsTo(Project::class, 'id', 'projectid'); + return $this->belongsTo(Project::class, 'projectid'); } /** diff --git a/app/cdash/app/Model/Build.php b/app/cdash/app/Model/Build.php index a333ce82bb..9fa22b8681 100644 --- a/app/cdash/app/Model/Build.php +++ b/app/cdash/app/Model/Build.php @@ -370,7 +370,7 @@ public function FillFromId($buildid): void $this->SubProjectId = $subprojectid; } - $this->GroupId = (int) (DB::select('SELECT groupid FROM build2group WHERE buildid = ?', [$buildid])[0]->groupid ?? false); + $this->GroupId = $model->buildGroups()->first()->id ?? 0; $this->Filled = true; } @@ -1475,12 +1475,15 @@ public function GetGroup(): int|false ]); return false; } - $stmt = $this->PDO->prepare( - 'SELECT groupid FROM build2group WHERE buildid = ?'); - if (!pdo_execute($stmt, [$this->Id])) { + + /** @var \App\Models\BuildGroup $buildgroup */ + $buildgroup = EloquentBuild::findOrFail((int) $this->Id)->buildGroups()->first(); + + if ($buildgroup === null) { return false; } - return (int) $stmt->fetchColumn(); + + return $buildgroup->id; } /** Get the number of errors for a build */ diff --git a/app/cdash/app/Model/BuildGroup.php b/app/cdash/app/Model/BuildGroup.php index 7ed38b34d5..9616ea39e0 100644 --- a/app/cdash/app/Model/BuildGroup.php +++ b/app/cdash/app/Model/BuildGroup.php @@ -321,12 +321,11 @@ public function Save(): bool // Insert the default position for this group // Find the position for this group $position = $this->GetNextPosition(); - DB::insert(' - INSERT INTO buildgroupposition - (buildgroupid, position, starttime, endtime) - VALUES - (?, ?, ?, ?) - ', [$this->eloquent_model->id, $position, $this->eloquent_model->starttime, $this->eloquent_model->endtime]); + $this->eloquent_model->positions()->create([ + 'position' => $position, + 'starttime' => $this->eloquent_model->starttime, + 'endtime' => $this->eloquent_model->endtime, + ]); } return true; } @@ -341,48 +340,30 @@ public function Delete(): bool // We delete all the build2grouprule associated with the group $this->eloquent_model->rules()->delete(); - // We delete the buildgroup - $this->eloquent_model->delete(); - // Restore the builds that were associated with this group - $oldbuilds = DB::select(' - SELECT id, type - FROM build - WHERE id IN ( - SELECT buildid AS id - FROM build2group - WHERE groupid=? - ) - ', [$this->eloquent_model->id]); - - foreach ($oldbuilds as $oldbuilds_array) { - // Move the builds - $buildid = $oldbuilds_array->id; - $buildtype = $oldbuilds_array->type; + $oldbuilds = $this->eloquent_model->builds()->get(); + /** @var \App\Models\Build $oldbuild */ + foreach ($oldbuilds as $oldbuild) { // Find the group corresponding to the build type - $query = DB::select(' - SELECT id - FROM buildgroup - WHERE name=? AND projectid=? - ', [$buildtype, $this->eloquent_model->projectid])[0] ?? []; - - if ($query === []) { - $query = DB::select(" - SELECT id - FROM buildgroup - WHERE name='Experimental' AND projectid=? - ", [$this->eloquent_model->projectid])[0]; + + /** @var ?EloquentBuildGroup $newGroup */ + $newGroup = $this->eloquent_model->project?->buildgroups()->where([ + 'name' => $oldbuild->type, + ])->first(); + + if ($newGroup === null) { + $newGroup = $this->eloquent_model->project?->buildgroups()->where([ + 'name' => 'Experimental', + ])->first(); } - $grouptype = $query->id; - DB::update(' - UPDATE build2group - SET groupid=? - WHERE buildid=? - ', [$grouptype, $buildid]); + $newGroup?->builds()->attach($oldbuild); } + // We delete the buildgroup + $this->eloquent_model->delete(); + // Delete the buildgroupposition and update the position // of the other groups. DB::delete('DELETE FROM buildgroupposition WHERE buildgroupid=?', [$this->eloquent_model->id]); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e7c6b5a00c..47b0a0a61a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -627,7 +627,7 @@ parameters: - rawMessage: Cannot cast mixed to int. identifier: cast.int - count: 11 + count: 13 path: app/Http/Controllers/BuildController.php - @@ -8907,7 +8907,7 @@ parameters: v2.5.0 01/22/2018 ''' identifier: function.deprecated - count: 15 + count: 14 path: app/cdash/app/Model/Build.php - @@ -9321,6 +9321,12 @@ parameters: count: 3 path: app/cdash/app/Model/Build.php + - + rawMessage: 'PHPDoc tag @var with type App\Models\BuildGroup is not subtype of type (App\Models\BuildGroup&object{pivot: Illuminate\Database\Eloquent\Relations\Pivot})|null.' + identifier: varTag.type + count: 1 + path: app/cdash/app/Model/Build.php + - rawMessage: PHPDoc tag @var with type App\Models\BuildUpdateFile is not subtype of type TValue. identifier: varTag.type