refactor: Remove common repo code from Radarr guide parser

pull/56/head
Robert Dailey 3 years ago
parent 89210ec756
commit 67cc4bd98f

@ -12,6 +12,7 @@ using TrashLib.Cache;
using TrashLib.Config;
using TrashLib.Radarr;
using TrashLib.Radarr.Config;
using TrashLib.Repo;
using TrashLib.Sonarr;
using TrashLib.Startup;
using VersionControl;
@ -83,6 +84,7 @@ public static class CompositionRoot
builder.RegisterModule<CacheAutofacModule>();
builder.RegisterType<CacheStoragePath>().As<ICacheStoragePath>();
builder.RegisterType<RepoUpdater>().As<IRepoUpdater>();
ConfigurationRegistrations(builder);
CommandRegistrations(builder);

@ -1,81 +1,27 @@
using System.IO.Abstractions;
using Common;
using LibGit2Sharp;
using Serilog;
using TrashLib.Config.Settings;
using TrashLib.Radarr.Config;
using VersionControl;
using TrashLib.Repo;
namespace TrashLib.Radarr.CustomFormat.Guide;
internal class LocalRepoCustomFormatJsonParser : IRadarrGuideService
{
private readonly ILogger _log;
private readonly IFileSystem _fileSystem;
private readonly IGitRepositoryFactory _repositoryFactory;
private readonly IFileUtilities _fileUtils;
private readonly ISettingsProvider _settingsProvider;
private readonly string _repoPath;
private readonly IRepoUpdater _repoUpdater;
public LocalRepoCustomFormatJsonParser(
ILogger log,
IFileSystem fileSystem,
IResourcePaths paths,
IGitRepositoryFactory repositoryFactory,
IFileUtilities fileUtils,
ISettingsProvider settingsProvider)
public LocalRepoCustomFormatJsonParser(IFileSystem fileSystem, IRepoUpdater repoUpdater)
{
_log = log;
_fileSystem = fileSystem;
_repositoryFactory = repositoryFactory;
_fileUtils = fileUtils;
_settingsProvider = settingsProvider;
_repoPath = paths.RepoPath;
_repoUpdater = repoUpdater;
}
public IEnumerable<string> GetCustomFormatJson()
{
// Retry only once if there's a failure. This gives us an opportunity to delete the git repository and start
// fresh.
var exception = CheckoutAndUpdateRepo();
if (exception is not null)
{
_log.Information("Deleting local git repo and retrying git operation...");
_fileUtils.DeleteReadOnlyDirectory(_repoPath);
_repoUpdater.UpdateRepo();
exception = CheckoutAndUpdateRepo();
if (exception is not null)
{
throw exception;
}
}
var jsonDir = Path.Combine(_repoPath, "docs/json/radarr");
var jsonDir = Path.Combine(_repoUpdater.RepoPath, "docs/json/radarr");
var tasks = _fileSystem.Directory.GetFiles(jsonDir, "*.json")
.Select(async f => await _fileSystem.File.ReadAllTextAsync(f));
return Task.WhenAll(tasks).Result;
}
private Exception? CheckoutAndUpdateRepo()
{
var repoSettings = _settingsProvider.Settings.Repository;
var cloneUrl = repoSettings.CloneUrl;
const string branch = "master";
try
{
using var repo = _repositoryFactory.CreateAndCloneIfNeeded(cloneUrl, _repoPath, branch);
repo.ForceCheckout(branch);
repo.Fetch();
repo.ResetHard($"origin/{branch}");
}
catch (LibGit2SharpException e)
{
_log.Error(e, "An exception occurred during git operations on path: {RepoPath}", _repoPath);
return e;
}
return null;
}
}

@ -0,0 +1,7 @@
namespace TrashLib.Repo;
public interface IRepoUpdater
{
string RepoPath { get; }
void UpdateRepo();
}

@ -0,0 +1,72 @@
using Common;
using LibGit2Sharp;
using Serilog;
using TrashLib.Config.Settings;
using TrashLib.Radarr.Config;
using VersionControl;
namespace TrashLib.Repo;
public class RepoUpdater : IRepoUpdater
{
private readonly ILogger _log;
private readonly IGitRepositoryFactory _repositoryFactory;
private readonly IFileUtilities _fileUtils;
private readonly ISettingsProvider _settingsProvider;
public RepoUpdater(
ILogger log,
IResourcePaths paths,
IGitRepositoryFactory repositoryFactory,
IFileUtilities fileUtils,
ISettingsProvider settingsProvider)
{
_log = log;
_repositoryFactory = repositoryFactory;
_fileUtils = fileUtils;
_settingsProvider = settingsProvider;
RepoPath = paths.RepoPath;
}
public string RepoPath { get; }
public void UpdateRepo()
{
// Retry only once if there's a failure. This gives us an opportunity to delete the git repository and start
// fresh.
var exception = CheckoutAndUpdateRepo();
if (exception is not null)
{
_log.Information("Deleting local git repo and retrying git operation...");
_fileUtils.DeleteReadOnlyDirectory(RepoPath);
exception = CheckoutAndUpdateRepo();
if (exception is not null)
{
throw exception;
}
}
}
private Exception? CheckoutAndUpdateRepo()
{
var repoSettings = _settingsProvider.Settings.Repository;
var cloneUrl = repoSettings.CloneUrl;
const string branch = "master";
try
{
using var repo = _repositoryFactory.CreateAndCloneIfNeeded(cloneUrl, RepoPath, branch);
repo.ForceCheckout(branch);
repo.Fetch();
repo.ResetHard($"origin/{branch}");
}
catch (LibGit2SharpException e)
{
_log.Error(e, "An exception occurred during git operations on path: {RepoPath}", RepoPath);
return e;
}
return null;
}
}
Loading…
Cancel
Save