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
Expand Up @@ -32,7 +32,7 @@ public void SavePersistsACachedAnalysisAndGeneratesAnId()

var cache = cacheManager.GetCacheDb(_tempCacheDir);

var expectedAnalysis = new CachedAnalysis("https://git.example.com", "main", "1m");
var expectedAnalysis = new CachedAnalysis("https://git.example.com", "main", "1m", _tempCacheDir, "git");

var id = cache.SaveAnalysis(expectedAnalysis);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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", "cacheDirectory", "git");
_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(
_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 @@ -10,14 +10,17 @@ namespace Corgibytes.Freshli.Cli.CommandRunners.Cache;
public class PrepareCacheActivity : IApplicationActivity
{
public PrepareCacheActivity(string cacheDirectory, string repositoryUrl = "", string? repositoryBranch = null,
string historyInterval = "")
string historyInterval = "", string gitPath = "")
{
CacheDirectory = cacheDirectory;
RepositoryUrl = repositoryUrl;
RepositoryBranch = repositoryBranch;
HistoryInterval = historyInterval;
GitPath = gitPath;
}

public string GitPath { get; init; }
mscottford marked this conversation as resolved.
Show resolved Hide resolved

public string RepositoryUrl { get; init; }

public string? RepositoryBranch { get; init; }
Expand All @@ -35,7 +38,7 @@ public void Handle(IApplicationEventEngine eventClient)
{
cacheManager.Prepare(CacheDirectory).ToExitCode();
var cacheDb = cacheManager.GetCacheDb(CacheDirectory);
cacheDb.SaveAnalysis(new CachedAnalysis(RepositoryUrl, RepositoryBranch, HistoryInterval));
cacheDb.SaveAnalysis(new CachedAnalysis(RepositoryUrl, RepositoryBranch, HistoryInterval, CacheDirectory, GitPath));
eventClient.Fire(new CachePreparedEvent());
}
catch (CacheException e)
Expand Down
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
8 changes: 7 additions & 1 deletion Corgibytes.Freshli.Cli/DataModel/CachedAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ namespace Corgibytes.Freshli.Cli.DataModel;
[Index(nameof(Id), IsUnique = true)]
public class CachedAnalysis
{
public CachedAnalysis(string repositoryUrl, string? repositoryBranch, string historyInterval)
public CachedAnalysis(string repositoryUrl, string? repositoryBranch, string historyInterval, string cacheDirectory, string gitPath)
{
CacheDirectory = cacheDirectory;
RepositoryUrl = repositoryUrl;
RepositoryBranch = repositoryBranch;
HistoryInterval = historyInterval;
GitPath = gitPath;
}

[Required] public string GitPath { get; set; }

[Required] public Guid Id { get; set; }
[Required] public string RepositoryUrl { get; set; }

public string? RepositoryBranch { get; set; }

[Required] public string CacheDirectory { get; set; }
mscottford marked this conversation as resolved.
Show resolved Hide resolved

// TODO: Research how to use a value class here instead of a string
[Required] public string HistoryInterval { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CacheWasNotPreparedEvent : ErrorEvent
public string RepositoryUrl { get; init; } = null!;
public string? RepositoryBranch { get; init; }
public string HistoryInterval { get; init; } = null!;
public string GitPath { get; init; } = null!;

public override void Handle(IApplicationActivityEngine eventClient)
{
Expand All @@ -21,7 +22,8 @@ public override void Handle(IApplicationActivityEngine eventClient)
CacheDirectory = CacheDirectory,
RepositoryUrl = RepositoryUrl,
RepositoryBranch = RepositoryBranch,
HistoryInterval = HistoryInterval
HistoryInterval = HistoryInterval,
GitPath = GitPath
});

eventClient.Dispatch(new RestartAnalysisActivity(
Expand All @@ -32,7 +34,8 @@ public override void Handle(IApplicationActivityEngine eventClient)
CacheDirectory = CacheDirectory,
RepositoryUrl = RepositoryUrl,
RepositoryBranch = RepositoryBranch,
HistoryInterval = HistoryInterval
HistoryInterval = HistoryInterval,
GitPath = GitPath
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected override CacheWasNotPreparedEvent CreateErrorEvent() =>
CacheDirectory = CacheDirectory,
RepositoryUrl = RepositoryUrl,
RepositoryBranch = RepositoryBranch,
HistoryInterval = HistoryInterval
HistoryInterval = HistoryInterval,
GitPath = GitPath
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected StartAnalysisActivityBase(ICacheManager cacheManager, IHistoryInterval
public string RepositoryUrl { get; init; } = null!;
public string? RepositoryBranch { get; init; }
public string HistoryInterval { get; init; } = null!;
public string GitPath { get; init; } = null!;

public ICacheManager CacheManager { get; }
public IHistoryIntervalParser HistoryIntervalParser { get; }
Expand All @@ -25,7 +26,7 @@ protected StartAnalysisActivityBase(ICacheManager cacheManager, IHistoryInterval
private void FireAnalysisStartedEvent(IApplicationEventEngine eventClient)
{
var cacheDb = CacheManager.GetCacheDb(CacheDirectory);
var id = cacheDb.SaveAnalysis(new CachedAnalysis(RepositoryUrl, RepositoryBranch, HistoryInterval));
var id = cacheDb.SaveAnalysis(new CachedAnalysis(RepositoryUrl, RepositoryBranch, HistoryInterval, CacheDirectory, GitPath));
eventClient.Fire(new AnalysisStartedEvent { AnalysisId = id });
}

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,45 @@
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
{
[JsonProperty] private readonly ICacheDb _cacheDb;
[JsonProperty] private readonly IComputeHistory _computeHistoryService;
[JsonProperty] private readonly Guid _analysisId;
[JsonProperty] private readonly string _repositoryId;

public ComputeHistoryActivity(ICacheDb cacheDb, IComputeHistory computeHistoryService, Guid analysisId, string repositoryId)
{
_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, analysis.GitPath, analysis.HistoryInterval, analysis.CacheDirectory);

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)
{
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ protected override void Up(MigrationBuilder migrationBuilder)
Id = table.Column<Guid>(type: "TEXT", nullable: false),
RepositoryUrl = table.Column<string>(type: "TEXT", nullable: false),
RepositoryBranch = table.Column<string>(type: "TEXT", nullable: true),
HistoryInterval = table.Column<string>(type: "TEXT", nullable: false)
HistoryInterval = table.Column<string>(type: "TEXT", nullable: false),
CacheDirectory = table.Column<string>(type: "TEXT", nullable: false),
GitPath = table.Column<string>(type: "TEXT", nullable: false),
},
constraints: table =>
{
Expand Down