Skip to content

Commit be0ce11

Browse files
authored
Add dependency update details to codeflow commits (#4831)
#4697 (comment)
1 parent d56ee9c commit be0ce11

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

src/Microsoft.DotNet.Darc/DarcLib/Helpers/StringUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.IO.Hashing;
8+
using System.Linq;
79
using System.Text;
10+
using Microsoft.DotNet.DarcLib.Models.Darc;
11+
using Microsoft.VisualStudio.Services.Common;
812

913
#nullable enable
1014
namespace Microsoft.DotNet.DarcLib.Helpers;

src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VersionFileCodeFlowUpdater.cs

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Text.RegularExpressions;
7+
using System.Text;
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.DotNet.DarcLib.Helpers;
@@ -579,8 +579,13 @@ private async Task<List<DependencyUpdate>> BackflowDependenciesAndToolset(
579579

580580
await targetRepo.StageAsync(["."], cancellationToken);
581581

582+
string commitMessage = string.Concat(
583+
$"Update dependencies from {build.GetRepository()} build {build.Id}",
584+
Environment.NewLine,
585+
BuildDependencyUpdateCommitMessage(repoChanges));
586+
582587
await targetRepo.CommitAsync(
583-
"Update dependencies from " + build.GetRepository() + " build " + build.Id,
588+
commitMessage,
584589
allowEmpty: false,
585590
cancellationToken: cancellationToken);
586591

@@ -666,6 +671,80 @@ private static List<DependencyUpdate> ComputeChanges(Matcher? excludedAssetsMatc
666671
.ToList();
667672
}
668673

674+
public static string BuildDependencyUpdateCommitMessage(IEnumerable<DependencyUpdate> updates)
675+
{
676+
if (!updates.Any())
677+
{
678+
return "No dependency updates to commit";
679+
}
680+
Dictionary<string, List<string>> removedDependencies = new();
681+
Dictionary<string, List<string>> addedDependencies = new();
682+
Dictionary<string, List<string>> updatedDependencies = new();
683+
foreach (DependencyUpdate dependencyUpdate in updates)
684+
{
685+
if (dependencyUpdate.To != null && dependencyUpdate.From == null)
686+
{
687+
string versionBlurb = $"Version {dependencyUpdate.To.Version}";
688+
AddDependencyToDictionary(addedDependencies, versionBlurb, dependencyUpdate.To.Name);
689+
continue;
690+
}
691+
if (dependencyUpdate.To == null && dependencyUpdate.From != null)
692+
{
693+
string versionBlurb = $"Version {dependencyUpdate.From.Version}";
694+
AddDependencyToDictionary(removedDependencies, versionBlurb, dependencyUpdate.From.Name);
695+
continue;
696+
}
697+
if (dependencyUpdate.To != null && dependencyUpdate.From != null)
698+
{
699+
string versionBlurb = $"Version {dependencyUpdate.From.Version} -> {dependencyUpdate.To.Version}";
700+
AddDependencyToDictionary(updatedDependencies, versionBlurb, dependencyUpdate.From.Name);
701+
continue;
702+
}
703+
}
704+
705+
var result = new StringBuilder();
706+
if (updatedDependencies.Any())
707+
{
708+
result.AppendLine("Updated Dependencies:");
709+
foreach ((string versionBlurb, List<string> packageNames) in updatedDependencies)
710+
{
711+
result.AppendLine(string.Join(", ", packageNames) + $" ({versionBlurb})");
712+
}
713+
result.AppendLine();
714+
}
715+
if (addedDependencies.Any())
716+
{
717+
result.AppendLine("Added Dependencies:");
718+
foreach ((string versionBlurb, List<string> packageNames) in addedDependencies)
719+
{
720+
result.AppendLine(string.Join(", ", packageNames) + $" ({versionBlurb})");
721+
}
722+
result.AppendLine();
723+
}
724+
if (removedDependencies.Any())
725+
{
726+
result.AppendLine("Removed Dependencies:");
727+
foreach ((string versionBlurb, List<string> packageNames) in removedDependencies)
728+
{
729+
result.AppendLine(string.Join(", ", packageNames) + $" ({versionBlurb})");
730+
}
731+
result.AppendLine();
732+
}
733+
return result.ToString().TrimEnd();
734+
}
735+
736+
private static void AddDependencyToDictionary(Dictionary<string, List<string>> dictionary, string versionBlurb, string dependencyName)
737+
{
738+
if (dictionary.TryGetValue(versionBlurb, out var list))
739+
{
740+
list.Add(dependencyName);
741+
}
742+
else
743+
{
744+
dictionary[versionBlurb] = new List<string> { dependencyName };
745+
}
746+
}
747+
669748
private async Task<VersionDetails> GetRepoDependencies(ILocalGitRepo repo, string commit)
670749
=> GetDependencies(await repo.GetFileFromGitAsync(VersionFiles.VersionDetailsXml, commit));
671750

test/Microsoft.DotNet.DarcLib.Tests/VirtualMonoRepo/VersionFileCodeFlowUpdaterTests.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Text;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using FluentAssertions;
@@ -353,6 +354,82 @@ public async Task ConflictingChangesThrowTest()
353354
await action.Should().ThrowAsync<ConflictingDependencyUpdateException>();
354355
}
355356

357+
[Test]
358+
public void TestCodeflowDependencyUpdateCommitMessage()
359+
{
360+
DependencyUpdate dep1 = new()
361+
{
362+
From = new DependencyDetail()
363+
{
364+
Name = "Foo",
365+
Version = "2.0.0"
366+
},
367+
To = new DependencyDetail()
368+
{
369+
Name = "Foo",
370+
Version = "3.0.0"
371+
},
372+
};
373+
374+
DependencyUpdate dep2 = new()
375+
{
376+
From = new DependencyDetail()
377+
{
378+
Name = "Bar",
379+
Version = "2.0.0"
380+
},
381+
To = new DependencyDetail()
382+
{
383+
Name = "Bar",
384+
Version = "3.0.0"
385+
},
386+
};
387+
388+
DependencyUpdate dep3 = new()
389+
{
390+
From = new DependencyDetail()
391+
{
392+
Name = "Boz",
393+
Version = "1.0.0"
394+
},
395+
To = new DependencyDetail()
396+
{
397+
Name = "Boz",
398+
Version = "4.0.0"
399+
},
400+
};
401+
DependencyUpdate dep4 = new()
402+
{
403+
To = new DependencyDetail()
404+
{
405+
Name = "Bop",
406+
Version = "3.0.0"
407+
},
408+
};
409+
DependencyUpdate dep5 = new()
410+
{
411+
From = new DependencyDetail()
412+
{
413+
Name = "Bam",
414+
Version = "2.0.0"
415+
},
416+
};
417+
418+
VersionFileCodeFlowUpdater.BuildDependencyUpdateCommitMessage([dep1, dep2, dep3, dep4, dep5]).Should().BeEquivalentTo(
419+
"""
420+
Updated Dependencies:
421+
Foo, Bar (Version 2.0.0 -> 3.0.0)
422+
Boz (Version 1.0.0 -> 4.0.0)
423+
424+
Added Dependencies:
425+
Bop (Version 3.0.0)
426+
427+
Removed Dependencies:
428+
Bam (Version 2.0.0)
429+
""".Trim()
430+
);
431+
432+
}
356433
private async Task TestConflictResolver(
357434
Build build,
358435
Codeflow lastFlow,

0 commit comments

Comments
 (0)