Skip to content

Commit

Permalink
fix: Correct Notes parsing (#67)
Browse files Browse the repository at this point in the history
Fixed #64
  • Loading branch information
svrooij authored May 7, 2024
1 parent 538532c commit c5ddf30
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
35 changes: 24 additions & 11 deletions src/WingetIntune/Models/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ public WinGetApp ToWinGetApp(DisplayCatalogResponse displayCatalogResponse)
var product = displayCatalogResponse.Products.FirstOrDefault();
var displaySku = product?.DisplaySkuAvailabilities?.FirstOrDefault()?.Sku.LocalizedProperties.FirstOrDefault();
var productProperties = product?.LocalizedProperties?.FirstOrDefault();
var app = new WinGetApp();
app.DisplayName = displaySku!.SkuTitle;
app.PackageIdentifier = product!.ProductId;
app.InformationUrl = productProperties?.SupportUri;
app.PrivacyInformationUrl = productProperties?.PublisherWebsiteUri;
app.Description = productProperties!.ProductDescription;
var app = new WinGetApp
{
DisplayName = displaySku!.SkuTitle,
PackageIdentifier = product!.ProductId,
InformationUrl = productProperties?.SupportUri,
PrivacyInformationUrl = productProperties?.PublisherWebsiteUri,
Description = productProperties!.ProductDescription
};
app.AdditionalData.Add("repositoryType", "microsoftstore");
app.InstallExperience = new WinGetAppInstallExperience()
{
Expand All @@ -156,7 +158,7 @@ internal static IntuneApp ToIntuneApp(Win32LobApp? win32LobApp)
{
ArgumentNullException.ThrowIfNull(win32LobApp, nameof(win32LobApp));

var (packageId, source) = win32LobApp.Notes.ExtractPackageIdAndSourceFromNotes();
var (packageId, _) = win32LobApp.Notes.ExtractPackageIdAndSourceFromNotes();
return new IntuneApp
{
PackageId = packageId!,
Expand Down Expand Up @@ -187,19 +189,30 @@ internal static class StringExtensions
{
internal static (string?, string?) ExtractPackageIdAndSourceFromNotes(this string? notes)
{
if (notes is null || !notes.Contains("[WingetIntune|"))
if (notes is not null)
{
return (null, null);
return notes.Contains("[WinTuner|") ? notes.extractNewNotesFormat() : notes.extractOldNotesFormat();
}
return (null, null);
}

private static (string?, string?) extractOldNotesFormat(this string notes)
{
var match = Regex.Match(notes, @"\[WingetIntune\|(?<source>[^\|]+)\|(?<packageId>[^\]]+)\]");
if (match.Success)
{
return (match.Groups["packageId"].Value, match.Groups["source"].Value);
}
else
return (null, null);
}

private static (string?, string?) extractNewNotesFormat(this string notes)
{
var match = Regex.Match(notes, @"\[WinTuner\|(?<source>[^\|]+)\|(?<packageId>[^\]]+)\]");
if (match.Success)
{
return (null, null);
return (match.Groups["packageId"].Value, match.Groups["source"].Value);
}
return (null, null);
}
}
5 changes: 3 additions & 2 deletions src/WingetIntune/WingetServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using WingetIntune.Interfaces;
using WingetIntune.Intune;

[assembly: InternalsVisibleTo("WingetIntune.Tests")]
namespace WingetIntune;

public static class WingetServiceCollectionExtension
Expand Down
52 changes: 52 additions & 0 deletions tests/WingetIntune.Tests/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using WingetIntune.Models;
namespace WingetIntune.Tests;
public class StringExtensionsTests
{
[Fact]
public void ExtractPackageIdAndSourceFromNotes_WhenNotesIsNull_ReturnsNull()
{
// Arrange
string? notes = null;
// Act
var result = notes.ExtractPackageIdAndSourceFromNotes();
// Assert
Assert.Null(result.Item1);
Assert.Null(result.Item2);
}

[Fact]
public void ExtractPackageIdAndSourceFromNotes_WhenNotesContainsOldNotesFormat_ReturnsPackageIdAndSource()
{
// Arrange
string notes = " dadfdafdf [WingetIntune|winget|Microsoft.AzureCLI] $%#%$@";
// Act
var result = notes.ExtractPackageIdAndSourceFromNotes();
// Assert
Assert.Equal("Microsoft.AzureCLI", result.Item1);
Assert.Equal("winget", result.Item2);
}

[Fact]
public void ExtractPackageIdAndSourceFromNotes_WhenNotesContainsWinTunerNotesFormat_ReturnsPackageIdAndSource()
{
// Arrange
string notes = " dadfdafdf [WinTuner|winget|Microsoft.AzureCLI] $%#%$@";
// Act
var result = notes.ExtractPackageIdAndSourceFromNotes();
// Assert
Assert.Equal("Microsoft.AzureCLI", result.Item1);
Assert.Equal("winget", result.Item2);
}

[Fact]
public void ExtractPackageIdAndSourceFromNotes_WhenNotesContainsInvalidFormat_ReturnsNull()
{
// Arrange
string notes = " dadfdafdf [WinTuner|winget] $%#%$@";
// Act
var result = notes.ExtractPackageIdAndSourceFromNotes();
// Assert
Assert.Null(result.Item1);
Assert.Null(result.Item2);
}
}

0 comments on commit c5ddf30

Please sign in to comment.