Example #1
0
        public void WritePersistentId()
        {
            var guid = "69e1cf41-71cb-4384-86c9-c66706dcf3ea";

            var clientFileId = ClientFileId.Persistent(Guid.Parse(guid));

            var text = ClientFileId.Write(clientFileId);

            Assert.That(text, Is.EqualTo(guid));
        }
        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 <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);
        }