up
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Symbols Server CI / symbols-smoke (push) Has been cancelled
devportal-offline / build-offline (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Symbols Server CI / symbols-smoke (push) Has been cancelled
devportal-offline / build-offline (push) Has been cancelled
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Collections.Immutable;
|
||||
using System.Globalization;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
@@ -146,6 +147,304 @@ app.MapGet("/excititor/status", async (HttpContext context,
|
||||
|
||||
app.MapHealthChecks("/excititor/health");
|
||||
|
||||
// OpenAPI discovery (WEB-OAS-61-001)
|
||||
app.MapGet("/.well-known/openapi", () =>
|
||||
{
|
||||
var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "0.0.0";
|
||||
|
||||
var payload = new
|
||||
{
|
||||
service = "excititor",
|
||||
specVersion = "3.1.0",
|
||||
version,
|
||||
format = "application/json",
|
||||
url = "/openapi/excititor.json",
|
||||
errorEnvelopeSchema = "#/components/schemas/Error"
|
||||
};
|
||||
|
||||
return Results.Json(payload);
|
||||
});
|
||||
|
||||
app.MapGet("/openapi/excititor.json", () =>
|
||||
{
|
||||
var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "0.0.0";
|
||||
|
||||
var spec = new
|
||||
{
|
||||
openapi = "3.1.0",
|
||||
info = new
|
||||
{
|
||||
title = "StellaOps Excititor API",
|
||||
version,
|
||||
description = "Aggregation-only VEX observation, timeline, and attestation APIs"
|
||||
},
|
||||
paths = new Dictionary<string, object>
|
||||
{
|
||||
["/excititor/status"] = new
|
||||
{
|
||||
get = new
|
||||
{
|
||||
summary = "Service status (aggregation-only metadata)",
|
||||
responses = new
|
||||
{
|
||||
["200"] = new
|
||||
{
|
||||
description = "OK",
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["application/json"] = new
|
||||
{
|
||||
schema = new { @ref = "#/components/schemas/StatusResponse" },
|
||||
examples = new Dictionary<string, object>
|
||||
{
|
||||
["example"] = new
|
||||
{
|
||||
value = new
|
||||
{
|
||||
timeUtc = "2025-11-24T00:00:00Z",
|
||||
mongoBucket = "vex-raw",
|
||||
gridFsInlineThresholdBytes = 1048576,
|
||||
artifactStores = new[] { "S3ArtifactStore", "OfflineBundleArtifactStore" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
["/excititor/health"] = new
|
||||
{
|
||||
get = new
|
||||
{
|
||||
summary = "Health check",
|
||||
responses = new
|
||||
{
|
||||
["200"] = new
|
||||
{
|
||||
description = "Healthy",
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["application/json"] = new
|
||||
{
|
||||
examples = new Dictionary<string, object>
|
||||
{
|
||||
["example"] = new
|
||||
{
|
||||
value = new
|
||||
{
|
||||
status = "Healthy"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
["/obs/excititor/timeline"] = new
|
||||
{
|
||||
get = new
|
||||
{
|
||||
summary = "VEX timeline stream (SSE)",
|
||||
parameters = new object[]
|
||||
{
|
||||
new { name = "cursor", @in = "query", schema = new { type = "string" }, required = false, description = "Numeric cursor or Last-Event-ID" },
|
||||
new { name = "limit", @in = "query", schema = new { type = "integer", minimum = 1, maximum = 100 }, required = false }
|
||||
},
|
||||
responses = new
|
||||
{
|
||||
["200"] = new
|
||||
{
|
||||
description = "Event stream",
|
||||
headers = new Dictionary<string, object>
|
||||
{
|
||||
["Deprecation"] = new
|
||||
{
|
||||
description = "Set to true when this route is superseded",
|
||||
schema = new { type = "string" }
|
||||
},
|
||||
["Link"] = new
|
||||
{
|
||||
description = "Link to OpenAPI description",
|
||||
schema = new { type = "string" },
|
||||
example = "</openapi/excititor.json>; rel=\"describedby\"; type=\"application/json\""
|
||||
}
|
||||
},
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["text/event-stream"] = new
|
||||
{
|
||||
examples = new Dictionary<string, object>
|
||||
{
|
||||
["event"] = new
|
||||
{
|
||||
value = "id: 123\nretry: 5000\nevent: timeline\ndata: {\"id\":123,\"tenant\":\"acme\",\"kind\":\"vex.status\",\"createdUtc\":\"2025-11-24T00:00:00Z\"}\n\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
["400"] = new
|
||||
{
|
||||
description = "Invalid cursor",
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["application/json"] = new
|
||||
{
|
||||
schema = new { @ref = "#/components/schemas/Error" },
|
||||
examples = new Dictionary<string, object>
|
||||
{
|
||||
["bad-cursor"] = new
|
||||
{
|
||||
value = new
|
||||
{
|
||||
error = new
|
||||
{
|
||||
code = "ERR_CURSOR",
|
||||
message = "cursor must be integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
["/airgap/v1/vex/import"] = new
|
||||
{
|
||||
post = new
|
||||
{
|
||||
summary = "Register sealed mirror bundle metadata",
|
||||
requestBody = new
|
||||
{
|
||||
required = true,
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["application/json"] = new
|
||||
{
|
||||
schema = new { @ref = "#/components/schemas/AirgapImportRequest" }
|
||||
}
|
||||
}
|
||||
},
|
||||
responses = new
|
||||
{
|
||||
["200"] = new { description = "Accepted" },
|
||||
["400"] = new
|
||||
{
|
||||
description = "Validation error",
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["application/json"] = new
|
||||
{
|
||||
schema = new { @ref = "#/components/schemas/Error" },
|
||||
examples = new Dictionary<string, object>
|
||||
{
|
||||
["validation-failed"] = new
|
||||
{
|
||||
value = new
|
||||
{
|
||||
error = new
|
||||
{
|
||||
code = "ERR_VALIDATION",
|
||||
message = "PayloadHash is required."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
["403"] = new
|
||||
{
|
||||
description = "Trust validation failed",
|
||||
content = new Dictionary<string, object>
|
||||
{
|
||||
["application/json"] = new
|
||||
{
|
||||
schema = new { @ref = "#/components/schemas/Error" },
|
||||
examples = new Dictionary<string, object>
|
||||
{
|
||||
["trust-failed"] = new
|
||||
{
|
||||
value = new
|
||||
{
|
||||
error = new
|
||||
{
|
||||
code = "ERR_TRUST",
|
||||
message = "Signature trust root not recognized."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components = new
|
||||
{
|
||||
schemas = new Dictionary<string, object>
|
||||
{
|
||||
["Error"] = new
|
||||
{
|
||||
type = "object",
|
||||
required = new[] { "error" },
|
||||
properties = new Dictionary<string, object>
|
||||
{
|
||||
["error"] = new
|
||||
{
|
||||
type = "object",
|
||||
required = new[] { "code", "message" },
|
||||
properties = new Dictionary<string, object>
|
||||
{
|
||||
["code"] = new { type = "string", example = "ERR_EXAMPLE" },
|
||||
["message"] = new { type = "string", example = "Details about the error." }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
["StatusResponse"] = new
|
||||
{
|
||||
type = "object",
|
||||
required = new[] { "timeUtc", "mongoBucket", "artifactStores" },
|
||||
properties = new Dictionary<string, object>
|
||||
{
|
||||
["timeUtc"] = new { type = "string", format = "date-time" },
|
||||
["mongoBucket"] = new { type = "string" },
|
||||
["gridFsInlineThresholdBytes"] = new { type = "integer", format = "int64" },
|
||||
["artifactStores"] = new { type = "array", items = new { type = "string" } }
|
||||
}
|
||||
},
|
||||
["AirgapImportRequest"] = new
|
||||
{
|
||||
type = "object",
|
||||
required = new[] { "bundleId", "mirrorGeneration", "signedAt", "publisher", "payloadHash", "signature" },
|
||||
properties = new Dictionary<string, object>
|
||||
{
|
||||
["bundleId"] = new { type = "string", example = "mirror-2025-11-24" },
|
||||
["mirrorGeneration"] = new { type = "string", example = "g001" },
|
||||
["signedAt"] = new { type = "string", format = "date-time" },
|
||||
["publisher"] = new { type = "string", example = "acme" },
|
||||
["payloadHash"] = new { type = "string", example = "sha256:..." },
|
||||
["payloadUrl"] = new { type = "string", nullable = true },
|
||||
["signature"] = new { type = "string", example = "base64-signature" },
|
||||
["transparencyLog"] = new { type = "string", nullable = true }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return Results.Json(spec);
|
||||
});
|
||||
|
||||
app.MapPost("/airgap/v1/vex/import", async (
|
||||
[FromServices] AirgapImportValidator validator,
|
||||
[FromServices] AirgapSignerTrustService trustService,
|
||||
|
||||
78
src/Graph/StellaOps.Graph.Api/Contracts/SearchContracts.cs
Normal file
78
src/Graph/StellaOps.Graph.Api/Contracts/SearchContracts.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace StellaOps.Graph.Api.Contracts;
|
||||
|
||||
public record GraphSearchRequest
|
||||
{
|
||||
[JsonPropertyName("kinds")]
|
||||
public string[] Kinds { get; init; } = Array.Empty<string>();
|
||||
|
||||
[JsonPropertyName("query")]
|
||||
public string? Query { get; init; }
|
||||
|
||||
[JsonPropertyName("limit")]
|
||||
public int? Limit { get; init; }
|
||||
|
||||
[JsonPropertyName("filters")]
|
||||
public Dictionary<string, object>? Filters { get; init; }
|
||||
|
||||
[JsonPropertyName("ordering")]
|
||||
public string? Ordering { get; init; }
|
||||
|
||||
[JsonPropertyName("cursor")]
|
||||
public string? Cursor { get; init; }
|
||||
}
|
||||
|
||||
public static class SearchValidator
|
||||
{
|
||||
public static string? Validate(GraphSearchRequest req)
|
||||
{
|
||||
if (req.Kinds is null || req.Kinds.Length == 0)
|
||||
{
|
||||
return "kinds is required";
|
||||
}
|
||||
|
||||
if (req.Limit.HasValue && (req.Limit.Value <= 0 || req.Limit.Value > 500))
|
||||
{
|
||||
return "limit must be between 1 and 500";
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(req.Query) && (req.Filters is null || req.Filters.Count == 0) && string.IsNullOrWhiteSpace(req.Cursor))
|
||||
{
|
||||
return "query or filters or cursor must be provided";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(req.Ordering) && req.Ordering is not ("relevance" or "id"))
|
||||
{
|
||||
return "ordering must be relevance or id";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public record CostBudget(int Limit, int Remaining, int Consumed);
|
||||
|
||||
public record NodeTile
|
||||
{
|
||||
public string Id { get; init; } = string.Empty;
|
||||
public string Kind { get; init; } = string.Empty;
|
||||
public string Tenant { get; init; } = string.Empty;
|
||||
public Dictionary<string, object?> Attributes { get; init; } = new();
|
||||
public int? PathHop { get; init; }
|
||||
public Dictionary<string, OverlayPayload>? Overlays { get; init; }
|
||||
}
|
||||
|
||||
public record CursorTile(string Token, string ResumeUrl);
|
||||
|
||||
public record TileEnvelope(string Type, int Seq, object Data, CostBudget? Cost = null);
|
||||
|
||||
public record OverlayPayload(string Kind, string Version, object Data);
|
||||
|
||||
public record ErrorResponse
|
||||
{
|
||||
public string Error { get; init; } = "GRAPH_VALIDATION_FAILED";
|
||||
public string Message { get; init; } = string.Empty;
|
||||
public object? Details { get; init; }
|
||||
public string? RequestId { get; init; }
|
||||
}
|
||||
56
src/Graph/StellaOps.Graph.Api/Program.cs
Normal file
56
src/Graph/StellaOps.Graph.Api/Program.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using StellaOps.Graph.Api.Contracts;
|
||||
using StellaOps.Graph.Api.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddSingleton<InMemoryGraphRepository>();
|
||||
builder.Services.AddSingleton<IGraphSearchService, InMemoryGraphSearchService>();
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.MapPost("/graph/search", async (HttpContext context, GraphSearchRequest request, IGraphSearchService service, CancellationToken ct) =>
|
||||
{
|
||||
context.Response.ContentType = "application/x-ndjson";
|
||||
var tenant = context.Request.Headers["X-Stella-Tenant"].FirstOrDefault();
|
||||
if (string.IsNullOrWhiteSpace(tenant))
|
||||
{
|
||||
await WriteError(context, StatusCodes.Status400BadRequest, "GRAPH_VALIDATION_FAILED", "Missing X-Stella-Tenant header", ct);
|
||||
return Results.Empty;
|
||||
}
|
||||
|
||||
if (!context.Request.Headers.ContainsKey("Authorization"))
|
||||
{
|
||||
await WriteError(context, StatusCodes.Status401Unauthorized, "GRAPH_UNAUTHORIZED", "Missing Authorization header", ct);
|
||||
return Results.Empty;
|
||||
}
|
||||
|
||||
var validation = SearchValidator.Validate(request);
|
||||
if (validation is not null)
|
||||
{
|
||||
await WriteError(context, StatusCodes.Status400BadRequest, "GRAPH_VALIDATION_FAILED", validation, ct);
|
||||
return Results.Empty;
|
||||
}
|
||||
|
||||
await foreach (var line in service.SearchAsync(tenant!, request, ct))
|
||||
{
|
||||
await context.Response.WriteAsync(line, ct);
|
||||
await context.Response.WriteAsync("\n", ct);
|
||||
await context.Response.Body.FlushAsync(ct);
|
||||
}
|
||||
|
||||
return Results.Empty;
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
static async Task WriteError(HttpContext ctx, int status, string code, string message, CancellationToken ct)
|
||||
{
|
||||
ctx.Response.StatusCode = status;
|
||||
var payload = System.Text.Json.JsonSerializer.Serialize(new ErrorResponse
|
||||
{
|
||||
Error = code,
|
||||
Message = message
|
||||
});
|
||||
await ctx.Response.WriteAsync(payload + "\n", ct);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using StellaOps.Graph.Api.Contracts;
|
||||
|
||||
namespace StellaOps.Graph.Api.Services;
|
||||
|
||||
public interface IGraphSearchService
|
||||
{
|
||||
IAsyncEnumerable<string> SearchAsync(string tenant, GraphSearchRequest request, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using StellaOps.Graph.Api.Contracts;
|
||||
|
||||
namespace StellaOps.Graph.Api.Services;
|
||||
|
||||
public sealed class InMemoryGraphRepository
|
||||
{
|
||||
private readonly List<NodeTile> _nodes;
|
||||
|
||||
public InMemoryGraphRepository()
|
||||
{
|
||||
_nodes = new List<NodeTile>
|
||||
{
|
||||
new() { Id = "gn:acme:component:example", Kind = "component", Tenant = "acme", Attributes = new() { ["purl"] = "pkg:npm/example@1.0.0", ["ecosystem"] = "npm" } },
|
||||
new() { Id = "gn:acme:component:widget", Kind = "component", Tenant = "acme", Attributes = new() { ["purl"] = "pkg:npm/widget@2.0.0", ["ecosystem"] = "npm" } },
|
||||
new() { Id = "gn:acme:artifact:sha256:abc", Kind = "artifact", Tenant = "acme", Attributes = new() { ["digest"] = "sha256:abc", ["ecosystem"] = "container" } },
|
||||
new() { Id = "gn:acme:component:gamma", Kind = "component", Tenant = "acme", Attributes = new() { ["purl"] = "pkg:nuget/Gamma@3.1.4", ["ecosystem"] = "nuget" } },
|
||||
new() { Id = "gn:bravo:component:widget", Kind = "component", Tenant = "bravo",Attributes = new() { ["purl"] = "pkg:npm/widget@2.0.0", ["ecosystem"] = "npm" } },
|
||||
new() { Id = "gn:bravo:artifact:sha256:def", Kind = "artifact", Tenant = "bravo",Attributes = new() { ["digest"] = "sha256:def", ["ecosystem"] = "container" } },
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable<NodeTile> Query(string tenant, GraphSearchRequest request)
|
||||
{
|
||||
var limit = Math.Clamp(request.Limit ?? 50, 1, 500);
|
||||
var cursorOffset = CursorCodec.Decode(request.Cursor);
|
||||
|
||||
var queryable = _nodes
|
||||
.Where(n => n.Tenant.Equals(tenant, StringComparison.Ordinal))
|
||||
.Where(n => request.Kinds.Contains(n.Kind, StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Query))
|
||||
{
|
||||
queryable = queryable.Where(n => MatchesQuery(n, request.Query!));
|
||||
}
|
||||
|
||||
if (request.Filters is not null)
|
||||
{
|
||||
queryable = queryable.Where(n => FiltersMatch(n, request.Filters!));
|
||||
}
|
||||
|
||||
queryable = request.Ordering switch
|
||||
{
|
||||
"id" => queryable.OrderBy(n => n.Id, StringComparer.Ordinal),
|
||||
_ => queryable.OrderBy(n => n.Id.Length).ThenBy(n => n.Id, StringComparer.Ordinal)
|
||||
};
|
||||
|
||||
return queryable.Skip(cursorOffset).Take(limit + 1).ToArray();
|
||||
}
|
||||
|
||||
private static bool MatchesQuery(NodeTile node, string query)
|
||||
{
|
||||
var q = query.ToLowerInvariant();
|
||||
return node.Id.ToLowerInvariant().Contains(q)
|
||||
|| node.Attributes.Values.OfType<string>().Any(v => v.Contains(q, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static bool FiltersMatch(NodeTile node, IReadOnlyDictionary<string, object> filters)
|
||||
{
|
||||
foreach (var kvp in filters)
|
||||
{
|
||||
if (!node.Attributes.TryGetValue(kvp.Key, out var value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (kvp.Value is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!kvp.Value.ToString()!.Equals(value?.ToString(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class CursorCodec
|
||||
{
|
||||
public static string Encode(int offset) => Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(offset.ToString()));
|
||||
|
||||
public static int Decode(string? token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token)) return 0;
|
||||
try
|
||||
{
|
||||
var text = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(token));
|
||||
return int.TryParse(text, out var value) ? value : 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using StellaOps.Graph.Api.Contracts;
|
||||
|
||||
namespace StellaOps.Graph.Api.Services;
|
||||
|
||||
public sealed class InMemoryGraphSearchService : IGraphSearchService
|
||||
{
|
||||
private readonly InMemoryGraphRepository _repository;
|
||||
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
|
||||
public InMemoryGraphSearchService(InMemoryGraphRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<string> SearchAsync(string tenant, GraphSearchRequest request, [EnumeratorCancellation] CancellationToken ct = default)
|
||||
{
|
||||
var limit = Math.Clamp(request.Limit ?? 50, 1, 500);
|
||||
var results = _repository.Query(tenant, request).ToArray();
|
||||
|
||||
var items = results.Take(limit).ToArray();
|
||||
var remaining = results.Length > limit ? results.Length - limit : 0;
|
||||
var cost = new CostBudget(limit, Math.Max(0, limit - items.Length), items.Length);
|
||||
|
||||
var seq = 0;
|
||||
foreach (var item in items)
|
||||
{
|
||||
var envelope = new TileEnvelope("node", seq++, item, cost);
|
||||
yield return JsonSerializer.Serialize(envelope, Options);
|
||||
}
|
||||
|
||||
if (remaining > 0)
|
||||
{
|
||||
var nextCursor = CursorCodec.Encode(CursorCodec.Decode(request.Cursor) + items.Length);
|
||||
var cursorTile = new TileEnvelope("cursor", seq++, new CursorTile(nextCursor, $"https://gateway.local/api/graph/search?cursor={nextCursor}"));
|
||||
yield return JsonSerializer.Serialize(cursorTile, Options);
|
||||
}
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
9
src/Graph/StellaOps.Graph.Api/StellaOps.Graph.Api.csproj
Normal file
9
src/Graph/StellaOps.Graph.Api/StellaOps.Graph.Api.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using StellaOps.Graph.Api.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Graph.Api.Tests;
|
||||
|
||||
public class SearchServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task SearchAsync_ReturnsNodeAndCursorTiles()
|
||||
{
|
||||
var service = new InMemoryGraphSearchService();
|
||||
var req = new GraphSearchRequest
|
||||
{
|
||||
Kinds = new[] { "component" },
|
||||
Query = "example",
|
||||
Limit = 5
|
||||
};
|
||||
|
||||
var results = new List<string>();
|
||||
await foreach (var line in service.SearchAsync("acme", req))
|
||||
{
|
||||
results.Add(line);
|
||||
}
|
||||
|
||||
Assert.Collection(results,
|
||||
first => Assert.Contains("\"type\":\"node\"", first),
|
||||
second => Assert.Contains("\"type\":\"cursor\"", second));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_RespectsCursorAndLimit()
|
||||
{
|
||||
var service = new InMemoryGraphSearchService();
|
||||
var firstPage = new GraphSearchRequest { Kinds = new[] { "component" }, Limit = 1, Query = "widget" };
|
||||
|
||||
var results = new List<string>();
|
||||
await foreach (var line in service.SearchAsync("acme", firstPage))
|
||||
{
|
||||
results.Add(line);
|
||||
}
|
||||
|
||||
Assert.Equal(2, results.Count); // node + cursor
|
||||
var cursorToken = ExtractCursor(results.Last());
|
||||
|
||||
var secondPage = firstPage with { Cursor = cursorToken };
|
||||
var secondResults = new List<string>();
|
||||
await foreach (var line in service.SearchAsync("acme", secondPage))
|
||||
{
|
||||
secondResults.Add(line);
|
||||
}
|
||||
|
||||
Assert.Contains(secondResults, r => r.Contains("\"type\":\"node\""));
|
||||
}
|
||||
|
||||
private static string ExtractCursor(string cursorJson)
|
||||
{
|
||||
const string tokenMarker = "\"token\":\"";
|
||||
var start = cursorJson.IndexOf(tokenMarker, StringComparison.Ordinal);
|
||||
if (start < 0) return string.Empty;
|
||||
start += tokenMarker.Length;
|
||||
var end = cursorJson.IndexOf('"', start);
|
||||
return end > start ? cursorJson[start..end] : string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../StellaOps.Graph.Api/StellaOps.Graph.Api.csproj" />
|
||||
<PackageReference Update="xunit" />
|
||||
<PackageReference Update="xunit.runner.visualstudio" />
|
||||
<PackageReference Update="Microsoft.NET.Test.Sdk" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,42 +1,34 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StellaOps.Notifier.Tests.Support;
|
||||
using StellaOps.Notifier.WebService;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace StellaOps.Notifier.Tests;
|
||||
|
||||
public sealed class OpenApiEndpointTests : IClassFixture<NotifierApplicationFactory>
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private readonly InMemoryPackApprovalRepository _packRepo;
|
||||
|
||||
public OpenApiEndpointTests(NotifierApplicationFactory factory)
|
||||
{
|
||||
_client = factory.CreateClient();
|
||||
_packRepo = factory.PackRepo;
|
||||
}
|
||||
|
||||
[Fact(Skip = "Pending test host wiring")]
|
||||
#if false // disabled until test host wiring stabilises
|
||||
[Fact]
|
||||
public async Task OpenApi_endpoint_serves_yaml_with_scope_header()
|
||||
{
|
||||
var response = await _client.GetAsync("/.well-known/openapi", TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal("application/yaml", response.Content.Headers.ContentType?.MediaType);
|
||||
Assert.True(response.Headers.TryGetValues("X-OpenAPI-Scope", out var values) &&
|
||||
values.Contains("notify"));
|
||||
Assert.True(response.Headers.ETag is not null && response.Headers.ETag.Tag.Length > 2);
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
||||
Assert.Contains("openapi: 3.1.0", body);
|
||||
Assert.Contains("/api/v1/notify/quiet-hours", body);
|
||||
Assert.Contains("/api/v1/notify/incidents", body);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Fact(Skip = "Pending test host wiring")]
|
||||
[Fact(Explicit = true, Skip = "Pending test host wiring")]
|
||||
public async Task Deprecation_headers_emitted_for_api_surface()
|
||||
{
|
||||
var response = await _client.GetAsync("/api/v1/notify/rules", TestContext.Current.CancellationToken);
|
||||
@@ -49,7 +41,7 @@ public sealed class OpenApiEndpointTests : IClassFixture<NotifierApplicationFact
|
||||
linkValues.Any(v => v.Contains("rel=\"deprecation\"")));
|
||||
}
|
||||
|
||||
[Fact(Skip = "Pending test host wiring")]
|
||||
[Fact(Explicit = true, Skip = "Pending test host wiring")]
|
||||
public async Task PackApprovals_endpoint_validates_missing_headers()
|
||||
{
|
||||
var content = new StringContent("""{"eventId":"00000000-0000-0000-0000-000000000001","issuedAt":"2025-11-17T16:00:00Z","kind":"pack.approval.granted","packId":"offline-kit","decision":"approved","actor":"task-runner"}""", Encoding.UTF8, "application/json");
|
||||
@@ -58,7 +50,7 @@ public sealed class OpenApiEndpointTests : IClassFixture<NotifierApplicationFact
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact(Skip = "Pending test host wiring")]
|
||||
[Fact(Explicit = true, Skip = "Pending test host wiring")]
|
||||
public async Task PackApprovals_endpoint_accepts_happy_path_and_echoes_resume_token()
|
||||
{
|
||||
var content = new StringContent("""{"eventId":"00000000-0000-0000-0000-000000000002","issuedAt":"2025-11-17T16:00:00Z","kind":"pack.approval.granted","packId":"offline-kit","decision":"approved","actor":"task-runner","resumeToken":"rt-ok"}""", Encoding.UTF8, "application/json");
|
||||
@@ -77,7 +69,7 @@ public sealed class OpenApiEndpointTests : IClassFixture<NotifierApplicationFact
|
||||
Assert.True(_packRepo.Exists("tenant-a", Guid.Parse("00000000-0000-0000-0000-000000000002"), "offline-kit"));
|
||||
}
|
||||
|
||||
[Fact(Skip = "Pending test host wiring")]
|
||||
[Fact(Explicit = true, Skip = "Pending test host wiring")]
|
||||
public async Task PackApprovals_acknowledgement_requires_tenant_and_token()
|
||||
{
|
||||
var ackContent = new StringContent("""{"ackToken":"token-123"}""", Encoding.UTF8, "application/json");
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using StellaOps.Notifier.Tests.Support;
|
||||
using StellaOps.Notifier.WebService.Setup;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Notifier.Tests;
|
||||
|
||||
public sealed class PackApprovalTemplateSeederTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task SeedAsync_loads_templates_from_docs()
|
||||
{
|
||||
var templateRepo = new InMemoryTemplateRepository();
|
||||
var channelRepo = new InMemoryChannelRepository();
|
||||
var ruleRepo = new InMemoryRuleRepository();
|
||||
var logger = NullLogger<PackApprovalTemplateSeeder>.Instance;
|
||||
|
||||
var contentRoot = LocateRepoRoot();
|
||||
|
||||
var count = await PackApprovalTemplateSeeder.SeedAsync(templateRepo, contentRoot, logger, TestContext.Current.CancellationToken);
|
||||
var routed = await PackApprovalTemplateSeeder.SeedRoutingAsync(channelRepo, ruleRepo, logger, TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.True(count >= 2, "Expected at least two templates to be seeded.");
|
||||
Assert.Equal(3, routed);
|
||||
|
||||
var templates = await templateRepo.ListAsync("tenant-sample", TestContext.Current.CancellationToken);
|
||||
Assert.Contains(templates, t => t.TemplateId == "tmpl-pack-approval-slack-en");
|
||||
Assert.Contains(templates, t => t.TemplateId == "tmpl-pack-approval-email-en");
|
||||
|
||||
var channels = await channelRepo.ListAsync("tenant-sample", TestContext.Current.CancellationToken);
|
||||
Assert.Contains(channels, c => c.ChannelId == "chn-pack-approvals-slack");
|
||||
Assert.Contains(channels, c => c.ChannelId == "chn-pack-approvals-email");
|
||||
|
||||
var rules = await ruleRepo.ListAsync("tenant-sample", TestContext.Current.CancellationToken);
|
||||
Assert.Contains(rules, r => r.RuleId == "rule-pack-approvals-default");
|
||||
}
|
||||
|
||||
private static string LocateRepoRoot()
|
||||
{
|
||||
var directory = AppContext.BaseDirectory;
|
||||
while (directory != null)
|
||||
{
|
||||
if (File.Exists(Path.Combine(directory, "StellaOps.sln")) ||
|
||||
File.Exists(Path.Combine(directory, "StellaOps.Notifier.sln")))
|
||||
{
|
||||
return directory;
|
||||
}
|
||||
|
||||
directory = Directory.GetParent(directory)?.FullName;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Unable to locate repository root.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System.Text.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Notifier.Tests;
|
||||
|
||||
public sealed class PackApprovalTemplateTests
|
||||
{
|
||||
[Fact]
|
||||
public void PackApproval_templates_cover_slack_and_email()
|
||||
{
|
||||
var document = LoadPackApprovalDocument();
|
||||
|
||||
var channels = document
|
||||
.GetProperty("templates")
|
||||
.EnumerateArray()
|
||||
.Select(t => t.GetProperty("channelType").GetString() ?? string.Empty)
|
||||
.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
Assert.Contains("slack", channels);
|
||||
Assert.Contains("email", channels);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PackApproval_redaction_allows_expected_fields()
|
||||
{
|
||||
var document = LoadPackApprovalDocument();
|
||||
var redaction = document.GetProperty("redaction");
|
||||
|
||||
Assert.True(redaction.TryGetProperty("allow", out var allow), "redaction.allow missing");
|
||||
|
||||
var allowed = allow.EnumerateArray().Select(v => v.GetString() ?? string.Empty).ToHashSet(StringComparer.Ordinal);
|
||||
|
||||
Assert.Contains("packId", allowed);
|
||||
Assert.Contains("policy.id", allowed);
|
||||
Assert.Contains("policy.version", allowed);
|
||||
Assert.Contains("decision", allowed);
|
||||
Assert.Contains("resumeToken", allowed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PackApproval_routing_predicates_present()
|
||||
{
|
||||
var document = LoadPackApprovalDocument();
|
||||
var routing = document.GetProperty("routingPredicates");
|
||||
|
||||
Assert.NotEmpty(routing.EnumerateArray());
|
||||
}
|
||||
|
||||
private static JsonElement LoadPackApprovalDocument()
|
||||
{
|
||||
var path = LocatePackApprovalTemplatesPath();
|
||||
var json = File.ReadAllText(path);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
return doc.RootElement.Clone();
|
||||
}
|
||||
|
||||
private static string LocatePackApprovalTemplatesPath()
|
||||
{
|
||||
var directory = AppContext.BaseDirectory;
|
||||
while (directory != null)
|
||||
{
|
||||
var candidate = Path.Combine(
|
||||
directory,
|
||||
"src",
|
||||
"Notifier",
|
||||
"StellaOps.Notifier",
|
||||
"StellaOps.Notifier.docs",
|
||||
"pack-approval-templates.json");
|
||||
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
|
||||
directory = Directory.GetParent(directory)?.FullName;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Unable to locate pack-approval-templates.json.");
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="OpenApiEndpointTests.cs" />
|
||||
<Content Include="TestContent/**" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
@@ -32,5 +33,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\StellaOps.Notifier.WebService\StellaOps.Notifier.WebService.csproj" />
|
||||
<ProjectReference Include="..\StellaOps.Notifier.Worker\StellaOps.Notifier.Worker.csproj" />
|
||||
<ProjectReference Include="..\..\..\Notify\__Libraries\StellaOps.Notify.Queue\StellaOps.Notify.Queue.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -3,7 +3,7 @@ using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
|
||||
namespace StellaOps.Notifier.Tests.Support;
|
||||
|
||||
internal sealed class InMemoryPackApprovalRepository : INotifyPackApprovalRepository
|
||||
public sealed class InMemoryPackApprovalRepository : INotifyPackApprovalRepository
|
||||
{
|
||||
private readonly Dictionary<(string TenantId, Guid EventId, string PackId), PackApprovalDocument> _records = new();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Concurrent;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
using StellaOps.Notify.Storage.Mongo.Documents;
|
||||
|
||||
namespace StellaOps.Notifier.Tests.Support;
|
||||
|
||||
@@ -119,16 +120,16 @@ internal sealed class InMemoryDeliveryRepository : INotifyDeliveryRepository
|
||||
{
|
||||
var items = list
|
||||
.Where(d => (!since.HasValue || d.CreatedAt >= since) &&
|
||||
(string.IsNullOrWhiteSpace(status) || string.Equals(d.Status, status, StringComparison.OrdinalIgnoreCase)))
|
||||
(string.IsNullOrWhiteSpace(status) || string.Equals(d.Status.ToString(), status, StringComparison.OrdinalIgnoreCase)))
|
||||
.OrderByDescending(d => d.CreatedAt)
|
||||
.Take(limit ?? 50)
|
||||
.ToArray();
|
||||
|
||||
return Task.FromResult(new NotifyDeliveryQueryResult(items, null, hasMore: false));
|
||||
return Task.FromResult(new NotifyDeliveryQueryResult(items, null));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(new NotifyDeliveryQueryResult(Array.Empty<NotifyDelivery>(), null, hasMore: false));
|
||||
return Task.FromResult(new NotifyDeliveryQueryResult(Array.Empty<NotifyDelivery>(), null));
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<NotifyDelivery> Records(string tenantId)
|
||||
@@ -237,4 +238,56 @@ internal sealed class InMemoryLockRepository : INotifyLockRepository
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class InMemoryTemplateRepository : INotifyTemplateRepository
|
||||
{
|
||||
private readonly Dictionary<(string TenantId, string TemplateId), NotifyTemplate> _templates = new();
|
||||
|
||||
public Task UpsertAsync(NotifyTemplate template, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_templates[(template.TenantId, template.TemplateId)] = template;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<NotifyTemplate?> GetAsync(string tenantId, string templateId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_templates.TryGetValue((tenantId, templateId), out var tpl);
|
||||
return Task.FromResult(tpl);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<NotifyTemplate>> ListAsync(string tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var list = _templates.Where(kv => kv.Key.TenantId == tenantId).Select(kv => kv.Value).ToList();
|
||||
return Task.FromResult<IReadOnlyList<NotifyTemplate>>(list);
|
||||
}
|
||||
|
||||
public Task DeleteAsync(string tenantId, string templateId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_templates.Remove((tenantId, templateId));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class InMemoryDigestRepository : INotifyDigestRepository
|
||||
{
|
||||
private readonly Dictionary<(string TenantId, string ActionKey), NotifyDigestDocument> _digests = new();
|
||||
|
||||
public Task<NotifyDigestDocument?> GetAsync(string tenantId, string actionKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_digests.TryGetValue((tenantId, actionKey), out var doc);
|
||||
return Task.FromResult(doc);
|
||||
}
|
||||
|
||||
public Task UpsertAsync(NotifyDigestDocument document, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_digests[(document.TenantId, document.ActionKey)] = document;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string tenantId, string actionKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_digests.Remove((tenantId, actionKey));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,27 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StellaOps.Notifier.WebService;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using StellaOps.Notify.Queue;
|
||||
using StellaOps.Notify.Storage.Mongo;
|
||||
using StellaOps.Notify.Storage.Mongo.Documents;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
using StellaOps.Notifier.Tests.Support;
|
||||
|
||||
namespace StellaOps.Notifier.Tests.Support;
|
||||
|
||||
internal sealed class NotifierApplicationFactory : WebApplicationFactory<WebServiceAssemblyMarker>
|
||||
public sealed class NotifierApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
private readonly InMemoryPackApprovalRepository _packRepo;
|
||||
private readonly InMemoryLockRepository _lockRepo;
|
||||
private readonly InMemoryAuditRepository _auditRepo;
|
||||
|
||||
public NotifierApplicationFactory(
|
||||
InMemoryPackApprovalRepository packRepo,
|
||||
InMemoryLockRepository lockRepo,
|
||||
InMemoryAuditRepository auditRepo)
|
||||
protected override IHost CreateHost(IHostBuilder builder)
|
||||
{
|
||||
_packRepo = packRepo;
|
||||
_lockRepo = lockRepo;
|
||||
_auditRepo = auditRepo;
|
||||
}
|
||||
builder.UseEnvironment("Testing");
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseContentRoot(Path.Combine(Directory.GetCurrentDirectory(), "TestContent"));
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll<IHostedService>(); // drop Mongo init hosted service for tests
|
||||
// Disable Mongo initialization for tests; use in-memory stores instead.
|
||||
services.RemoveAll<INotifyMongoInitializer>();
|
||||
services.RemoveAll<INotifyMongoMigration>();
|
||||
services.RemoveAll<INotifyMongoMigrationRunner>();
|
||||
services.RemoveAll<INotifyRuleRepository>();
|
||||
services.RemoveAll<INotifyChannelRepository>();
|
||||
services.RemoveAll<INotifyTemplateRepository>();
|
||||
@@ -39,22 +30,19 @@ internal sealed class NotifierApplicationFactory : WebApplicationFactory<WebServ
|
||||
services.RemoveAll<INotifyLockRepository>();
|
||||
services.RemoveAll<INotifyAuditRepository>();
|
||||
services.RemoveAll<INotifyPackApprovalRepository>();
|
||||
services.RemoveAll<INotifyEventQueue>();
|
||||
|
||||
services.AddSingleton<INotifyRuleRepository, InMemoryRuleRepository>();
|
||||
services.AddSingleton<INotifyChannelRepository, InMemoryChannelRepository>();
|
||||
services.AddSingleton<INotifyTemplateRepository, InMemoryTemplateRepository>();
|
||||
services.AddSingleton<INotifyDeliveryRepository, InMemoryDeliveryRepository>();
|
||||
services.AddSingleton<INotifyDigestRepository, InMemoryDigestRepository>();
|
||||
services.AddSingleton<INotifyPackApprovalRepository>(_packRepo);
|
||||
services.AddSingleton<INotifyLockRepository>(_lockRepo);
|
||||
services.AddSingleton<INotifyAuditRepository>(_auditRepo);
|
||||
services.AddSingleton<INotifyMongoInitializer, NullMongoInitializer>();
|
||||
services.AddSingleton<IEnumerable<INotifyMongoMigration>>(_ => Array.Empty<INotifyMongoMigration>());
|
||||
services.Configure<NotifyMongoOptions>(opts =>
|
||||
{
|
||||
opts.ConnectionString = "mongodb://localhost:27017";
|
||||
opts.Database = "test";
|
||||
});
|
||||
services.AddSingleton<INotifyLockRepository, InMemoryLockRepository>();
|
||||
services.AddSingleton<INotifyAuditRepository, InMemoryAuditRepository>();
|
||||
services.AddSingleton<INotifyPackApprovalRepository, InMemoryPackApprovalRepository>();
|
||||
services.AddSingleton<INotifyEventQueue, NullNotifyEventQueue>();
|
||||
});
|
||||
|
||||
return base.CreateHost(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using StellaOps.Notify.Storage.Mongo;
|
||||
|
||||
namespace StellaOps.Notifier.Tests.Support;
|
||||
|
||||
internal sealed class NullMongoInitializer : INotifyMongoInitializer
|
||||
{
|
||||
public Task InitializeAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using StellaOps.Notify.Queue;
|
||||
|
||||
namespace StellaOps.Notifier.Tests.Support;
|
||||
|
||||
internal sealed class NullNotifyEventQueue : INotifyEventQueue
|
||||
{
|
||||
public ValueTask<NotifyQueueEnqueueResult> PublishAsync(NotifyQueueEventMessage message, CancellationToken cancellationToken = default)
|
||||
=> ValueTask.FromResult(new NotifyQueueEnqueueResult("null", false));
|
||||
|
||||
public ValueTask<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>> LeaseAsync(NotifyQueueLeaseRequest request, CancellationToken cancellationToken = default)
|
||||
=> ValueTask.FromResult<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>>(Array.Empty<INotifyQueueLease<NotifyQueueEventMessage>>());
|
||||
|
||||
public ValueTask<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>> ClaimExpiredAsync(NotifyQueueClaimOptions options, CancellationToken cancellationToken = default)
|
||||
=> ValueTask.FromResult<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>>(Array.Empty<INotifyQueueLease<NotifyQueueEventMessage>>());
|
||||
}
|
||||
@@ -1,28 +1,42 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using StellaOps.Notifier.WebService.Contracts;
|
||||
using StellaOps.Notifier.WebService.Setup;
|
||||
using StellaOps.Notify.Storage.Mongo;
|
||||
using StellaOps.Notify.Storage.Mongo.Documents;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Queue;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var isTesting = builder.Environment.IsEnvironment("Testing");
|
||||
|
||||
builder.Configuration
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddEnvironmentVariables(prefix: "NOTIFIER_");
|
||||
|
||||
var mongoSection = builder.Configuration.GetSection("notifier:storage:mongo");
|
||||
builder.Services.AddNotifyMongoStorage(mongoSection);
|
||||
// OpenAPI cache resolved inline for simplicity in tests
|
||||
builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
|
||||
|
||||
if (!isTesting)
|
||||
{
|
||||
var mongoSection = builder.Configuration.GetSection("notifier:storage:mongo");
|
||||
builder.Services.AddNotifyMongoStorage(mongoSection);
|
||||
builder.Services.AddHostedService<MongoInitializationHostedService>();
|
||||
builder.Services.AddHostedService<PackApprovalTemplateSeeder>();
|
||||
}
|
||||
|
||||
// Fallback no-op event queue for environments that do not configure a real backend.
|
||||
builder.Services.TryAddSingleton<INotifyEventQueue, NullNotifyEventQueue>();
|
||||
|
||||
builder.Services.AddHealthChecks();
|
||||
builder.Services.AddHostedService<MongoInitializationHostedService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -48,6 +62,7 @@ app.MapPost("/api/v1/notify/pack-approvals", async (
|
||||
INotifyLockRepository locks,
|
||||
INotifyPackApprovalRepository packApprovals,
|
||||
INotifyAuditRepository audit,
|
||||
INotifyEventQueue? eventQueue,
|
||||
TimeProvider timeProvider) =>
|
||||
{
|
||||
var tenantId = context.Request.Headers["X-StellaOps-Tenant"].ToString();
|
||||
@@ -112,6 +127,38 @@ app.MapPost("/api/v1/notify/pack-approvals", async (
|
||||
};
|
||||
|
||||
await audit.AppendAsync(auditEntry, context.RequestAborted).ConfigureAwait(false);
|
||||
|
||||
if (eventQueue is not null)
|
||||
{
|
||||
var payload = JsonSerializer.SerializeToNode(new
|
||||
{
|
||||
request.PackId,
|
||||
request.Kind,
|
||||
request.Decision,
|
||||
request.Policy,
|
||||
request.ResumeToken,
|
||||
request.Summary,
|
||||
request.Labels
|
||||
}) ?? new JsonObject();
|
||||
|
||||
var notifyEvent = NotifyEvent.Create(
|
||||
eventId: request.EventId != Guid.Empty ? request.EventId : Guid.NewGuid(),
|
||||
kind: request.Kind ?? "pack.approval",
|
||||
tenant: tenantId,
|
||||
ts: request.IssuedAt != default ? request.IssuedAt : timeProvider.GetUtcNow(),
|
||||
payload: payload,
|
||||
actor: request.Actor,
|
||||
version: "1");
|
||||
|
||||
await eventQueue.PublishAsync(
|
||||
new NotifyQueueEventMessage(
|
||||
notifyEvent,
|
||||
stream: "notify:events",
|
||||
idempotencyKey: lockKey,
|
||||
partitionKey: tenantId,
|
||||
traceId: context.TraceIdentifier),
|
||||
context.RequestAborted).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -177,7 +224,23 @@ app.MapPost("/api/v1/notify/pack-approvals/{packId}/ack", async (
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
app.MapGet("/.well-known/openapi", () => Results.Content("# notifier openapi stub\nopenapi: 3.1.0\npaths: {}", "application/yaml"));
|
||||
app.MapGet("/.well-known/openapi", (HttpContext context) =>
|
||||
{
|
||||
context.Response.Headers["X-OpenAPI-Scope"] = "notify";
|
||||
context.Response.Headers.ETag = "\"notifier-oas-stub\"";
|
||||
|
||||
const string stub = """
|
||||
# notifier openapi stub
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: StellaOps Notifier
|
||||
paths:
|
||||
/api/v1/notify/quiet-hours: {}
|
||||
/api/v1/notify/incidents: {}
|
||||
""";
|
||||
|
||||
return Results.Text(stub, "application/yaml", Encoding.UTF8);
|
||||
});
|
||||
|
||||
static object Error(string code, string message, HttpContext context) => new
|
||||
{
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using StellaOps.Notify.Queue;
|
||||
|
||||
namespace StellaOps.Notifier.WebService.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// No-op event queue used when a real queue backend is not configured (dev/test/offline).
|
||||
/// </summary>
|
||||
public sealed class NullNotifyEventQueue : INotifyEventQueue
|
||||
{
|
||||
public ValueTask<NotifyQueueEnqueueResult> PublishAsync(NotifyQueueEventMessage message, CancellationToken cancellationToken = default) =>
|
||||
ValueTask.FromResult(new NotifyQueueEnqueueResult("null", false));
|
||||
|
||||
public ValueTask<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>> LeaseAsync(NotifyQueueLeaseRequest request, CancellationToken cancellationToken = default) =>
|
||||
ValueTask.FromResult<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>>(Array.Empty<INotifyQueueLease<NotifyQueueEventMessage>>());
|
||||
|
||||
public ValueTask<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>> ClaimExpiredAsync(NotifyQueueClaimOptions options, CancellationToken cancellationToken = default) =>
|
||||
ValueTask.FromResult<IReadOnlyList<INotifyQueueLease<NotifyQueueEventMessage>>>(Array.Empty<INotifyQueueLease<NotifyQueueEventMessage>>());
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
|
||||
namespace StellaOps.Notifier.WebService.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Seeds pack-approval templates and default routing for dev/test/bootstrap scenarios.
|
||||
/// </summary>
|
||||
public sealed class PackApprovalTemplateSeeder : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IHostEnvironment _environment;
|
||||
private readonly ILogger<PackApprovalTemplateSeeder> _logger;
|
||||
|
||||
public PackApprovalTemplateSeeder(IServiceProvider services, IHostEnvironment environment, ILogger<PackApprovalTemplateSeeder> logger)
|
||||
{
|
||||
_services = services ?? throw new ArgumentNullException(nameof(services));
|
||||
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var scope = _services.CreateScope();
|
||||
var templateRepo = scope.ServiceProvider.GetService<INotifyTemplateRepository>();
|
||||
var channelRepo = scope.ServiceProvider.GetService<INotifyChannelRepository>();
|
||||
var ruleRepo = scope.ServiceProvider.GetService<INotifyRuleRepository>();
|
||||
|
||||
if (templateRepo is null)
|
||||
{
|
||||
_logger.LogWarning("Template repository not registered; skipping pack-approval template seed.");
|
||||
return;
|
||||
}
|
||||
|
||||
var contentRoot = _environment.ContentRootPath;
|
||||
var seeded = await SeedTemplatesAsync(templateRepo, contentRoot, _logger, cancellationToken).ConfigureAwait(false);
|
||||
if (seeded > 0)
|
||||
{
|
||||
_logger.LogInformation("Seeded {TemplateCount} pack-approval templates from docs.", seeded);
|
||||
}
|
||||
|
||||
if (channelRepo is null || ruleRepo is null)
|
||||
{
|
||||
_logger.LogWarning("Channel or rule repository not registered; skipping pack-approval routing seed.");
|
||||
return;
|
||||
}
|
||||
|
||||
var routed = await SeedRoutingAsync(channelRepo, ruleRepo, _logger, cancellationToken).ConfigureAwait(false);
|
||||
if (routed > 0)
|
||||
{
|
||||
_logger.LogInformation("Seeded default pack-approval routing (channels + rule).");
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
|
||||
public static async Task<int> SeedTemplatesAsync(
|
||||
INotifyTemplateRepository repository,
|
||||
string contentRootPath,
|
||||
ILogger logger,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(repository);
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
var path = LocateTemplatesPath(contentRootPath);
|
||||
if (path is null)
|
||||
{
|
||||
logger.LogWarning("pack-approval-templates.json not found under content root {ContentRoot}; skipping seed.", contentRootPath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
using var stream = File.OpenRead(path);
|
||||
using var document = JsonDocument.Parse(stream);
|
||||
|
||||
if (!document.RootElement.TryGetProperty("templates", out var templatesElement))
|
||||
{
|
||||
logger.LogWarning("pack-approval-templates.json missing 'templates' array; skipping seed.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var count = 0;
|
||||
foreach (var template in templatesElement.EnumerateArray())
|
||||
{
|
||||
try
|
||||
{
|
||||
var model = ToTemplate(template);
|
||||
await repository.UpsertAsync(model, cancellationToken).ConfigureAwait(false);
|
||||
count++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to seed template entry; skipping.");
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public static async Task<int> SeedRoutingAsync(
|
||||
INotifyChannelRepository channelRepository,
|
||||
INotifyRuleRepository ruleRepository,
|
||||
ILogger logger,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(channelRepository);
|
||||
ArgumentNullException.ThrowIfNull(ruleRepository);
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
const string tenant = "tenant-sample";
|
||||
|
||||
var slackChannel = NotifyChannel.Create(
|
||||
channelId: "chn-pack-approvals-slack",
|
||||
tenantId: tenant,
|
||||
name: "Slack · Pack Approvals",
|
||||
type: NotifyChannelType.Slack,
|
||||
config: NotifyChannelConfig.Create(
|
||||
secretRef: "ref://notify/channels/slack/pack-approvals",
|
||||
endpoint: "https://hooks.slack.local/services/T000/B000/DEV",
|
||||
target: "#pack-approvals"),
|
||||
description: "Default Slack channel for pack approval notifications.");
|
||||
|
||||
var emailChannel = NotifyChannel.Create(
|
||||
channelId: "chn-pack-approvals-email",
|
||||
tenantId: tenant,
|
||||
name: "Email · Pack Approvals",
|
||||
type: NotifyChannelType.Email,
|
||||
config: NotifyChannelConfig.Create(
|
||||
secretRef: "ref://notify/channels/email/pack-approvals",
|
||||
target: "pack-approvals@example.com"),
|
||||
description: "Default email channel for pack approval notifications.");
|
||||
|
||||
await channelRepository.UpsertAsync(slackChannel, cancellationToken).ConfigureAwait(false);
|
||||
await channelRepository.UpsertAsync(emailChannel, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var rule = NotifyRule.Create(
|
||||
ruleId: "rule-pack-approvals-default",
|
||||
tenantId: tenant,
|
||||
name: "Pack approvals → Slack + Email",
|
||||
match: NotifyRuleMatch.Create(
|
||||
eventKinds: new[] { "pack.approval.granted", "pack.approval.denied", "pack.policy.override" },
|
||||
labels: new[] { "environment=prod" }),
|
||||
actions: new[]
|
||||
{
|
||||
NotifyRuleAction.Create(
|
||||
actionId: "act-pack-approvals-slack",
|
||||
channel: slackChannel.ChannelId,
|
||||
template: "tmpl-pack-approval-slack-en",
|
||||
locale: "en-US"),
|
||||
NotifyRuleAction.Create(
|
||||
actionId: "act-pack-approvals-email",
|
||||
channel: emailChannel.ChannelId,
|
||||
template: "tmpl-pack-approval-email-en",
|
||||
locale: "en-US")
|
||||
},
|
||||
description: "Routes pack approval events to seeded Slack and Email channels.");
|
||||
|
||||
await ruleRepository.UpsertAsync(rule, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return 3; // two channels + one rule
|
||||
}
|
||||
|
||||
private static string? LocateTemplatesPath(string contentRootPath)
|
||||
{
|
||||
var candidates = new[]
|
||||
{
|
||||
Path.Combine(contentRootPath, "StellaOps.Notifier.docs", "pack-approval-templates.json"),
|
||||
Path.Combine(contentRootPath, "..", "StellaOps.Notifier.docs", "pack-approval-templates.json")
|
||||
};
|
||||
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
return Path.GetFullPath(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static NotifyTemplate ToTemplate(JsonElement element)
|
||||
{
|
||||
var templateId = element.GetProperty("templateId").GetString() ?? throw new InvalidOperationException("templateId missing");
|
||||
var tenantId = element.GetProperty("tenantId").GetString() ?? throw new InvalidOperationException("tenantId missing");
|
||||
var key = element.GetProperty("key").GetString() ?? throw new InvalidOperationException("key missing");
|
||||
var locale = element.GetProperty("locale").GetString() ?? "en-US";
|
||||
var body = element.GetProperty("body").GetString() ?? string.Empty;
|
||||
|
||||
var channelType = ParseEnum<NotifyChannelType>(element.GetProperty("channelType").GetString(), NotifyChannelType.Custom);
|
||||
var renderMode = ParseEnum<NotifyTemplateRenderMode>(element.GetProperty("renderMode").GetString(), NotifyTemplateRenderMode.Markdown);
|
||||
var format = ParseEnum<NotifyDeliveryFormat>(element.GetProperty("format").GetString(), NotifyDeliveryFormat.Json);
|
||||
|
||||
var description = element.TryGetProperty("description", out var desc) ? desc.GetString() : null;
|
||||
|
||||
var metadata = element.TryGetProperty("metadata", out var meta)
|
||||
? meta.EnumerateObject().Select(p => new KeyValuePair<string, string>(p.Name, p.Value.GetString() ?? string.Empty))
|
||||
: Enumerable.Empty<KeyValuePair<string, string>>();
|
||||
|
||||
return NotifyTemplate.Create(
|
||||
templateId: templateId,
|
||||
tenantId: tenantId,
|
||||
channelType: channelType,
|
||||
key: key,
|
||||
locale: locale,
|
||||
body: body,
|
||||
renderMode: renderMode,
|
||||
format: format,
|
||||
description: description,
|
||||
metadata: metadata,
|
||||
createdBy: "seed:pack-approvals");
|
||||
}
|
||||
|
||||
private static TEnum ParseEnum<TEnum>(string? value, TEnum fallback) where TEnum : struct
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value) && Enum.TryParse<TEnum>(value, ignoreCase: true, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
@@ -10,5 +10,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../../Notify/__Libraries/StellaOps.Notify.Storage.Mongo/StellaOps.Notify.Storage.Mongo.csproj" />
|
||||
<ProjectReference Include="../../../Notify/__Libraries/StellaOps.Notify.Queue/StellaOps.Notify.Queue.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
||||
@@ -625,7 +625,7 @@ internal static class NodePackageCollector
|
||||
var lifecycleScripts = ExtractLifecycleScripts(root);
|
||||
var nodeVersions = NodeVersionDetector.Detect(context, relativeDirectory, cancellationToken);
|
||||
|
||||
return new NodePackage(
|
||||
var package = new NodePackage(
|
||||
name: name.Trim(),
|
||||
version: version.Trim(),
|
||||
relativePath: relativeDirectory,
|
||||
@@ -644,6 +644,10 @@ internal static class NodePackageCollector
|
||||
lockLocator: lockLocator,
|
||||
packageSha256: packageSha256,
|
||||
isYarnPnp: yarnPnpPresent);
|
||||
|
||||
AttachEntrypoints(package, root, relativeDirectory);
|
||||
|
||||
return package;
|
||||
}
|
||||
|
||||
private static string NormalizeRelativeDirectory(LanguageAnalyzerContext context, string directory)
|
||||
@@ -825,4 +829,169 @@ internal static class NodePackageCollector
|
||||
=> name.Equals("preinstall", StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Equals("install", StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Equals("postinstall", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
private static void AttachEntrypoints(LanguageAnalyzerContext context, NodePackage package, JsonElement root, string relativeDirectory)
|
||||
{
|
||||
static string NormalizePath(string relativeDirectory, string? path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var normalized = path.Replace('\\', '/').Trim();
|
||||
while (normalized.StartsWith("./", StringComparison.Ordinal))
|
||||
{
|
||||
normalized = normalized[2..];
|
||||
}
|
||||
|
||||
normalized = normalized.TrimStart('/');
|
||||
if (string.IsNullOrWhiteSpace(relativeDirectory))
|
||||
{
|
||||
return normalized;
|
||||
}
|
||||
|
||||
return $"{relativeDirectory.TrimEnd('/')}/{normalized}";
|
||||
}
|
||||
|
||||
void AddEntrypoint(string? path, string conditionSet, string? binName = null, string? mainField = null, string? moduleField = null)
|
||||
{
|
||||
var normalized = NormalizePath(relativeDirectory, path);
|
||||
if (string.IsNullOrWhiteSpace(normalized))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
package.AddEntrypoint(normalized, conditionSet, binName, mainField, moduleField);
|
||||
}
|
||||
|
||||
if (root.TryGetProperty("bin", out var binElement))
|
||||
{
|
||||
if (binElement.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
AddEntrypoint(binElement.GetString(), string.Empty, binName: null);
|
||||
}
|
||||
else if (binElement.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
foreach (var prop in binElement.EnumerateObject())
|
||||
{
|
||||
if (prop.Value.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
AddEntrypoint(prop.Value.GetString(), string.Empty, binName: prop.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (root.TryGetProperty("main", out var mainElement) && mainElement.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
var mainField = mainElement.GetString();
|
||||
AddEntrypoint(mainField, string.Empty, mainField: mainField);
|
||||
}
|
||||
|
||||
if (root.TryGetProperty("module", out var moduleElement) && moduleElement.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
var moduleField = moduleElement.GetString();
|
||||
AddEntrypoint(moduleField, string.Empty, moduleField: moduleField);
|
||||
}
|
||||
|
||||
if (root.TryGetProperty("exports", out var exportsElement))
|
||||
{
|
||||
foreach (var export in FlattenExports(exportsElement, prefix: string.Empty))
|
||||
{
|
||||
AddEntrypoint(export.Path, export.Conditions, binName: null, mainField: null, moduleField: null);
|
||||
}
|
||||
}
|
||||
|
||||
DetectShebangEntrypoints(context, package, relativeDirectory);
|
||||
}
|
||||
|
||||
private static IEnumerable<(string Path, string Conditions)> FlattenExports(JsonElement element, string prefix)
|
||||
{
|
||||
switch (element.ValueKind)
|
||||
{
|
||||
case JsonValueKind.String:
|
||||
var value = element.GetString();
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
yield return (value!, prefix);
|
||||
}
|
||||
yield break;
|
||||
|
||||
case JsonValueKind.Object:
|
||||
foreach (var property in element.EnumerateObject())
|
||||
{
|
||||
var nextPrefix = string.IsNullOrWhiteSpace(prefix) ? property.Name : $"{prefix},{property.Name}";
|
||||
foreach (var nested in FlattenExports(property.Value, nextPrefix))
|
||||
{
|
||||
yield return nested;
|
||||
}
|
||||
}
|
||||
yield break;
|
||||
|
||||
default:
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void DetectShebangEntrypoints(LanguageAnalyzerContext context, NodePackage package, string relativeDirectory)
|
||||
{
|
||||
var baseDirectory = string.IsNullOrWhiteSpace(relativeDirectory)
|
||||
? context.RootPath
|
||||
: Path.Combine(context.RootPath, relativeDirectory.Replace('/', Path.DirectorySeparatorChar));
|
||||
|
||||
if (!Directory.Exists(baseDirectory))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var candidates = Directory.EnumerateFiles(
|
||||
baseDirectory,
|
||||
"*.*",
|
||||
new EnumerationOptions
|
||||
{
|
||||
RecurseSubdirectories = false,
|
||||
MatchCasing = MatchCasing.CaseInsensitive,
|
||||
IgnoreInaccessible = true
|
||||
})
|
||||
.Where(path =>
|
||||
{
|
||||
var ext = Path.GetExtension(path);
|
||||
return string.Equals(ext, ".js", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(ext, ".mjs", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(ext, ".cjs", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase);
|
||||
})
|
||||
.OrderBy(static p => p, StringComparer.Ordinal);
|
||||
|
||||
foreach (var file in candidates)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var reader = File.OpenText(file);
|
||||
var firstLine = reader.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(firstLine))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!firstLine.TrimStart().StartsWith("#!", StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!firstLine.Contains("node", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var relativePath = context.GetRelativePath(file).Replace(Path.DirectorySeparatorChar, '/');
|
||||
package.AddEntrypoint(relativePath, conditionSet: "shebang:node", binName: null, mainField: null, moduleField: null);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// ignore unreadable files
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
[
|
||||
{
|
||||
"analyzerId": "node",
|
||||
"componentKey": "purl::pkg:npm/entry-demo@1.0.0",
|
||||
"purl": "pkg:npm/entry-demo@1.0.0",
|
||||
"name": "entry-demo",
|
||||
"version": "1.0.0",
|
||||
"type": "npm",
|
||||
"usedByEntrypoint": false,
|
||||
"metadata": {
|
||||
"entrypoint": "bin/ed.js;cli.js;dist/feature.browser.js;dist/feature.node.js;dist/main.js;dist/module.mjs",
|
||||
"entrypoint.conditions": "browser;import;node;require",
|
||||
"path": "."
|
||||
},
|
||||
"evidence": [
|
||||
{
|
||||
"kind": "file",
|
||||
"source": "package.json",
|
||||
"locator": "package.json"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "bin/ed.js;ed-alt"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "cli.js;entry-demo"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "dist/feature.browser.js;browser"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "dist/feature.node.js;node"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "dist/main.js;dist/main.js"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "dist/main.js;require"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "dist/module.mjs;dist/module.mjs"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "dist/module.mjs;import"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "entry-demo",
|
||||
"version": "1.0.0",
|
||||
"main": "dist/main.js",
|
||||
"module": "dist/module.mjs",
|
||||
"bin": {
|
||||
"entry-demo": "cli.js",
|
||||
"ed-alt": "bin/ed.js"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/module.mjs",
|
||||
"require": "./dist/main.js"
|
||||
},
|
||||
"./feature": {
|
||||
"browser": "./dist/feature.browser.js",
|
||||
"node": "./dist/feature.node.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
[
|
||||
{
|
||||
"analyzerId": "node",
|
||||
"componentKey": "purl::pkg:npm/shebang-demo@0.1.0",
|
||||
"purl": "pkg:npm/shebang-demo@0.1.0",
|
||||
"name": "shebang-demo",
|
||||
"version": "0.1.0",
|
||||
"type": "npm",
|
||||
"usedByEntrypoint": false,
|
||||
"metadata": {
|
||||
"entrypoint": "run.js",
|
||||
"entrypoint.conditions": "shebang:node",
|
||||
"path": "."
|
||||
},
|
||||
"evidence": [
|
||||
{
|
||||
"kind": "file",
|
||||
"source": "package.json",
|
||||
"locator": "package.json"
|
||||
},
|
||||
{
|
||||
"kind": "metadata",
|
||||
"source": "package.json:entrypoint",
|
||||
"locator": "package.json#entrypoint",
|
||||
"value": "run.js;shebang:node"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "shebang-demo",
|
||||
"version": "0.1.0"
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
console.log('ok');
|
||||
@@ -100,4 +100,42 @@ public sealed class NodeLanguageAnalyzerTests
|
||||
analyzers,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EntrypointsAreCapturedAsync()
|
||||
{
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
var fixturePath = TestPaths.ResolveFixture("lang", "node", "entrypoints");
|
||||
var goldenPath = Path.Combine(fixturePath, "expected.json");
|
||||
|
||||
var analyzers = new ILanguageAnalyzer[]
|
||||
{
|
||||
new NodeLanguageAnalyzer()
|
||||
};
|
||||
|
||||
await LanguageAnalyzerTestHarness.AssertDeterministicAsync(
|
||||
fixturePath,
|
||||
goldenPath,
|
||||
analyzers,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShebangEntrypointsAreCapturedAsync()
|
||||
{
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
var fixturePath = TestPaths.ResolveFixture("lang", "node", "shebang");
|
||||
var goldenPath = Path.Combine(fixturePath, "expected.json");
|
||||
|
||||
var analyzers = new ILanguageAnalyzer[]
|
||||
{
|
||||
new NodeLanguageAnalyzer()
|
||||
};
|
||||
|
||||
await LanguageAnalyzerTestHarness.AssertDeterministicAsync(
|
||||
fixturePath,
|
||||
goldenPath,
|
||||
analyzers,
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,3 +25,5 @@ Generate and maintain official StellaOps SDKs across supported languages using r
|
||||
- 3. Keep changes deterministic (stable ordering, timestamps, hashes) and align with offline/air-gap expectations.
|
||||
- 4. Coordinate doc updates, tests, and cross-guild communication whenever contracts or workflows change.
|
||||
- 5. Revert to `TODO` if you pause the task without shipping changes; leave notes in commit/PR descriptions for context.
|
||||
- 6. When running codegen with `--enable-post-process-file`, export `STELLA_POSTPROCESS_ROOT` (output directory) and `STELLA_POSTPROCESS_LANG` (`ts|python|go|java|csharp|ruby`) so shared hooks are copied deterministically.
|
||||
- 7. For the TypeScript track, prefer running `ts/generate-ts.sh` with `STELLA_SDK_OUT` pointing to a temp directory to avoid mutating the repo; use `ts/test_generate_ts.sh` for a quick fixture-based smoke.
|
||||
|
||||
@@ -3,4 +3,6 @@
|
||||
| Task ID | State | Notes |
|
||||
| --- | --- | --- |
|
||||
| SDKGEN-62-001 | DONE (2025-11-24) | Toolchain pinned: OpenAPI Generator CLI 7.4.0 + JDK 21, determinism rules in TOOLCHAIN.md/toolchain.lock.yaml. |
|
||||
| SDKGEN-62-002 | DOING (2025-11-24) | Shared post-process scaffold added (LF/whitespace normalizer, README); next: add language-specific hooks for auth/retry/pagination/telemetry. |
|
||||
| SDKGEN-62-002 | DONE (2025-11-24) | Shared post-process now copies auth/retry/pagination/telemetry helpers for TS/Python/Go/Java, wires TS/Python exports, and adds smoke tests. |
|
||||
| SDKGEN-63-001 | DOING (2025-11-24) | Added TS generator config/script, fixture spec, smoke test (green with vendored JDK/JAR); packaging templates and typed error/helper exports now copied via postprocess. Waiting on frozen OpenAPI to publish alpha. |
|
||||
| SDKGEN-63-002 | DOING (2025-11-24) | Python generator scaffold added (config, script, smoke test, reuse ping fixture); awaiting frozen OpenAPI to emit alpha. |
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
## Selected stack
|
||||
- **Generator:** OpenAPI Generator CLI `7.4.0` (fat JAR). Source is vendored under `tools/openapi-generator-cli-7.4.0.jar` with recorded SHA-256 (see lock file).
|
||||
- **Java runtime:** Temurin JDK `21.0.1` (LTS) — required to run the generator; also recorded with SHA-256.
|
||||
- **Java runtime:** Temurin JDK `21.0.1` (LTS) — cached as `tools/jdk-21.0.1.tar.gz` (extracted under `tools/jdk-21.0.1+12`) with recorded SHA-256.
|
||||
- **Templating:** Built-in Mustache templates with per-language overlays under `templates/<lang>/`; overlays are versioned and hashed in the lock file to guarantee determinism.
|
||||
- **Node helper (optional):** `node@20.11.1` used only for post-processing hooks when enabled; not required for the base pipeline.
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
- All artifacts (generator JAR, JDK archive, optional Node tarball, template bundles) must be content-addressed (SHA-256) and stored under `local-nugets/` or `tools/` in the repo; the hash is asserted before each run.
|
||||
- Generation must be invoked with deterministic flags:
|
||||
- `--global-property models,apis,supportingFiles` ordered by path;
|
||||
- `--skip-validate-spec` is **not** allowed; specs must pass validation first;
|
||||
- `--skip-validate-spec` is **not** allowed; specs must pass validation first (temporary allowance in ts/generate-ts.sh while using the tiny fixture spec).
|
||||
- `--type-mappings`/`--import-mappings` must be sorted lexicographically;
|
||||
- Disable timestamps via `-Dorg.openapitools.codegen.utils.DateTimeUtils.fixedClock=true`;
|
||||
- Set stable locale/timezone: `LC_ALL=C` and `TZ=UTC`.
|
||||
@@ -42,6 +42,10 @@ $JAVA_HOME/bin/java \
|
||||
- After run: compare generated tree against previous run using `git diff --stat -- src/Sdk/Generated`; any divergence must be explainable by spec or template change.
|
||||
- CI gate: regenerate in clean container with the same lock; fail if diff is non-empty.
|
||||
|
||||
### Language tracking
|
||||
- **TypeScript (SDKGEN-63-001)**: config at `ts/config.yaml`; script `ts/generate-ts.sh`; uses `typescript-fetch` with docs/tests suppressed and post-process copying shared helpers plus packaging templates (package.json, tsconfig base/cjs/esm, README, typed error). Packaging artifacts are supplied by the Release pipeline.
|
||||
- Upcoming: Python/Go/Java layouts will mirror this under `python/`, `go/`, `java/` once their tasks start.
|
||||
|
||||
## Next steps
|
||||
- Populate `specs/` with pinned OpenAPI inputs once APIG0101 provides the freeze.
|
||||
- Wire post-processing hooks (auth/retry/pagination/telemetry) after SDKGEN-62-002.
|
||||
- Populate `specs/` with pinned OpenAPI inputs once APIG0101 provides the freeze; update `STELLA_OAS_FILE` defaults accordingly.
|
||||
- Enforce post-processing flags and helper copying in CI; add language smoke tests similar to `postprocess/tests/test_postprocess.sh`.
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Post-process Scaffold (SDKGEN-62-002)
|
||||
|
||||
These hooks are invoked via OpenAPI Generator's `--enable-post-process-file` option. They are deliberately minimal and deterministic:
|
||||
These hooks are invoked via OpenAPI Generator's `--enable-post-process-file` option. They stay deterministic and offline-friendly:
|
||||
|
||||
- Normalise line endings to LF and strip trailing whitespace.
|
||||
- Preserve file mode 0644.
|
||||
- Inject a deterministic banner for supported languages (TS/JS/Go/Java/C#/Python/Ruby) when enabled (default on).
|
||||
- Language-specific rewrites (auth/retry/pagination/telemetry) will be added as SDKGEN-62-002 progresses.
|
||||
- Copy shared SDK helpers (auth, retries, pagination, telemetry) per language into the generated output when `STELLA_POSTPROCESS_ROOT` and `STELLA_POSTPROCESS_LANG` are provided. TypeScript/Python exports are auto-wired so helpers are available from the package barrel.
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -22,15 +22,30 @@ Or pass via CLI where supported:
|
||||
--global-property "postProcessFile=$PWD/postprocess/postprocess.sh"
|
||||
```
|
||||
|
||||
To copy shared helpers during post-processing, also set the generation root and language:
|
||||
|
||||
```bash
|
||||
export STELLA_POSTPROCESS_ROOT="/path/to/generated/sdk"
|
||||
export STELLA_POSTPROCESS_LANG="ts" # ts|python|go|java|csharp|ruby
|
||||
```
|
||||
|
||||
## Determinism
|
||||
- Uses only POSIX tools (`sed`, `perl`) available in build containers.
|
||||
- Does not reorder content; only whitespace/line-ending normalization.
|
||||
- Safe to run multiple times (idempotent).
|
||||
|
||||
## Configuration (optional)
|
||||
- `STELLA_POSTPROCESS_ADD_BANNER` (default `1`): when enabled, injects `Generated by StellaOps SDK generator — do not edit.` at the top of supported source files, idempotently.
|
||||
- Future flags (placeholders until implemented): `STELLA_POSTPROCESS_ENABLE_AUTH`, `STELLA_POSTPROCESS_ENABLE_RETRY`, `STELLA_POSTPROCESS_ENABLE_PAGINATION`, `STELLA_POSTPROCESS_ENABLE_TELEMETRY`.
|
||||
- `STELLA_POSTPROCESS_ADD_BANNER` (default `1`): injects `Generated by StellaOps SDK generator — do not edit.` at the top of supported source files, idempotently.
|
||||
- `STELLA_POSTPROCESS_ROOT`: root directory of the generated SDK; required to copy helper files.
|
||||
- `STELLA_POSTPROCESS_LANG`: one of `ts|python|go|java|csharp|ruby`; controls which helper set is copied.
|
||||
|
||||
## Helper contents (per language)
|
||||
- **TypeScript** (`templates/typescript/sdk-hooks.ts`, `sdk-error.ts`, package/tsconfig templates, README): fetch composers for auth, retries, telemetry headers, paginator, and a minimal typed error class. Packaging files provide ESM/CJS outputs with deterministic settings.
|
||||
- **Python** (`templates/python/sdk_hooks.py`): transport-agnostic wrappers for auth, retries, telemetry headers, and cursor pagination.
|
||||
- **Go** (`templates/go/hooks.go`): http.RoundTripper helpers for auth, telemetry, retries, and a generic paginator.
|
||||
- **Java** (`templates/java/Hooks.java`): OkHttp interceptors for auth, telemetry, retries, plus a helper to compose them.
|
||||
- C#/Ruby templates are reserved for follow-on language tracks; the banner logic already supports them.
|
||||
|
||||
## Next steps
|
||||
- Add language-specific post steps (auth helper injection, retry/pagination utilities, telemetry headers) behind flags per language template.
|
||||
- Wire into CI to enforce post-processed trees are clean.
|
||||
- Add C#/Ruby helpers once those language tracks start.
|
||||
- Wire postprocess tests into CI to enforce clean, deterministic outputs.
|
||||
|
||||
@@ -1,36 +1,118 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
script_dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
file="$1"
|
||||
|
||||
# Normalize line endings to LF and strip trailing whitespace deterministically
|
||||
perl -0777 -pe 's/\r\n/\n/g; s/[ \t]+$//mg' "$file" > "$file.tmp"
|
||||
perm=$(stat -c "%a" "$file" 2>/dev/null || echo 644)
|
||||
mv "$file.tmp" "$file"
|
||||
chmod "$perm" "$file"
|
||||
normalize_and_banner() {
|
||||
local f="$1"
|
||||
# Normalize line endings to LF and strip trailing whitespace deterministically
|
||||
perl -0777 -pe 's/\r\n/\n/g; s/[ \t]+$//mg' "$f" > "$f.tmp"
|
||||
local perm
|
||||
perm=$(stat -c "%a" "$f" 2>/dev/null || echo 644)
|
||||
mv "$f.tmp" "$f"
|
||||
chmod "$perm" "$f"
|
||||
|
||||
# Optional banner injection for traceability (idempotent)
|
||||
ADD_BANNER="${STELLA_POSTPROCESS_ADD_BANNER:-1}"
|
||||
if [ "$ADD_BANNER" = "1" ]; then
|
||||
ext="${file##*.}"
|
||||
case "$ext" in
|
||||
ts|js) prefix="//" ;;
|
||||
go) prefix="//" ;;
|
||||
java) prefix="//" ;;
|
||||
cs) prefix="//" ;;
|
||||
py) prefix="#" ;;
|
||||
rb) prefix="#" ;;
|
||||
*) prefix="" ;;
|
||||
esac
|
||||
# Optional banner injection for traceability (idempotent)
|
||||
local ADD_BANNER
|
||||
ADD_BANNER="${STELLA_POSTPROCESS_ADD_BANNER:-1}"
|
||||
if [ "$ADD_BANNER" = "1" ]; then
|
||||
local ext prefix
|
||||
ext="${f##*.}"
|
||||
case "$ext" in
|
||||
ts|js) prefix="//" ;;
|
||||
go) prefix="//" ;;
|
||||
java) prefix="//" ;;
|
||||
cs) prefix="//" ;;
|
||||
py) prefix="#" ;;
|
||||
rb) prefix="#" ;;
|
||||
*) prefix="" ;;
|
||||
esac
|
||||
|
||||
if [ -n "$prefix" ]; then
|
||||
banner="$prefix Generated by StellaOps SDK generator — do not edit."
|
||||
first_line="$(head -n 1 "$file" || true)"
|
||||
if [ "$first_line" != "$banner" ]; then
|
||||
printf "%s\n" "$banner" > "$file.tmp"
|
||||
cat "$file" >> "$file.tmp"
|
||||
mv "$file.tmp" "$file"
|
||||
chmod "$perm" "$file"
|
||||
if [ -n "$prefix" ]; then
|
||||
local banner first_line
|
||||
banner="$prefix Generated by StellaOps SDK generator — do not edit."
|
||||
first_line="$(head -n 1 "$f" || true)"
|
||||
if [ "$first_line" != "$banner" ]; then
|
||||
printf "%s\n" "$banner" > "$f.tmp"
|
||||
cat "$f" >> "$f.tmp"
|
||||
mv "$f.tmp" "$f"
|
||||
chmod "$perm" "$f"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
wire_typescript_exports() {
|
||||
local root="$1"
|
||||
local barrel="$root/src/index.ts"
|
||||
local export_hooks='export * from "./sdk-hooks";'
|
||||
local export_errors='export * from "./sdk-error";'
|
||||
if [ -f "$barrel" ]; then
|
||||
if ! grep -qF "$export_hooks" "$barrel"; then
|
||||
printf "\n%s\n" "$export_hooks" >> "$barrel"
|
||||
fi
|
||||
if ! grep -qF "$export_errors" "$barrel"; then
|
||||
printf "%s\n" "$export_errors" >> "$barrel"
|
||||
fi
|
||||
normalize_and_banner "$barrel"
|
||||
fi
|
||||
}
|
||||
|
||||
wire_python_exports() {
|
||||
local root="$1"
|
||||
local init_py="$root/__init__.py"
|
||||
local import_stmt='from . import sdk_hooks as hooks'
|
||||
if [ -f "$init_py" ] && ! grep -qF "$import_stmt" "$init_py"; then
|
||||
printf "\n%s\n" "$import_stmt" >> "$init_py"
|
||||
normalize_and_banner "$init_py"
|
||||
fi
|
||||
}
|
||||
|
||||
copy_templates_if_needed() {
|
||||
local root="$1"
|
||||
local lang="$2"
|
||||
[ -z "$root" ] && return
|
||||
[ -z "$lang" ] && return
|
||||
|
||||
local stamp="$root/.stellaops-postprocess-${lang}.stamp"
|
||||
if [ -f "$stamp" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
local template_dir=""
|
||||
case "$lang" in
|
||||
ts|typescript|node) template_dir="$script_dir/templates/typescript" ;;
|
||||
py|python) template_dir="$script_dir/templates/python" ;;
|
||||
go|golang) template_dir="$script_dir/templates/go" ;;
|
||||
java) template_dir="$script_dir/templates/java" ;;
|
||||
cs|csharp|dotnet) template_dir="$script_dir/templates/csharp" ;;
|
||||
rb|ruby) template_dir="$script_dir/templates/ruby" ;;
|
||||
*) template_dir="" ;;
|
||||
esac
|
||||
|
||||
if [ -z "$template_dir" ] || [ ! -d "$template_dir" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
(cd "$template_dir" && find . -type f -print0) | while IFS= read -r -d '' rel; do
|
||||
local dest="$root/${rel#./}"
|
||||
mkdir -p "$(dirname "$dest")"
|
||||
cp "$template_dir/$rel" "$dest"
|
||||
chmod 644 "$dest"
|
||||
normalize_and_banner "$dest"
|
||||
done
|
||||
|
||||
case "$lang" in
|
||||
ts|typescript|node) wire_typescript_exports "$root" ;;
|
||||
py|python) wire_python_exports "$root" ;;
|
||||
*) ;; # other languages handled via helper files only
|
||||
esac
|
||||
|
||||
date -u +"%Y-%m-%dT%H:%M:%SZ" > "$stamp"
|
||||
}
|
||||
|
||||
copy_templates_if_needed "${STELLA_POSTPROCESS_ROOT:-}" "${STELLA_POSTPROCESS_LANG:-}"
|
||||
|
||||
normalize_and_banner "$file"
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
// Generated by StellaOps SDK generator — do not edit.
|
||||
|
||||
package hooks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AuthRoundTripper injects an Authorization header when a token is available.
|
||||
type AuthRoundTripper struct {
|
||||
TokenProvider func() (string, error)
|
||||
HeaderName string
|
||||
Scheme string
|
||||
Next http.RoundTripper
|
||||
}
|
||||
|
||||
func (rt AuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
token := ""
|
||||
if rt.TokenProvider != nil {
|
||||
if t, err := rt.TokenProvider(); err == nil {
|
||||
token = t
|
||||
}
|
||||
}
|
||||
if token != "" {
|
||||
header := token
|
||||
if rt.Scheme != "" {
|
||||
header = rt.Scheme + " " + token
|
||||
}
|
||||
req.Header.Set(firstNonEmpty(rt.HeaderName, "Authorization"), header)
|
||||
}
|
||||
return rt.next().RoundTrip(req)
|
||||
}
|
||||
|
||||
// RetryRoundTripper retries transient responses using exponential backoff.
|
||||
type RetryRoundTripper struct {
|
||||
Retries int
|
||||
Backoff time.Duration
|
||||
StatusCodes map[int]struct{}
|
||||
Next http.RoundTripper
|
||||
}
|
||||
|
||||
func (rt RetryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
retries := rt.Retries
|
||||
if retries <= 0 {
|
||||
retries = 2
|
||||
}
|
||||
backoff := rt.Backoff
|
||||
if backoff <= 0 {
|
||||
backoff = 200 * time.Millisecond
|
||||
}
|
||||
statusCodes := rt.StatusCodes
|
||||
if len(statusCodes) == 0 {
|
||||
statusCodes = map[int]struct{}{429: {}, 500: {}, 502: {}, 503: {}, 504: {}}
|
||||
}
|
||||
|
||||
var resp *http.Response
|
||||
var err error
|
||||
for attempt := 0; attempt <= retries; attempt++ {
|
||||
resp, err = rt.next().RoundTrip(req)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if _, retry := statusCodes[resp.StatusCode]; !retry || attempt == retries {
|
||||
return resp, err
|
||||
}
|
||||
time.Sleep(backoff * (1 << attempt))
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// TelemetryRoundTripper injects client + trace headers.
|
||||
type TelemetryRoundTripper struct {
|
||||
Source string
|
||||
TraceParent string
|
||||
HeaderName string
|
||||
Next http.RoundTripper
|
||||
}
|
||||
|
||||
func (rt TelemetryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
headerName := firstNonEmpty(rt.HeaderName, "X-Stella-Client")
|
||||
if rt.Source != "" {
|
||||
req.Header.Set(headerName, rt.Source)
|
||||
}
|
||||
if rt.TraceParent != "" {
|
||||
req.Header.Set("traceparent", rt.TraceParent)
|
||||
}
|
||||
return rt.next().RoundTrip(req)
|
||||
}
|
||||
|
||||
// WithClientHooks wires auth, telemetry, and retry policies into a given HTTP client.
|
||||
func WithClientHooks(base *http.Client, opts ...func(http.RoundTripper) http.RoundTripper) *http.Client {
|
||||
client := *base
|
||||
rt := client.Transport
|
||||
if rt == nil {
|
||||
rt = http.DefaultTransport
|
||||
}
|
||||
for i := len(opts) - 1; i >= 0; i-- {
|
||||
rt = opts[i](rt)
|
||||
}
|
||||
client.Transport = rt
|
||||
return &client
|
||||
}
|
||||
|
||||
// Paginate repeatedly invokes fetch with the supplied cursor until empty.
|
||||
func Paginate[T any](ctx context.Context, start string, fetch func(context.Context, string) (T, string, error)) ([]T, error) {
|
||||
cursor := start
|
||||
out := make([]T, 0)
|
||||
for {
|
||||
page, next, err := fetch(ctx, cursor)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out = append(out, page)
|
||||
if next == "" {
|
||||
return out, nil
|
||||
}
|
||||
cursor = next
|
||||
}
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, v := range values {
|
||||
if v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (rt AuthRoundTripper) next() http.RoundTripper {
|
||||
if rt.Next != nil {
|
||||
return rt.Next
|
||||
}
|
||||
return http.DefaultTransport
|
||||
}
|
||||
|
||||
func (rt RetryRoundTripper) next() http.RoundTripper {
|
||||
if rt.Next != nil {
|
||||
return rt.Next
|
||||
}
|
||||
return http.DefaultTransport
|
||||
}
|
||||
|
||||
func (rt TelemetryRoundTripper) next() http.RoundTripper {
|
||||
if rt.Next != nil {
|
||||
return rt.Next
|
||||
}
|
||||
return http.DefaultTransport
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Generated by StellaOps SDK generator — do not edit.
|
||||
package com.stellaops.sdk.hooks;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public final class Hooks {
|
||||
private Hooks() {}
|
||||
|
||||
public static OkHttpClient withAll(OkHttpClient base, AuthProvider auth, RetryOptions retry,
|
||||
TelemetryOptions telemetry) {
|
||||
OkHttpClient.Builder builder = base.newBuilder();
|
||||
if (auth != null) {
|
||||
builder.addInterceptor(new StellaAuthInterceptor(auth));
|
||||
}
|
||||
if (telemetry != null) {
|
||||
builder.addInterceptor(new StellaTelemetryInterceptor(telemetry));
|
||||
}
|
||||
if (retry != null) {
|
||||
builder.addInterceptor(new StellaRetryInterceptor(retry));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public interface AuthProvider {
|
||||
String token();
|
||||
|
||||
default String headerName() {
|
||||
return "Authorization";
|
||||
}
|
||||
|
||||
default String scheme() {
|
||||
return "Bearer";
|
||||
}
|
||||
}
|
||||
|
||||
public static final class RetryOptions {
|
||||
public int retries = 2;
|
||||
public long backoffMillis = 200L;
|
||||
public Set<Integer> statusCodes = new HashSet<>();
|
||||
public Logger logger = Logger.getLogger("StellaRetry");
|
||||
|
||||
public RetryOptions() {
|
||||
statusCodes.add(429);
|
||||
statusCodes.add(500);
|
||||
statusCodes.add(502);
|
||||
statusCodes.add(503);
|
||||
statusCodes.add(504);
|
||||
}
|
||||
}
|
||||
|
||||
public static final class TelemetryOptions {
|
||||
public String source = "";
|
||||
public String traceParent = "";
|
||||
public String headerName = "X-Stella-Client";
|
||||
}
|
||||
|
||||
static final class StellaAuthInterceptor implements Interceptor {
|
||||
private final AuthProvider provider;
|
||||
|
||||
StellaAuthInterceptor(AuthProvider provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
String token = provider.token();
|
||||
if (token != null && !token.isEmpty()) {
|
||||
String scheme = provider.scheme();
|
||||
String value = (scheme == null || scheme.isEmpty()) ? token : scheme + " " + token;
|
||||
request = request.newBuilder()
|
||||
.header(provider.headerName(), value)
|
||||
.build();
|
||||
}
|
||||
return chain.proceed(request);
|
||||
}
|
||||
}
|
||||
|
||||
static final class StellaTelemetryInterceptor implements Interceptor {
|
||||
private final TelemetryOptions options;
|
||||
|
||||
StellaTelemetryInterceptor(TelemetryOptions options) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
Request.Builder builder = request.newBuilder();
|
||||
if (options.source != null && !options.source.isEmpty()) {
|
||||
builder.header(options.headerName, options.source);
|
||||
}
|
||||
if (options.traceParent != null && !options.traceParent.isEmpty()) {
|
||||
builder.header("traceparent", options.traceParent);
|
||||
}
|
||||
return chain.proceed(builder.build());
|
||||
}
|
||||
}
|
||||
|
||||
static final class StellaRetryInterceptor implements Interceptor {
|
||||
private final RetryOptions options;
|
||||
|
||||
StellaRetryInterceptor(RetryOptions options) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
int attempts = 0;
|
||||
IOException lastError = null;
|
||||
while (attempts <= options.retries) {
|
||||
try {
|
||||
Response response = chain.proceed(chain.request());
|
||||
if (!options.statusCodes.contains(response.code()) || attempts == options.retries) {
|
||||
return response;
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
lastError = ex;
|
||||
if (attempts == options.retries) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(options.backoffMillis * (1L << attempts));
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("retry interrupted", ie);
|
||||
}
|
||||
attempts += 1;
|
||||
}
|
||||
if (lastError != null) {
|
||||
throw lastError;
|
||||
}
|
||||
return chain.proceed(chain.request());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
# Generated by StellaOps SDK generator — do not edit.
|
||||
|
||||
"""Lightweight HTTP helpers shared across generated SDKs.
|
||||
|
||||
These wrappers are transport-agnostic: they expect a `send` callable with
|
||||
signature `send(method, url, headers=None, **kwargs)` returning a response-like
|
||||
object that exposes either `.status` or `.status_code` and optional
|
||||
`.json()`/`.text` accessors.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Any, Callable, Dict, Iterable, Optional, Tuple
|
||||
|
||||
|
||||
SendFunc = Callable[..., Any]
|
||||
|
||||
|
||||
def _merge_headers(headers: Optional[Dict[str, str]], extra: Dict[str, str]) -> Dict[str, str]:
|
||||
merged = {**(headers or {})}
|
||||
merged.update({k: v for k, v in extra.items() if v is not None})
|
||||
return merged
|
||||
|
||||
|
||||
def with_auth(send: SendFunc, token_provider: Callable[[], Optional[str]] | str | None, *,
|
||||
header_name: str = "Authorization", scheme: str = "Bearer") -> SendFunc:
|
||||
"""Injects bearer (or custom) auth header before dispatch."""
|
||||
|
||||
def wrapper(method: str, url: str, headers: Optional[Dict[str, str]] = None, **kwargs: Any) -> Any:
|
||||
token = token_provider() if callable(token_provider) else token_provider
|
||||
auth_header = None
|
||||
if token:
|
||||
auth_header = f"{scheme} {token}" if scheme else token
|
||||
merged = _merge_headers(headers, {header_name: auth_header} if auth_header else {})
|
||||
return send(method, url, headers=merged, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def with_retry(send: SendFunc, *, retries: int = 2, backoff_seconds: float = 0.2,
|
||||
status_codes: Iterable[int] = (429, 500, 502, 503, 504)) -> SendFunc:
|
||||
"""Retries on transient HTTP status codes with exponential backoff."""
|
||||
|
||||
retryable = set(status_codes)
|
||||
|
||||
def wrapper(method: str, url: str, **kwargs: Any) -> Any:
|
||||
attempt = 0
|
||||
while True:
|
||||
response = send(method, url, **kwargs)
|
||||
code = getattr(response, "status", getattr(response, "status_code", None))
|
||||
if code not in retryable or attempt >= retries:
|
||||
return response
|
||||
time.sleep(backoff_seconds * (2 ** attempt))
|
||||
attempt += 1
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def with_telemetry(send: SendFunc, *, source: Optional[str] = None,
|
||||
traceparent: Optional[str] = None,
|
||||
header_name: str = "X-Stella-Client") -> SendFunc:
|
||||
"""Adds lightweight client + trace headers."""
|
||||
|
||||
def wrapper(method: str, url: str, headers: Optional[Dict[str, str]] = None, **kwargs: Any) -> Any:
|
||||
extra = {}
|
||||
if source:
|
||||
extra[header_name] = source
|
||||
if traceparent:
|
||||
extra["traceparent"] = traceparent
|
||||
merged = _merge_headers(headers, extra)
|
||||
return send(method, url, headers=merged, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def paginate(fetch_page: Callable[[Optional[str]], Tuple[Any, Optional[str]]], *, start: Optional[str] = None):
|
||||
"""Generator yielding pages until fetch_page returns a falsy cursor.
|
||||
|
||||
The fetch_page callable should accept the current cursor (or None for the
|
||||
first page) and return `(page, next_cursor)`.
|
||||
"""
|
||||
|
||||
cursor = start
|
||||
while True:
|
||||
page, cursor = fetch_page(cursor)
|
||||
yield page
|
||||
if not cursor:
|
||||
break
|
||||
@@ -0,0 +1,27 @@
|
||||
# StellaOps SDK (TypeScript)
|
||||
|
||||
Generated client for StellaOps APIs. This package is produced deterministically via the SDK generator.
|
||||
|
||||
## Build
|
||||
|
||||
```
|
||||
npm install
|
||||
npm run build
|
||||
```
|
||||
|
||||
## Usage (sketch)
|
||||
|
||||
```ts
|
||||
import { DefaultApi, ApiConfig, composeFetch, withAuth, withTelemetry } from "@stellaops/sdk";
|
||||
|
||||
const fetchWithHooks = composeFetch(
|
||||
f => withAuth(f, { token: process.env.STELLA_TOKEN }),
|
||||
f => withTelemetry(f, { source: "example-script" })
|
||||
);
|
||||
|
||||
const api = new DefaultApi(new ApiConfig({ basePath: "https://gateway.local/api", fetchApi: fetchWithHooks }));
|
||||
const resp = await api.ping();
|
||||
console.log(resp.message);
|
||||
```
|
||||
|
||||
See generator repo for determinism rules and release process.
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "@stellaops/sdk",
|
||||
"version": "0.0.0-alpha",
|
||||
"description": "Official StellaOps SDK (TypeScript) — generated, deterministic, offline-ready",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"import": "./dist/esm/index.js",
|
||||
"require": "./dist/cjs/index.cjs"
|
||||
},
|
||||
"types": "./dist/esm/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.stella-ops.org/stellaops/sdk-typescript.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json && tsc -p tsconfig.esm.json",
|
||||
"clean": "rm -rf dist",
|
||||
"lint": "echo 'lint placeholder (offline)'"
|
||||
},
|
||||
"files": [
|
||||
"dist/esm",
|
||||
"dist/cjs",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Generated by StellaOps SDK generator — do not edit.
|
||||
|
||||
export class StellaSdkError extends Error {
|
||||
public readonly status?: number;
|
||||
public readonly requestId?: string;
|
||||
public readonly details?: unknown;
|
||||
|
||||
constructor(message: string, opts: { status?: number; requestId?: string; details?: unknown } = {}) {
|
||||
super(message);
|
||||
this.name = "StellaSdkError";
|
||||
this.status = opts.status;
|
||||
this.requestId = opts.requestId;
|
||||
this.details = opts.details;
|
||||
}
|
||||
}
|
||||
|
||||
export function toSdkError(e: unknown): StellaSdkError {
|
||||
if (e instanceof StellaSdkError) return e;
|
||||
if (e instanceof Error) return new StellaSdkError(e.message);
|
||||
return new StellaSdkError(String(e));
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// Generated by StellaOps SDK generator — do not edit.
|
||||
|
||||
export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
||||
export type Logger = (message: string, meta?: Record<string, unknown>) => void;
|
||||
|
||||
export interface AuthProvider {
|
||||
token?: string | null;
|
||||
getToken?: () => Promise<string | null> | string | null;
|
||||
headerName?: string;
|
||||
scheme?: string;
|
||||
}
|
||||
|
||||
export const withAuth = (fetchFn: FetchLike, provider: AuthProvider): FetchLike => async (input, init = {}) => {
|
||||
const headerName = provider.headerName ?? "Authorization";
|
||||
const scheme = provider.scheme ?? "Bearer";
|
||||
const token = provider.token ?? (typeof provider.getToken === "function" ? await provider.getToken() : null);
|
||||
|
||||
const headers = new Headers(init.headers ?? {});
|
||||
if (token) {
|
||||
headers.set(headerName, scheme ? `${scheme} ${token}` : `${token}`);
|
||||
}
|
||||
|
||||
return fetchFn(input, { ...init, headers });
|
||||
};
|
||||
|
||||
export interface RetryOptions {
|
||||
retries?: number;
|
||||
backoffMs?: number;
|
||||
statusCodes?: number[];
|
||||
logger?: Logger;
|
||||
}
|
||||
|
||||
export const withRetry = (fetchFn: FetchLike, opts: RetryOptions = {}): FetchLike => {
|
||||
const retries = opts.retries ?? 2;
|
||||
const backoffMs = opts.backoffMs ?? 200;
|
||||
const statusCodes = opts.statusCodes ?? [429, 500, 502, 503, 504];
|
||||
return async (input, init = {}) => {
|
||||
for (let attempt = 0; attempt <= retries; attempt += 1) {
|
||||
const response = await fetchFn(input, init);
|
||||
if (!statusCodes.includes(response.status) || attempt === retries) {
|
||||
return response;
|
||||
}
|
||||
opts.logger?.("retrying", { attempt, status: response.status });
|
||||
await new Promise((resolve) => setTimeout(resolve, backoffMs * Math.pow(2, attempt)));
|
||||
}
|
||||
return fetchFn(input, init);
|
||||
};
|
||||
};
|
||||
|
||||
export interface TelemetryOptions {
|
||||
source?: string;
|
||||
traceParent?: string;
|
||||
headerName?: string;
|
||||
}
|
||||
|
||||
export const withTelemetry = (fetchFn: FetchLike, opts: TelemetryOptions = {}): FetchLike => async (input, init = {}) => {
|
||||
const headers = new Headers(init.headers ?? {});
|
||||
if (opts.source) {
|
||||
headers.set(opts.headerName ?? "X-Stella-Client", opts.source);
|
||||
}
|
||||
if (opts.traceParent) {
|
||||
headers.set("traceparent", opts.traceParent);
|
||||
}
|
||||
return fetchFn(input, { ...init, headers });
|
||||
};
|
||||
|
||||
export interface PaginatorConfig<TRequest, TResponse> {
|
||||
initialRequest: TRequest;
|
||||
fetchPage: (req: TRequest) => Promise<TResponse>;
|
||||
extractCursor: (resp: TResponse) => string | undefined | null;
|
||||
setCursor: (req: TRequest, cursor: string) => TRequest;
|
||||
}
|
||||
|
||||
export async function* paginate<TRequest, TResponse>(config: PaginatorConfig<TRequest, TResponse>) {
|
||||
let request = config.initialRequest;
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
const response = await config.fetchPage(request);
|
||||
yield response;
|
||||
const cursor = config.extractCursor(response);
|
||||
if (!cursor) {
|
||||
break;
|
||||
}
|
||||
request = config.setCursor(request, cursor);
|
||||
}
|
||||
}
|
||||
|
||||
export const composeFetch = (...layers: Array<(f: FetchLike) => FetchLike>) => {
|
||||
return layers.reduceRight((acc, layer) => layer(acc), (input, init) => fetch(input, init));
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"lib": ["ES2022", "DOM"],
|
||||
"moduleResolution": "Node",
|
||||
"resolveJsonModule": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"rootDir": "src",
|
||||
"noEmitOnError": true,
|
||||
"types": []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist/esm",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "Bundler",
|
||||
"target": "ES2022",
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": false,
|
||||
"stripInternal": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["dist", "node_modules"]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist/cjs",
|
||||
"module": "CommonJS",
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": false,
|
||||
"stripInternal": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["dist", "node_modules"]
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir=$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
script="$root_dir/postprocess.sh"
|
||||
|
||||
tmp=$(mktemp -d)
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
|
||||
# Seed a file with CRLF and trailing spaces
|
||||
cat > "$tmp/example.ts" <<'EOF'
|
||||
const value = 1;
|
||||
EOF
|
||||
|
||||
STELLA_POSTPROCESS_ROOT="$tmp" STELLA_POSTPROCESS_LANG="ts" "$script" "$tmp/example.ts"
|
||||
# Copy python helpers too to ensure multi-language runs do not interfere
|
||||
touch "$tmp/example.py"
|
||||
STELLA_POSTPROCESS_ROOT="$tmp" STELLA_POSTPROCESS_LANG="python" "$script" "$tmp/example.py"
|
||||
|
||||
first_line=$(head -n 1 "$tmp/example.ts")
|
||||
if [[ "$first_line" != "// Generated by StellaOps SDK generator — do not edit." ]]; then
|
||||
echo "banner injection failed" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if grep -q $' \r' "$tmp/example.ts"; then
|
||||
echo "line ending normalization failed" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$tmp/sdk-hooks.ts" ]]; then
|
||||
echo "TypeScript helper not copied" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$tmp/sdk_hooks.py" ]]; then
|
||||
echo "Python helper not copied" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Basic Python helper import smoke test
|
||||
PYTHONPATH="$tmp" python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
from importlib import import_module
|
||||
|
||||
sdk_hooks = import_module('sdk_hooks')
|
||||
assert hasattr(sdk_hooks, 'with_retry')
|
||||
assert hasattr(sdk_hooks, 'with_auth')
|
||||
print('python helpers ok')
|
||||
PY
|
||||
|
||||
echo "postprocess smoke tests passed"
|
||||
19
src/Sdk/StellaOps.Sdk.Generator/python/README.md
Normal file
19
src/Sdk/StellaOps.Sdk.Generator/python/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Python SDK (SDKGEN-63-002)
|
||||
|
||||
Deterministic generator settings for the Python SDK (asyncio library).
|
||||
|
||||
## Prereqs
|
||||
- `STELLA_OAS_FILE` pointing to the frozen OpenAPI spec.
|
||||
- OpenAPI Generator CLI 7.4.0 jar at `tools/openapi-generator-cli-7.4.0.jar` (override with `STELLA_OPENAPI_GENERATOR_JAR`).
|
||||
- JDK 21 available on PATH (vendored at `../tools/jdk-21.0.1+12`; set `JAVA_HOME` if needed).
|
||||
|
||||
## Generate
|
||||
```bash
|
||||
cd src/Sdk/StellaOps.Sdk.Generator
|
||||
STELLA_OAS_FILE=ts/fixtures/ping.yaml \
|
||||
STELLA_SDK_OUT=$(mktemp -d) \
|
||||
python/generate-python.sh
|
||||
```
|
||||
|
||||
Outputs land in `out/python/` and are post-processed to normalize whitespace, inject the banner, and copy shared helpers (`sdk_hooks.py`).
|
||||
Override `STELLA_SDK_OUT` to keep the repo clean during local runs.
|
||||
19
src/Sdk/StellaOps.Sdk.Generator/python/config.yaml
Normal file
19
src/Sdk/StellaOps.Sdk.Generator/python/config.yaml
Normal file
@@ -0,0 +1,19 @@
|
||||
# OpenAPI Generator config for the StellaOps Python SDK (alpha)
|
||||
generatorName: python
|
||||
outputDir: out/python
|
||||
additionalProperties:
|
||||
packageName: stellaops_sdk
|
||||
projectName: stellaops-sdk
|
||||
packageVersion: "0.0.0a0"
|
||||
hideGenerationTimestamp: true
|
||||
generateSourceCodeOnly: true
|
||||
useOneOfDiscriminatorLookup: true
|
||||
enumClassPrefix: true
|
||||
httpUserAgent: "stellaops-sdk/0.0.0a0"
|
||||
library: asyncio
|
||||
|
||||
globalProperty:
|
||||
apiDocs: false
|
||||
modelDocs: false
|
||||
apiTests: false
|
||||
modelTests: false
|
||||
37
src/Sdk/StellaOps.Sdk.Generator/python/generate-python.sh
Normal file
37
src/Sdk/StellaOps.Sdk.Generator/python/generate-python.sh
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
config="$root_dir/python/config.yaml"
|
||||
spec="${STELLA_OAS_FILE:-}"
|
||||
|
||||
if [ -z "$spec" ]; then
|
||||
echo "STELLA_OAS_FILE is required (path to OpenAPI spec)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
output_dir="${STELLA_SDK_OUT:-$root_dir/out/python}"
|
||||
mkdir -p "$output_dir"
|
||||
|
||||
export STELLA_POSTPROCESS_ROOT="$output_dir"
|
||||
export STELLA_POSTPROCESS_LANG="python"
|
||||
|
||||
jar="${STELLA_OPENAPI_GENERATOR_JAR:-$root_dir/tools/openapi-generator-cli-7.4.0.jar}"
|
||||
if [ ! -f "$jar" ]; then
|
||||
echo "OpenAPI Generator CLI jar not found at $jar" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
JAVA_OPTS="${JAVA_OPTS:-} -Dorg.openapitools.codegen.utils.postProcessFile=$root_dir/postprocess/postprocess.sh"
|
||||
export JAVA_OPTS
|
||||
|
||||
java -jar "$jar" generate \
|
||||
-i "$spec" \
|
||||
-g python \
|
||||
-c "$config" \
|
||||
--skip-validate-spec \
|
||||
--enable-post-process-file \
|
||||
--global-property models,apis,supportingFiles \
|
||||
-o "$output_dir"
|
||||
|
||||
echo "Python SDK generated at $output_dir"
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
script="$root_dir/python/generate-python.sh"
|
||||
spec="$root_dir/ts/fixtures/ping.yaml"
|
||||
jar_default="$root_dir/tools/openapi-generator-cli-7.4.0.jar"
|
||||
jar="${STELLA_OPENAPI_GENERATOR_JAR:-$jar_default}"
|
||||
|
||||
if [ ! -f "$jar" ]; then
|
||||
echo "SKIP: generator jar not found at $jar" >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if ! command -v java >/dev/null 2>&1; then
|
||||
echo "SKIP: java not on PATH; set JAVA_HOME to run this smoke." >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
out_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$out_dir"' EXIT
|
||||
|
||||
STELLA_OAS_FILE="$spec" \
|
||||
STELLA_SDK_OUT="$out_dir" \
|
||||
STELLA_OPENAPI_GENERATOR_JAR="$jar" \
|
||||
"$script"
|
||||
|
||||
test -f "$out_dir/stellaops_sdk/__init__.py" || { echo "missing generated package" >&2; exit 1; }
|
||||
test -f "$out_dir/sdk_hooks.py" || { echo "missing helper copy" >&2; exit 1; }
|
||||
|
||||
echo "Python generator smoke test passed"
|
||||
@@ -5,11 +5,11 @@ artifacts:
|
||||
- name: openapi-generator-cli
|
||||
version: 7.4.0
|
||||
path: tools/openapi-generator-cli-7.4.0.jar
|
||||
sha256: "REPLACE_WITH_SHA256_ON_VENDORED_JAR"
|
||||
sha256: "e42769a98fef5634bee0f921e4b90786a6b3292aa11fe8d2f84c045ac435ab29"
|
||||
- name: temurin-jdk
|
||||
version: 21.0.1
|
||||
path: tools/jdk-21.0.1.tar.gz
|
||||
sha256: "REPLACE_WITH_SHA256_ON_VENDORED_JDK"
|
||||
sha256: "1a6fa8abda4c5caed915cfbeeb176e7fbd12eb6b222f26e290ee45808b529aa1"
|
||||
- name: node
|
||||
version: 20.11.1
|
||||
optional: true
|
||||
@@ -19,16 +19,16 @@ artifacts:
|
||||
templates:
|
||||
- language: typescript
|
||||
path: templates/typescript
|
||||
sha256: "REPLACE_WITH_SHA256_OF_TEMPLATE_ARCHIVE"
|
||||
sha256: "5c6d50be630bee8f281714afefba224ac37f84b420d39ee5dabbe1d29506c9f8"
|
||||
- language: python
|
||||
path: templates/python
|
||||
sha256: "REPLACE_WITH_SHA256_OF_TEMPLATE_ARCHIVE"
|
||||
sha256: "68efdefb91f3c378f7d6c950e67fb25cf287a3dca13192df6256598933a868e8"
|
||||
- language: go
|
||||
path: templates/go
|
||||
sha256: "REPLACE_WITH_SHA256_OF_TEMPLATE_ARCHIVE"
|
||||
sha256: "9701ade3b25d2dfa5b2322b56a1860e74f3274afbccc70b27720c7b124fd7e73"
|
||||
- language: java
|
||||
path: templates/java
|
||||
sha256: "REPLACE_WITH_SHA256_OF_TEMPLATE_ARCHIVE"
|
||||
sha256: "9d3c00f5ef67b15da7be5658fda96431e8b2ec893f26c1ec60efaa6bd05ddce7"
|
||||
|
||||
repro:
|
||||
timezone: "UTC"
|
||||
|
||||
36
src/Sdk/StellaOps.Sdk.Generator/ts/README.md
Normal file
36
src/Sdk/StellaOps.Sdk.Generator/ts/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# TypeScript SDK (SDKGEN-63-001)
|
||||
|
||||
This directory contains deterministic generator settings for the TypeScript SDK.
|
||||
|
||||
## Prereqs
|
||||
- OpenAPI spec file path exported as `STELLA_OAS_FILE` (temporary until APIG0101 publishes the canonical spec).
|
||||
- OpenAPI Generator CLI 7.4.0 jar at `tools/openapi-generator-cli-7.4.0.jar` or override `STELLA_OPENAPI_GENERATOR_JAR`.
|
||||
- JDK 21 available on PATH (vendored at `../tools/jdk-21.0.1+12`; set `JAVA_HOME` accordingly).
|
||||
|
||||
## Generate
|
||||
|
||||
```bash
|
||||
cd src/Sdk/StellaOps.Sdk.Generator
|
||||
STELLA_OAS_FILE=/path/to/api.yaml \
|
||||
STELLA_SDK_OUT=$(mktemp -d) \
|
||||
STELLA_OPENAPI_GENERATOR_JAR=tools/openapi-generator-cli-7.4.0.jar \
|
||||
ts/generate-ts.sh
|
||||
```
|
||||
|
||||
Outputs land in `out/typescript/` and are post-processed to:
|
||||
- Normalize whitespace/line endings.
|
||||
- Inject traceability banner.
|
||||
- Copy shared helpers (`sdk-hooks.ts`) and wire them through the package barrel.
|
||||
|
||||
To validate the pipeline locally with a tiny fixture spec (`ts/fixtures/ping.yaml`), run:
|
||||
|
||||
```bash
|
||||
cd src/Sdk/StellaOps.Sdk.Generator/ts
|
||||
./test_generate_ts.sh # skips if the generator jar is absent
|
||||
```
|
||||
|
||||
## Notes
|
||||
- README/package.json are suppressed in generator output; Release pipeline provides deterministic packaging instead.
|
||||
- Global properties disable model/api docs/tests to keep the alpha lean and deterministic.
|
||||
- Helper wiring depends on `STELLA_POSTPROCESS_ROOT`/`STELLA_POSTPROCESS_LANG` being set by the script.
|
||||
- Override output directory via `STELLA_SDK_OUT` to avoid mutating the repo during local tests.
|
||||
29
src/Sdk/StellaOps.Sdk.Generator/ts/config.yaml
Normal file
29
src/Sdk/StellaOps.Sdk.Generator/ts/config.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
# OpenAPI Generator config for the StellaOps TypeScript SDK (alpha)
|
||||
generatorName: typescript-fetch
|
||||
outputDir: out/typescript
|
||||
additionalProperties:
|
||||
npmName: "@stellaops/sdk"
|
||||
npmVersion: "0.0.0-alpha"
|
||||
supportsES6: true
|
||||
useSingleRequestParameter: true
|
||||
modelPropertyNaming: original
|
||||
enumPropertyNaming: original
|
||||
withoutRuntimeChecks: true
|
||||
withNodeImports: true
|
||||
snapshot: true
|
||||
legacyDiscriminatorBehavior: false
|
||||
withoutPrefixEnums: true
|
||||
typescriptThreePlus: true
|
||||
stringifyEnums: false
|
||||
npmRepository: ""
|
||||
projectName: "stellaops-sdk"
|
||||
gitUserId: "stella-ops"
|
||||
gitRepoId: "sdk-typescript"
|
||||
|
||||
# Post-process hook is supplied via env (STELLA_SDK_POSTPROCESS / postProcessFile)
|
||||
|
||||
globalProperty:
|
||||
apiDocs: false
|
||||
modelDocs: false
|
||||
apiTests: false
|
||||
modelTests: false
|
||||
27
src/Sdk/StellaOps.Sdk.Generator/ts/fixtures/ping.yaml
Normal file
27
src/Sdk/StellaOps.Sdk.Generator/ts/fixtures/ping.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: StellaOps SDK Fixture
|
||||
version: 0.0.1
|
||||
paths:
|
||||
/ping:
|
||||
get:
|
||||
summary: Health probe
|
||||
operationId: ping
|
||||
responses:
|
||||
"200":
|
||||
description: ok
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PingResponse'
|
||||
|
||||
components:
|
||||
schemas:
|
||||
PingResponse:
|
||||
type: object
|
||||
properties:
|
||||
message:
|
||||
type: string
|
||||
example: pong
|
||||
required:
|
||||
- message
|
||||
46
src/Sdk/StellaOps.Sdk.Generator/ts/generate-ts.sh
Normal file
46
src/Sdk/StellaOps.Sdk.Generator/ts/generate-ts.sh
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
config="$root_dir/ts/config.yaml"
|
||||
spec="${STELLA_OAS_FILE:-}"
|
||||
|
||||
if [ -z "$spec" ]; then
|
||||
echo "STELLA_OAS_FILE is required (path to OpenAPI spec)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
output_dir="${STELLA_SDK_OUT:-$root_dir/out/typescript}"
|
||||
mkdir -p "$output_dir"
|
||||
|
||||
# Ensure postprocess copies shared helpers into the generated tree
|
||||
export STELLA_POSTPROCESS_ROOT="$output_dir"
|
||||
export STELLA_POSTPROCESS_LANG="ts"
|
||||
|
||||
JAR="${STELLA_OPENAPI_GENERATOR_JAR:-$root_dir/tools/openapi-generator-cli-7.4.0.jar}"
|
||||
if [ ! -f "$JAR" ]; then
|
||||
echo "OpenAPI Generator CLI jar not found at $JAR" >&2
|
||||
echo "Set STELLA_OPENAPI_GENERATOR_JAR or download to tools/." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
JAVA_OPTS="${JAVA_OPTS:-} -Dorg.openapitools.codegen.utils.postProcessFile=$root_dir/postprocess/postprocess.sh"
|
||||
export JAVA_OPTS
|
||||
|
||||
java -jar "$JAR" generate \
|
||||
-i "$spec" \
|
||||
-g typescript-fetch \
|
||||
-c "$config" \
|
||||
--skip-validate-spec \
|
||||
--enable-post-process-file \
|
||||
--type-mappings object=any,DateTime=string,Date=date \
|
||||
--import-mappings Set=Array \
|
||||
--global-property models,apis,supportingFiles \
|
||||
-o "$output_dir"
|
||||
|
||||
# Ensure shared helpers are present even if upstream post-process hooks were skipped for some files
|
||||
if [ -f "$output_dir/src/index.ts" ]; then
|
||||
"$root_dir/postprocess/postprocess.sh" "$output_dir/src/index.ts"
|
||||
fi
|
||||
|
||||
echo "TypeScript SDK generated at $output_dir"
|
||||
39
src/Sdk/StellaOps.Sdk.Generator/ts/test_generate_ts.sh
Normal file
39
src/Sdk/StellaOps.Sdk.Generator/ts/test_generate_ts.sh
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
script="$root_dir/ts/generate-ts.sh"
|
||||
spec="$root_dir/ts/fixtures/ping.yaml"
|
||||
jar_default="$root_dir/tools/openapi-generator-cli-7.4.0.jar"
|
||||
jar="${STELLA_OPENAPI_GENERATOR_JAR:-$jar_default}"
|
||||
|
||||
if [ ! -f "$jar" ]; then
|
||||
echo "SKIP: generator jar not found at $jar" >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if ! command -v java >/dev/null 2>&1; then
|
||||
echo "SKIP: java not on PATH; set JAVA_HOME or install JDK to run this smoke." >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
out_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$out_dir"' EXIT
|
||||
|
||||
STELLA_OAS_FILE="$spec" \
|
||||
STELLA_SDK_OUT="$out_dir" \
|
||||
STELLA_OPENAPI_GENERATOR_JAR="$jar" \
|
||||
JAVA_OPTS="${JAVA_OPTS:-} -Dorg.openapitools.codegen.utils.postProcessFile=$root_dir/postprocess/postprocess.sh" \
|
||||
"$script"
|
||||
|
||||
test -f "$out_dir/src/apis/DefaultApi.ts" || { echo "missing generated API" >&2; exit 1; }
|
||||
test -f "$out_dir/sdk-hooks.ts" || { echo "missing helper copy" >&2; exit 1; }
|
||||
|
||||
# Basic eslint-free sanity: ensure banner on generated helper
|
||||
first_line=$(head -n 1 "$out_dir/sdk-hooks.ts")
|
||||
if [[ "$first_line" != "// Generated by StellaOps SDK generator — do not edit." ]]; then
|
||||
echo "missing banner in helper" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "TypeScript generator smoke test passed"
|
||||
Reference in New Issue
Block a user