-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci:.NET Innovation Days Q3FY25: Update Dotty to update test library v…
…ersions automatically via PR (#2810)
- Loading branch information
Showing
9 changed files
with
292 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
.github/workflows/scripts/nugetSlackNotifications/CsprojHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using System.Xml.Serialization; | ||
using Serilog; | ||
|
||
namespace nugetSlackNotifications | ||
{ | ||
public class CsprojHandler | ||
{ | ||
public static async Task<List<string>> UpdatePackageReferences(string csprojPath, List<NugetVersionData> versionDatas) | ||
{ | ||
var updateLog = new List<string>(); | ||
var csprojLines = await File.ReadAllLinesAsync(csprojPath); | ||
|
||
var packages = Parse(csprojLines); | ||
if (packages.Count == 0) | ||
{ | ||
Log.Warning("No packages found in csproj file " + csprojPath); | ||
return updateLog; | ||
} | ||
|
||
foreach (var versionData in versionDatas) | ||
{ | ||
var matchingPackages = packages.Where(p => p.Include == versionData.PackageName).ToList(); | ||
if (matchingPackages.Count == 0) | ||
{ | ||
Log.Warning($"No matching packages found in csproj file for {versionData.PackageName}"); | ||
continue; | ||
} | ||
|
||
foreach (var package in matchingPackages) | ||
{ | ||
if(package.VersionAsVersion < versionData.NewVersionAsVersion && package.Pin) | ||
{ | ||
Log.Warning($"Not updating {package.Include} for {package.TargetFramework}, it is pinned to {package.Version}. Manual verification recommended."); | ||
continue; | ||
} | ||
|
||
if (package.VersionAsVersion < versionData.NewVersionAsVersion) | ||
{ | ||
Log.Information($"Updating {package.Include} from {package.Version} to {versionData.NewVersion}"); | ||
var pattern = @"\d+(\.\d+){2,3}"; | ||
var result = Regex.Replace(csprojLines[package.LineNumber], pattern, versionData.NewVersion); | ||
csprojLines[package.LineNumber] = result; | ||
|
||
updateLog.Add($"- Package [{versionData.PackageName}]({versionData.Url}) " + | ||
$"for {package.TargetFramework} " + | ||
$"was updated from {versionData.OldVersion} to {versionData.NewVersion} " + | ||
$"on {versionData.PublishDate.ToShortDateString()}."); | ||
} | ||
} | ||
} | ||
|
||
await File.WriteAllLinesAsync(csprojPath, csprojLines); | ||
updateLog.Add(""); | ||
return updateLog; | ||
} | ||
|
||
private static List<PackageReference> Parse(string[] csprojLines) | ||
{ | ||
var packages = new List<PackageReference>(); | ||
try | ||
{ | ||
for (int i = 0; i < csprojLines.Length; i++) | ||
{ | ||
var line = csprojLines[i]; | ||
if (!line.Contains("PackageReference")) | ||
{ | ||
continue; | ||
} | ||
|
||
var serializer = new XmlSerializer(typeof(PackageReference)); | ||
using (var reader = new StringReader(line)) | ||
{ | ||
var packageReference = (PackageReference)serializer.Deserialize(reader); | ||
packageReference.LineNumber = i; | ||
packages.Add(packageReference); | ||
} | ||
} | ||
|
||
return packages; | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "XML issue"); | ||
return packages; | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.github/workflows/scripts/nugetSlackNotifications/NugetVersionData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace nugetSlackNotifications | ||
{ | ||
public class NugetVersionData | ||
{ | ||
public string PackageName { get; set; } | ||
public string OldVersion { get; set; } | ||
public string NewVersion { get; set; } | ||
public Version NewVersionAsVersion { get; set; } | ||
public string Url { get; set; } | ||
public DateTime PublishDate { get; set; } | ||
|
||
public NugetVersionData(string packageName, string oldVersion, string newVersion, string url, DateTime publishDate) | ||
{ | ||
PackageName = packageName; | ||
OldVersion = oldVersion; | ||
NewVersion = newVersion; | ||
NewVersionAsVersion = new Version(newVersion); | ||
Url = url; | ||
PublishDate = publishDate; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.github/workflows/scripts/nugetSlackNotifications/PackageInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace nugetSlackNotifications | ||
{ | ||
public class PackageInfo | ||
{ | ||
[JsonPropertyName("packageName")] | ||
public string PackageName { get; set; } | ||
[JsonPropertyName("ignorePatch")] | ||
public bool IgnorePatch { get; set; } | ||
[JsonPropertyName("ignoreMinor")] | ||
public bool IgnoreMinor { get; set; } | ||
[JsonPropertyName("ignoreReason")] | ||
public string IgnoreReason {get; set;} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.github/workflows/scripts/nugetSlackNotifications/PackageReference.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using System.Xml.Serialization; | ||
|
||
namespace nugetSlackNotifications | ||
{ | ||
public class PackageReference | ||
{ | ||
[XmlAttribute] | ||
public string Include { get; set; } | ||
|
||
[XmlAttribute] | ||
public string Version { get; set; } | ||
|
||
[XmlIgnore] | ||
public Version VersionAsVersion => new Version(Version); | ||
|
||
[XmlIgnore] | ||
public int LineNumber { get; set; } | ||
|
||
[XmlAttribute] | ||
public string Condition { get; set; } | ||
|
||
public string TargetFramework | ||
{ | ||
get | ||
{ | ||
if (Condition == null) | ||
{ | ||
return null; | ||
} | ||
var match = Regex.Match(Condition, @"net\d+\.*\d+"); | ||
return match.Success ? match.Value : null; | ||
} | ||
} | ||
|
||
[XmlAttribute] | ||
public bool Pin { get; set; } | ||
} | ||
} |
Oops, something went wrong.