Esempio n. 1
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)
                                    .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;
        }
            public void ShouldParsePr960()
            {
                var target = new DiffService(Substitute.For <IGitClient>());
                var result = DiffUtilities.ParseFragment(Properties.Resources.pr_960_diff).ToList();

                Assert.That(4, Is.EqualTo(result.Count));

                Assert.That(11, Is.EqualTo(result[0].OldLineNumber));
                Assert.That(11, Is.EqualTo(result[0].NewLineNumber));
                Assert.That(24, Is.EqualTo(result[0].Lines.Count));

                Assert.That(61, Is.EqualTo(result[1].OldLineNumber));
                Assert.That(61, Is.EqualTo(result[1].NewLineNumber));
                Assert.That(21, Is.EqualTo(result[1].Lines.Count));

                Assert.That(244, Is.EqualTo(result[2].OldLineNumber));
                Assert.That(247, Is.EqualTo(result[2].NewLineNumber));
                Assert.That(15, Is.EqualTo(result[2].Lines.Count));

                Assert.That(268, Is.EqualTo(result[3].OldLineNumber));
                Assert.That(264, Is.EqualTo(result[3].NewLineNumber));
                Assert.That(15, Is.EqualTo(result[3].Lines.Count));

                // -    public class UsageTracker : IUsageTracker
                Assert.That(17, Is.EqualTo(result[0].Lines[7].OldLineNumber));
                Assert.That(-1, Is.EqualTo(result[0].Lines[7].NewLineNumber));
                Assert.That(8, Is.EqualTo(result[0].Lines[7].DiffLineNumber));

                // +    public sealed class UsageTracker : IUsageTracker, IDisposable
                Assert.That(-1, Is.EqualTo(result[0].Lines[8].OldLineNumber));
                Assert.That(18, Is.EqualTo(result[0].Lines[8].NewLineNumber));
                Assert.That(9, Is.EqualTo(result[0].Lines[8].DiffLineNumber));

                //      IConnectionManager connectionManager;
                Assert.That(26, Is.EqualTo(result[0].Lines[17].OldLineNumber));
                Assert.That(25, Is.EqualTo(result[0].Lines[17].NewLineNumber));
                Assert.That(18, Is.EqualTo(result[0].Lines[17].DiffLineNumber));
            }
Esempio n. 4
0
        /// <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 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,
                    comments.Key.Item1,
                    comments.Key.Item2,
                    diffLines,
                    comments);
                threads.Add(thread);
            }

            UpdateCommentThreads(threads, diff);
            return(threads);
        }
Esempio n. 5
0
            public void EmptyDiff_NoDiffChunks()
            {
                var chunks = DiffUtilities.ParseFragment("");

                Assert.Empty(chunks);
            }
Esempio n. 6
0
            public void EmptyDiff_NoDiffChunks()
            {
                var chunks = DiffUtilities.ParseFragment("");

                Assert.Equal(0, chunks.Count());
            }