Browse Source

Add support for printing the JSON encoding of detached comments

Omar Jarjur 11 months ago
parent
commit
1c2e154de4
3 changed files with 20 additions and 2 deletions
  1. 10 0
      commands/output/output.go
  2. 1 2
      commands/show.go
  3. 9 0
      review/review.go

+ 10 - 0
commands/output/output.go

@@ -210,6 +210,16 @@ func PrintDetails(r *review.Review) error {
 	return nil
 }
 
+// PrintCommentsJSON pretty prints the given review in JSON format.
+func PrintCommentsJSON(c []review.CommentThread) error {
+	json, err := review.GetCommentsJSON(c)
+	if err != nil {
+		return err
+	}
+	fmt.Println(json)
+	return nil
+}
+
 // PrintJSON pretty prints the given review in JSON format.
 func PrintJSON(r *review.Review) error {
 	json, err := r.GetJSON()

+ 1 - 2
commands/show.go

@@ -52,8 +52,7 @@ func showDetachedComments(repo repository.Repo, args []string) error {
 		return fmt.Errorf("Failed to load the comments for %q: %v\n", path, err)
 	}
 	if *showJSONOutput {
-		return errors.New("Not yet implemented")
-		//return output.PrintJSON(r)
+		return output.PrintCommentsJSON(comments)
 	}
 	return output.PrintComments(repo, comments)
 }

+ 9 - 0
review/review.go

@@ -536,6 +536,15 @@ func prettyPrintJSON(jsonBytes []byte) (string, error) {
 	return prettyBytes.String(), nil
 }
 
+// GetCommentsJSON returns the pretty printed JSON for a slice of comment threads.
+func GetCommentsJSON(cs []CommentThread) (string, error) {
+	jsonBytes, err := json.Marshal(cs)
+	if err != nil {
+		return "", err
+	}
+	return prettyPrintJSON(jsonBytes)
+}
+
 // GetJSON returns the pretty printed JSON for a review summary.
 func (r *Summary) GetJSON() (string, error) {
 	jsonBytes, err := json.Marshal(*r)