beads/cmd/bd/priority.go
coffeegoddd☕️✨ d7d7a34607 : refactor to use RunE, emit usage metrics, skip metrics in CI
(cherry picked from commit 9596c7233d)
2026-06-22 20:44:43 +00:00

108 lines
2.6 KiB
Go

package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/ui"
"github.com/steveyegge/beads/internal/validation"
)
var priorityCmd = &cobra.Command{
Use: "priority <id> <n>",
GroupID: "issues",
Short: "Set the priority of an issue",
Long: `Set the priority of an issue.
Shorthand for 'bd update <id> --priority <n>'.
Priority levels:
0 - Critical (security, data loss, broken builds)
1 - High (major features, important bugs)
2 - Medium (default)
3 - Low (polish, optimization)
4 - Backlog (future ideas)
Examples:
bd priority bd-123 0 # Critical
bd priority bd-123 2 # Medium`,
Args: cobra.ExactArgs(2),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
CheckReadonly("priority")
evt := metrics.NewCommandEvent("priority")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
id := args[0]
priorityStr := args[1]
priority, err := validation.ValidatePriority(priorityStr)
if err != nil {
return HandleErrorRespectJSON("%v", err)
}
ctx := rootCtx
result, err := resolveAndGetIssueForMutation(ctx, store, id)
if err != nil {
if result != nil {
result.Close()
}
return HandleErrorRespectJSON("resolving %s: %v", id, err)
}
if result == nil || result.Issue == nil {
if result != nil {
result.Close()
}
return HandleErrorRespectJSON("issue %s not found", id)
}
defer result.Close()
issueStore := result.Store
if err := validateIssueUpdatable(id, result.Issue); err != nil {
return HandleErrorRespectJSON("%s", err)
}
updates := map[string]interface{}{
"priority": priority,
}
if err := issueStore.UpdateIssue(ctx, result.ResolvedID, updates, actor); err != nil {
return HandleErrorRespectJSON("updating %s: %v", id, err)
}
if err := commitPendingIfEmbedded(ctx, issueStore, actor, doltAutoCommitParams{
Command: "priority",
IssueIDs: []string{result.ResolvedID},
}); err != nil {
return HandleErrorRespectJSON("failed to commit: %v", err)
}
SetLastTouchedID(result.ResolvedID)
updatedIssue, _ := issueStore.GetIssue(ctx, result.ResolvedID)
title := ""
if updatedIssue != nil {
title = updatedIssue.Title
}
if jsonOutput {
if updatedIssue != nil {
return outputJSON(updatedIssue)
}
return nil
}
fmt.Printf("%s Set priority of %s to P%d\n", ui.RenderPass("✓"), formatFeedbackID(result.ResolvedID, title), priority)
return nil
},
}
func init() {
priorityCmd.ValidArgsFunction = issueIDCompletion
rootCmd.AddCommand(priorityCmd)
}