Skip to content

Commit a715702

Browse files
Delete Repository from SQLite if it has been deleted from GitHub
1 parent 4667c1d commit a715702

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

GitTrends/Database/RepositoryDatabase.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using GitTrends.Mobile.Common;
56
using GitTrends.Shared;
67
using SQLite;
78
using Xamarin.Essentials.Interfaces;
@@ -42,6 +43,29 @@ public async Task DeleteExpiredData()
4243
await dailyViewsDatabaseConnection.DeleteAsync(expiredDailyView).ConfigureAwait(false);
4344
}
4445

46+
public async Task DeleteRepository(Repository repository)
47+
{
48+
var (repositoryDatabaseConnection,
49+
dailyClonesDatabaseConnection,
50+
dailyViewsDatabaseConnection,
51+
starGazerInfoDatabaseConnection) = await GetDatabaseConnections().ConfigureAwait(false);
52+
53+
var dailyViews = await dailyViewsDatabaseConnection.Table<DailyViewsDatabaseModel>().ToListAsync();
54+
var dailyClones = await dailyClonesDatabaseConnection.Table<DailyClonesDatabaseModel>().ToListAsync();
55+
var starGazerInfos = await starGazerInfoDatabaseConnection.Table<StarGazerInfoDatabaseModel>().ToListAsync();
56+
57+
foreach (var dailyClone in dailyClones.Where(x => x.RepositoryUrl == repository.Url))
58+
await dailyClonesDatabaseConnection.DeleteAsync(dailyClone).ConfigureAwait(false);
59+
60+
foreach (var dailyView in dailyViews.Where(x => x.RepositoryUrl == repository.Url))
61+
await dailyViewsDatabaseConnection.DeleteAsync(dailyView).ConfigureAwait(false);
62+
63+
foreach (var starGazerInfo in starGazerInfos.Where(x => x.RepositoryUrl == repository.Url))
64+
await starGazerInfoDatabaseConnection.DeleteAsync(starGazerInfo).ConfigureAwait(false);
65+
66+
await repositoryDatabaseConnection.DeleteAsync(RepositoryDatabaseModel.ToRepositoryDatabase(repository)).ConfigureAwait(false);
67+
}
68+
4569
public async Task SaveRepository(Repository repository)
4670
{
4771
var databaseConnection = await GetDatabaseConnection<RepositoryDatabaseModel>().ConfigureAwait(false);

GitTrends/Services/GitHubApiRepositoriesService.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class GitHubApiRepositoriesService
1919
readonly IAnalyticsService _analyticsService;
2020
readonly GitHubUserService _gitHubUserService;
2121
readonly GitHubApiV3Service _gitHubApiV3Service;
22+
readonly RepositoryDatabase _repositoryDatabase;
2223
readonly ReferringSitesDatabase _referringSitesDatabase;
2324
readonly GitHubApiStatusService _gitHubApiStatusService;
2425
readonly GitHubGraphQLApiService _gitHubGraphQLApiService;
@@ -27,6 +28,7 @@ public GitHubApiRepositoriesService(FavIconService favIconService,
2728
IAnalyticsService analyticsService,
2829
GitHubUserService gitHubUserService,
2930
GitHubApiV3Service gitHubApiV3Service,
31+
RepositoryDatabase repositoryDatabase,
3032
ReferringSitesDatabase referringSitesDatabase,
3133
GitHubApiStatusService gitHubApiStatusService,
3234
GitHubGraphQLApiService gitHubGraphQLApiService)
@@ -35,6 +37,7 @@ public GitHubApiRepositoriesService(FavIconService favIconService,
3537
_analyticsService = analyticsService;
3638
_gitHubUserService = gitHubUserService;
3739
_gitHubApiV3Service = gitHubApiV3Service;
40+
_repositoryDatabase = repositoryDatabase;
3841
_referringSitesDatabase = referringSitesDatabase;
3942
_gitHubApiStatusService = gitHubApiStatusService;
4043
_gitHubGraphQLApiService = gitHubGraphQLApiService;
@@ -138,9 +141,9 @@ public async IAsyncEnumerable<Repository> UpdateRepositoriesWithViewsClonesAndSt
138141
await getCloneStatisticsTask.ConfigureAwait(false),
139142
await getStarGazrsTask.ConfigureAwait(false));
140143
}
141-
catch (ApiException e) when (_gitHubApiStatusService.IsAbuseRateLimit(e.Headers, out var timespan) && timespan is TimeSpan retryTimeSpan)
144+
catch (ApiException e) when (_gitHubApiStatusService.IsAbuseRateLimit(e.Headers, out var timespan))
142145
{
143-
OnAbuseRateLimitFound_UpdateRepositoriesWithViewsClonesAndStarsData(repository, retryTimeSpan);
146+
OnAbuseRateLimitFound_UpdateRepositoriesWithViewsClonesAndStarsData(repository, timespan.Value);
144147

145148
return (null, null, null);
146149
}
@@ -155,6 +158,17 @@ await getCloneStatisticsTask.ConfigureAwait(false),
155158
reportException(e);
156159

157160
return (null, null, null);
161+
}
162+
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
163+
{
164+
reportException(e);
165+
166+
var repositoryFromDatabase = await _repositoryDatabase.GetRepository(repository.Url).ConfigureAwait(false);
167+
if (repositoryFromDatabase is null)
168+
throw;
169+
170+
await _repositoryDatabase.DeleteRepository(repository).ConfigureAwait(false);
171+
return (null, null, null);
158172
}
159173

160174
void reportException(in Exception e)

GitTrends/ViewModels/RepositoryViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ async Task ExecutePullToRefreshCommand(string repositoryOwner)
254254
catch (Exception e) when (_gitHubApiStatusService.IsAbuseRateLimit(e, out var retryTimeSpan)
255255
|| (e is HttpRequestException && finalResponse is not null && _gitHubApiStatusService.IsAbuseRateLimit(finalResponse.Headers, out retryTimeSpan)))
256256
{
257-
if (repositoriesFromDatabase is null)
258-
repositoriesFromDatabase = await repositoriesFromDatabaseTask.ConfigureAwait(false);
257+
repositoriesFromDatabase ??= await repositoriesFromDatabaseTask.ConfigureAwait(false);
259258

260259
//Rate Limiting may cause some data to not return successfully from the GitHub API
261260
var missingRepositories = _gitHubUserService.ShouldIncludeOrganizations switch

0 commit comments

Comments
 (0)