Esempio n. 1
0
// WriteNewReports takes a list of CI reports read from GitHub, and writes to the repo any that are new.
//
// The passed in logChan variable is used as our intermediary for logging, and allows us to
// use the same logic for logging messages in either our CLI or our App Engine apps, even though
// the two have different logging frameworks.
func WriteNewReports(reportsMap map[string][]ci.Report, repo repository.Repo, logChan chan<- string) error {
	for commit, commitReports := range reportsMap {
		existingReports := ci.ParseAllValid(repo.GetNotes(ci.Ref, commit))
		for _, report := range commitReports {
			bytes, err := json.Marshal(report)
			note := repository.Note(bytes)
			if err != nil {
				return err
			}
			missing := true
			for _, existing := range existingReports {
				if existing == report {
					missing = false
				}
			}
			if missing {
				logChan <- fmt.Sprintf("Found a new report for %.12s: %q", commit, string(bytes))
				if err := repo.AppendNote(ci.Ref, commit, note); err != nil {
					return err
				}
			}
		}
	}
	return nil
}
Esempio n. 2
0
// GetSummary returns the summary of the specified code review.
//
// If no review request exists, the returned review summary is nil.
func GetSummary(repo repository.Repo, revision string) (*Summary, error) {
	requestNotes := repo.GetNotes(request.Ref, revision)
	commentNotes := repo.GetNotes(comment.Ref, revision)
	summary, err := getSummaryFromNotes(repo, revision, requestNotes, commentNotes)
	if err != nil {
		return nil, err
	}
	currentCommit := revision
	if summary.Request.Alias != "" {
		currentCommit = summary.Request.Alias
	}
	submitted, err := repo.IsAncestor(currentCommit, summary.Request.TargetRef)
	if err != nil {
		return nil, err
	}
	summary.Submitted = submitted
	return summary, nil
}
Esempio n. 3
0
// Get returns the specified code review.
//
// If no review request exists, the returned review is nil.
func Get(repo repository.Repo, revision string) *Review {
	requestNotes := repo.GetNotes(request.Ref, revision)
	requests := request.ParseAllValid(requestNotes)
	if requests == nil {
		return nil
	}
	review := Review{
		Repo:     repo,
		Revision: revision,
		Request:  requests[len(requests)-1],
	}
	review.Comments = review.loadComments()
	review.Resolved = updateThreadsStatus(review.Comments)
	review.Submitted = repo.IsAncestor(revision, review.Request.TargetRef)
	// TODO(ojarjur): Optionally fetch the CI status of the last commit
	// in the review for which there are comments.
	return &review
}
Esempio n. 4
0
// Get returns the summary of the specified code review.
//
// If no review request exists, the returned review summary is nil.
func GetSummary(repo repository.Repo, revision string) (*ReviewSummary, error) {
	requestNotes := repo.GetNotes(request.Ref, revision)
	requests := request.ParseAllValid(requestNotes)
	if requests == nil {
		return nil, nil
	}
	reviewSummary := ReviewSummary{
		Repo:     repo,
		Revision: revision,
		Request:  requests[len(requests)-1],
	}
	reviewSummary.Comments = reviewSummary.loadComments()
	reviewSummary.Resolved = updateThreadsStatus(reviewSummary.Comments)
	submitted, err := repo.IsAncestor(revision, reviewSummary.Request.TargetRef)
	if err != nil {
		return nil, err
	}
	reviewSummary.Submitted = submitted
	return &reviewSummary, nil
}
Esempio n. 5
0
// GetCurrent returns the current, open code review.
//
// If there are multiple matching reviews, then an error is returned.
func GetCurrent(repo repository.Repo) (*Review, error) {
	reviewRef := repo.GetHeadRef()
	currentCommit := repo.GetCommitHash(reviewRef)
	var matchingReviews []Review
	for _, review := range ListOpen(repo) {
		if review.Request.ReviewRef == reviewRef {
			matchingReviews = append(matchingReviews, review)
		}
	}
	if matchingReviews == nil {
		return nil, nil
	}
	if len(matchingReviews) != 1 {
		return nil, fmt.Errorf("There are %d open reviews for the ref \"%s\"", len(matchingReviews), reviewRef)
	}
	r := &matchingReviews[0]
	reports := ci.ParseAllValid(repo.GetNotes(ci.Ref, currentCommit))
	r.Reports = reports
	return r, nil
}
Esempio n. 6
0
// WriteNewComments takes a list of review comments read from GitHub, and writes to the repo any that are new.
//
// The passed in logChan variable is used as our intermediary for logging, and allows us to
// use the same logic for logging messages in either our CLI or our App Engine apps, even though
// the two have different logging frameworks.
func WriteNewComments(r review.Review, repo repository.Repo, logChan chan<- string) error {
	existingComments := comment.ParseAllValid(repo.GetNotes(comment.Ref, r.Revision))
	for _, commentThread := range r.Comments {
		commentNote, err := commentThread.Comment.Write()
		if err != nil {
			return err
		}
		missing := true
		for _, existing := range existingComments {
			if CommentsOverlap(existing, commentThread.Comment) {
				missing = false
			}
		}
		if missing {
			logChan <- fmt.Sprintf("Found a new comment: %q", string(commentNote))
			if err := repo.AppendNote(comment.Ref, r.Revision, commentNote); err != nil {
				return err
			}
		}
	}
	return nil
}
Esempio n. 7
0
// Get returns the specified code review.
//
// If no review request exists, the returned review is nil.
func Get(repo repository.Repo, revision string) *Review {
	requestNotes := repo.GetNotes(request.Ref, revision)
	requests := request.ParseAllValid(requestNotes)
	if requests == nil {
		return nil
	}
	review := Review{
		Repo:     repo,
		Revision: revision,
		Request:  requests[len(requests)-1],
	}
	review.Comments = review.loadComments()
	review.Resolved = updateThreadsStatus(review.Comments)
	review.Submitted = repo.IsAncestor(revision, review.Request.TargetRef)
	currentCommit, err := review.GetHeadCommit()
	if err == nil {
		review.Reports = ci.ParseAllValid(repo.GetNotes(ci.Ref, currentCommit))
		review.Analyses = analyses.ParseAllValid(repo.GetNotes(analyses.Ref, currentCommit))
	}
	return &review
}