Skip to content

Commit

Permalink
Merge pull request #233 from corgibytes/feature/git_clone_repository
Browse files Browse the repository at this point in the history
Dispatches compute history activity from git repository cloned event
  • Loading branch information
klhoot authored Sep 13, 2022
2 parents b7213cd + 844d4ed commit 0045d1b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void HandlerFiresGitRepositoryClonedEventWhenInvokedFromCli()
activity.Handle(_eventEngine.Object);

_eventEngine.Verify(mock =>
mock.Fire(It.Is<GitRepositoryClonedEvent>(value => value.GitRepositoryId == _repositoryId)));
mock.Fire(It.Is<GitRepositoryClonedEvent>(value =>
value.AnalysisLocation.RepositoryId == _repositoryId)));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Corgibytes.Freshli.Cli.Functionality.Analysis;
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.Git;

[UnitTest]
public class GitRepositoryClonedEventTest
{
[Fact]
public void CorrectlyDispatchesComputeHistoryActivity()
{
var gitPath = "test";
var cacheDir = "example";
var gitRepositoryId = "example";
var analysisId = new Guid();
var analysisLocation = new AnalysisLocation(cacheDir, gitRepositoryId);

var clonedEvent = new GitRepositoryClonedEvent
{
AnalysisId = analysisId,
GitPath = gitPath,
AnalysisLocation = analysisLocation
};

var engine = new Mock<IApplicationActivityEngine>();

clonedEvent.Handle(engine.Object);

// Verify that it dispatches ComputeHistoryActivity
engine.Verify(mock => mock.Dispatch(It.Is<ComputeHistoryActivity>(value =>
value.AnalysisId == analysisId &&
value.GitExecutablePath == gitPath &&
value.AnalysisLocation == analysisLocation)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ public void FiresHistoryIntervalStopFoundEvents()

var analysisLocation = new Mock<IAnalysisLocation>();

var serviceProvider = new Mock<IServiceProvider>();
_eventEngine.Setup(mock => mock.ServiceProvider).Returns(serviceProvider.Object);

var cacheManager = new Mock<ICacheManager>();
cacheManager.Setup(mock => mock.GetCacheDb(It.IsAny<string>())).Returns(_cacheDb.Object);
serviceProvider.Setup(mock => mock.GetService(typeof(ICacheManager))).Returns(cacheManager.Object);

serviceProvider.Setup(mock => mock.GetService(typeof(IComputeHistory))).Returns(_computeHistory.Object);

// Act
new ComputeHistoryActivity(
"git",
_cacheDb.Object,
_computeHistory.Object,
new Guid("cbc83480-ae47-46de-91df-60747ca8fb09"),
analysisLocation.Object
).Handle(_eventEngine.Object);
Expand Down Expand Up @@ -97,11 +104,18 @@ public void FiresHistoryIntervalStopFoundEventsForComputeHistory()

var analysisLocation = new Mock<IAnalysisLocation>();

var serviceProvider = new Mock<IServiceProvider>();
_eventEngine.Setup(mock => mock.ServiceProvider).Returns(serviceProvider.Object);

var cacheManager = new Mock<ICacheManager>();
cacheManager.Setup(mock => mock.GetCacheDb(It.IsAny<string>())).Returns(_cacheDb.Object);
serviceProvider.Setup(mock => mock.GetService(typeof(ICacheManager))).Returns(cacheManager.Object);

serviceProvider.Setup(mock => mock.GetService(typeof(IComputeHistory))).Returns(_computeHistory.Object);

// Act
new ComputeHistoryActivity(
"git",
_cacheDb.Object,
_computeHistory.Object,
new Guid("cbc83480-ae47-46de-91df-60747ca8fb09"),
analysisLocation.Object
).Handle(_eventEngine.Object);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Corgibytes.Freshli.Cli.Exceptions;
using Corgibytes.Freshli.Cli.Functionality.Analysis;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
Expand All @@ -9,34 +10,48 @@ namespace Corgibytes.Freshli.Cli.Functionality.Git;
public class CloneGitRepositoryActivity : IApplicationActivity
{
[JsonProperty] private readonly Guid _analysisId;
[JsonProperty] private readonly string? _branch;

[JsonProperty] private readonly string _cacheDir;
[JsonProperty] private readonly string _gitPath;

[JsonProperty] private readonly string _repoUrl;

public CloneGitRepositoryActivity(string repoUrl, string? branch, string cacheDir, string gitPath,
Guid analysisId = new())
{
_repoUrl = repoUrl;
_branch = branch;
RepoUrl = repoUrl;
Branch = branch;
_cacheDir = cacheDir;
_gitPath = gitPath;
_analysisId = analysisId;
}

[JsonProperty] private string? Branch { get; set; }
[JsonProperty] private string RepoUrl { get; set; }

public void Handle(IApplicationEventEngine eventClient)
{
// Clone or pull the given repository and branch.
try
{
var cacheManager = eventClient.ServiceProvider.GetRequiredService<ICacheManager>();
var cacheDb = cacheManager.GetCacheDb(_cacheDir);
var cachedAnalysis = cacheDb.RetrieveAnalysis(_analysisId);
if (cachedAnalysis != null)
{
RepoUrl = cachedAnalysis.RepositoryUrl;
Branch = cachedAnalysis.RepositoryBranch;
}

var gitRepository =
eventClient.ServiceProvider.GetRequiredService<ICachedGitSourceRepository>().CloneOrPull(_repoUrl, _branch, _cacheDir, _gitPath);
eventClient.ServiceProvider.GetRequiredService<ICachedGitSourceRepository>()
.CloneOrPull(RepoUrl, Branch, _cacheDir, _gitPath);

var analysisLocation = new AnalysisLocation(_cacheDir, gitRepository.Id);

eventClient.Fire(new GitRepositoryClonedEvent
{
GitRepositoryId = gitRepository.Id,
AnalysisId = _analysisId
AnalysisId = _analysisId,
GitPath = _gitPath,
AnalysisLocation = analysisLocation
});
}
catch (GitException e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using Corgibytes.Freshli.Cli.Functionality.Analysis;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Functionality.History;

namespace Corgibytes.Freshli.Cli.Functionality.Git;

public class GitRepositoryClonedEvent : IApplicationEvent
{
public string GitRepositoryId { get; init; } = null!;

// ReSharper disable once UnusedAutoPropertyAccessor.Global
public Guid AnalysisId { get; init; }

public void Handle(IApplicationActivityEngine eventClient)
{
// TODO - Dispatch(ComputeHistoryActivity)
}
public string GitPath { get; init; } = null!;

public AnalysisLocation AnalysisLocation { get; init; } = null!;

public void Handle(IApplicationActivityEngine eventClient) =>
eventClient.Dispatch(new ComputeHistoryActivity(GitPath, AnalysisId, AnalysisLocation));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
using Corgibytes.Freshli.Cli.Functionality.Analysis;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Functionality.Git;
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;

namespace Corgibytes.Freshli.Cli.Functionality.History;

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

public ComputeHistoryActivity(string gitExecutablePath, ICacheDb cacheDb, IComputeHistory computeHistoryService,
Guid analysisId, IAnalysisLocation analysisLocation)
public readonly IAnalysisLocation AnalysisLocation;

public readonly string GitExecutablePath;
public Guid AnalysisId;

public ComputeHistoryActivity(string gitExecutablePath, Guid analysisId,
IAnalysisLocation analysisLocation)
{
_gitExecutablePath = gitExecutablePath;
_cacheDb = cacheDb;
_computeHistoryService = computeHistoryService;
_analysisId = analysisId;
_analysisLocation = analysisLocation;
GitExecutablePath = gitExecutablePath;
AnalysisId = analysisId;
AnalysisLocation = analysisLocation;
}

public void Handle(IApplicationEventEngine eventClient)
{
var analysis = _cacheDb.RetrieveAnalysis(_analysisId);
var computeHistoryService = eventClient.ServiceProvider.GetRequiredService<IComputeHistory>();
var cacheManager = eventClient.ServiceProvider.GetRequiredService<ICacheManager>();
var cacheDb = cacheManager.GetCacheDb(AnalysisLocation.CacheDirectory);
var analysis = cacheDb.RetrieveAnalysis(AnalysisId);
if (analysis == null)
{
return;
Expand All @@ -37,21 +37,21 @@ public void Handle(IApplicationEventEngine eventClient)

if (analysis.UseCommitHistory.Equals(CommitHistory.AtInterval))
{
historyIntervalStops = _computeHistoryService
.ComputeWithHistoryInterval(_analysisLocation, _gitExecutablePath, analysis.HistoryInterval);
historyIntervalStops = computeHistoryService
.ComputeWithHistoryInterval(AnalysisLocation, GitExecutablePath, analysis.HistoryInterval);
}
else
{
historyIntervalStops =
_computeHistoryService.ComputeCommitHistory(_analysisLocation, _gitExecutablePath);
computeHistoryService.ComputeCommitHistory(AnalysisLocation, GitExecutablePath);
}

foreach (var historyIntervalStop in historyIntervalStops)
{
eventClient.Fire(new HistoryIntervalStopFoundEvent
{
GitCommitIdentifier = historyIntervalStop.GitCommitIdentifier,
AnalysisLocation = _analysisLocation
AnalysisLocation = AnalysisLocation
});
}
}
Expand Down

0 comments on commit 0045d1b

Please sign in to comment.