|
| 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 | +); |
0 commit comments