Skip to content

Commit

Permalink
Cleanup backfilling of syncTime for securityAdvisory (dart-lang#7178)
Browse files Browse the repository at this point in the history
  • Loading branch information
szakarias authored Nov 14, 2023
1 parent 6089eeb commit cd758d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
11 changes: 4 additions & 7 deletions app/lib/service/security_advisories/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ class SecurityAdvisoryBackend {
return errors.isEmpty;
}

/// Overwrites existing advisory with the same id, if [osv] is newer or
/// unconditionally if [resync] is set.
/// Overwrites existing advisory with the same id, if [osv] is newer.
///
/// If id is already listed as `alias` for another advisory, no action will be
/// taken to resolve this. Instead both advisories will be stored and served.
/// It's assumed that security advisory database owners take care to keep the
/// security advisories sound, and that inconsistencies are intentional.
Future<SecurityAdvisory?> ingestSecurityAdvisory(OSV osv, DateTime syncTime,
{bool resync = false}) async {
Future<SecurityAdvisory?> ingestSecurityAdvisory(
OSV osv, DateTime syncTime) async {
return await withRetryTransaction(_db, (tx) async {
DateTime modified;
try {
Expand All @@ -81,9 +80,7 @@ class SecurityAdvisoryBackend {

final oldAdvisory = await lookupById(osv.id);

if (!resync &&
oldAdvisory != null &&
oldAdvisory.modified!.isAtOrAfter(modified)) {
if (oldAdvisory != null && oldAdvisory.modified!.isAtOrAfter(modified)) {
return oldAdvisory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ Future<(Map<String, OSV>, List<String>)> loadAdvisoriesFromDir(
return (osvs, failedFiles);
}

Future<void> updateAdvisories(Map<String, OSV> osvs,
{bool resync = false}) async {
Future<void> updateAdvisories(Map<String, OSV> osvs) async {
final syncTime = clock.now();

final oldAdvisories = await securityAdvisoryBackend.listAdvisories();
Expand All @@ -70,20 +69,19 @@ Future<void> updateAdvisories(Map<String, OSV> osvs,
}

for (final osv in osvs.values) {
await securityAdvisoryBackend.ingestSecurityAdvisory(osv, syncTime,
resync: resync);
await securityAdvisoryBackend.ingestSecurityAdvisory(osv, syncTime);
}
}

/// Synchronizes the security advisory backend with security advisories from
/// osv.dev, overwriting existing advisories with the same id, if the fetched
/// advisories are newer or overwriting unconditionally if [resync] is set.
Future<void> syncSecurityAdvisories({bool resync = false}) async {
/// advisories are newer.
Future<void> syncSecurityAdvisories() async {
final tempDir = await Directory.systemTemp.createTemp();
try {
await fetchAdvisories(tempDir);
final (osvs, failedFiles) = await loadAdvisoriesFromDir(tempDir);
await updateAdvisories(osvs, resync: resync);
await updateAdvisories(osvs);

if (failedFiles.isNotEmpty) {
throw Exception(
Expand Down
5 changes: 1 addition & 4 deletions app/lib/tool/backfill/backfill_new_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:logging/logging.dart';
import 'package:pub_dev/service/security_advisories/sync_security_advisories.dart';

final _logger = Logger('backfill_new_fields');

Expand All @@ -13,7 +12,5 @@ final _logger = Logger('backfill_new_fields');
/// CHANGELOG.md must be updated with the new fields, and the next
/// release could remove the backfill from here.
Future<void> backfillNewFields() async {
_logger.info('Resyncing all security advisories...');
// This will backfill the `syncTime` field on the `SecurityAdvisory` entity.
await syncSecurityAdvisories(resync: true);
_logger.info('Nothing to do.');
}

0 comments on commit cd758d1

Please sign in to comment.