Repositories

Modified commands/list.go

@@ -19,6 +19,7 @@ package commands
import (
"flag"
"fmt"
"github.com/google/git-appraise/commands/output"
"github.com/google/git-appraise/repository"
"github.com/google/git-appraise/review"
)
@@ -42,7 +43,7 @@ func listReviews(repo repository.Repo, args []string) {
fmt.Printf("Loaded %d open reviews:\n", len(reviews))
}
for _, r := range reviews {
r.PrintSummary()
output.PrintSummary(&r)
}
}

Added commands/output/output.go

@@ -0,0 +1,204 @@
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package output contains helper methods for pretty-printing code reviews.
package output
import (
"fmt"
"github.com/google/git-appraise/review"
"strconv"
"strings"
"time"
)
const (
// Template for printing the summary of a code review.
reviewSummaryTemplate = `[%s] %.12s
%s
`
// Template for printing the summary of a code review.
reviewDetailsTemplate = ` %q -> %q
reviewers: %q
requester: %q
build status: %s
`
// Template for printing the location of an inline comment
commentLocationTemplate = `%s%[email protected]%.12s
`
// Template for printing a single comment.
commentTemplate = `comment: %s
author: %s
time: %s
status: %s
%s`
// Template for displaying the summary of the comment threads for a review
commentSummaryTemplate = ` comments (%d threads):
`
// Number of lines of context to print for inline comments
contextLineCount = 5
)
// PrintSummary prints a single-line summary of a review.
func PrintSummary(r *review.Review) {
statusString := "pending"
if r.Resolved != nil {
if *r.Resolved {
if r.Submitted {
statusString = "submitted"
} else {
statusString = "accepted"
}
} else {
if r.Submitted {
statusString = "danger"
} else {
statusString = "rejected"
}
}
} else if r.Submitted {
statusString = "tbr"
}
indentedDescription := strings.Replace(r.Request.Description, "\n", "\n ", -1)
fmt.Printf(reviewSummaryTemplate, statusString, r.Revision, indentedDescription)
}
// reformatTimestamp takes a timestamp string of the form "0123456789" and changes it
// to the form "Mon Jan _2 13:04:05 UTC 2006".
//
// Timestamps that are not in the format we expect are left alone.
func reformatTimestamp(timestamp string) string {
parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
// The timestamp is an unexpected format, so leave it alone
return timestamp
}
t := time.Unix(parsedTimestamp, 0)
return t.Format(time.UnixDate)
}
// showThread prints the detailed output for an entire comment thread.
func showThread(r *review.Review, thread review.CommentThread) error {
comment := thread.Comment
indent := " "
if comment.Location != nil && comment.Location.Path != "" && comment.Location.Range != nil && comment.Location.Range.StartLine > 0 {
contents, err := r.Repo.Show(comment.Location.Commit, comment.Location.Path)
if err != nil {
return err
}
lines := strings.Split(contents, "\n")
if comment.Location.Range.StartLine <= uint32(len(lines)) {
var firstLine uint32 = 0
lastLine := comment.Location.Range.StartLine
if lastLine > contextLineCount {
firstLine = lastLine - contextLineCount
}
fmt.Printf(commentLocationTemplate, indent, comment.Location.Path, comment.Location.Commit)
fmt.Println(indent + "|" + strings.Join(lines[firstLine:lastLine], "\n"+indent+"|"))
}
}
return showSubThread(r, thread, indent)
}
// showSubThread prints the given comment (sub)thread, indented by the given prefix string.
func showSubThread(r *review.Review, thread review.CommentThread, indent string) error {
statusString := "fyi"
if thread.Resolved != nil {
if *thread.Resolved {
statusString = "lgtm"
} else {
statusString = "needs work"
}
}
comment := thread.Comment
threadHash, err := comment.Hash()
if err != nil {
return err
}
timestamp := reformatTimestamp(comment.Timestamp)
commentSummary := fmt.Sprintf(indent+commentTemplate, threadHash, comment.Author, timestamp, statusString, comment.Description)
indent = indent + " "
indentedSummary := strings.Replace(commentSummary, "\n", "\n"+indent, -1)
fmt.Println(indentedSummary)
for _, child := range thread.Children {
err := showSubThread(r, child, indent)
if err != nil {
return err
}
}
return nil
}
// printAnalyses prints the static analysis results for the latest commit in the review.
func printAnalyses(r *review.Review) {
analysesNotes, err := r.GetAnalysesNotes()
if err != nil {
fmt.Println(" analyses: ", err)
return
}
if analysesNotes == nil {
fmt.Println(" analyses: passed")
return
}
fmt.Printf(" analyses: %d warnings\n", len(analysesNotes))
// TODO(ojarjur): Print the actual notes
}
// printComments prints all of the comments for the review, with snippets of the preceding source code.
func printComments(r *review.Review) error {
fmt.Printf(commentSummaryTemplate, len(r.Comments))
for _, thread := range r.Comments {
err := showThread(r, thread)
if err != nil {
return err
}
}
return nil
}
// PrintDetails prints a multi-line overview of a review, including all comments.
func PrintDetails(r *review.Review) error {
PrintSummary(r)
fmt.Printf(reviewDetailsTemplate, r.Request.ReviewRef, r.Request.TargetRef,
strings.Join(r.Request.Reviewers, ", "),
r.Request.Requester, r.GetBuildStatusMessage())
printAnalyses(r)
if err := printComments(r); err != nil {
return err
}
return nil
}
// PrintJson pretty prints the given review in JSON format.
func PrintJson(r *review.Review) error {
json, err := r.GetJson()
if err != nil {
return err
}
fmt.Println(json)
return nil
}
// PrintDiff prints the diff of the review.
func PrintDiff(r *review.Review, diffArgs ...string) error {
diff, err := r.GetDiff(diffArgs...)
if err != nil {
return err
}
fmt.Println(diff)
return nil
}

Modified commands/show.go

@@ -20,6 +20,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/google/git-appraise/commands/output"
"github.com/google/git-appraise/repository"
"github.com/google/git-appraise/review"
"strings"
@@ -57,16 +58,16 @@ func showReview(repo repository.Repo, args []string) error {
return errors.New("There is no matching review.")
}
if *showJsonOutput {
return r.PrintJson()
return output.PrintJson(r)
}
if *showDiffOutput {
var diffArgs []string
if *showDiffOptions != "" {
diffArgs = strings.Split(*showDiffOptions, ",")
}
return r.PrintDiff(diffArgs...)
return output.PrintDiff(r, diffArgs...)
}
return r.PrintDetails()
return output.PrintDetails(r)
}
// showCmd defines the "show" subcommand.

Modified review/review.go

@@ -27,36 +27,6 @@ import (
"github.com/google/git-appraise/review/comment"
"github.com/google/git-appraise/review/request"
"sort"
"strconv"
"strings"
"time"
)
const (
// Template for printing the summary of a code review.
reviewSummaryTemplate = `[%s] %.12s
%s
`
// Template for printing the summary of a code review.
reviewDetailsTemplate = ` %q -> %q
reviewers: %q
requester: %q
build status: %s
`
// Template for printing the location of an inline comment
commentLocationTemplate = `%s%[email protected]%.12s
`
// Template for printing a single comment.
commentTemplate = `comment: %s
author: %s
time: %s
status: %s
%s`
// Template for displaying the summary of the comment threads for a review
commentSummaryTemplate = ` comments (%d threads):
`
// Number of lines of context to print for inline comments
contextLineCount = 5
)
// CommentThread represents the tree-based hierarchy of comments.
@@ -277,97 +247,6 @@ func GetCurrent(repo repository.Repo) (*Review, error) {
return r, nil
}
// PrintSummary prints a single-line summary of a review.
func (r *Review) PrintSummary() {
statusString := "pending"
if r.Resolved != nil {
if *r.Resolved {
if r.Submitted {
statusString = "submitted"
} else {
statusString = "accepted"
}
} else {
if r.Submitted {
statusString = "danger"
} else {
statusString = "rejected"
}
}
} else if r.Submitted {
statusString = "tbr"
}
indentedDescription := strings.Replace(r.Request.Description, "\n", "\n ", -1)
fmt.Printf(reviewSummaryTemplate, statusString, r.Revision, indentedDescription)
}
// reformatTimestamp takes a timestamp string of the form "0123456789" and changes it
// to the form "Mon Jan _2 13:04:05 UTC 2006".
//
// Timestamps that are not in the format we expect are left alone.
func reformatTimestamp(timestamp string) string {
parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
// The timestamp is an unexpected format, so leave it alone
return timestamp
}
t := time.Unix(parsedTimestamp, 0)
return t.Format(time.UnixDate)
}
// showThread prints the detailed output for an entire comment thread.
func (r *Review) showThread(thread CommentThread) error {
comment := thread.Comment
indent := " "
if comment.Location != nil && comment.Location.Path != "" && comment.Location.Range != nil && comment.Location.Range.StartLine > 0 {
contents, err := r.Repo.Show(comment.Location.Commit, comment.Location.Path)
if err != nil {
return err
}
lines := strings.Split(contents, "\n")
if comment.Location.Range.StartLine <= uint32(len(lines)) {
var firstLine uint32 = 0
lastLine := comment.Location.Range.StartLine
if lastLine > contextLineCount {
firstLine = lastLine - contextLineCount
}
fmt.Printf(commentLocationTemplate, indent, comment.Location.Path, comment.Location.Commit)
fmt.Println(indent + "|" + strings.Join(lines[firstLine:lastLine], "\n"+indent+"|"))
}
}
return r.showSubThread(thread, indent)
}
// showSubThread prints the given comment (sub)thread, indented by the given prefix string.
func (r *Review) showSubThread(thread CommentThread, indent string) error {
statusString := "fyi"
if thread.Resolved != nil {
if *thread.Resolved {
statusString = "lgtm"
} else {
statusString = "needs work"
}
}
comment := thread.Comment
threadHash, err := comment.Hash()
if err != nil {
return err
}
timestamp := reformatTimestamp(comment.Timestamp)
commentSummary := fmt.Sprintf(indent+commentTemplate, threadHash, comment.Author, timestamp, statusString, comment.Description)
indent = indent + " "
indentedSummary := strings.Replace(commentSummary, "\n", "\n"+indent, -1)
fmt.Println(indentedSummary)
for _, child := range thread.Children {
err := r.showSubThread(child, indent)
if err != nil {
return err
}
}
return nil
}
// GetBuildStatusMessage returns a string of the current build-and-test status
// of the review, or "unknown" if the build-and-test status cannot be determined.
func (r *Review) GetBuildStatusMessage() string {
@@ -403,59 +282,18 @@ func (r *Review) GetAnalysesNotes() ([]analyses.Note, error) {
return analysesNotes, nil
}
// printAnalyses prints the static analysis results for the latest commit in the review.
func (r *Review) printAnalyses() {
analysesNotes, err := r.GetAnalysesNotes()
if err != nil {
fmt.Println(" analyses: ", err)
return
}
if analysesNotes == nil {
fmt.Println(" analyses: passed")
return
}
fmt.Printf(" analyses: %d warnings\n", len(analysesNotes))
// TODO(ojarjur): Print the actual notes
}
// printComments prints all of the comments for the review, with snippets of the preceding source code.
func (r *Review) printComments() error {
fmt.Printf(commentSummaryTemplate, len(r.Comments))
for _, thread := range r.Comments {
err := r.showThread(thread)
if err != nil {
return err
}
}
return nil
}
// PrintDetails prints a multi-line overview of a review, including all comments.
func (r *Review) PrintDetails() error {
r.PrintSummary()
fmt.Printf(reviewDetailsTemplate, r.Request.ReviewRef, r.Request.TargetRef,
strings.Join(r.Request.Reviewers, ", "),
r.Request.Requester, r.GetBuildStatusMessage())
r.printAnalyses()
if err := r.printComments(); err != nil {
return err
}
return nil
}
// PrintJson pretty prints a review (including comments) formatted as JSON.
func (r *Review) PrintJson() error {
// GetJson returns the pretty printed JSON for a review.
func (r *Review) GetJson() (string, error) {
jsonBytes, err := json.Marshal(*r)
if err != nil {
return err
return "", err
}
var prettyBytes bytes.Buffer
err = json.Indent(&prettyBytes, jsonBytes, "", " ")
if err != nil {
return err
return "", err
}
fmt.Println(prettyBytes.String())
return nil
return prettyBytes.String(), nil
}
// findLastCommit returns the later (newest) commit from the union of the provided commit
@@ -539,17 +377,17 @@ func (r *Review) GetBaseCommit() (string, error) {
return r.Repo.MergeBase(leftHandSide, rightHandSide), nil
}
// PrintDiff displays the diff for a review.
func (r *Review) PrintDiff(diffArgs ...string) error {
// GetDiff returns the diff for a review.
func (r *Review) GetDiff(diffArgs ...string) (string, error) {
var baseCommit, headCommit string
baseCommit, err := r.GetBaseCommit()
if err == nil {
headCommit, err = r.GetHeadCommit()
}
if err == nil {
fmt.Println(r.Repo.Diff(baseCommit, headCommit, diffArgs...))
return r.Repo.Diff(baseCommit, headCommit, diffArgs...), nil
}
return err
return "", err
}
// AddComment adds the given comment to the review.