Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/Http/Controllers/BuildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/BuildGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
13 changes: 8 additions & 5 deletions app/cdash/app/Model/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down
63 changes: 22 additions & 41 deletions app/cdash/app/Model/BuildGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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]);
Expand Down
10 changes: 8 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ parameters:
-
rawMessage: Cannot cast mixed to int.
identifier: cast.int
count: 11
count: 13
path: app/Http/Controllers/BuildController.php

-
Expand Down Expand Up @@ -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

-
Expand Down Expand Up @@ -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
Expand Down