Skip to content

Commit

Permalink
Add support for Get Profile Status By ID in Cli and Api (#5100)
Browse files Browse the repository at this point in the history
Ref #4999
  • Loading branch information
navnitms authored Jan 9, 2025
1 parent d7ef7a1 commit 1bad8de
Show file tree
Hide file tree
Showing 12 changed files with 4,025 additions and 3,159 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)
}
}
2 changes: 2 additions & 0 deletions docs/docs/ref/cli/minder_profile_status_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ minder profile status get [flags]
### Options

```
-i, --id string ID to get profile status for
-n, --name string Profile name to get profile status for
-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
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.

Loading

0 comments on commit 1bad8de

Please sign in to comment.