-
Notifications
You must be signed in to change notification settings - Fork 609
feat: scrape and diff openapi.yaml #3632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ffb566
feat: scrape and diff openapi.yaml
chronark 056c771
📝 CodeRabbit Chat: Fix pointer reuse in convertChangesToProto by copy…
coderabbitai[bot] 0d981c9
📝 CodeRabbit Chat: Fix pointer reuse in convertChangesToProto by copy…
coderabbitai[bot] e5f29bc
Update utils.go
chronark 2eacb07
fix: remove botched merge
chronark b194d4a
fix: sync column
chronark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "github.com/oasdiff/oasdiff/checker" | ||
| "github.com/oasdiff/oasdiff/diff" | ||
| ctrlv1 "github.com/unkeyed/unkey/go/gen/proto/ctrl/v1" | ||
| "github.com/unkeyed/unkey/go/pkg/ptr" | ||
| ) | ||
|
|
||
| func convertSummaryToProto(summary *diff.Summary) *ctrlv1.DiffSummary { | ||
| // Helper function to get counts safely | ||
| getCounts := func(name string) *ctrlv1.DiffCounts { | ||
| if details, exists := summary.Details[diff.DetailName(name)]; exists { | ||
| return &ctrlv1.DiffCounts{ | ||
| Added: int32(details.Added), | ||
| Deleted: int32(details.Deleted), | ||
| Modified: int32(details.Modified), | ||
| } | ||
| } | ||
| return &ctrlv1.DiffCounts{Added: 0, Deleted: 0, Modified: 0} | ||
| } | ||
|
|
||
| return &ctrlv1.DiffSummary{ | ||
| Diff: summary.Diff, | ||
| Details: &ctrlv1.DiffDetails{ | ||
| Endpoints: getCounts("endpoints"), | ||
| Paths: getCounts("paths"), | ||
| Schemas: getCounts("schemas"), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func convertChangesToProto(changes checker.Changes) []*ctrlv1.ChangelogEntry { | ||
| localizer := checker.NewLocalizer("en") | ||
| result := make([]*ctrlv1.ChangelogEntry, len(changes)) | ||
|
|
||
| for i, change := range changes { | ||
| level := int32(1) // INFO | ||
| switch change.GetLevel() { | ||
| case checker.WARN: | ||
| level = 2 | ||
| case checker.ERR: | ||
| level = 3 | ||
| } | ||
|
|
||
| result[i] = &ctrlv1.ChangelogEntry{ | ||
| Id: change.GetId(), | ||
| Text: change.GetUncolorizedText(localizer), | ||
| Level: level, | ||
| Operation: change.GetOperation(), | ||
| Path: change.GetPath(), | ||
| OperationId: ptr.P(change.GetOperationId()), | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "connectrpc.com/connect" | ||
| "github.com/getkin/kin-openapi/openapi3" | ||
| "github.com/oasdiff/oasdiff/checker" | ||
| "github.com/oasdiff/oasdiff/diff" | ||
| ctrlv1 "github.com/unkeyed/unkey/go/gen/proto/ctrl/v1" | ||
| "github.com/unkeyed/unkey/go/pkg/fault" | ||
| ) | ||
|
|
||
| func (s *Service) GetOpenApiDiff(ctx context.Context, req *connect.Request[ctrlv1.GetOpenApiDiffRequest]) (*connect.Response[ctrlv1.GetOpenApiDiffResponse], error) { | ||
| // Load old version spec | ||
| oldSpec, err := s.loadVersionSpec(ctx, req.Msg.OldVersionId) | ||
| if err != nil { | ||
| return nil, connect.NewError(connect.CodeNotFound, fault.Wrap(err, | ||
| fault.Internal("failed to load old version spec"), | ||
| fault.Public("Old version not found"), | ||
| )) | ||
| } | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Load new version spec | ||
| newSpec, err := s.loadVersionSpec(ctx, req.Msg.NewVersionId) | ||
| if err != nil { | ||
| return nil, connect.NewError(connect.CodeNotFound, fault.Wrap(err, | ||
| fault.Internal("failed to load new version spec"), | ||
| fault.Public("New version not found"), | ||
| )) | ||
| } | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Parse OpenAPI specs | ||
| loader := openapi3.NewLoader() | ||
| loader.IsExternalRefsAllowed = true | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| s1, err := loader.LoadFromData([]byte(oldSpec)) | ||
| if err != nil { | ||
| return nil, connect.NewError(connect.CodeInvalidArgument, fault.Wrap(err, | ||
| fault.Internal("failed to parse old OpenAPI spec"), | ||
| fault.Public("Invalid OpenAPI specification in old version"), | ||
| )) | ||
| } | ||
|
|
||
| s2, err := loader.LoadFromData([]byte(newSpec)) | ||
| if err != nil { | ||
| return nil, connect.NewError(connect.CodeInvalidArgument, fault.Wrap(err, | ||
| fault.Internal("failed to parse new OpenAPI spec"), | ||
| fault.Public("Invalid OpenAPI specification in new version"), | ||
| )) | ||
| } | ||
|
|
||
| // Generate diff report | ||
| diffReport, err := diff.Get(&diff.Config{}, s1, s2) | ||
| if err != nil { | ||
| return nil, connect.NewError(connect.CodeInternal, fault.Wrap(err, | ||
| fault.Internal("failed to generate diff report"), | ||
| fault.Public("Failed to generate diff report"), | ||
| )) | ||
| } | ||
|
|
||
| // Generate changelog using checker | ||
| config := checker.NewConfig(checker.GetAllChecks()) | ||
| changes := checker.CheckBackwardCompatibility( | ||
| config, | ||
| diffReport, | ||
| &diff.OperationsSourcesMap{}, | ||
| ) | ||
|
|
||
| // Check if there are any breaking changes | ||
| hasBreakingChanges := false | ||
| for _, change := range changes { | ||
| if change.GetLevel() == checker.ERR { | ||
| hasBreakingChanges = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return connect.NewResponse(&ctrlv1.GetOpenApiDiffResponse{ | ||
| Summary: convertSummaryToProto(diffReport.GetSummary()), | ||
| HasBreakingChanges: hasBreakingChanges, | ||
| Changes: convertChangesToProto(changes), | ||
| }), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "github.com/unkeyed/unkey/go/gen/proto/ctrl/v1/ctrlv1connect" | ||
| "github.com/unkeyed/unkey/go/pkg/db" | ||
| "github.com/unkeyed/unkey/go/pkg/otel/logging" | ||
| ) | ||
|
|
||
| type Service struct { | ||
| ctrlv1connect.UnimplementedOpenApiServiceHandler | ||
| db db.Database | ||
| logger logging.Logger | ||
| } | ||
|
|
||
| func New(database db.Database, logger logging.Logger) *Service { | ||
| return &Service{ | ||
| UnimplementedOpenApiServiceHandler: ctrlv1connect.UnimplementedOpenApiServiceHandler{}, | ||
| db: database, | ||
| logger: logger, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/unkeyed/unkey/go/pkg/db" | ||
| "github.com/unkeyed/unkey/go/pkg/fault" | ||
| ) | ||
|
|
||
| func (s *Service) loadVersionSpec(ctx context.Context, versionID string) (string, error) { | ||
| version, err := db.Query.FindVersionById(ctx, s.db.RO(), versionID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if !version.OpenapiSpec.Valid { | ||
| return "", fault.New("version has no OpenAPI spec stored", | ||
| fault.Public("OpenAPI specification not available for this version"), | ||
| ) | ||
| } | ||
|
|
||
| return version.OpenapiSpec.String, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.