Skip to content

Commit 7f75b06

Browse files
net0wellclaude
andcommitted
feat(api): GET /stages filtra pelas Etapas de uma trilha (spec 007)
- `pipelineKey` traz só as Etapas da última versão publicada da trilha, na ordem dos passos (D-164, D-165). Uma Etapa usada em dois passos aparece uma vez. Combina com `search` e `status`. Trilha inexistente ou só com rascunho devolve página vazia. - Na biblioteca o filtro é `CatalogQuery.UsedByPipeline`, só para Etapas; o ListPipelinesAsync o rejeita. - AC-26 testado pela API e snapshot do OpenAPI atualizado (só o parâmetro novo). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9f82ec0 commit 7f75b06

6 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎src/PythonScriptRunner.Abstractions/Management/CatalogAdmin.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,12 @@ Task<ScheduleDetail> SetScheduleEnabledAsync(
311311
/// <param name="PageNumber">1-based page number.</param>
312312
/// <param name="PageSize">Items per page (1-100).</param>
313313
/// <param name="Status">Only items that have a version with this status.</param>
314-
public sealed record CatalogQuery(string? Search = null, int PageNumber = 1, int PageSize = 25, VersionStatus? Status = null);
314+
/// <param name="UsedByPipeline">
315+
/// Stages only: the stages used by the latest published version of this pipeline, in step order; a pipeline that does not
316+
/// exist or has no published version gives an empty page. <see cref="ICatalogAdmin.ListPipelinesAsync"/> rejects it.
317+
/// </param>
318+
public sealed record CatalogQuery(
319+
string? Search = null, int PageNumber = 1, int PageSize = 25, VersionStatus? Status = null, string? UsedByPipeline = null);
315320

316321
/// <summary>A new stage or pipeline.</summary>
317322
/// <param name="Key">Slug (immutable).</param>

‎src/PythonScriptRunner.Dashboard/Api/StageEndpoints.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public static void Map(RouteGroupBuilder api)
5252
}
5353

5454
private static async Task<ApiPage<StageSummary>> ListAsync(
55-
string? search, VersionStatus? status, int? page, int? pageSize, ICatalogAdmin admin, CancellationToken cancellationToken) =>
55+
string? search, VersionStatus? status, string? pipelineKey, int? page, int? pageSize, ICatalogAdmin admin, CancellationToken cancellationToken) =>
5656
ApiPage<StageSummary>.From(await admin.ListStagesAsync(
57-
new CatalogQuery(search, page ?? 1, Math.Clamp(pageSize ?? 25, 1, 100), status), cancellationToken).ConfigureAwait(false));
57+
new CatalogQuery(search, page ?? 1, Math.Clamp(pageSize ?? 25, 1, 100), status, string.IsNullOrWhiteSpace(pipelineKey) ? null : pipelineKey.Trim()),
58+
cancellationToken).ConfigureAwait(false));
5859

5960
private static async Task<IResult> CreateAsync(NewCatalogItem stage, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken)
6061
{

‎src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.Pipelines.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ internal sealed partial class EfCatalogAdmin
1010
public async Task<Page<PipelineSummary>> ListPipelinesAsync(CatalogQuery query, CancellationToken cancellationToken = default)
1111
{
1212
var (pageNumber, pageSize) = Paging(query);
13+
if (query.UsedByPipeline is not null)
14+
{
15+
throw new ArgumentException("UsedByPipeline filters stages only.", nameof(query));
16+
}
17+
1318
var db = await factory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
1419
await using (db.ConfigureAwait(false))
1520
{

‎src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.cs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@ public async Task<Page<StageSummary>> ListStagesAsync(CatalogQuery query, Cancel
5353
stages = stages.Where(s => s.Versions.Any(v => v.Status == status));
5454
}
5555

56+
var ordered = stages.OrderBy(s => s.Key);
57+
if (query.UsedByPipeline is { } pipelineKey)
58+
{
59+
// The steps of the latest published version of the pipeline (D-164, D-165); a stage used by two steps shows
60+
// once, at its first step.
61+
var steps = db.PipelineSteps.Where(step => db.PipelineVersions.Any(v =>
62+
v.Id == step.PipelineVersionId && v.Status == VersionStatus.Published
63+
&& v.Pipeline.Key == pipelineKey && v.Version == v.Pipeline.LatestPublishedVersion));
64+
stages = stages.Where(s => steps.Any(step => step.StageVersion.StageId == s.Id));
65+
ordered = stages.OrderBy(s => steps.Where(step => step.StageVersion.StageId == s.Id).Min(step => step.Order)).ThenBy(s => s.Key);
66+
}
67+
5668
var total = await stages.CountAsync(cancellationToken).ConfigureAwait(false);
57-
var items = await stages.OrderBy(s => s.Key)
69+
var items = await ordered
5870
.Skip((pageNumber - 1) * pageSize).Take(pageSize)
5971
.Select(s => new StageSummary(
6072
s.Key, s.Name, s.Description, s.LatestPublishedVersion,

‎tests/PythonScriptRunner.Dashboard.Tests/CatalogApiTests.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ public async Task Spec007_AC16_Secrets_ShowStatusButNeverValues()
9595
secrets[1]!["declaredBy"]!.AsArray().Select(d => d!.GetValue<string>()).ShouldBe(["envia@1", "outra@1"]);
9696
}
9797

98+
[Fact]
99+
public async Task Spec007_AC26_StagesOfAPipeline_AreThoseOfItsLatestPublishedVersion_InStepOrder()
100+
{
101+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
102+
var host = await env.StartHostAsync(s => s.Worker = false);
103+
await env.ImportFixtureAsync(host, "calculo-progressao");
104+
await using var app = await DashboardApp.StartAsync(env);
105+
await app.PublishStageAsync("eco", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]);
106+
var draft = await app.Admin.CreatePipelineDraftFromAsync("calculo-progressao", 1, PsrEnvironment.Author, Ct);
107+
var withEco = draft.Data with
108+
{
109+
Steps = [.. draft.Data.Steps, new PipelineStepDefinition("eco", "eco", 1) { Bindings = [new InputBinding("texto", new ConstantSource("x"))] }],
110+
};
111+
await app.Admin.SavePipelineDraftAsync("calculo-progressao", draft.Version, withEco, draft.RowVersion, PsrEnvironment.Author, Ct);
112+
var onlyDraft = await app.Admin.CreatePipelineAsync(new NewCatalogItem("so-rascunho", "Só rascunho"), PsrEnvironment.Author, Ct);
113+
await app.Admin.SavePipelineDraftAsync("so-rascunho", onlyDraft.Version, withEco, onlyDraft.RowVersion, PsrEnvironment.Author, Ct);
114+
using var reader = await app.LoginAsync("leitor");
115+
116+
async Task<string[]> KeysAsync(string query) =>
117+
(await (await reader.GetAsync($"/psr/api/stages?{query}", Ct)).JsonAsync())["items"]!.AsArray()
118+
.Select(s => s!["key"]!.GetValue<string>()).ToArray();
119+
120+
(await KeysAsync("pipelineKey=calculo-progressao")).ShouldBe(["progressao-termos", "polinomio-avaliar", "estatisticas", "montar-tabela"]);
121+
(await KeysAsync("pipelineKey=calculo-progressao&search=poli")).ShouldBe(["polinomio-avaliar"]);
122+
(await KeysAsync("pipelineKey=so-rascunho")).ShouldBeEmpty();
123+
(await KeysAsync("pipelineKey=nao-existe")).ShouldBeEmpty();
124+
(await KeysAsync(string.Empty)).ShouldContain("eco");
125+
}
126+
98127
[Fact]
99128
public async Task FieldTypes_DescribeTheTwelveTypesAndTheCompatibility()
100129
{

‎tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,13 @@
409409
"$ref": "#/components/schemas/VersionStatus"
410410
}
411411
},
412+
{
413+
"name": "pipelineKey",
414+
"in": "query",
415+
"schema": {
416+
"type": "string"
417+
}
418+
},
412419
{
413420
"name": "page",
414421
"in": "query",

0 commit comments

Comments
 (0)