Repository navigation
fix: prevent removal of services on blocklist #307
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
marceljk
merged 3 commits into
main
from
fix/STACKITSDK-553-prevent-removal-of-services-on-blocklist
Oct 23, 2026
+419
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // runCopy implements the `copy --plan <path/to/generation-plan.json>` command. | ||
| // It prints all services which have the action "block" in the generation-plan. These services should be copied | ||
| // during the generation, to prevent that they will be removed. | ||
| func runCopy(_ context.Context, args []string, env Environment) error { | ||
| copyFlags := flag.NewFlagSet("copy", flag.ContinueOnError) | ||
| copyFlags.SetOutput(env.Err) | ||
|
|
||
| var planPath string | ||
| copyFlags.StringVar(&planPath, "plan", "", "path to the generation plan") | ||
| if err := copyFlags.Parse(args); err != nil { | ||
| return fmt.Errorf("parsing copy flags: %w", err) | ||
| } | ||
| if len(copyFlags.Args()) != 0 { | ||
| return fmt.Errorf("unexpected arguments: %s", strings.Join(copyFlags.Args(), " ")) | ||
| } | ||
| if planPath == "" { | ||
| return fmt.Errorf("--plan is required") | ||
| } | ||
|
|
||
| plan, err := readPlan(env.FS, planPath) | ||
| if err != nil { | ||
| return fmt.Errorf("read generation plan: %w", err) | ||
| } | ||
|
|
||
| blockedServices := findBlockedServices(plan) | ||
|
|
||
| return outputResult(env, blockedServices) | ||
| } | ||
|
|
||
| // findBlockedServices finds all services in `Plan.Service` which are blocked and returns the service name in a string slice. | ||
| func findBlockedServices(plan Plan) []string { | ||
| var blockedServices []string | ||
| for _, service := range plan.Services { | ||
| if service.Action == ActionBlock { | ||
| blockedServices = append(blockedServices, service.Service) | ||
| } | ||
| } | ||
|
|
||
| return blockedServices | ||
| } | ||
|
|
||
| // outputResult takes a list of blockedServices and writes service in a separate line into `env.Out`. | ||
| func outputResult(env Environment, blockedServices []string) error { | ||
| if len(blockedServices) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| result := fmt.Sprintf("%s\n", strings.Join(blockedServices, "\n")) | ||
|
|
||
| _, err := fmt.Fprint(env.Out, result) | ||
| if err != nil { | ||
| return fmt.Errorf("writing result: %w", err) | ||
| } | ||
|
|
||
| return 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,266 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "reflect" | ||
| "testing" | ||
| "testing/fstest" | ||
| ) | ||
|
|
||
| func TestRunCopy(t *testing.T) { | ||
| fixtureFlags := []string{"--plan", "generation-plan.json"} | ||
| fixtureGenerationPlan := []byte(`{ | ||
| "services": [ | ||
| {"service": "remain", "oas_service": "remain-oas", "action": "block"}, | ||
| {"service": "foo", "oas_service": "foo-bar", "action": "generate"}, | ||
| {"service": "obsolete", "action": "delete"}, | ||
| {"service": "bar", "oas_service": "bar-oas", "action": "block"}, | ||
| {"service": "service2", "action": "delete"} | ||
| ] | ||
| }`) | ||
|
|
||
| type args struct { | ||
| flags []string | ||
| generationPlan []byte | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "success", | ||
| args: args{ | ||
| flags: fixtureFlags, | ||
| generationPlan: fixtureGenerationPlan, | ||
| }, | ||
| want: "remain\nbar\n", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "no blocked services", | ||
| args: args{ | ||
| flags: fixtureFlags, | ||
| generationPlan: []byte(`{ | ||
| "services": [ | ||
| {"service": "remain", "oas_service": "remain-oas", "action": "generate"}, | ||
| {"service": "foo", "oas_service": "foo-bar", "action": "generate"}, | ||
| {"service": "obsolete", "action": "delete"}, | ||
| {"service": "bar", "oas_service": "bar-oas", "action": "generate"}, | ||
| {"service": "service2", "action": "delete"} | ||
| ] | ||
| }`), | ||
| }, | ||
| want: "", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "missing flags", | ||
| args: args{ | ||
| flags: []string{}, | ||
| generationPlan: fixtureGenerationPlan, | ||
| }, | ||
| want: "", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "flag to invalid generationPlan", | ||
| args: args{ | ||
| flags: []string{"--plan", "not-existing-file.json"}, | ||
| generationPlan: fixtureGenerationPlan, | ||
| }, | ||
| want: "", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| filesystem := fstest.MapFS{ | ||
| "generation-plan.json": { | ||
| Data: tt.args.generationPlan, | ||
| }, | ||
| } | ||
|
|
||
| var stdout bytes.Buffer | ||
| env := Environment{ | ||
| Out: &stdout, | ||
| FS: readOnlyFileSystem{FileSystem: filesystem}, | ||
| } | ||
|
|
||
| err := runCopy(context.Background(), tt.args.flags, env) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("runCopy() error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| if got := stdout.String(); got != tt.want { | ||
| t.Errorf("runCopy() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestFindBlockedServices(t *testing.T) { | ||
| type args struct { | ||
| plan Plan | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "finds blocked services", | ||
| args: args{ | ||
| plan: Plan{ | ||
| Services: []PlannedService{ | ||
| { | ||
| Service: "remain", | ||
| OASService: "remain-oas", | ||
| Action: ActionBlock, | ||
| }, | ||
| { | ||
| Service: "obsolete", | ||
| Action: ActionDelete, | ||
| }, | ||
| { | ||
| Service: "foo", | ||
| OASService: "foo-bar", | ||
| Action: ActionGenerate, | ||
| }, | ||
| { | ||
| Service: "bar", | ||
| OASService: "bar-oas", | ||
| Action: ActionBlock, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| want: []string{"remain", "bar"}, | ||
| }, | ||
| { | ||
| name: "no blocked services", | ||
| args: args{ | ||
| plan: Plan{ | ||
| Services: []PlannedService{ | ||
| { | ||
| Service: "remain", | ||
| OASService: "remain-oas", | ||
| Action: ActionGenerate, | ||
| }, | ||
| { | ||
| Service: "obsolete", | ||
| OASService: "obsolete-oas", | ||
| Action: ActionDelete, | ||
| }, | ||
| { | ||
| Service: "foo", | ||
| OASService: "foo-bar", | ||
| Action: ActionGenerate, | ||
| }, | ||
| { | ||
| Service: "bar", | ||
| OASService: "bar-oas", | ||
| Action: ActionDelete, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "all services are blocked", | ||
| args: args{ | ||
| plan: Plan{ | ||
| Services: []PlannedService{ | ||
| { | ||
| Service: "remain", | ||
| OASService: "remain-oas", | ||
| Action: ActionBlock, | ||
| }, | ||
| { | ||
| Service: "obsolete", | ||
| OASService: "remain-oas", | ||
| Action: ActionBlock, | ||
| }, | ||
| { | ||
| Service: "foo", | ||
| OASService: "foo-bar", | ||
| Action: ActionBlock, | ||
| }, | ||
| { | ||
| Service: "bar", | ||
| OASService: "bar-oas", | ||
| Action: ActionBlock, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| want: []string{"remain", "obsolete", "foo", "bar"}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := findBlockedServices(tt.args.plan); !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("findBlockedServices() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestOutputResult(t *testing.T) { | ||
| type args struct { | ||
| blockedServices []string | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "outputs blocked services", | ||
| args: args{ | ||
| blockedServices: []string{ | ||
| "remain", | ||
| "obsolete", | ||
| }, | ||
| }, | ||
| want: "remain\nobsolete\n", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "empty blocked services", | ||
| args: args{ | ||
| blockedServices: []string{}, | ||
| }, | ||
| want: "", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "nil blocked services", | ||
| args: args{ | ||
| blockedServices: nil, | ||
| }, | ||
| want: "", | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| var stdout bytes.Buffer | ||
| env := Environment{ | ||
| Out: &stdout, | ||
| } | ||
|
|
||
| if err := outputResult(env, tt.args.blockedServices); (err != nil) != tt.wantErr { | ||
| t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
|
|
||
| got := stdout.String() | ||
| if got != tt.want { | ||
| t.Errorf("stdout = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,2 @@ | ||
| github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||
| github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= |
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
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.