-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-292660: add a new 'foascli sunset list' command to foascli to list all endpoints with theirs sunset date #339
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a59569
CLOUDP-292660: add a new 'foascli sunset list' command to foascli to …
andreaangiolillo feac427
add support for team
andreaangiolillo 8315494
Addressed PR comments - Part 1
andreaangiolillo 75c7044
addressed PR comments - Part 2
andreaangiolillo 9d50b35
renamed NewSunsetListFromSpec to NewListFromSpec
andreaangiolillo 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
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,103 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sunset | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mongodb/openapi/tools/cli/internal/cli/flag" | ||
"github.com/mongodb/openapi/tools/cli/internal/cli/usage" | ||
"github.com/mongodb/openapi/tools/cli/internal/openapi" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type ListOpts struct { | ||
fs afero.Fs | ||
basePath string | ||
outputPath string | ||
format string | ||
} | ||
|
||
func (o *ListOpts) Run() error { | ||
loader := openapi.NewOpenAPI3() | ||
specInfo, err := loader.CreateOpenAPISpecFromPath(o.basePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bytes, err := o.newSunsetListBytes(openapi.NewSunsetListFromSpec(specInfo)) | ||
if err != nil { | ||
return err | ||
} | ||
if o.outputPath != "" { | ||
return afero.WriteFile(o.fs, o.outputPath, bytes, 0o600) | ||
} | ||
|
||
fmt.Println(string(bytes)) | ||
return nil | ||
} | ||
|
||
func (o *ListOpts) newSunsetListBytes(versions []*openapi.Sunset) ([]byte, error) { | ||
data, err := json.MarshalIndent(versions, "", " ") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if format := strings.ToLower(o.format); format == "json" { | ||
return data, nil | ||
} | ||
|
||
var jsonData interface{} | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if mErr := json.Unmarshal(data, &jsonData); mErr != nil { | ||
return nil, mErr | ||
} | ||
|
||
yamlData, err := yaml.Marshal(jsonData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return yamlData, nil | ||
} | ||
|
||
// ListBuilder builds the merge command with the following signature: | ||
// changelog create -b path_folder -r path_folder --dry-run | ||
func ListBuilder() *cobra.Command { | ||
opts := &ListOpts{ | ||
fs: afero.NewOsFs(), | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list -s spec.json -o json", | ||
ciprian-tibulca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Short: "List API endpoints with a Sunset date for a given OpenAPI spec.", | ||
Aliases: []string{"ls"}, | ||
Args: cobra.NoArgs, | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
return opts.Run() | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "", usage.Spec) | ||
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output) | ||
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, "json", usage.Format) | ||
|
||
_ = cmd.MarkFlagRequired(flag.Spec) | ||
|
||
return cmd | ||
} |
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,41 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sunset | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestList_Run(t *testing.T) { | ||
fs := afero.NewMemMapFs() | ||
opts := &ListOpts{ | ||
basePath: "../../../test/data/base_spec.json", | ||
outputPath: "foas.json", | ||
fs: fs, | ||
} | ||
|
||
if err := opts.Run(); err != nil { | ||
t.Fatalf("Run() unexpected error: %v", err) | ||
} | ||
|
||
b, err := afero.ReadFile(fs, opts.outputPath) | ||
if err != nil { | ||
t.Fatalf("ReadFile() unexpected error: %v", err) | ||
} | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assert.NotEmpty(t, b) | ||
} |
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,33 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sunset | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Builder() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "sunset", | ||
Short: "Manage the Sunset API for the OpenAPI spec.", | ||
Annotations: map[string]string{ | ||
"toc": "true", | ||
}, | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
cmd.AddCommand(ListBuilder()) | ||
|
||
return cmd | ||
} |
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,30 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sunset | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mongodb/openapi/tools/cli/internal/test" | ||
) | ||
|
||
func TestBuilder(t *testing.T) { | ||
test.CmdValidator( | ||
t, | ||
Builder(), | ||
1, | ||
[]string{}, | ||
) | ||
} |
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,102 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package openapi | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/tufin/oasdiff/load" | ||
) | ||
|
||
const ( | ||
sunsetExtensionName = "x-sunset" | ||
apiVersionExtensionName = "x-xgen-version" | ||
teamExtensionName = "x-xgen-owner-team" | ||
) | ||
|
||
type Sunset struct { | ||
Operation string `json:"http_method" yaml:"http_method"` | ||
Path string `json:"path" yaml:"path"` | ||
Version string `json:"version" yaml:"version"` | ||
SunsetDate string `json:"sunset_date" yaml:"sunset_date"` | ||
Team string `json:"team" yaml:"team"` | ||
} | ||
|
||
func NewSunsetListFromSpec(spec *load.SpecInfo) []*Sunset { | ||
var sunsets []*Sunset | ||
paths := spec.Spec.Paths | ||
|
||
for path, pathBody := range paths.Map() { | ||
for operationName, operationBody := range pathBody.Operations() { | ||
teamName := newTeamNameFromOperation(operationBody) | ||
extensions := newExtensionsFrom2xxResponse(operationBody.Responses.Map()) | ||
if extensions == nil { | ||
continue | ||
} | ||
|
||
apiVersion, ok := extensions[apiVersionExtensionName] | ||
if !ok { | ||
continue | ||
} | ||
|
||
sunsetExt, ok := extensions[sunsetExtensionName] | ||
if !ok { | ||
continue | ||
} | ||
|
||
sunset := Sunset{ | ||
Operation: operationName, | ||
Path: path, | ||
SunsetDate: sunsetExt.(string), | ||
Version: apiVersion.(string), | ||
Team: teamName, | ||
} | ||
|
||
sunsets = append(sunsets, &sunset) | ||
} | ||
} | ||
|
||
return sunsets | ||
} | ||
|
||
func newTeamNameFromOperation(op *openapi3.Operation) string { | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if value, ok := op.Extensions[teamExtensionName]; ok { | ||
return value.(string) | ||
} | ||
return "" | ||
} | ||
|
||
func newExtensionsFrom2xxResponse(responsesMap map[string]*openapi3.ResponseRef) map[string]any { | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if val, ok := responsesMap["200"]; ok { | ||
return newExtensionsFromContent(val.Value.Content) | ||
} | ||
if val, ok := responsesMap["201"]; ok { | ||
return newExtensionsFromContent(val.Value.Content) | ||
} | ||
if val, ok := responsesMap["202"]; ok { | ||
return newExtensionsFromContent(val.Value.Content) | ||
} | ||
if val, ok := responsesMap["204"]; ok { | ||
return newExtensionsFromContent(val.Value.Content) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func newExtensionsFromContent(content openapi3.Content) map[string]any { | ||
andreaangiolillo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
for _, v := range content { | ||
return v.Extensions | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
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.