dmitri.shuralyov.com/service/change/...

githubapi: Use timeline field to fetch comments.

This simplifies things. No need to fetch them out of band.
dmitshur committed 7 years ago commit 78830e26a094378ac19c22daa80deedf1380896b
Collapse all
githubapi/githubapi.go
@@ -271,11 +271,21 @@ func (s service) ListTimeline(ctx context.Context, rs string, id uint64, opt *ch
				ReactionGroups  reactionGroups
				ViewerCanUpdate bool

				Timeline struct {
					Nodes []struct {
						Typename    string `graphql:"__typename"`
						Typename     string `graphql:"__typename"`
						IssueComment struct {
							DatabaseID      uint64
							Author          githubqlActor
							PublishedAt     githubql.DateTime
							LastEditedAt    *githubql.DateTime
							Editor          *githubqlActor
							Body            string
							ReactionGroups  reactionGroups
							ViewerCanUpdate bool
						} `graphql:"...on IssueComment"`
						ClosedEvent struct {
							event
						} `graphql:"...on ClosedEvent"`
						ReopenedEvent struct {
							event
@@ -328,22 +338,10 @@ func (s service) ListTimeline(ctx context.Context, rs string, id uint64, opt *ch
							CreatedAt githubql.DateTime
							State     githubql.PullRequestReviewState
						} `graphql:"...on PullRequestReview"`
					}
				} `graphql:"timeline(first:100)"` // TODO: Pagination...
				Comments struct {
					Nodes []struct {
						DatabaseID      githubql.Int
						Author          githubqlActor
						PublishedAt     githubql.DateTime
						LastEditedAt    *githubql.DateTime
						Editor          *githubqlActor
						Body            githubql.String
						ReactionGroups  reactionGroups
						ViewerCanUpdate bool
					}
				} `graphql:"comments(first:100)"` // TODO: Pagination...
				Reviews struct {
					Nodes []struct {
						DatabaseID      uint64
						Author          githubqlActor
						PublishedAt     githubql.DateTime
@@ -387,11 +385,15 @@ func (s service) ListTimeline(ctx context.Context, rs string, id uint64, opt *ch
			Body:      string(pr.Body),
			Reactions: reactions,
			Editable:  pr.ViewerCanUpdate,
		})
	}
	for _, comment := range q.Repository.PullRequest.Comments.Nodes {
	for _, node := range q.Repository.PullRequest.Timeline.Nodes {
		if node.Typename != "IssueComment" {
			continue
		}
		comment := node.IssueComment
		reactions, err := s.reactions(comment.ReactionGroups)
		if err != nil {
			return timeline, err
		}
		var edited *changes.Edited
@@ -400,15 +402,15 @@ func (s service) ListTimeline(ctx context.Context, rs string, id uint64, opt *ch
				By: ghActor(*comment.Editor),
				At: comment.LastEditedAt.Time,
			}
		}
		timeline = append(timeline, changes.Comment{
			ID:        uint64(comment.DatabaseID),
			ID:        comment.DatabaseID,
			User:      ghActor(comment.Author),
			CreatedAt: comment.PublishedAt.Time,
			Edited:    edited,
			Body:      string(comment.Body),
			Body:      comment.Body,
			Reactions: reactions,
			Editable:  comment.ViewerCanUpdate,
		})
	}
	for _, review := range q.Repository.PullRequest.Reviews.Nodes {
@@ -611,11 +613,11 @@ func ghColor(hex string) issues.RGB {

type reactionGroups []struct {
	Content githubql.ReactionContent
	Users   struct {
		Nodes      []githubqlActor
		TotalCount githubql.Int
		TotalCount int
	} `graphql:"users(first:10)"`
	ViewerHasReacted bool
}

// reactions converts []githubql.ReactionGroup to []reactions.Reaction.
@@ -627,11 +629,11 @@ func (s service) reactions(rgs reactionGroups) ([]reactions.Reaction, error) {
		}

		// Only return the details of first few users and authed user.
		var us []users.User
		addedAuthedUser := false
		for i := 0; i < int(rg.Users.TotalCount); i++ {
		for i := 0; i < rg.Users.TotalCount; i++ {
			if i < len(rg.Users.Nodes) {
				actor := ghActor(rg.Users.Nodes[i])
				us = append(us, actor)
				if s.currentUser.ID != 0 && actor.UserSpec == s.currentUser.UserSpec {
					addedAuthedUser = true