Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Use GraphQL for Pull Request Models. #1712

Merged
merged 19 commits into from
Jun 21, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factor out creating threads to separate method.
  • Loading branch information
grokys committed May 31, 2018
commit 7e0485518baa13eaeafb4141c63d520f643fe3b6
117 changes: 74 additions & 43 deletions src/GitHub.InlineReviews/Services/PullRequestSessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,49 +348,7 @@ public virtual async Task<PullRequestDetailModel> ReadPullRequestDetail(HostAddr
Status = (PullRequestFileStatus)Enum.Parse(typeof(PullRequestFileStatus), file.Status, true),
}).ToList();

var commentsByReplyId = new Dictionary<string, List<CommentAdapter>>();

foreach (CommentAdapter comment in result.Reviews.SelectMany(x => x.Comments))
{
if (comment.ReplyTo == null)
{
commentsByReplyId.Add(comment.Id, new List<CommentAdapter> { comment });
}
}

foreach (CommentAdapter comment in result.Reviews.SelectMany(x => x.Comments))
{
if (comment.ReplyTo != null)
{
List<CommentAdapter> thread;

if (commentsByReplyId.TryGetValue(comment.ReplyTo, out thread))
{
thread.Add(comment);
}
}
}

var threads = new List<PullRequestReviewThreadModel>();

foreach (var thread in commentsByReplyId)
{
var c = thread.Value[0];
threads.Add(new PullRequestReviewThreadModel
{
Comments = thread.Value,
CommitSha = c.CommitSha,
DiffHunk = c.DiffHunk,
Id = c.Id,
IsOutdated = c.Position == null,
OriginalCommitSha = c.OriginalCommitId,
OriginalPosition = c.OriginalPosition,
Path = c.Path,
Position = c.Position,
});
}

result.Threads = threads;
BuildPullRequestThreads(result);
return result;
}

Expand Down Expand Up @@ -803,6 +761,79 @@ Task<IRepository> GetRepository(ILocalRepositoryModel repository)
return Task.Factory.StartNew(() => gitService.GetRepository(repository.LocalPath));
}

static void BuildPullRequestThreads(PullRequestDetailModel model)
{
var commentsByReplyId = new Dictionary<string, List<CommentAdapter>>();

// Get all comments that are not replies.
foreach (CommentAdapter comment in model.Reviews.SelectMany(x => x.Comments))
{
if (comment.ReplyTo == null)
{
commentsByReplyId.Add(comment.Id, new List<CommentAdapter> { comment });
}
}

// Get the comments that are replies and place them into the relevant list.
foreach (CommentAdapter comment in model.Reviews.SelectMany(x => x.Comments))
{
if (comment.ReplyTo != null)
{
var threadId = comment.ReplyTo;
List<CommentAdapter> thread;

while (threadId != null)
{
if (commentsByReplyId.TryGetValue(comment.ReplyTo, out thread))
{
thread.Add(comment);
break;
}
else
{
// If the comment that was replied to was not a top-level comment, then
// try to find the parent comment and get its `replyTo`.
threadId = model.Reviews
.SelectMany(x => x.Comments)
.Cast<CommentAdapter>()
.FirstOrDefault(x => x.Id == threadId)?.ReplyTo;
}
}
}
}

// Build a collection of threads for the information collected above.
var threads = new List<PullRequestReviewThreadModel>();

foreach (var threadSource in commentsByReplyId)
{
var adapter = threadSource.Value[0];

var thread = new PullRequestReviewThreadModel
{
Comments = threadSource.Value,
CommitSha = adapter.CommitSha,
DiffHunk = adapter.DiffHunk,
Id = adapter.Id,
IsOutdated = adapter.Position == null,
OriginalCommitSha = adapter.OriginalCommitId,
OriginalPosition = adapter.OriginalPosition,
Path = adapter.Path,
Position = adapter.Position,
};

// Set a reference to the thread in the comment.
foreach (var comment in threadSource.Value)
{
comment.Thread = thread;
}

threads.Add(thread);
}

model.Threads = threads;
}

static GitHub.Models.PullRequestReviewState FromGraphQL(Octokit.GraphQL.Model.PullRequestReviewState s)
{
return (GitHub.Models.PullRequestReviewState)s;
Expand Down