Skip to content

Commit

Permalink
ci:.NET Innovation Days Q3FY25: Update Dotty to update test library v…
Browse files Browse the repository at this point in the history
…ersions automatically via PR (#2810)
  • Loading branch information
jaffinito authored Oct 4, 2024
1 parent 5059eae commit 9673ef1
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nuget_slack_notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
NEW_RELIC_HOST: staging-collector.newrelic.com
NEW_RELIC_LICENSE_KEY: ${{ secrets.STAGING_LICENSE_KEY }}
DOTTY_LAST_RUN_TIMESTAMP: ${{ env.LAST_RUN_TIMESTAMP }}
DOTTY_SEARCH_ROOT_PATH: ${{ github.workspace }}

run: |
if [ ${{ inputs.daysToSearch }} != "" ]; then
Expand Down
94 changes: 94 additions & 0 deletions .github/workflows/scripts/nugetSlackNotifications/CsprojHandler.cs
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;
}
}
}
}
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 .github/workflows/scripts/nugetSlackNotifications/PackageInfo.cs
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;}
}
}
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; }
}
}
Loading

0 comments on commit 9673ef1

Please sign in to comment.