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

Move memory cache and logging into Updater class to support multiple providers. #42

Merged
merged 1 commit into from
Oct 9, 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
42 changes: 38 additions & 4 deletions src/DependencyUpdated.Core/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
using DependencyUpdated.Core.Interfaces;
using DependencyUpdated.Core.Models;
using DependencyUpdated.Core.Models.Enums;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Serilog;
using System.IO.Enumeration;

namespace DependencyUpdated.Core;

public sealed class Updater(IServiceProvider serviceProvider, IOptions<UpdaterConfig> config, ILogger logger)
public sealed class Updater(IServiceProvider serviceProvider, IOptions<UpdaterConfig> config, ILogger logger, IMemoryCache memoryCache)
{
private readonly List<string> _cacheKeys = new();

public async Task DoUpdate()
{
var repositoryProvider =
Expand All @@ -27,6 +30,7 @@ public async Task DoUpdate()
var projectName = ResolveProjectName(project, directory);
var alreadyProcessed = new List<DependencyDetails>();
var allProjectDependencies = await updater.ExtractAllPackages(projectFiles);
logger.Debug("Found packages {Packages} in projects {Project}", allProjectDependencies, projectFiles);
foreach (var group in project.Groups)
{
repositoryProvider.SwitchToUpdateBranch(repositoryPath, projectName, group);
Expand All @@ -35,27 +39,43 @@ public async Task DoUpdate()
{
continue;
}


logger.Debug("Filtered packages {Packages}", filteredPackages);
var allDependenciesToUpdate = await GetLatestVersions(filteredPackages, updater, project);
if (allDependenciesToUpdate.Count == 0)
{
continue;
}


logger.Verbose("Found new versions: {Packages}", allDependenciesToUpdate);
var allUpdates = updater.HandleProjectUpdate(projectFiles, allDependenciesToUpdate);
if (allUpdates.Count == 0)
{
continue;
}

logger.Information("Updated packages {Packages}", allUpdates);
alreadyProcessed.AddRange(allDependenciesToUpdate);
repositoryProvider.CommitChanges(repositoryPath, projectName, group);
await repositoryProvider.SubmitPullRequest(allUpdates, projectName, group);
repositoryProvider.CleanAndSwitchToDefaultBranch(repositoryPath);
}
}

foreach (var key in _cacheKeys)
{
memoryCache.Remove(key);
}

_cacheKeys.Clear();
}
}

internal void AddToCache(DependencyDetails dependencyDetails, IReadOnlyCollection<DependencyDetails> packages)
{
memoryCache.Set(dependencyDetails.Name, packages);
_cacheKeys.Add(dependencyDetails.Name);
}

private static ICollection<DependencyDetails> FilterPackages(
ICollection<DependencyDetails> allPackagesFromProjects, IReadOnlyCollection<DependencyDetails> alreadyProcessed,
Expand Down Expand Up @@ -131,7 +151,7 @@ private async Task<HashSet<DependencyDetails>> GetLatestVersions(
{
logger.Verbose("Processing {PackageName}:{PackageVersion}", dependencyDetails.Name,
dependencyDetails.Version);
var allVersions = await projectUpdater.GetVersions(dependencyDetails, projectConfiguration);
var allVersions = await GetVersions(projectUpdater, dependencyDetails, projectConfiguration);
var latestVersion = GetMaxVersion(allVersions, dependencyDetails.Version, projectConfiguration);
if (latestVersion is null)
{
Expand All @@ -151,4 +171,18 @@ private async Task<HashSet<DependencyDetails>> GetLatestVersions(

return returnList;
}

private async Task<IReadOnlyCollection<DependencyDetails>> GetVersions(IProjectUpdater projectUpdater,
DependencyDetails dependencyDetails, Project projectConfiguration)
{
if (memoryCache.TryGetValue<IReadOnlyCollection<DependencyDetails>>(dependencyDetails.Name,
out var versions) && versions is not null)
{
return versions;
}

var packages = await projectUpdater.GetVersions(dependencyDetails, projectConfiguration);
AddToCache(dependencyDetails, packages);
return packages;
}
}
13 changes: 1 addition & 12 deletions src/DependencyUpdated.Projects.DotNet/DotNetUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using DependencyUpdated.Core.Config;
using DependencyUpdated.Core.Interfaces;
using DependencyUpdated.Core.Models;
using Microsoft.Extensions.Caching.Memory;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using ILogger = Serilog.ILogger;

namespace DependencyUpdated.Projects.DotNet;

internal sealed class DotNetUpdater(ILogger logger, IMemoryCache memoryCache) : IProjectUpdater
internal sealed class DotNetUpdater : IProjectUpdater
{
private static readonly string[] ValidDotnetPatterns =
[
Expand Down Expand Up @@ -42,13 +40,6 @@ public async Task<ICollection<DependencyDetails>> ExtractAllPackages(IReadOnlyCo
public async Task<IReadOnlyCollection<DependencyDetails>> GetVersions(DependencyDetails package,
Project projectConfiguration)
{
var existsInCache =
memoryCache.TryGetValue<IReadOnlyCollection<DependencyDetails>>(package.Name, out var cachedVersion);
if (existsInCache && cachedVersion is not null)
{
return cachedVersion;
}

if (!projectConfiguration.DependencyConfigurations.Any())
{
throw new InvalidOperationException(
Expand Down Expand Up @@ -99,7 +90,6 @@ public async Task<IReadOnlyCollection<DependencyDetails>> GetVersions(Dependency
.DistinctBy(x => x.Version)
.Select(x => package with { Version = x.Version })
.ToHashSet();
memoryCache.Set(package.Name, result);
return result;
}

Expand Down Expand Up @@ -147,7 +137,6 @@ private IReadOnlyCollection<UpdateResult> UpdateCsProj(IReadOnlyCollection<strin
private IReadOnlyCollection<UpdateResult> UpdateCsProj(string fullPath,
ICollection<DependencyDetails> packagesToUpdate)
{
logger.Information("Updating: {FullPath} project", fullPath);
var results = new List<UpdateResult>();
var document = XDocument.Load(fullPath, LoadOptions.PreserveWhitespace);

Expand Down
51 changes: 49 additions & 2 deletions tests/DependencyUpdated.Core.UnitTests/UpdaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using DependencyUpdated.Core.Interfaces;
using DependencyUpdated.Core.Models;
using DependencyUpdated.Core.Models.Enums;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using Serilog;
using Xunit;

Expand All @@ -21,6 +22,7 @@ public class UpdaterTests
private readonly ILogger _logger;
private readonly Updater _target;
private readonly string _currentDir;
private readonly IMemoryCache _memoryCache;

public UpdaterTests()
{
Expand All @@ -47,7 +49,8 @@ public UpdaterTests()
.AddKeyedSingleton(_config.Value.RepositoryType, _repositoryProvider)
.BuildServiceProvider();
_logger = Substitute.For<ILogger>();
_target = new Updater(_serviceProvider, _config, _logger);
_memoryCache = new MemoryCache(new MemoryCacheOptions());
_target = new Updater(_serviceProvider, _config, _logger, _memoryCache);
}

[Fact]
Expand Down Expand Up @@ -371,4 +374,48 @@ await _repositoryProvider.Received(1).SubmitPullRequest(expectedUpdateResult, _c
_config.Value.Projects[0].Groups[0]);
}
}

[Fact]
public async Task Update_Should_GetPackageFromCacheIfExists()
{
// Arrange
var projectList = new List<string>() { "TestProjectFile" };
_projectUpdater
.GetAllProjectFiles(_config.Value.Projects[0].Directories[0])
.Returns(projectList);
var projectDependencies =
new List<DependencyDetails>()
{
new("TestDependency", new Version(1, 0, 0)),
};
_projectUpdater.ExtractAllPackages(projectList).Returns(projectDependencies);
var expectedDependencyUpdate = new List<DependencyDetails>(new[]
{
new DependencyDetails(projectDependencies[0].Name, new Version(2, 0, 0))
});
_target.AddToCache(expectedDependencyUpdate[0], expectedDependencyUpdate);
var expectedUpdateResult = new List<UpdateResult> { new(projectDependencies[0].Name, "1.0.0", "2.0.0") };
_projectUpdater
.HandleProjectUpdate(projectList,
Arg.Is<ICollection<DependencyDetails>>(detailsCollection =>
detailsCollection.SequenceEqual(expectedDependencyUpdate)))
.Returns(expectedUpdateResult);


// Act
await _target.DoUpdate();

// Assert
using (new AssertionScope())
{
_projectUpdater.Received(1).HandleProjectUpdate(projectList,
Arg.Is<ICollection<DependencyDetails>>(detailsCollection =>
detailsCollection.SequenceEqual(expectedDependencyUpdate)));
_repositoryProvider.Received(1).CommitChanges(_currentDir, _config.Value.Projects[0].Name,
_config.Value.Projects[0].Groups[0]);
await _repositoryProvider.Received(1).SubmitPullRequest(expectedUpdateResult, _config.Value.Projects[0].Name,
_config.Value.Projects[0].Groups[0]);
_memoryCache.Get(expectedDependencyUpdate[0].Name).Should().BeNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@
using DependencyUpdated.Core.Models.Enums;
using FluentAssertions;
using FluentAssertions.Execution;
using NSubstitute;
using Serilog;
using Xunit;

namespace DependencyUpdated.Projects.DotNet.UnitTests;

public class DotNetUpdaterTests
{
private readonly DotNetUpdater _target;
private readonly ILogger _logger;
private readonly MockMemoryCache _memoryCahce;
private readonly string _searchPath;
private readonly DotNetUpdater _target = new();
private readonly string _searchPath = "Projects";

public DotNetUpdaterTests()
{
_logger = Substitute.For<ILogger>();
_memoryCahce = new MockMemoryCache();
_target = new DotNetUpdater(_logger, _memoryCahce);
_searchPath = "Projects";
}

[Fact]
public void GetAllProjectFiles_Should_ReturnAllProjects()
{
Expand Down Expand Up @@ -63,25 +51,6 @@ public async Task ExtractAllPackages_Should_ReturnPackagesFromCsProjFile()
}
}

[Fact]
public async Task GetVersions_Should_ReturnVersionFromCache()
{
// Arrange
var packages = new DependencyDetails("TestName", new Version(1, 0, 0));
var projectConfiguration = new Project();
var cacheData = new List<DependencyDetails> { new("TestName", new Version(1, 2, 3)) };
_memoryCahce.AddEntry(packages.Name, cacheData);

// Act
var result = await _target.GetVersions(packages, projectConfiguration);

// Assert
using (new AssertionScope())
{
result.Should().BeEquivalentTo(cacheData);
}
}

[Fact]
public async Task GetVersions_Should_ThrowForMissingDependencyConfiguration()
{
Expand Down

This file was deleted.

Loading