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

Compute history #204

Merged
merged 16 commits into from
Aug 30, 2022
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using Corgibytes.Freshli.Cli.DataModel;
using Corgibytes.Freshli.Cli.Functionality;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Functionality.Git;
using Corgibytes.Freshli.Cli.Functionality.History;
using Moq;
using Xunit;

namespace Corgibytes.Freshli.Cli.Test.Functionality.History;

public class ComputeHistoryActivityTest
{
private readonly Mock<IApplicationEventEngine> _eventEngine = new();
private readonly Mock<IComputeHistory> _computeHistory = new();
private readonly Mock<ICacheDb> _cacheDb = new();

[Fact]
public void FiresHistoryIntervalStopFoundEvents()
{
// Arrange
const string repositoryId = "2dbc2fd2358e1ea1b7a6bc08ea647b9a337ac92d";

// Have an analysis available
var cachedAnalysis = new CachedAnalysis("https://lorem-ipsum.com", "main", "month");
_cacheDb.Setup(mock => mock.RetrieveAnalysis(It.IsAny<Guid>())).Returns(cachedAnalysis);


// Have interval stops available
var historyIntervalStops = new List<HistoryIntervalStop>
{
new(
"75c7fcc7336ee718050c4a5c8dfb5598622787b2",
new DateTimeOffset(2021, 2, 20, 12, 31, 34, TimeSpan.Zero)
),
new(
"583d813db3e28b9b44a29db352e2f0e1b4c6e420",
new DateTimeOffset(2021, 5, 19, 15, 24, 24, TimeSpan.Zero)
)
};
_computeHistory.Setup(mock => mock.ComputeWithHistoryInterval(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())
)
.Returns(historyIntervalStops);

// Act
new ComputeHistoryActivity(
"git",
_cacheDb.Object,
_computeHistory.Object,
new Guid("cbc83480-ae47-46de-91df-60747ca8fb09"),
repositoryId
).Handle(_eventEngine.Object);

// Assert
_eventEngine.Verify(
mock => mock.Fire(
It.Is<HistoryIntervalStopFoundEvent>(
value => value.GitCommitIdentifier == "75c7fcc7336ee718050c4a5c8dfb5598622787b2" && value.RepositoryId == repositoryId
)
)
);
_eventEngine.Verify(
mock => mock.Fire(
It.Is<HistoryIntervalStopFoundEvent>(
value => value.GitCommitIdentifier == "583d813db3e28b9b44a29db352e2f0e1b4c6e420" && value.RepositoryId == repositoryId
)
)
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Corgibytes.Freshli.Cli.CommandRunners.Git;

public class ComputeHistoryCommandRunner : CommandRunner<ComputeHistoryCommand, ComputeHistoryCommandOptions>
{
private readonly ComputeHistory _computeHistory;
private readonly IComputeHistory _computeHistory;

public ComputeHistoryCommandRunner(IServiceProvider serviceProvider, Runner runner, ComputeHistory computeHistory) :
public ComputeHistoryCommandRunner(IServiceProvider serviceProvider, Runner runner, IComputeHistory computeHistory) :
base(serviceProvider, runner) => _computeHistory = computeHistory;

public override int Run(ComputeHistoryCommandOptions options, InvocationContext context)
Expand Down
7 changes: 6 additions & 1 deletion Corgibytes.Freshli.Cli/Functionality/CacheDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ public class CacheDb : ICacheDb, IDisposable
{
private bool _disposed;

public CacheDb(string cacheDir) => Db = new CacheContext(cacheDir);
public CacheDb(string cacheDir)
{
CacheDir = cacheDir;
Db = new CacheContext(cacheDir);
}

private CacheContext Db { get; }
public string CacheDir { get; }

public Guid SaveAnalysis(CachedAnalysis analysis)
{
Expand Down
2 changes: 1 addition & 1 deletion Corgibytes.Freshli.Cli/Functionality/Git/ComputeHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Corgibytes.Freshli.Cli.Functionality.Git;

public class ComputeHistory
public class ComputeHistory : IComputeHistory
{
private readonly IListCommits _listCommits;

Expand Down
15 changes: 15 additions & 0 deletions Corgibytes.Freshli.Cli/Functionality/Git/IComputeHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace Corgibytes.Freshli.Cli.Functionality.Git;

public interface IComputeHistory
{
public IEnumerable<HistoryIntervalStop> ComputeCommitHistory(string repositoryId, string gitPath, string cacheDir);

public IEnumerable<HistoryIntervalStop> ComputeWithHistoryInterval(
string repositoryId,
string gitPath,
string historyInterval,
string cacheDir
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Functionality.Git;
using Newtonsoft.Json;

namespace Corgibytes.Freshli.Cli.Functionality.History;

public class ComputeHistoryActivity : IApplicationActivity
{
private readonly string _gitExecutablePath;
[JsonProperty] private readonly ICacheDb _cacheDb;
[JsonProperty] private readonly IComputeHistory _computeHistoryService;
[JsonProperty] private readonly Guid _analysisId;
[JsonProperty] private readonly string _repositoryId;

public ComputeHistoryActivity(string gitExecutablePath, ICacheDb cacheDb, IComputeHistory computeHistoryService, Guid analysisId, string repositoryId)
{
_gitExecutablePath = gitExecutablePath;
_cacheDb = cacheDb;
_computeHistoryService = computeHistoryService;
_analysisId = analysisId;
_repositoryId = repositoryId;
}

public void Handle(IApplicationEventEngine eventClient)
{
var analysis = _cacheDb.RetrieveAnalysis(_analysisId);
if (analysis == null)
{
return;
}

var historyIntervalStops =
_computeHistoryService.
ComputeWithHistoryInterval(_repositoryId, _gitExecutablePath, analysis.HistoryInterval, _cacheDb.CacheDir);

foreach (var historyIntervalStop in historyIntervalStops)
{
eventClient.Fire(new HistoryIntervalStopFoundEvent
{
GitCommitIdentifier = historyIntervalStop.GitCommitIdentifier,
RepositoryId = _repositoryId
});
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Corgibytes.Freshli.Cli.Functionality.Engine;

namespace Corgibytes.Freshli.Cli.Functionality.History;

public class HistoryIntervalStopFoundEvent : IApplicationEvent
{
public string? GitCommitIdentifier { get; init; }
public string? RepositoryId { get; init; }

public void Handle(IApplicationActivityEngine eventClient)
{
}
}

1 change: 1 addition & 0 deletions Corgibytes.Freshli.Cli/Functionality/ICache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Corgibytes.Freshli.Cli.Functionality;

public interface ICacheDb
{
public string CacheDir { get; }
public Guid SaveAnalysis(CachedAnalysis analysis);
public CachedAnalysis? RetrieveAnalysis(Guid id);
}
2 changes: 1 addition & 1 deletion Corgibytes.Freshli.Cli/IoC/FreshliServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void RegisterGitCommand()
ComputeHistoryCommandRunner>();
Services.AddOptions<ComputeHistoryCommandOptions>().BindCommandLine();

Services.AddTransient<ComputeHistory>();
Services.AddScoped<IComputeHistory, ComputeHistory>();
Services.AddScoped<IListCommits, ListCommits>();

Services.AddScoped<GitArchive>();
Expand Down