Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public ListClientPublishedVersionsCommand() : base("published-versions")
Description = "List all published versions of a client.";

Options.Add(Opt<OptionalClientIdOption>.Instance);
Options.Add(Opt<StageNameOption>.Instance);
Options.Add(Opt<OptionalCursorOption>.Instance);

this.AddGlobalNitroOptions();

this.AddExamples("client list published-versions --client-id \"<client-id>\"");
this.AddExamples(
"""
client list published-versions \
--client-id "<client-id>" \
--stage "dev"
""");

this.SetActionWithExceptionHandling(ExecuteAsync);
}
Expand All @@ -38,14 +44,15 @@ private static async Task<int> ExecuteAsync(

parseResult.AssertHasAuthentication(sessionService);

var stage = parseResult.GetRequiredValue(Opt<StageNameOption>.Instance);
var cursor = parseResult.GetValue(Opt<OptionalCursorOption>.Instance);

if (console.IsInteractive)
{
return await RenderInteractiveAsync(parseResult, console, client, apisClient, sessionService, resultHolder, cursor, ct);
return await RenderInteractiveAsync(parseResult, console, client, apisClient, sessionService, resultHolder, stage, cursor, ct);
}

return await RenderNonInteractiveAsync(parseResult, client, resultHolder, cursor, ct);
return await RenderNonInteractiveAsync(parseResult, client, resultHolder, stage, cursor, ct);
}

private static async Task<int> RenderInteractiveAsync(
Expand All @@ -55,6 +62,7 @@ private static async Task<int> RenderInteractiveAsync(
IApisClient apisClient,
ISessionService sessionService,
IResultHolder resultHolder,
string stage,
string? cursor,
CancellationToken ct)
{
Expand All @@ -76,13 +84,13 @@ private static async Task<int> RenderInteractiveAsync(
var container = PaginationContainer
.CreateConnectionData(async (after, first, cancellationToken) =>
{
var page = await client.ListClientVersionsAsync(
clientId, after ?? cursor, first, cancellationToken)
var page = await client.ListClientPublishedVersionsAsync(
clientId, stage, after ?? cursor, first, cancellationToken)
?? throw new ExitException("The client was not found.");

var mappedItems = page.Items
.Where(e => e.Node.Version is not null)
.Select(ToResult)
.Where(v => v.Stages.Count > 0)
.ToArray();

return new ConnectionPage<ClientPublishedVersionResult>(
Expand All @@ -96,8 +104,7 @@ private static async Task<int> RenderInteractiveAsync(
.From(container)
.Title("Published Versions")
.AddColumn("Tag", x => x.Tag)
.AddColumn("Created", x => x.CreatedAt.ToString("u"))
.AddColumn("Stages", x => string.Join(", ", x.Stages))
.AddColumn("Published", x => x.PublishedAt.ToString("u"))
.RenderAsync(console, ct);

if (selectedVersion is not null)
Expand All @@ -112,41 +119,36 @@ private static async Task<int> RenderNonInteractiveAsync(
ParseResult parseResult,
IClientsClient client,
IResultHolder resultHolder,
string stage,
string? cursor,
CancellationToken ct)
{
var clientId = parseResult.GetRequiredOptionalValue(Opt<OptionalClientIdOption>.Instance);

var page = await client.ListClientVersionsAsync(clientId, cursor, 10, ct)
var page = await client.ListClientPublishedVersionsAsync(clientId, stage, cursor, 10, ct)
?? throw new ExitException("The client was not found.");

var items = page.Items
.Where(e => e.Node.Version is not null)
.Select(ToResult)
.Where(v => v.Stages.Count > 0)
.ToArray();

resultHolder.SetResult(new PaginatedListResult<ClientPublishedVersionResult>(items, page.EndCursor));

return ExitCodes.Success;
}

private static ClientPublishedVersionResult ToResult(IClientDetailPrompt_ClientVersionEdge version)
private static ClientPublishedVersionResult ToResult(IListClientPublishedVersionsCommand_PublishedClientVersionEdge edge)
=> new()
{
Tag = version.Node.Tag,
CreatedAt = version.Node.CreatedAt,
Stages = version.Node.PublishedTo
.Select(t => t.Stage?.Name)
.OfType<string>()
.ToArray()
Tag = edge.Node.Version!.Tag,
PublishedAt = edge.Node.PublishedAt
};

public class ClientPublishedVersionResult
{
public required string Tag { get; init; }

public required DateTimeOffset CreatedAt { get; init; }

public required IReadOnlyList<string> Stages { get; init; }
public required DateTimeOffset PublishedAt { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,59 @@ private static IClientDetailPrompt_ClientVersionEdge CreateVersionEdge(

#endregion

#region ListClientPublishedVersions

protected void SetupListClientPublishedVersionsQuery(
string? cursor = null,
string? endCursor = null,
bool hasNextPage = false,
params (string Tag, DateTimeOffset PublishedAt)[] versions)
{
var items = versions
.Select(static v => CreatePublishedVersionEdge(v.Tag, v.PublishedAt))
.ToArray();

var page = new ConnectionPage<IListClientPublishedVersionsCommand_PublishedClientVersionEdge>(
items, endCursor, hasNextPage);

ClientsClientMock.Setup(x => x.ListClientPublishedVersionsAsync(
ClientId, Stage, cursor, 10, It.IsAny<CancellationToken>()))
.ReturnsAsync(page);
}

protected void SetupListClientPublishedVersionsQueryException()
{
ClientsClientMock.Setup(x => x.ListClientPublishedVersionsAsync(
ClientId, Stage, null, 10, It.IsAny<CancellationToken>()))
.ThrowsAsync(new InvalidOperationException("Something unexpected happened."));
}

protected void SetupListClientPublishedVersionsQueryNotFound(string? cursor = null)
{
ClientsClientMock.Setup(x => x.ListClientPublishedVersionsAsync(
ClientId, Stage, cursor, 10, It.IsAny<CancellationToken>()))
.ReturnsAsync((ConnectionPage<IListClientPublishedVersionsCommand_PublishedClientVersionEdge>?)null);
}

protected static IListClientPublishedVersionsCommand_PublishedClientVersionEdge CreatePublishedVersionEdge(
string tag,
DateTimeOffset publishedAt)
{
var version = new Mock<IListClientPublishedVersionsCommandQuery_Node_PublishedVersions_Edges_Node_Version>(MockBehavior.Strict);
version.SetupGet(x => x.Tag).Returns(tag);

var node = new Mock<IListClientPublishedVersionsCommandQuery_Node_PublishedVersions_Edges_Node>(MockBehavior.Strict);
node.SetupGet(x => x.PublishedAt).Returns(publishedAt);
node.SetupGet(x => x.Version).Returns(version.Object);

var edge = new Mock<IListClientPublishedVersionsCommand_PublishedClientVersionEdge>(MockBehavior.Strict);
edge.SetupGet(x => x.Node).Returns(node.Object);

return edge.Object;
}

#endregion

#region Download

protected void SetupDownloadPersistedQueries(Stream? result)
Expand Down
Loading
Loading