Skip to content

Commit 52475c1

Browse files
authored
Migrate admin action to set Package.publisherId (#8999)
1 parent 1f2ffd8 commit 52475c1

File tree

5 files changed

+84
-58
lines changed

5 files changed

+84
-58
lines changed

app/lib/admin/actions/actions.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:pub_dev/admin/actions/package_invite_uploader.dart';
6-
75
import '../../shared/exceptions.dart';
86
import 'download_counts_backfill.dart';
97
import 'download_counts_delete.dart';
@@ -24,7 +22,9 @@ import 'moderation_transparency_metrics.dart';
2422
import 'package_delete.dart';
2523
import 'package_discontinue.dart';
2624
import 'package_info.dart';
25+
import 'package_invite_uploader.dart';
2726
import 'package_latest_update.dart';
27+
import 'package_publisher_set.dart';
2828
import 'package_reservation_create.dart';
2929
import 'package_reservation_delete.dart';
3030
import 'package_reservation_list.dart';
@@ -115,6 +115,7 @@ final class AdminAction {
115115
packageInfo,
116116
packageInviteUploader,
117117
packageLatestUpdate,
118+
packagePublisherSet,
118119
packageReservationCreate,
119120
packageReservationDelete,
120121
packageReservationList,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:clock/clock.dart';
6+
import 'package:pub_dev/package/backend.dart';
7+
import 'package:pub_dev/package/models.dart';
8+
import 'package:pub_dev/publisher/backend.dart';
9+
import 'package:pub_dev/shared/datastore.dart';
10+
11+
import 'actions.dart';
12+
13+
final packagePublisherSet = AdminAction(
14+
name: 'package-publisher-set',
15+
summary: 'Sets the publisher for a package.',
16+
description: 'Sets a new `publisherId` for a `package`.',
17+
options: {
18+
'package': 'The package to be updated.',
19+
'publisher': 'The `publisherId` to set.',
20+
},
21+
invoke: (options) async {
22+
final packageName = options['package'];
23+
final publisherId = options['publisher'];
24+
25+
if (packageName == null) {
26+
throw InvalidInputException('Missing --package argument.');
27+
}
28+
if (publisherId == null) {
29+
throw InvalidInputException('Missing --publisher argument.');
30+
}
31+
32+
final package = (await packageBackend.lookupPackage(packageName))!;
33+
final publisher = await publisherBackend.lookupPublisher(publisherId);
34+
if (publisher == null || !publisher.isVisible) {
35+
InvalidInputException(
36+
'Publisher `$publisherId` does not exists or is not visible.',
37+
);
38+
}
39+
final currentPublisherId = package.publisherId;
40+
if (currentPublisherId != publisherId) {
41+
await withRetryTransaction(dbService, (tx) async {
42+
final pkg = await tx.lookupValue<Package>(package.key);
43+
pkg.publisherId = publisherId;
44+
pkg.updated = clock.now().toUtc();
45+
tx.insert(pkg);
46+
});
47+
await purgePublisherCache(publisherId: publisherId);
48+
triggerPackagePostUpdates(
49+
packageName,
50+
skipReanalysis: true,
51+
skipVersionsExport: true,
52+
);
53+
if (currentPublisherId != null) {
54+
await purgePublisherCache(publisherId: currentPublisherId);
55+
}
56+
}
57+
58+
final pkg = await packageBackend.lookupPackage(packageName);
59+
return {
60+
'before': {'publisherId': currentPublisherId},
61+
'after': {'publisherId': pkg!.publisherId},
62+
};
63+
},
64+
);

app/lib/admin/backend.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import 'actions/actions.dart' show AdminAction;
3636
import 'tools/delete_all_staging.dart';
3737
import 'tools/list_tools.dart';
3838
import 'tools/notify_service.dart';
39-
import 'tools/package_publisher.dart';
4039
import 'tools/recent_uploaders.dart';
4140
import 'tools/user_merger.dart';
4241

@@ -55,7 +54,6 @@ typedef Tool = Future<String> Function(List<String> args);
5554
final Map<String, Tool> availableTools = {
5655
'delete-all-staging': executeDeleteAllStaging,
5756
'notify-service': executeNotifyService,
58-
'package-publisher': executeSetPackagePublisher,
5957
'recent-uploaders': executeRecentUploaders,
6058
'user-merger': executeUserMergerTool,
6159
'list-tools': executeListTools,

app/lib/admin/tools/package_publisher.dart

Lines changed: 0 additions & 54 deletions
This file was deleted.

app/test/admin/package_actions_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,22 @@ void main() {
7575
expect(rs.output, {'updatedCount': 0});
7676
},
7777
);
78+
79+
testWithProfile(
80+
'set publisher on a package',
81+
fn: () async {
82+
final client = createPubApiClient(authToken: siteAdminToken);
83+
final rs = await client.adminInvokeAction(
84+
'package-publisher-set',
85+
AdminInvokeActionArguments(
86+
arguments: {'package': 'oxygen', 'publisher': 'example.com'},
87+
),
88+
);
89+
expect(rs.output, {
90+
'before': {'publisherId': null},
91+
'after': {'publisherId': 'example.com'},
92+
});
93+
},
94+
);
7895
});
7996
}

0 commit comments

Comments
 (0)