Пример #1
0
        /// <inheritdoc/>
        public async Task <IPullRequestSessionFile> GetFile(string relativePath)
        {
            await getFilesLock.WaitAsync();

            try
            {
                PullRequestSessionFile file;

                relativePath = relativePath.Replace("\\", "/");

                if (!fileIndex.TryGetValue(relativePath, out file))
                {
                    file = new PullRequestSessionFile(relativePath);
                    await UpdateFile(file);

                    fileIndex.Add(relativePath, file);
                }

                return(file);
            }
            finally
            {
                getFilesLock.Release();
            }
        }
        /// <inheritdoc/>
        public async Task <IPullRequestSessionFile> GetFile(
            string relativePath,
            string commitSha = "HEAD")
        {
            await getFilesLock.WaitAsync();

            try
            {
                PullRequestSessionFile file;
                var normalizedPath = relativePath.Replace("\\", "/");
                var key            = normalizedPath + '@' + commitSha;

                if (!fileIndex.TryGetValue(key, out file))
                {
                    file = new PullRequestSessionFile(normalizedPath, commitSha);
                    await UpdateFile(file);

                    fileIndex.Add(key, file);
                }

                return(file);
            }
            finally
            {
                getFilesLock.Release();
            }
        }
Пример #3
0
        /// <inheritdoc/>
        public async Task <IPullRequestSessionFile> GetFile(
            string relativePath,
            string commitSha = "HEAD")
        {
            await getFilesLock.WaitAsync();

            try
            {
                PullRequestSessionFile file;
                var gitPath = Paths.ToGitPath(relativePath);
                var key     = gitPath + '@' + commitSha;

                if (!fileIndex.TryGetValue(key, out file))
                {
                    file = new PullRequestSessionFile(relativePath, commitSha);
                    await UpdateFile(file);

                    fileIndex.Add(key, file);
                }

                return(file);
            }
            finally
            {
                getFilesLock.Release();
            }
        }
Пример #4
0
        async Task UpdateFile(PullRequestSessionFile file)
        {
            var mergeBaseSha = await GetMergeBase();

            file.BaseSha   = PullRequest.Base.Sha;
            file.CommitSha = PullRequest.Head.Sha;
            file.Diff      = await service.Diff(LocalRepository, mergeBaseSha, file.CommitSha, file.RelativePath);

            file.InlineCommentThreads = service.BuildCommentThreads(PullRequest, file.RelativePath, file.Diff);
        }
        async Task <PullRequestSessionFile> CreateFile(
            string relativePath,
            IEditorContentSource contentSource)
        {
            var file = new PullRequestSessionFile(relativePath);

            file.ContentSource = contentSource;
            await UpdateFile(file);

            return(file);
        }
Пример #6
0
        async Task UpdateFile(PullRequestSessionFile file)
        {
            var mergeBaseSha = await GetMergeBase();

            file.BaseSha   = PullRequest.BaseRefSha;
            file.CommitSha = file.IsTrackingHead ? PullRequest.HeadRefSha : file.CommitSha;
            file.Diff      = await service.Diff(LocalRepository, mergeBaseSha, file.CommitSha, file.RelativePath);

            file.InlineCommentThreads = service.BuildCommentThreads(PullRequest, file.RelativePath, file.Diff, file.CommitSha);
            file.InlineAnnotations    = service.BuildAnnotations(PullRequest, file.RelativePath);
        }
        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;
        }