Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct Notes parsing #67

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}