/// <inheritdoc/>
        public IReadOnlyList <IInlineCommentThreadModel> BuildCommentThreads(
            IPullRequestModel pullRequest,
            string relativePath,
            IReadOnlyList <DiffChunk> diff)
        {
            relativePath = relativePath.Replace("\\", "/");

            var commentsByPosition = pullRequest.ReviewComments
                                     .Where(x => x.Path == relativePath && x.OriginalPosition.HasValue)
                                     .OrderBy(x => x.Id)
                                     .GroupBy(x => Tuple.Create(x.OriginalCommitId, x.OriginalPosition.Value));
            var threads = new List <IInlineCommentThreadModel>();

            foreach (var comments in commentsByPosition)
            {
                var hunk      = comments.First().DiffHunk;
                var chunks    = DiffUtilities.ParseFragment(hunk);
                var chunk     = chunks.Last();
                var diffLines = chunk.Lines.Reverse().Take(5).ToList();
                var thread    = new InlineCommentThreadModel(
                    relativePath,
                    comments.Key.Item1,
                    comments.Key.Item2,
                    diffLines,
                    comments);
                threads.Add(thread);
            }

            UpdateCommentThreads(threads, diff);
            return(threads);
        }
Beispiel #2
0
        /// <inheritdoc/>
        public IReadOnlyList <IInlineCommentThreadModel> BuildCommentThreads(
            PullRequestDetailModel pullRequest,
            string relativePath,
            IReadOnlyList <DiffChunk> diff,
            string headSha)
        {
            relativePath = relativePath.Replace("\\", "/");

            var threadsByPosition = pullRequest.Threads
                                    .Where(x => x.Path == relativePath && !x.IsOutdated)
                                    .OrderBy(x => x.Id)
                                    .GroupBy(x => Tuple.Create(x.OriginalCommitSha, x.OriginalPosition));
            var threads = new List <IInlineCommentThreadModel>();

            foreach (var thread in threadsByPosition)
            {
                var hunk      = thread.First().DiffHunk;
                var chunks    = DiffUtilities.ParseFragment(hunk);
                var chunk     = chunks.Last();
                var diffLines = chunk.Lines.Reverse().Take(5).ToList();
                var firstLine = diffLines.FirstOrDefault();
                if (firstLine == null)
                {
                    log.Warning("Ignoring in-line comment in {RelativePath} with no diff line context", relativePath);
                    continue;
                }

                var inlineThread = new InlineCommentThreadModel(
                    relativePath,
                    headSha,
                    diffLines,
                    thread.SelectMany(t => t.Comments.Select(c => new InlineCommentModel
                {
                    Comment = c,
                    Review  = pullRequest.Reviews.FirstOrDefault(x => x.Comments.Contains(c)),
                })));
                threads.Add(inlineThread);
            }

            UpdateCommentThreads(threads, diff);
            return(threads);
        }
        async Task UpdateFile(PullRequestSessionFile file)
        {
            // NOTE: We must call GetPullRequestMergeBase before GetFileContent.
            var mergeBaseSha = await service.GetPullRequestMergeBase(LocalRepository, PullRequest);

            var headSha = await CalculateHeadSha();

            var content = await GetFileContent(file);

            file.BaseSha   = PullRequest.Base.Sha;
            file.CommitSha = await CalculateContentCommitSha(file, content);

            file.Diff = await service.Diff(LocalRepository, mergeBaseSha, headSha, file.RelativePath, content);

            var commentsByPosition = PullRequest.ReviewComments
                                     .Where(x => x.Path == file.RelativePath && x.OriginalPosition.HasValue)
                                     .OrderBy(x => x.Id)
                                     .GroupBy(x => Tuple.Create(x.OriginalCommitId, x.OriginalPosition.Value));
            var threads = new List <IInlineCommentThreadModel>();

            foreach (var comments in commentsByPosition)
            {
                var hunk      = comments.First().DiffHunk;
                var chunks    = DiffUtilities.ParseFragment(hunk);
                var chunk     = chunks.Last();
                var diffLines = chunk.Lines.Reverse().Take(5).ToList();
                var thread    = new InlineCommentThreadModel(
                    file.RelativePath,
                    comments.Key.Item1,
                    comments.Key.Item2,
                    diffLines,
                    comments);

                thread.LineNumber = GetUpdatedLineNumber(thread, file.Diff);
                threads.Add(thread);
            }

            file.InlineCommentThreads = threads;
        }
        /// <inheritdoc/>
        public IReadOnlyList <IInlineCommentThreadModel> BuildCommentThreads(
            IPullRequestModel pullRequest,
            string relativePath,
            IReadOnlyList <DiffChunk> diff,
            string headSha)
        {
            relativePath = relativePath.Replace("\\", "/");

            var commentsByPosition = pullRequest.ReviewComments
                                     .Where(x => x.Path == relativePath && x.OriginalPosition.HasValue)
                                     .OrderBy(x => x.Id)
                                     .GroupBy(x => Tuple.Create(x.OriginalCommitId, x.OriginalPosition.Value));
            var threads = new List <IInlineCommentThreadModel>();

            foreach (var comments in commentsByPosition)
            {
                var hunk      = comments.First().DiffHunk;
                var chunks    = DiffUtilities.ParseFragment(hunk);
                var chunk     = chunks.Last();
                var diffLines = chunk.Lines.Reverse().Take(5).ToList();
                var firstLine = diffLines.FirstOrDefault();
                if (firstLine == null)
                {
                    log.Warning("Ignoring in-line comment in {RelativePath} with no diff line context", relativePath);
                    continue;
                }

                var thread = new InlineCommentThreadModel(
                    relativePath,
                    headSha,
                    diffLines,
                    comments);
                threads.Add(thread);
            }

            UpdateCommentThreads(threads, diff);
            return(threads);
        }