Ejemplo n.º 1
0
        public async Task Publish(IEnumerable <NewReviewDiscussion> discussions, Dictionary <string, Guid> newCommentsMap, Dictionary <string, Guid> newDiscussionsMap)
        {
            foreach (var discussion in discussions)
            {
                var commentId    = GuidComb.Generate();
                var discussionId = GuidComb.Generate();

                newCommentsMap[discussion.TemporaryId]    = commentId;
                newDiscussionsMap[discussion.TemporaryId] = discussionId;

                var review = _reviewForRevision(discussion.TargetRevisionId);

                _sessionAdapter.Save(new ReviewDiscussion
                {
                    Id          = discussionId,
                    RevisionId  = review.RevisionId,
                    State       = discussion.State.AsCommentState(),
                    RootComment = new Comment
                    {
                        Id               = commentId,
                        Content          = discussion.Content,
                        PostedInReviewId = review.Id,
                        CreatedAt        = DateTimeOffset.UtcNow
                    }
                });
            }
        }
Ejemplo n.º 2
0
        public async Task Publish(List <Guid> resolvedDiscussions)
        {
            var discussionsToResolve = _sessionAdapter.GetDiscussions(resolvedDiscussions);

            foreach (var comment in discussionsToResolve)
            {
                comment.State = CommentState.Resolved;

                _sessionAdapter.Save(comment);
            }
        }
        public async Task MarkFiles(Dictionary <RevisionId, List <ClientFileId> > reviewedFiles, Dictionary <RevisionId, List <ClientFileId> > unreviewedFiles)
        {
            foreach (var(revisionId, fileIds) in reviewedFiles)
            {
                var review = _reviewForRevision(revisionId);
                var files  = UnpackIds(review.RevisionId, fileIds);
                var toAdd  = files.Where(x => review.Files.All(y => y.FileId != _resolveFileId(x.Id))).ToList();

                if (toAdd.Any())
                {
                    foreach (var(clientFileId, path) in toAdd)
                    {
                        var fileId = _resolveFileId(clientFileId);
                        review.Files.Add(new FileReview(path, fileId)
                        {
                            Status = FileReviewStatus.Reviewed
                        });
                    }

                    _sessionAdapter.Save(review);
                }
            }

            foreach (var(revisionId, fileIds) in unreviewedFiles)
            {
                var review = _reviewForRevision(revisionId);

                var toRemove = review.Files.Where(x => fileIds.Any(y => _resolveFileId(y) == x.FileId)).ToList();

                if (toRemove.Any())
                {
                    review.Files.RemoveRange(toRemove);

                    _sessionAdapter.Save(review);
                }
            }
        }
        public async Task <(Guid RevisionId, Dictionary <ClientFileId, Guid> ClientFileIdMap, Dictionary <string, Guid> ByName)> FindOrCreateRevision(ReviewIdentifier reviewId, PublishReview.RevisionCommits commits, FeatureToggle features)
        {
            var existingRevision = await _sessionAdapter.GetRevision(reviewId, commits);

            if (existingRevision != null)
            {
                var fileHistory = _sessionAdapter.GetFileHistoryEntries(existingRevision);

                var idMap = fileHistory.ToDictionary(x => ClientFileId.Persistent(x.FileId), x => x.FileId);

                var nameMap = fileHistory.GroupBy(x => x.FileName).ToDictionary(x => x.Key, x => x.First().FileId);

                return(existingRevision.Id, idMap, nameMap);
            }

            // create revision
            var nextNumber = _sessionAdapter.GetNextRevisionNumber(reviewId);

            await CreateRef(reviewId, nextNumber, commits.Base, "base", _api);

            try
            {
                await CreateRef(reviewId, nextNumber, commits.Head, "head", _api);
            }
            catch (ExistingRefConflictException unexpectedException)
            {
                // The base ref is already created, we must add the record to database no matter what
                Console.WriteLine("Failed to create ref for head commit - ignoring");
                Console.WriteLine(unexpectedException.ToString());
            }

            var revision = await _factory.Create(reviewId, nextNumber, commits.Base, commits.Head);

            await _sessionAdapter.Save(revision);

            var clientFileIdMap = await new FillFileHistory(_sessionAdapter, _api, revision, features).Fill();

            var byClientId = clientFileIdMap.ToDictionary(x => x.Key, x => x.Value.Item2);

            var byName = clientFileIdMap.GroupBy(x => x.Value.Item1).ToDictionary(x => x.Key, x => x.First().Value.Item2);

            return(RevisionId : revision.Id, ClientFileIdMap : byClientId, byName);
        }
        public async Task <Review> FindOrCreateReview(PublishReview command, ReviewIdentifier reviewId, Guid revisionId)
        {
            var review = _sessionAdapter.GetReview(revisionId, _user);

            if (review == null)
            {
                review = new Review
                {
                    Id         = GuidComb.Generate(),
                    RevisionId = revisionId,
                    UserId     = _user.Id
                };
            }

            review.ReviewedAt = DateTimeOffset.Now;

            _sessionAdapter.Save(review);
            return(review);
        }
Ejemplo n.º 6
0
        public async Task Publish(NewFileDiscussion[] discussions, Dictionary <string, Guid> newCommentsMap, Dictionary <string, Guid> newDiscussionsMap)
        {
            foreach (var discussion in discussions)
            {
                var commentId    = GuidComb.Generate();
                var discussionId = GuidComb.Generate();

                newCommentsMap[discussion.TemporaryId]    = commentId;
                newDiscussionsMap[discussion.TemporaryId] = discussionId;

                var review = _reviewForRevision(discussion.TargetRevisionId);

                var resolvedFileId = _resolveFileId(discussion.FileId);

                var currentRevision = _sessionAdapter.GetRevision(review.RevisionId);
                var prevRevision    = _sessionAdapter.GetPreviousRevision(currentRevision);

                var currentEntry = _sessionAdapter.GetFileHistoryEntry(resolvedFileId, currentRevision);
                var prevEntry    = _sessionAdapter.GetFileHistoryEntry(resolvedFileId, prevRevision);

                _sessionAdapter.Save(new FileDiscussion
                {
                    RevisionId  = review.RevisionId,
                    Id          = discussionId,
                    FileId      = resolvedFileId,
                    File        = PathPair.Make(prevEntry?.FileName ?? currentEntry.FileName, currentEntry.FileName),
                    LineNumber  = discussion.LineNumber,
                    State       = discussion.State.AsCommentState(),
                    RootComment = new Comment
                    {
                        Id = commentId,
                        PostedInReviewId = review.Id,
                        Content          = discussion.Content,
                        CreatedAt        = DateTimeOffset.UtcNow
                    }
                });
            }
        }
Ejemplo n.º 7
0
        public async Task <Dictionary <ClientFileId, (string, Guid)> > Fill()
        {
            var(previousRevId, previousHead) = _sessionAdapter.FindPreviousRevision(_currentRevision.ReviewId, _currentRevision.RevisionNumber, _currentRevision.BaseCommit);

            var diff = await _api.GetDiff(_currentRevision.ReviewId.ProjectId, previousHead, _currentRevision.HeadCommit);

            if (_features.For("dont-show-excesive-files-from-rebases").IsActive)
            {
                var fileHistoryEntries = await _sessionAdapter.GetReviewFileHistory(_currentRevision.ReviewId);

                diff = (await RelevantFilesFilter.Filter(diff, fileHistoryEntries, _currentRevision.ReviewId, _currentRevision.BaseCommit, _currentRevision.HeadCommit, _api)).ToList();
            }

            TMP_FIllOldRevisionFiles(diff);

            var fileIds = _sessionAdapter.FetchFileIds(previousRevId);

            var clientFileIdMap = fileIds.ToDictionary(x => ClientFileId.Persistent(x.Key), x => (x.Value, x.Key));

            var remainingDiffs = new HashSet <FileDiff>(diff);

            foreach (var(fileId, latestFileName) in fileIds)
            {
                var allMatchingDiffs = remainingDiffs.Where(x => x.Path.OldPath == latestFileName);
                if (allMatchingDiffs.Count() > 1)
                {
                    Logger.Error($"More that one diff found for {latestFileName}");
                }

                var matchingDiff = allMatchingDiffs.FirstOrDefault();
                if (matchingDiff != null)
                {
                    remainingDiffs.Remove(matchingDiff);
                }

                _sessionAdapter.Save(new FileHistoryEntry
                {
                    Id         = GuidComb.Generate(),
                    RevisionId = _currentRevision.Id,
                    ReviewId   = _currentRevision.ReviewId,
                    FileId     = fileId,
                    FileName   = matchingDiff?.Path.NewPath ?? latestFileName,
                    IsNew      = matchingDiff?.NewFile ?? false,
                    IsDeleted  = matchingDiff?.DeletedFile ?? false,
                    IsRenamed  = matchingDiff?.RenamedFile ?? false,
                    IsModified = matchingDiff != null
                });
            }

            foreach (var file in remainingDiffs)
            {
                var fileId = GuidComb.Generate();

                clientFileIdMap[ClientFileId.Provisional(file.Path)] = (file.Path.NewPath, fileId);

                if (file.RenamedFile)
                {
                    _sessionAdapter.Save(new FileHistoryEntry
                    {
                        Id         = GuidComb.Generate(),
                        RevisionId = null,
                        ReviewId   = _currentRevision.ReviewId,
                        FileId     = fileId,
                        FileName   = file.Path.OldPath,
                        IsNew      = false,
                        IsDeleted  = false,
                        IsRenamed  = false,
                        IsModified = false
                    });
                }

                _sessionAdapter.Save(new FileHistoryEntry
                {
                    Id         = GuidComb.Generate(),
                    RevisionId = _currentRevision.Id,
                    ReviewId   = _currentRevision.ReviewId,
                    FileId     = fileId,
                    FileName   = file.Path.NewPath,
                    IsNew      = file.NewFile,
                    IsDeleted  = file.DeletedFile,
                    IsRenamed  = file.RenamedFile,
                    IsModified = true
                });
            }

            return(clientFileIdMap);
        }