beads/cmd/bd/comments.go
Julian Knutsen eb872d33cf
Preflight graph apply blocking cycles (#4384)
* fix: preflight graph apply blocking cycles

* fix: maintainer review fixes for adopted graph-apply cycle PR

Keep dep/link dependency targets read-only: resolveIDForMutation is used
only for the mutated source issue, while the depends-on/link target is
resolved via read-only resolveIDWithRouting so resolving a routed target
never opens a foreign project writable or runs open-time migrations into
its history (bd-6dnrw.32, GH#3231).

Make the embedded graph-apply blocking-cycle preflight mirror the storage
SQL cycle check: its existing-edge walk is restricted to blocking dep
types, so a blocking edge whose return path runs only through an existing
parent-child or waits-for dep is no longer rejected when bd dep add allows
it. The parent->child blocking-path validator keeps the broader
AffectsReadyWork traversal.

Port the blocking-cycle preflight to the domain/proxied-server graph-apply
path and drop the now-redundant per-edge HasCycle probe so embedded and
server modes agree. Remove the dead graphApplyPlanCanSkipSQLCycleChecks
helpers left by the strategy change.

Restore the bulk --no-cycle-check whole-graph rollback test and add routed
read-only-target, parent-child parity, and domain external-ID cycle tests.

---------

Co-authored-by: Eddie the Engineer <ci@beads.test>
2026-06-19 21:31:59 -07:00

214 lines
5.5 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/uimd"
)
var commentsCmd = &cobra.Command{
Use: "comments [issue-id]",
GroupID: "issues",
Short: "View or manage comments on an issue",
Long: `View or manage comments on an issue.
Examples:
# List all comments on an issue (issue id is required — there is no "comments list")
bd comments bd-123
# List comments in JSON format
bd comments bd-123 --json
# Add a comment
bd comments add bd-123 "This is a comment"
# Add a comment from a file
bd comments add bd-123 -f notes.txt`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
localTime, _ := cmd.Flags().GetBool("local-time")
issueID := args[0]
if err := ensureStoreActive(); err != nil {
FatalErrorRespectJSON("getting comments: %v", err)
}
ctx := rootCtx
result, err := resolveAndGetIssueWithRouting(ctx, store, issueID)
if err != nil {
if result != nil {
result.Close()
}
FatalErrorRespectJSON("resolving %s: %v", issueID, err)
}
if result == nil || result.Issue == nil {
if result != nil {
result.Close()
}
FatalErrorRespectJSON("issue %s not found", issueID)
}
defer result.Close()
issueID = result.ResolvedID
comments, err := result.Store.GetIssueComments(ctx, issueID)
if err != nil {
FatalErrorRespectJSON("getting comments: %v", err)
}
// Normalize nil to empty slice for consistent JSON output
if comments == nil {
comments = make([]*types.Comment, 0)
}
if jsonOutput {
outputJSON(comments)
return
}
// Human-readable output
if len(comments) == 0 {
fmt.Printf("No comments on %s\n", issueID)
return
}
fmt.Printf("\nComments on %s:\n\n", issueID)
for _, comment := range comments {
ts := comment.CreatedAt
if localTime {
ts = ts.Local()
}
fmt.Printf("[%s] at %s\n", comment.Author, ts.Format("2006-01-02 15:04"))
rendered := uimd.RenderMarkdown(comment.Text)
// TrimRight removes trailing newlines that Glamour adds, preventing extra blank lines
for _, line := range strings.Split(strings.TrimRight(rendered, "\n"), "\n") {
fmt.Printf(" %s\n", line)
}
fmt.Println()
}
},
}
// commentsMisplacedListCmd catches the reflexive "bd comments list" invocation (GH#3542).
// Listing comments always requires an issue id: bd comments <issue-id>.
var commentsMisplacedListCmd = &cobra.Command{
Use: "list",
Short: "Invalid — use bd comments <issue-id> to list comments",
Run: func(cmd *cobra.Command, args []string) {
FatalErrorRespectJSON(`"bd comments list" is not valid.
To list comments on an issue, run:
bd comments <issue-id>
Example:
bd comments bd-123
See: bd comments --help`)
},
}
var commentsAddCmd = &cobra.Command{
Use: "add [issue-id] [text]",
Short: "Add a comment to an issue",
Long: `Add a comment to an issue.
Examples:
# Add a comment
bd comments add bd-123 "Working on this now"
# Add a comment from a file
bd comments add bd-123 -f notes.txt`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
CheckReadonly("comment add")
issueID := args[0]
// Get comment text from flag or argument
commentText, _ := cmd.Flags().GetString("file")
if commentText != "" {
// Read from file
data, err := os.ReadFile(commentText) // #nosec G304 - user-provided file path is intentional
if err != nil {
FatalErrorRespectJSON("reading file: %v", err)
}
commentText = string(data)
} else if len(args) < 2 {
FatalErrorRespectJSON("comment text required (use -f to read from file)")
} else {
commentText = args[1]
}
if strings.TrimSpace(commentText) == "" {
FatalErrorRespectJSON("comment text cannot be empty")
}
// Get author from author flag, or use git-aware default
author, _ := cmd.Flags().GetString("author")
if author == "" {
author = getActorWithGit()
}
if err := ensureStoreActive(); err != nil {
FatalErrorRespectJSON("adding comment: %v", err)
}
ctx := rootCtx
result, err := resolveAndGetIssueForMutation(ctx, store, issueID)
if err != nil {
if result != nil {
result.Close()
}
FatalErrorRespectJSON("resolving %s: %v", issueID, err)
}
if result == nil || result.Issue == nil {
if result != nil {
result.Close()
}
FatalErrorRespectJSON("issue %s not found", issueID)
}
defer result.Close()
issueID = result.ResolvedID
comment, err := result.Store.AddIssueComment(ctx, issueID, author, commentText)
if err != nil {
FatalErrorRespectJSON("adding comment: %v", err)
}
if err := commitPendingIfEmbedded(ctx, result.Store, actor, doltAutoCommitParams{
Command: "comments add",
IssueIDs: []string{issueID},
}); err != nil {
FatalErrorRespectJSON("failed to commit: %v", err)
}
if jsonOutput {
outputJSON(comment)
return
}
fmt.Printf("Comment added to %s\n", issueID)
},
}
func init() {
commentsCmd.AddCommand(commentsMisplacedListCmd)
commentsCmd.AddCommand(commentsAddCmd)
commentsCmd.Flags().Bool("local-time", false, "Show timestamps in local time instead of UTC")
commentsAddCmd.Flags().StringP("file", "f", "", "Read comment text from file")
commentsAddCmd.Flags().StringP("author", "a", "", "Add author to comment")
// Issue ID completions
commentsCmd.ValidArgsFunction = issueIDCompletion
commentsAddCmd.ValidArgsFunction = issueIDCompletion
rootCmd.AddCommand(commentsCmd)
}
func isUnknownOperationError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "unknown operation")
}