Skip to content

Commit

Permalink
Merge branch 'main' into improve-profile_handlers-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
teodor-yanev committed Jan 9, 2025
2 parents e5392ee + de2ce2b commit 553ec90
Show file tree
Hide file tree
Showing 21 changed files with 4,050 additions and 3,180 deletions.
7 changes: 0 additions & 7 deletions cmd/cli/app/profile/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package status

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
Expand All @@ -28,12 +27,6 @@ var profileStatusCmd = &cobra.Command{
func init() {
profile.ProfileCmd.AddCommand(profileStatusCmd)
// Flags
profileStatusCmd.PersistentFlags().StringP("name", "n", "", "Profile name to get profile status for")
profileStatusCmd.PersistentFlags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
// Required
if err := profileStatusCmd.MarkPersistentFlagRequired("name"); err != nil {
profileStatusCmd.Printf("Error marking flag required: %s", err)
os.Exit(1)
}
}
88 changes: 87 additions & 1 deletion cmd/cli/app/profile/status/status_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func getCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.

project := viper.GetString("project")
profileName := viper.GetString("name")
profileId := viper.GetString("id")
entityId := viper.GetString("entity")
entityType := viper.GetString("entity-type")
format := viper.GetString("output")
Expand All @@ -42,6 +43,59 @@ func getCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.
return cli.MessageAndError(fmt.Sprintf("Output format %s not supported", format), fmt.Errorf("invalid argument"))
}

if profileId != "" {
resp, err := getProfileStatusById(ctx, client, project, profileId, entityId, entityType)
if err != nil {
return cli.MessageAndError("Error getting profile status", err)
}
return formatAndDisplayOutputById(cmd, format, resp)
} else if profileName != "" {
resp, err := getProfileStatusByName(ctx, client, project, profileName, entityId, entityType)
if err != nil {
return cli.MessageAndError("Error getting profile status", err)
}
return formatAndDisplayOutputByName(cmd, format, resp)
}

return cli.MessageAndError("Error getting profile status", fmt.Errorf("profile id or profile name required"))
}

func getProfileStatusById(
ctx context.Context,
client minderv1.ProfileServiceClient,
project, profileId, entityId, entityType string,
) (*minderv1.GetProfileStatusByIdResponse, error) {
if profileId == "" {
return nil, cli.MessageAndError("Error getting profile status", fmt.Errorf("profile id required"))
}

resp, err := client.GetProfileStatusById(ctx, &minderv1.GetProfileStatusByIdRequest{
Context: &minderv1.Context{Project: &project},
Id: profileId,
Entity: &minderv1.EntityTypedId{
Id: entityId,
Type: minderv1.EntityFromString(entityType),
},
})
if err != nil {
return nil, err
}

return &minderv1.GetProfileStatusByIdResponse{
ProfileStatus: resp.ProfileStatus,
RuleEvaluationStatus: resp.RuleEvaluationStatus,
}, nil
}

func getProfileStatusByName(
ctx context.Context,
client minderv1.ProfileServiceClient,
project, profileName, entityId, entityType string,
) (*minderv1.GetProfileStatusByNameResponse, error) {
if profileName == "" {
return nil, cli.MessageAndError("Error getting profile status", fmt.Errorf("profile name required"))
}

resp, err := client.GetProfileStatusByName(ctx, &minderv1.GetProfileStatusByNameRequest{
Context: &minderv1.Context{Project: &project},
Name: profileName,
Expand All @@ -51,9 +105,16 @@ func getCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.
},
})
if err != nil {
return cli.MessageAndError("Error getting profile status", err)
return nil, err
}

return &minderv1.GetProfileStatusByNameResponse{
ProfileStatus: resp.ProfileStatus,
RuleEvaluationStatus: resp.RuleEvaluationStatus,
}, nil
}

func formatAndDisplayOutputById(cmd *cobra.Command, format string, resp *minderv1.GetProfileStatusByIdResponse) error {
switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
Expand All @@ -72,7 +133,28 @@ func getCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.
profile.RenderProfileStatusTable(resp.ProfileStatus, table)
table.Render()
}
return nil
}

func formatAndDisplayOutputByName(cmd *cobra.Command, format string, resp *minderv1.GetProfileStatusByNameResponse) error {
switch format {
case app.JSON:
out, err := util.GetJsonFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(resp)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
case app.Table:
table := profile.NewProfileStatusTable()
profile.RenderProfileStatusTable(resp.ProfileStatus, table)
table.Render()
}
return nil
}

Expand All @@ -82,6 +164,10 @@ func init() {
getCmd.Flags().StringP("entity", "e", "", "Entity ID to get profile status for")
getCmd.Flags().StringP("entity-type", "t", "",
fmt.Sprintf("the entity type to get profile status for (one of %s)", entities.KnownTypesCSV()))
getCmd.Flags().StringP("id", "i", "", "ID to get profile status for")
getCmd.Flags().StringP("name", "n", "", "Profile name to get profile status for")

getCmd.MarkFlagsOneRequired("id", "name")
// Required
if err := getCmd.MarkFlagRequired("entity"); err != nil {
getCmd.Printf("Error marking flag required: %s", err)
Expand Down
8 changes: 8 additions & 0 deletions cmd/cli/app/profile/status/status_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package status
import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -84,4 +85,11 @@ func init() {
listCmd.Flags().BoolP("detailed", "d", false, "List all profile violations")
listCmd.Flags().StringP("ruleType", "r", "", "Filter profile status list by rule type")
listCmd.Flags().String("ruleName", "", "Filter profile status list by rule name")

listCmd.Flags().StringP("name", "n", "", "Profile name to list status for")

if err := listCmd.MarkFlagRequired("name"); err != nil {
listCmd.Printf("Error marking flag required: %s", err)
os.Exit(1)
}
}
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ minder profile status [flags]

```
-h, --help help for status
-n, --name string Profile name to get profile status for
-o, --output string Output format (one of json,yaml,table) (default "table")
```

Expand Down
3 changes: 2 additions & 1 deletion docs/docs/ref/cli/minder_profile_status_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ minder profile status get [flags]
-e, --entity string Entity ID to get profile status for
-t, --entity-type string the entity type to get profile status for (one of artifact, build, build_environment, pipeline_run, release, repository, task_run)
-h, --help help for get
-i, --id string ID to get profile status for
-n, --name string Profile name to get profile status for
```

### Options inherited from parent commands
Expand All @@ -30,7 +32,6 @@ minder profile status get [flags]
--grpc-port int Server port (default 443)
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-n, --name string Profile name to get profile status for
-o, --output string Output format (one of json,yaml,table) (default "table")
-j, --project string ID of the project
-v, --verbose Output additional messages to STDERR
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/ref/cli/minder_profile_status_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ minder profile status list [flags]
```
-d, --detailed List all profile violations
-h, --help help for list
-n, --name string Profile name to list status for
--ruleName string Filter profile status list by rule name
-r, --ruleType string Filter profile status list by rule type
```
Expand All @@ -31,7 +32,6 @@ minder profile status list [flags]
--grpc-port int Server port (default 443)
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-n, --name string Profile name to get profile status for
-o, --output string Output format (one of json,yaml,table) (default "table")
-j, --project string ID of the project
-v, --verbose Output additional messages to STDERR
Expand Down
29 changes: 29 additions & 0 deletions docs/docs/ref/proto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/open-feature/go-sdk v1.13.1
github.com/open-feature/go-sdk-contrib/providers/go-feature-flag-in-process v0.1.0
github.com/open-policy-agent/opa v0.70.0
github.com/open-policy-agent/opa v1.0.0
github.com/openfga/go-sdk v0.6.3
github.com/openfga/openfga v1.8.3
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
Expand Down Expand Up @@ -142,7 +142,7 @@ require (
github.com/containerd/containerd v1.7.24 // indirect
github.com/containerd/containerd/api v1.7.19 // indirect
github.com/containerd/continuity v0.4.5 // indirect
github.com/containerd/errdefs v0.3.0 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/fifo v1.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ github.com/containerd/containerd/api v1.7.19 h1:VWbJL+8Ap4Ju2mx9c9qS1uFSB1OVYr5J
github.com/containerd/containerd/api v1.7.19/go.mod h1:fwGavl3LNwAV5ilJ0sbrABL44AQxmNjDRcwheXDb6Ig=
github.com/containerd/continuity v0.4.5 h1:ZRoN1sXq9u7V6QoHMcVWGhOwDFqZ4B9i5H6un1Wh0x4=
github.com/containerd/continuity v0.4.5/go.mod h1:/lNJvtJKUQStBzpVQ1+rasXO1LAWtUQssk28EZvJ3nE=
github.com/containerd/errdefs v0.3.0 h1:FSZgGOeK4yuT/+DnF07/Olde/q4KBoMsaamhXxIMDp4=
github.com/containerd/errdefs v0.3.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/fifo v1.1.0 h1:4I2mbh5stb1u6ycIABlBw9zgtlK8viPI9QkQNRQEEmY=
github.com/containerd/fifo v1.1.0/go.mod h1:bmC4NWMbXlt2EZ0Hc7Fx7QzTFxgPID13eH0Qu+MAb2o=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down Expand Up @@ -850,8 +850,8 @@ github.com/open-feature/go-sdk v1.13.1 h1:RJbS70eyi7Jd3Zm5bFnaahNKNDXn+RAVnctpGu
github.com/open-feature/go-sdk v1.13.1/go.mod h1:O8r4mhgeRIsjJ0ZBXlnE0BtbT/79W44gQceR7K8KYgo=
github.com/open-feature/go-sdk-contrib/providers/go-feature-flag-in-process v0.1.0 h1:EFIT5QBQ/T3lNVLmma69SNQbAWBgAl+EtcH0VfrdM7Y=
github.com/open-feature/go-sdk-contrib/providers/go-feature-flag-in-process v0.1.0/go.mod h1:DpptytCB+FbUIoRjTGtSDEA82aojWC4MIxL8GOK26Rs=
github.com/open-policy-agent/opa v0.70.0 h1:B3cqCN2iQAyKxK6+GI+N40uqkin+wzIrM7YA60t9x1U=
github.com/open-policy-agent/opa v0.70.0/go.mod h1:Y/nm5NY0BX0BqjBriKUiV81sCl8XOjjvqQG7dXrggtI=
github.com/open-policy-agent/opa v1.0.0 h1:fZsEwxg1knpPvUn0YDJuJZBcbVg4G3zKpWa3+CnYK+I=
github.com/open-policy-agent/opa v1.0.0/go.mod h1:+JyoH12I0+zqyC1iX7a2tmoQlipwAEGvOhVJMhmy+rM=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
Expand Down
Loading

0 comments on commit 553ec90

Please sign in to comment.