Skip to content

Commit

Permalink
Delete Repository from SQLite if it has been deleted from GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
brminnick committed Mar 17, 2022
1 parent 4667c1d commit a715702
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
24 changes: 24 additions & 0 deletions GitTrends/Database/RepositoryDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DailyViewsDatabaseModel>().ToListAsync();
var dailyClones = await dailyClonesDatabaseConnection.Table<DailyClonesDatabaseModel>().ToListAsync();
var starGazerInfos = await starGazerInfoDatabaseConnection.Table<StarGazerInfoDatabaseModel>().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<RepositoryDatabaseModel>().ConfigureAwait(false);
Expand Down
18 changes: 16 additions & 2 deletions GitTrends/Services/GitHubApiRepositoriesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@ public GitHubApiRepositoriesService(FavIconService favIconService,
IAnalyticsService analyticsService,
GitHubUserService gitHubUserService,
GitHubApiV3Service gitHubApiV3Service,
RepositoryDatabase repositoryDatabase,
ReferringSitesDatabase referringSitesDatabase,
GitHubApiStatusService gitHubApiStatusService,
GitHubGraphQLApiService gitHubGraphQLApiService)
Expand All @@ -35,6 +37,7 @@ public GitHubApiRepositoriesService(FavIconService favIconService,
_analyticsService = analyticsService;
_gitHubUserService = gitHubUserService;
_gitHubApiV3Service = gitHubApiV3Service;
_repositoryDatabase = repositoryDatabase;
_referringSitesDatabase = referringSitesDatabase;
_gitHubApiStatusService = gitHubApiStatusService;
_gitHubGraphQLApiService = gitHubGraphQLApiService;
Expand Down Expand Up @@ -138,9 +141,9 @@ public async IAsyncEnumerable<Repository> 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);
}
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions GitTrends/ViewModels/RepositoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a715702

Please sign in to comment.