|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "text/tabwriter" |
| 7 | + |
| 8 | + gh "github.com/advanced-security/codeql-development-mcp-server/client/internal/github" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var listAlertsCmd = &cobra.Command{ |
| 13 | + Use: "list-alerts", |
| 14 | + Short: "List Code Scanning alerts for a repository", |
| 15 | + RunE: runListAlerts, |
| 16 | +} |
| 17 | + |
| 18 | +var listAlertsFlags struct { |
| 19 | + repo string |
| 20 | + ref string |
| 21 | + state string |
| 22 | + severity string |
| 23 | + toolName string |
| 24 | + sort string |
| 25 | + direction string |
| 26 | + perPage int |
| 27 | +} |
| 28 | + |
| 29 | +func init() { |
| 30 | + codeScanningCmd.AddCommand(listAlertsCmd) |
| 31 | + |
| 32 | + f := listAlertsCmd.Flags() |
| 33 | + f.StringVar(&listAlertsFlags.repo, "repo", "", "Repository in owner/repo format (required)") |
| 34 | + f.StringVar(&listAlertsFlags.ref, "ref", "", "Git ref to filter by") |
| 35 | + f.StringVar(&listAlertsFlags.state, "state", "", "Alert state: open, closed, dismissed, fixed") |
| 36 | + f.StringVar(&listAlertsFlags.severity, "severity", "", "Severity: critical, high, medium, low, warning, note, error") |
| 37 | + f.StringVar(&listAlertsFlags.toolName, "tool-name", "", "Tool name to filter by") |
| 38 | + f.StringVar(&listAlertsFlags.sort, "sort", "", "Sort by (created, updated)") |
| 39 | + f.StringVar(&listAlertsFlags.direction, "direction", "", "Sort direction (asc, desc)") |
| 40 | + f.IntVar(&listAlertsFlags.perPage, "per-page", 30, "Results per page (max 100)") |
| 41 | + |
| 42 | + _ = listAlertsCmd.MarkFlagRequired("repo") |
| 43 | +} |
| 44 | + |
| 45 | +func runListAlerts(cmd *cobra.Command, _ []string) error { |
| 46 | + owner, repo, err := parseRepo(listAlertsFlags.repo) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + client, err := gh.NewClient() |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + alerts, err := client.ListAlerts(gh.ListAlertsOptions{ |
| 57 | + Owner: owner, |
| 58 | + Repo: repo, |
| 59 | + Ref: listAlertsFlags.ref, |
| 60 | + State: listAlertsFlags.state, |
| 61 | + Severity: listAlertsFlags.severity, |
| 62 | + ToolName: listAlertsFlags.toolName, |
| 63 | + Sort: listAlertsFlags.sort, |
| 64 | + Direction: listAlertsFlags.direction, |
| 65 | + PerPage: listAlertsFlags.perPage, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + if OutputFormat() == "json" { |
| 72 | + enc := json.NewEncoder(cmd.OutOrStdout()) |
| 73 | + enc.SetIndent("", " ") |
| 74 | + return enc.Encode(alerts) |
| 75 | + } |
| 76 | + |
| 77 | + w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 4, 2, ' ', 0) |
| 78 | + fmt.Fprintln(w, "NUM\tSTATE\tRULE\tSEVERITY\tFILE:LINE\tCREATED") |
| 79 | + for _, a := range alerts { |
| 80 | + loc := a.MostRecentInstance.Location |
| 81 | + locStr := fmt.Sprintf("%s:%d", loc.Path, loc.StartLine) |
| 82 | + fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%s\n", |
| 83 | + a.Number, a.State, a.Rule.ID, a.Rule.Severity, locStr, a.CreatedAt) |
| 84 | + } |
| 85 | + return w.Flush() |
| 86 | +} |
0 commit comments