|
| 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/shared/datastore.dart'; |
| 9 | + |
| 10 | +import 'actions.dart'; |
| 11 | + |
| 12 | +final packageDiscontinue = AdminAction( |
| 13 | + name: 'package-discontinue', |
| 14 | + summary: 'Sets the package discontinued.', |
| 15 | + description: ''' |
| 16 | +Sets the `Package.isDiscontinued` and `Package.replacedBy` properties. |
| 17 | +''', |
| 18 | + options: { |
| 19 | + 'package': 'The package to be discontinued.', |
| 20 | + 'value': 'The value to set (defaults to true).', |
| 21 | + 'replaced-by': |
| 22 | + 'The Package.replacedBy field (if not set will be set to `null`).', |
| 23 | + }, |
| 24 | + invoke: (options) async { |
| 25 | + final package = options['package']; |
| 26 | + InvalidInputException.check( |
| 27 | + package != null && package.isNotEmpty, |
| 28 | + '`package` must be given', |
| 29 | + ); |
| 30 | + final value = options['value'] ?? 'true'; |
| 31 | + InvalidInputException.checkAnyOf(value, 'value', ['true', 'false']); |
| 32 | + final valueToSet = value == 'true'; |
| 33 | + |
| 34 | + final p = await packageBackend.lookupPackage(package!); |
| 35 | + if (p == null) { |
| 36 | + throw NotFoundException.resource(package); |
| 37 | + } |
| 38 | + |
| 39 | + final replacedBy = options['replaced-by']; |
| 40 | + if (replacedBy != null) { |
| 41 | + final rp = await packageBackend.lookupPackage(replacedBy); |
| 42 | + if (rp == null) { |
| 43 | + throw NotFoundException('Replacing package "$replacedBy" not found.'); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + final info = await withRetryTransaction(dbService, (tx) async { |
| 48 | + final pkg = await tx.lookupOrNull<Package>(p.key); |
| 49 | + if (pkg == null) { |
| 50 | + throw NotFoundException.resource(package); |
| 51 | + } |
| 52 | + pkg.isDiscontinued = valueToSet; |
| 53 | + pkg.replacedBy = valueToSet ? replacedBy : null; |
| 54 | + pkg.updated = clock.now().toUtc(); |
| 55 | + tx.insert(pkg); |
| 56 | + return pkg; |
| 57 | + }); |
| 58 | + |
| 59 | + return { |
| 60 | + 'package': { |
| 61 | + 'name': info.name, |
| 62 | + 'isDiscontinued': info.isDiscontinued, |
| 63 | + 'replacedBy': info.replacedBy, |
| 64 | + }, |
| 65 | + }; |
| 66 | + }, |
| 67 | +); |
0 commit comments