|
| 1 | +using System.Reactive; |
| 2 | +using System.Reactive.Linq; |
| 3 | +using System.Reactive.Subjects; |
| 4 | +using ChilliCream.Nitro.CommandLine.Cloud.Client; |
| 5 | +using ChilliCream.Nitro.CommandLine.Cloud.Option; |
| 6 | +using ChilliCream.Nitro.CommandLine.Cloud.Option.Binders; |
| 7 | +using StrawberryShake; |
| 8 | +using static ChilliCream.Nitro.CommandLine.Cloud.ThrowHelper; |
| 9 | + |
| 10 | +namespace ChilliCream.Nitro.CommandLine.Cloud; |
| 11 | + |
| 12 | +internal sealed class PublishOpenApiCollectionCommand : Command |
| 13 | +{ |
| 14 | + public PublishOpenApiCollectionCommand() : base("publish") |
| 15 | + { |
| 16 | + Description = "Publish an OpenAPI collection version to an stage"; |
| 17 | + |
| 18 | + AddOption(Opt<TagOption>.Instance); |
| 19 | + AddOption(Opt<StageNameOption>.Instance); |
| 20 | + AddOption(Opt<OpenApiCollectionIdOption>.Instance); |
| 21 | + AddOption(Opt<ForceOption>.Instance); |
| 22 | + AddOption(Opt<OptionalWaitForApprovalOption>.Instance); |
| 23 | + |
| 24 | + this.SetHandler( |
| 25 | + ExecuteAsync, |
| 26 | + Bind.FromServiceProvider<IAnsiConsole>(), |
| 27 | + Bind.FromServiceProvider<IApiClient>(), |
| 28 | + Opt<TagOption>.Instance, |
| 29 | + Opt<StageNameOption>.Instance, |
| 30 | + Opt<OpenApiCollectionIdOption>.Instance, |
| 31 | + Opt<ForceOption>.Instance, |
| 32 | + Opt<OptionalWaitForApprovalOption>.Instance, |
| 33 | + Bind.FromServiceProvider<CancellationToken>()); |
| 34 | + } |
| 35 | + |
| 36 | + private static async Task<int> ExecuteAsync( |
| 37 | + IAnsiConsole console, |
| 38 | + IApiClient client, |
| 39 | + string tag, |
| 40 | + string stage, |
| 41 | + string openApiCollectionId, |
| 42 | + bool force, |
| 43 | + bool waitForApproval, |
| 44 | + CancellationToken ct) |
| 45 | + { |
| 46 | + console.Title( |
| 47 | + $"Publish OpenAPI collection with tag {tag.EscapeMarkup()} to {stage.EscapeMarkup()}"); |
| 48 | + |
| 49 | + var committed = false; |
| 50 | + |
| 51 | + if (console.IsHumanReadable()) |
| 52 | + { |
| 53 | + await console |
| 54 | + .Status() |
| 55 | + .Spinner(Spinner.Known.BouncingBar) |
| 56 | + .SpinnerStyle(Style.Parse("green bold")) |
| 57 | + .StartAsync("Publishing...", PublishOpenApiCollection); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + await PublishOpenApiCollection(null); |
| 62 | + } |
| 63 | + |
| 64 | + return committed ? ExitCodes.Success : ExitCodes.Error; |
| 65 | + |
| 66 | + async Task PublishOpenApiCollection(StatusContext? ctx) |
| 67 | + { |
| 68 | + var input = new PublishOpenApiCollectionInput |
| 69 | + { |
| 70 | + OpenApiCollectionId = openApiCollectionId, |
| 71 | + Stage = stage, |
| 72 | + Tag = tag, |
| 73 | + WaitForApproval = waitForApproval |
| 74 | + }; |
| 75 | + |
| 76 | + if (force) |
| 77 | + { |
| 78 | + input = input with { Force = true }; |
| 79 | + console.Log("[yellow]Force push is enabled[/]"); |
| 80 | + } |
| 81 | + |
| 82 | + console.Log("Create publish request"); |
| 83 | + |
| 84 | + var requestId = await PublishOpenApiCollectionAsync(console, client, input, ct); |
| 85 | + |
| 86 | + console.Log($"Publish request created [grey](ID: {requestId.EscapeMarkup()})[/]"); |
| 87 | + |
| 88 | + using var stopSignal = new Subject<Unit>(); |
| 89 | + |
| 90 | + var subscription = client.PublishOpenApiCollectionCommandSubscription |
| 91 | + .Watch(requestId, ExecutionStrategy.NetworkOnly) |
| 92 | + .TakeUntil(stopSignal); |
| 93 | + |
| 94 | + await foreach (var x in subscription.ToAsyncEnumerable().WithCancellation(ct)) |
| 95 | + { |
| 96 | + if (x.Errors is { Count: > 0 } errors) |
| 97 | + { |
| 98 | + console.PrintErrorsAndExit(errors); |
| 99 | + throw Exit("No request id returned"); |
| 100 | + } |
| 101 | + |
| 102 | + switch (x.Data?.OnOpenApiCollectionVersionPublishingUpdate) |
| 103 | + { |
| 104 | + case IProcessingTaskIsQueued v: |
| 105 | + ctx?.Status( |
| 106 | + $"Your request is queued. The current position in the queue is {v.QueuePosition}."); |
| 107 | + break; |
| 108 | + |
| 109 | + case IOpenApiCollectionVersionPublishFailed { Errors: var openApiCollectionErrors }: |
| 110 | + console.ErrorLine("OpenAPI collection publish failed"); |
| 111 | + console.PrintErrorsAndExit(openApiCollectionErrors); |
| 112 | + stopSignal.OnNext(Unit.Default); |
| 113 | + break; |
| 114 | + |
| 115 | + case IOpenApiCollectionVersionPublishSuccess: |
| 116 | + committed = true; |
| 117 | + stopSignal.OnNext(Unit.Default); |
| 118 | + |
| 119 | + console.Success("Successfully published OpenAPI collection!"); |
| 120 | + break; |
| 121 | + |
| 122 | + case IProcessingTaskIsReady: |
| 123 | + console.Success("Your request is ready for processing."); |
| 124 | + break; |
| 125 | + |
| 126 | + case IOperationInProgress: |
| 127 | + ctx?.Status("Your request is in progress."); |
| 128 | + break; |
| 129 | + |
| 130 | + case IWaitForApproval e: |
| 131 | + if (e.Deployment is |
| 132 | + IOnSchemaVersionPublishUpdated_OnSchemaVersionPublishingUpdate_Deployment_OpenApiCollectionDeployment |
| 133 | + deployment) |
| 134 | + { |
| 135 | + // TODO: Print the errors here |
| 136 | + // console.PrintErrors(deployment.Errors); |
| 137 | + } |
| 138 | + |
| 139 | + ctx?.Status( |
| 140 | + "The processing of your request is waiting for approval. Check Nitro to approve the request."); |
| 141 | + break; |
| 142 | + |
| 143 | + case IProcessingTaskApproved: |
| 144 | + ctx?.Status("The processing of your request is approved."); |
| 145 | + |
| 146 | + break; |
| 147 | + |
| 148 | + default: |
| 149 | + ctx?.Status( |
| 150 | + "This is an unknown response, upgrade Nitro CLI to the latest version."); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static async Task<string> PublishOpenApiCollectionAsync( |
| 158 | + IAnsiConsole console, |
| 159 | + IApiClient client, |
| 160 | + PublishOpenApiCollectionInput input, |
| 161 | + CancellationToken ct) |
| 162 | + { |
| 163 | + var result = |
| 164 | + await client.PublishOpenApiCollectionCommandMutation.ExecuteAsync(input, ct); |
| 165 | + |
| 166 | + console.EnsureNoErrors(result); |
| 167 | + var data = console.EnsureData(result); |
| 168 | + console.PrintErrorsAndExit(data.PublishOpenApiCollection.Errors); |
| 169 | + |
| 170 | + if (data.PublishOpenApiCollection.Id is null) |
| 171 | + { |
| 172 | + throw new ExitException("Could not create publish request!"); |
| 173 | + } |
| 174 | + |
| 175 | + return data.PublishOpenApiCollection.Id; |
| 176 | + } |
| 177 | +} |
0 commit comments