Skip to content

Commit

Permalink
✨ Add Isses to the CLI (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Munklinde96 authored Nov 1, 2024
1 parent e9df72f commit 23453c9
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 3 deletions.
162 changes: 162 additions & 0 deletions cmd/rig/cmd/issue/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package issue

import (
"context"
"fmt"

"connectrpc.com/connect"
"github.com/jedib0t/go-pretty/v6/table"

"github.com/rigdev/rig-go-api/api/v1/issue"
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig-go-sdk"
"github.com/rigdev/rig/cmd/common"
"github.com/rigdev/rig/cmd/rig/cmd/flags"
"github.com/rigdev/rig/pkg/cli"
"github.com/rigdev/rig/pkg/cli/scope"
"github.com/spf13/cobra"
"go.uber.org/fx"
)

var (
projectFilter string
environmentFilter string
capsuleFilter string

limit int
offset int
)

type Cmd struct {
fx.In

Rig rig.Client
Scope scope.Scope
Prompter common.Prompter
}

var cmd Cmd

func initCmd(c Cmd) {
cmd.Rig = c.Rig
cmd.Scope = c.Scope
cmd.Prompter = c.Prompter
}

func Setup(parent *cobra.Command, s *cli.SetupContext) {
issue := &cobra.Command{
Use: "issues",
Short: "List issues in Rig",
PersistentPreRunE: s.MakeInvokePreRunE(initCmd),
Annotations: map[string]string{
"auth.OmitProject": "",
"auth.OmitEnvironment": "",
},
GroupID: common.ManagementGroupID,
RunE: cli.CtxWrap(cmd.list),
}

issue.Flags().IntVar(
&limit, "limit", 10,
"Limit the number of activities returned. Default is 10.",
)

issue.Flags().IntVar(
&offset, "offset", 0,
"Offset the activities returned. Default is 0.",
)

issue.Flags().StringVar(
&projectFilter, "project-filter", "",
"Filter activities by project ID",
)

issue.Flags().StringVar(
&environmentFilter, "environment-filter", "",
"Filter activities by environment ID",
)

issue.Flags().StringVar(
&capsuleFilter, "capsule-filter", "",
"Filter activities by capsule ID",
)

parent.AddCommand(issue)
}

func (c *Cmd) list(ctx context.Context, _ *cobra.Command, _ []string) error {
resp, err := c.Rig.Issue().GetIssues(ctx, connect.NewRequest(&issue.GetIssuesRequest{
Pagination: &model.Pagination{
Limit: uint32(limit),
Offset: uint32(offset),
},
Filter: &issue.Filter{
Project: projectFilter,
Environment: environmentFilter,
Capsule: capsuleFilter,
},
}))
if err != nil {
return err
}

issues := resp.Msg.GetIssues()

if len(issues) == 0 {
fmt.Println("No issues found")
return nil
}

if flags.Flags.OutputType != common.OutputTypePretty {
return common.FormatPrint(issues, flags.Flags.OutputType)
}

t := table.NewWriter()
t.AppendHeader(table.Row{
"Type", "Message", "Level", "Reference", "Count", "CreatedAt", "UpdatedAt", "ClosedAt", "StaleAt",
})

for _, i := range issues {
t.AppendRow(table.Row{
i.Type, i.Message, i.GetLevel().String(), issueReferenceToString(i.GetReference()),
fmt.Sprint(i.GetCount()),
i.GetCreatedAt().AsTime().Local().Format("2006-01-02 15:04:05"),
i.GetUpdatedAt().AsTime().Local().Format("2006-01-02 15:04:05"),
i.GetClosedAt().AsTime().Local().Format("2006-01-02 15:04:05"),
i.GetStaleAt().AsTime().Local().Format("2006-01-02 15:04:05"),
})
}

fmt.Println(t.Render())

return nil
}

func issueReferenceToString(r *issue.Reference) string {
if r == nil {
return "all"
}

var str string
if r.GetProjectId() != "" {
str += fmt.Sprintf("Project:%s\n", r.GetProjectId())
}

if r.GetEnvironmentId() != "" {
str += fmt.Sprintf("Environment:%s\n", r.GetEnvironmentId())
}

if r.GetCapsuleId() != "" {
str += fmt.Sprintf("Capsule:%s\n", r.GetCapsuleId())
}

if r.GetRolloutId() != 0 {
str += fmt.Sprintf("Rollout:%v\n", r.GetRolloutId())
}

if r.GetInstanceId() != "" {
str += fmt.Sprintf("Instance:%s\n", r.GetInstanceId())
}

return str
}
2 changes: 2 additions & 0 deletions cmd/rig/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rigdev/rig/cmd/rig/cmd/flags"
"github.com/rigdev/rig/cmd/rig/cmd/git"
"github.com/rigdev/rig/cmd/rig/cmd/group"
"github.com/rigdev/rig/cmd/rig/cmd/issue"
"github.com/rigdev/rig/cmd/rig/cmd/noop"
"github.com/rigdev/rig/cmd/rig/cmd/project"
"github.com/rigdev/rig/cmd/rig/cmd/role"
Expand Down Expand Up @@ -155,6 +156,7 @@ func Run(s *cli.SetupContext) error {
role.Setup(rootCmd, s)
git.Setup(rootCmd, s)
activity.Setup(rootCmd, s)
issue.Setup(rootCmd, s)

if s.AddTestCommand {
noop.Setup(rootCmd, s)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.70.0
github.com/rigdev/rig-go-api v0.0.0-20241029083126-a6814266e716
github.com/rigdev/rig-go-sdk v0.0.0-20241021090503-b515c1ca035f
github.com/rigdev/rig-go-sdk v0.0.0-20241029085104-d3fedb186c73
github.com/rivo/tview v0.0.0-20240524063012-037df494fb76
github.com/robfig/cron v1.2.0
github.com/robfig/cron/v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rigdev/rig-go-api v0.0.0-20241029083126-a6814266e716 h1:QpieTeTr+uVcjBcQJuGpLz7sFuHl87EKf0lST7gK9qc=
github.com/rigdev/rig-go-api v0.0.0-20241029083126-a6814266e716/go.mod h1:0yAwJnPID3gHfB3ETQAFYGM1tkNWkJKbVzauZiHmHKw=
github.com/rigdev/rig-go-sdk v0.0.0-20241021090503-b515c1ca035f h1:SScDJVwFt/QzURstwUdi/ejnRoqB1ZsBLlICPD6Lt0c=
github.com/rigdev/rig-go-sdk v0.0.0-20241021090503-b515c1ca035f/go.mod h1:DJAwoPmFI6Jo71n/2VfFk/Nyzlyd24ARWCLC4Hsm1gg=
github.com/rigdev/rig-go-sdk v0.0.0-20241029085104-d3fedb186c73 h1:Dyl1C7mdzWrTV7PJJyf3nrZE0yDog1Sx/xXa0X26hc8=
github.com/rigdev/rig-go-sdk v0.0.0-20241029085104-d3fedb186c73/go.mod h1:QcDI8rSkuMDmYPo1+MLQuuEC0IY2IgsUfBX4UckZXmM=
github.com/rivo/tview v0.0.0-20240524063012-037df494fb76 h1:iqvDlgyjmqleATtFbA7c14djmPh2n4mCYUv7JlD/ruA=
github.com/rivo/tview v0.0.0-20240524063012-037df494fb76/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down

0 comments on commit 23453c9

Please sign in to comment.