diff --git a/app/Models/BuildError.php b/app/Models/BuildError.php index 82717fd9b2..0509a1490f 100644 --- a/app/Models/BuildError.php +++ b/app/Models/BuildError.php @@ -85,4 +85,12 @@ protected function command(): Attribute get: fn (mixed $value, array $attributes): ?string => $this->arguments->isEmpty() ? null : $this->arguments->sortBy('pivot.place')->pluck('argument')->implode(' '), ); } + + /** + * @return BelongsToMany + */ + public function labels(): BelongsToMany + { + return $this->belongsToMany(Label::class, 'label2buildfailure', 'buildfailureid', 'labelid'); + } } diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 79fd08c831..3aea664360 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -997,6 +997,10 @@ type BuildError { sourceLine: Int @rename(attribute: "sourceline") @filterable command: String @with(relation: "arguments") + + labels( + filters: _ @filter + ): [Label!]! @belongsToMany(type: CONNECTION) @orderBy(column: "id") } diff --git a/tests/Feature/GraphQL/BuildErrorTypeTest.php b/tests/Feature/GraphQL/BuildErrorTypeTest.php index 523b1d34b2..e1a42feb14 100644 --- a/tests/Feature/GraphQL/BuildErrorTypeTest.php +++ b/tests/Feature/GraphQL/BuildErrorTypeTest.php @@ -5,6 +5,7 @@ use App\Models\Build; use App\Models\BuildError; use App\Models\BuildFailureArgument; +use App\Models\Label; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Str; use PHPUnit\Framework\Attributes\DataProvider; @@ -193,4 +194,77 @@ public function testCommandField(): void ], ]); } + + public function testLabelsRelationship(): void + { + $project = $this->makePublicProject(); + + /** @var Build $build */ + $build = $project->builds()->create([ + 'name' => Str::uuid()->toString(), + 'uuid' => Str::uuid()->toString(), + ]); + + $label1 = Label::create([ + 'text' => Str::uuid()->toString(), + ]); + $label2 = Label::create([ + 'text' => Str::uuid()->toString(), + ]); + + /** @var BuildError $buildError */ + $buildError = $build->buildErrors()->save(BuildError::factory()->make()); + + $buildError->labels()->attach([$label1->id, $label2->id]); + + // Test with no arguments + $this->graphQL(' + query build($id: ID) { + build(id: $id) { + buildErrors { + edges { + node { + labels { + edges { + node { + text + } + } + } + } + } + } + } + } + ', [ + 'id' => $build->id, + ])->assertExactJson([ + 'data' => [ + 'build' => [ + 'buildErrors' => [ + 'edges' => [ + [ + 'node' => [ + 'labels' => [ + 'edges' => [ + [ + 'node' => [ + 'text' => $label1->text, + ], + ], + [ + 'node' => [ + 'text' => $label2->text, + ], + ], + ], + ], + ], + ], + ], + ], + ], + ], + ]); + } }