Skip to content

Commit

Permalink
Added logging on GithubPluginUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisoft committed Dec 3, 2024
1 parent 847e9de commit 6f55098
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
5 changes: 4 additions & 1 deletion ASFFreeGames/ASFFreeGamesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,15 @@ private async Task RemoveBot(Bot bot) {
private void StartTimerIfNeeded() => CollectIntervalManager.StartTimerIfNeeded();

~ASFFreeGamesPlugin() => CollectIntervalManager.Dispose();
public readonly GithubPluginUpdater Updater = new(new Lazy<Version>(GetVersion));

#region IGitHubPluginUpdates implementation
private readonly GithubPluginUpdater Updater = new(new Lazy<Version>(GetVersion));
string IGitHubPluginUpdates.RepositoryName => GithubPluginUpdater.RepositoryName;

bool IGitHubPluginUpdates.CanUpdate => Updater.CanUpdate;

Task<Uri?> IGitHubPluginUpdates.GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) => Updater.GetTargetReleaseURL(asfVersion, asfVariant, asfUpdate, stable, forced);
#endregion
}

#pragma warning restore CA1812 // ASF uses this class during runtime
37 changes: 29 additions & 8 deletions ASFFreeGames/Github/GithubPluginUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,55 @@ public class GithubPluginUpdater(Lazy<Version> version) {

private Version CurrentVersion => version.Value;

private static void LogGenericError(string message) {
if (string.IsNullOrEmpty(message)) {
return;
}

ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericError($"{nameof(GithubPluginUpdater)}: {message}");
}

private static void LogGenericDebug(string message) {
if (string.IsNullOrEmpty(message)) {
return;
}

ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericDebug($"{nameof(GithubPluginUpdater)}: {message}");
}

public async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) {
ArgumentNullException.ThrowIfNull(asfVersion);
ArgumentException.ThrowIfNullOrEmpty(asfVariant);

if (!CanUpdate) {
LogGenericDebug("CanUpdate is false");

return null;
}

if (string.IsNullOrEmpty(RepositoryName)) {
//ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericError(Strings.FormatWarningFailedWithError(nameof(RepositoryName)));
LogGenericError("RepositoryName is null or empty");

return null;
}

ReleaseResponse? releaseResponse = await GitHubService.GetLatestRelease(RepositoryName).ConfigureAwait(false);

if (releaseResponse == null) {
LogGenericError("GetLatestRelease returned null");

return null;
}

if (releaseResponse.IsPreRelease) {
LogGenericError("GetLatestRelease returned pre-release");

return null;
}

if (stable && !((releaseResponse.PublishedAt - DateTime.UtcNow).Duration() > TimeSpan.FromHours(3))) {
// Skip updates that are too recent
if (stable && ((releaseResponse.PublishedAt - DateTime.UtcNow).Duration() < TimeSpan.FromHours(3))) {
LogGenericDebug("GetLatestRelease returned too recent");

return null;
}

Expand All @@ -48,27 +71,25 @@ public class GithubPluginUpdater(Lazy<Version> version) {
if (!forced && (CurrentVersion >= newVersion)) {
// Allow same version to be re-updated when we're updating ASF release and more than one asset is found - potential compatibility difference
if ((CurrentVersion > newVersion) || !asfUpdate || (releaseResponse.Assets.Count(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) < 2)) {
//ASF.ArchiLogger.LogGenericInfo(Strings.FormatPluginUpdateNotFound(Name, Version, newVersion));

return null;
}
}

if (releaseResponse.Assets.Count == 0) {
//ASF.ArchiLogger.LogGenericWarning(Strings.FormatPluginUpdateNoAssetFound(Name, Version, newVersion));
LogGenericError($"GetLatestRelease for version {newVersion} returned no assets");

return null;
}

ReleaseAsset? asset = releaseResponse.Assets.FirstOrDefault(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) && (asset.Size > (1 << 18)));

if ((asset == null) || !releaseResponse.Assets.Contains(asset)) {
//ASF.ArchiLogger.LogGenericWarning(Strings.FormatPluginUpdateNoAssetFound(Name, Version, newVersion));
LogGenericError($"GetLatestRelease for version {newVersion} returned no valid assets");

return null;
}

//.ArchiLogger.LogGenericInfo(Strings.FormatPluginUpdateFound(Name, Version, newVersion));
LogGenericDebug($"GetLatestRelease for version {newVersion} returned asset {asset.Name} with url {asset.DownloadURL}");

return asset.DownloadURL;
}
Expand Down

0 comments on commit 6f55098

Please sign in to comment.