private async Task RefreshDiscussions()
        {
            var discussions = await this.GetDiscussions();

            var sourceFileContent = await this.GetSourceFileContentAsync(this.change);

            var targetFileContent = await this.GetTargetFileContentAsync(this.change);

            var sourceFileLines = this.GetLines(sourceFileContent);
            var targetFileLines = this.GetLines(targetFileContent);

            var linesCanBeCommented = new List <LineViewModel>();

            if (change.IsDeletedFile)
            {
                this.lineViewModels = this.GetLineViewModelsFromSource(targetFileLines);
            }
            else if (change.IsNewFile)
            {
                this.lineViewModels = this.GetLineViewModelsFromTarget(sourceFileLines);
            }
            else
            {
                Gap[] gaps;
                var   hunks = HunkHelper.ParseHunks(this.change.Diff, out gaps);
                var   lineViewModelsFromHunks = this.GetLineViewModelsFromHunks(hunks);
                var   lineViewModelsFromGaps  = this.GetLineViewModelsFromGaps(gaps, sourceFileLines);
                this.lineViewModels = lineViewModelsFromHunks.Concat(lineViewModelsFromGaps).ToArray();
            }

            foreach (var diss in discussions)
            {
                var firstNote = diss.Notes.First();
                if (firstNote.Position == null)
                {
                    continue;
                }

                if (firstNote.Position.OldPath != change.OldPath && firstNote.Position.NewPath != change.NewPath)
                {
                    continue;
                }

                var dissViewModel = new DiscussionViewModel(diss, this.service);
                foreach (var noteDto in diss.Notes)
                {
                    var noteViewModel = new NoteViewModel(noteDto);
                    dissViewModel.Details.Notes.Add(noteViewModel);
                }

                var correspondentLine = firstNote.Position.NewLine != null
                    ? this.lineViewModels.First(line => line.NumberInSourceFile == firstNote.Position.NewLine.Value)
                    : this.lineViewModels.First(line => line.NumberInTargetFile == firstNote.Position.OldLine.Value);

                correspondentLine.Details.Discussions.Add(dissViewModel);
            }

            this.RefreshLines();
        }
Example #2
0
        private async void ExecuteNewDiscussion(object obj)
        {
            var position = new PositionDto
            {
                PositionType = "text",
                BaseSha      = this.mergeRequest.DiffRefs.BaseSha,
                StartSha     = this.mergeRequest.DiffRefs.StartSha,
                HeadSha      = this.mergeRequest.DiffRefs.HeadSha,
                NewPath      = this.change.NewPath,
                OldPath      = this.change.OldPath,
                NewLine      = this.NumberInSourceFile,
                OldLine      = this.NumberInTargetFile
            };

            var createDiscussionDto = new CreateDiscussionDto {
                Position = position
            };
            var dissDto = await this.service.AddDiscussion(createDiscussionDto, this.NewDiscussionText);

            var dissVm = new DiscussionViewModel(dissDto, this.service);

            this.Discussions.Add(dissVm);
            this.NewDiscussionText = string.Empty;
        }