From a7157022109a59422c736b1821c58143e999c7e4 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Wed, 16 Mar 2022 17:16:13 -0700 Subject: [PATCH] Delete Repository from SQLite if it has been deleted from GitHub --- GitTrends/Database/RepositoryDatabase.cs | 24 +++++++++++++++++++ .../Services/GitHubApiRepositoriesService.cs | 18 ++++++++++++-- GitTrends/ViewModels/RepositoryViewModel.cs | 3 +-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/GitTrends/Database/RepositoryDatabase.cs b/GitTrends/Database/RepositoryDatabase.cs index 832f8a397..353561587 100644 --- a/GitTrends/Database/RepositoryDatabase.cs +++ b/GitTrends/Database/RepositoryDatabase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using GitTrends.Mobile.Common; using GitTrends.Shared; using SQLite; using Xamarin.Essentials.Interfaces; @@ -42,6 +43,29 @@ public async Task DeleteExpiredData() await dailyViewsDatabaseConnection.DeleteAsync(expiredDailyView).ConfigureAwait(false); } + public async Task DeleteRepository(Repository repository) + { + var (repositoryDatabaseConnection, + dailyClonesDatabaseConnection, + dailyViewsDatabaseConnection, + starGazerInfoDatabaseConnection) = await GetDatabaseConnections().ConfigureAwait(false); + + var dailyViews = await dailyViewsDatabaseConnection.Table().ToListAsync(); + var dailyClones = await dailyClonesDatabaseConnection.Table().ToListAsync(); + var starGazerInfos = await starGazerInfoDatabaseConnection.Table().ToListAsync(); + + foreach (var dailyClone in dailyClones.Where(x => x.RepositoryUrl == repository.Url)) + await dailyClonesDatabaseConnection.DeleteAsync(dailyClone).ConfigureAwait(false); + + foreach (var dailyView in dailyViews.Where(x => x.RepositoryUrl == repository.Url)) + await dailyViewsDatabaseConnection.DeleteAsync(dailyView).ConfigureAwait(false); + + foreach (var starGazerInfo in starGazerInfos.Where(x => x.RepositoryUrl == repository.Url)) + await starGazerInfoDatabaseConnection.DeleteAsync(starGazerInfo).ConfigureAwait(false); + + await repositoryDatabaseConnection.DeleteAsync(RepositoryDatabaseModel.ToRepositoryDatabase(repository)).ConfigureAwait(false); + } + public async Task SaveRepository(Repository repository) { var databaseConnection = await GetDatabaseConnection().ConfigureAwait(false); diff --git a/GitTrends/Services/GitHubApiRepositoriesService.cs b/GitTrends/Services/GitHubApiRepositoriesService.cs index 37d18e70a..09286be37 100644 --- a/GitTrends/Services/GitHubApiRepositoriesService.cs +++ b/GitTrends/Services/GitHubApiRepositoriesService.cs @@ -19,6 +19,7 @@ public class GitHubApiRepositoriesService readonly IAnalyticsService _analyticsService; readonly GitHubUserService _gitHubUserService; readonly GitHubApiV3Service _gitHubApiV3Service; + readonly RepositoryDatabase _repositoryDatabase; readonly ReferringSitesDatabase _referringSitesDatabase; readonly GitHubApiStatusService _gitHubApiStatusService; readonly GitHubGraphQLApiService _gitHubGraphQLApiService; @@ -27,6 +28,7 @@ public GitHubApiRepositoriesService(FavIconService favIconService, IAnalyticsService analyticsService, GitHubUserService gitHubUserService, GitHubApiV3Service gitHubApiV3Service, + RepositoryDatabase repositoryDatabase, ReferringSitesDatabase referringSitesDatabase, GitHubApiStatusService gitHubApiStatusService, GitHubGraphQLApiService gitHubGraphQLApiService) @@ -35,6 +37,7 @@ public GitHubApiRepositoriesService(FavIconService favIconService, _analyticsService = analyticsService; _gitHubUserService = gitHubUserService; _gitHubApiV3Service = gitHubApiV3Service; + _repositoryDatabase = repositoryDatabase; _referringSitesDatabase = referringSitesDatabase; _gitHubApiStatusService = gitHubApiStatusService; _gitHubGraphQLApiService = gitHubGraphQLApiService; @@ -138,9 +141,9 @@ public async IAsyncEnumerable UpdateRepositoriesWithViewsClonesAndSt await getCloneStatisticsTask.ConfigureAwait(false), await getStarGazrsTask.ConfigureAwait(false)); } - catch (ApiException e) when (_gitHubApiStatusService.IsAbuseRateLimit(e.Headers, out var timespan) && timespan is TimeSpan retryTimeSpan) + catch (ApiException e) when (_gitHubApiStatusService.IsAbuseRateLimit(e.Headers, out var timespan)) { - OnAbuseRateLimitFound_UpdateRepositoriesWithViewsClonesAndStarsData(repository, retryTimeSpan); + OnAbuseRateLimitFound_UpdateRepositoriesWithViewsClonesAndStarsData(repository, timespan.Value); return (null, null, null); } @@ -155,6 +158,17 @@ await getCloneStatisticsTask.ConfigureAwait(false), reportException(e); return (null, null, null); + } + catch (ApiException e) when (e.StatusCode is System.Net.HttpStatusCode.NotFound) // Repository deleted from GitHub but has not yet been deleted from local SQLite Database + { + reportException(e); + + var repositoryFromDatabase = await _repositoryDatabase.GetRepository(repository.Url).ConfigureAwait(false); + if (repositoryFromDatabase is null) + throw; + + await _repositoryDatabase.DeleteRepository(repository).ConfigureAwait(false); + return (null, null, null); } void reportException(in Exception e) diff --git a/GitTrends/ViewModels/RepositoryViewModel.cs b/GitTrends/ViewModels/RepositoryViewModel.cs index 86ca03531..b1ee00434 100644 --- a/GitTrends/ViewModels/RepositoryViewModel.cs +++ b/GitTrends/ViewModels/RepositoryViewModel.cs @@ -254,8 +254,7 @@ async Task ExecutePullToRefreshCommand(string repositoryOwner) catch (Exception e) when (_gitHubApiStatusService.IsAbuseRateLimit(e, out var retryTimeSpan) || (e is HttpRequestException && finalResponse is not null && _gitHubApiStatusService.IsAbuseRateLimit(finalResponse.Headers, out retryTimeSpan))) { - if (repositoriesFromDatabase is null) - repositoriesFromDatabase = await repositoriesFromDatabaseTask.ConfigureAwait(false); + repositoriesFromDatabase ??= await repositoriesFromDatabaseTask.ConfigureAwait(false); //Rate Limiting may cause some data to not return successfully from the GitHub API var missingRepositories = _gitHubUserService.ShouldIncludeOrganizations switch