diff --git a/cmd/opencodereview/output.go b/cmd/opencodereview/output.go index d1da56d..b256fb4 100644 --- a/cmd/opencodereview/output.go +++ b/cmd/opencodereview/output.go @@ -132,8 +132,32 @@ func buildDiffLines(comment model.LlmComment) []suggestdiff.DiffLine { return suggestdiff.ComputeLineDiff(oldLines, newLines) } +type jsonOutput struct { + Status string `json:"status"` + Message string `json:"message,omitempty"` + Comments []model.LlmComment `json:"comments"` +} + func outputJSON(comments []model.LlmComment) error { + out := jsonOutput{ + Status: "success", + Comments: comments, + } + if len(comments) == 0 { + out.Message = "No comments generated." + } enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") - return enc.Encode(comments) + return enc.Encode(out) +} + +func outputJSONNoFiles() error { + out := jsonOutput{ + Status: "skipped", + Message: "No supported files changed.", + Comments: []model.LlmComment{}, + } + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + return enc.Encode(out) } diff --git a/cmd/opencodereview/review_cmd.go b/cmd/opencodereview/review_cmd.go index 95c8087..8fda2fd 100644 --- a/cmd/opencodereview/review_cmd.go +++ b/cmd/opencodereview/review_cmd.go @@ -133,6 +133,11 @@ func runReview(args []string) error { telemetry.RecordCommentsGenerated(ctx, int64(len(comments))) } + // If no files were reviewed (e.g. workspace has no changes), inform the caller in JSON mode. + if opts.outputFormat == "json" && len(comments) == 0 && ag.FilesReviewed() == 0 { + return outputJSONNoFiles() + } + telemetry.PrintTraceSummary(ag.FilesReviewed(), int64(len(comments)), ag.TotalInputTokens(), ag.TotalOutputTokens(), ag.TotalTokensUsed(), duration) if opts.outputFormat == "json" { diff --git a/internal/agent/agent.go b/internal/agent/agent.go index cb5e173..ca42d28 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -189,7 +189,7 @@ func (a *Agent) Run(ctx context.Context) ([]model.LlmComment, error) { fmt.Fprintln(stdout.Writer(), "[ocr] No supported files changed. Skipping review.") telemetry.Event(ctx, "no.files.changed") a.session.Finalize() - return nil, nil + return []model.LlmComment{}, nil } a.currentDate = time.Now().Format("2006-01-02 15:04")