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(); }
public void ParseHunksTest() { var diff = "@@ -1,14 +1,10 @@\n using System;\n-using System.Collections.Generic;\n-using System.Linq;\n-using System.Text;\n-using System.Threading.Tasks;\n \n namespace HelloGitLab\n {\n class Program\n {\n- static void Main(string[] args)\n+ static void Main()\n {\n var a = 1;\n a++;\n@@ -99,6 +95,8 @@ namespace HelloGitLab\n a++;\n Console.WriteLine(a);\n Console.WriteLine(\"Hello GitLab\");\n+ Console.WriteLine(\"\\n\");\n+ Console.WriteLine(\"@@ -1,14 +1,10 @@\");\n Console.ReadKey();\n }\n }\n"; Gap[] gaps; var hunks = HunkHelper.ParseHunks(diff, out gaps).ToList(); Assert.AreEqual(2, hunks.Count); Assert.AreEqual(5, hunks[0].Lines.Count(line => line.IsLineRemoved)); Assert.AreEqual(1, hunks[0].Lines.Count(line => line.IsLineAdded)); Assert.AreEqual(0, hunks[1].Lines.Count(line => line.IsLineRemoved)); Assert.AreEqual(2, hunks[1].Lines.Count(line => line.IsLineAdded)); }