-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReservedPackage + admin action. #8105
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:pub_dev/package/backend.dart'; | ||
import 'package:pub_dev/package/models.dart'; | ||
import 'package:pub_dev/shared/datastore.dart'; | ||
|
||
import 'actions.dart'; | ||
|
||
final packageReserve = AdminAction( | ||
name: 'package-reserve', | ||
summary: 'Creates a ReservedPackage entity.', | ||
description: ''' | ||
Reserves a package name that can be claimed by the specified list of email addresses. | ||
|
||
The action can be re-run with the same package name. In such cases the previous | ||
email list will be discarded, and the specified email list will be updated to the | ||
existing ReservedPackage entity. | ||
|
||
When no emails are specified, the package will be reserved, but no user may be | ||
able to claim it. | ||
''', | ||
options: { | ||
'package': 'The package name to be reserved.', | ||
'emails': 'The list of email addresses, separated by comma.' | ||
}, | ||
invoke: (options) async { | ||
final package = options['package']; | ||
InvalidInputException.check( | ||
package != null && package.isNotEmpty, | ||
'`package` must be given', | ||
); | ||
final emails = options['emails']?.split(','); | ||
|
||
final p = await packageBackend.lookupPackage(package!); | ||
if (p != null) { | ||
throw InvalidInputException('Package `$package` exists.'); | ||
} | ||
final mp = await packageBackend.lookupModeratedPackage(package); | ||
if (mp != null) { | ||
throw InvalidInputException('ModeratedPackage `$package` exists.'); | ||
} | ||
|
||
final entry = await withRetryTransaction(dbService, (tx) async { | ||
final existing = await tx.lookupOrNull<ReservedPackage>( | ||
dbService.emptyKey.append(ReservedPackage, id: package)); | ||
final entry = existing ?? ReservedPackage.init(package); | ||
entry.emails = <String>{...?emails}.toList(); | ||
tx.insert(entry); | ||
return entry; | ||
}); | ||
|
||
return { | ||
'ReservedPackage': { | ||
'name': entry.name, | ||
'created': entry.created.toIso8601String(), | ||
'emails': entry.emails, | ||
}, | ||
}; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:_pub_shared/data/admin_api.dart'; | ||
import 'package:clock/clock.dart'; | ||
import 'package:pub_dev/fake/backend/fake_auth_provider.dart'; | ||
import 'package:pub_dev/package/backend.dart'; | ||
import 'package:pub_dev/package/models.dart'; | ||
import 'package:pub_dev/shared/datastore.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../package/backend_test_utils.dart'; | ||
import '../shared/handlers_test_utils.dart'; | ||
import '../shared/test_models.dart'; | ||
import '../shared/test_services.dart'; | ||
|
||
void main() { | ||
group('Reserve package', () { | ||
Future<void> _reserve( | ||
String package, { | ||
List<String>? emails, | ||
}) async { | ||
final api = createPubApiClient(authToken: siteAdminToken); | ||
await api.adminInvokeAction( | ||
'package-reserve', | ||
AdminInvokeActionArguments(arguments: { | ||
'package': package, | ||
if (emails != null) 'emails': emails.join(','), | ||
}), | ||
); | ||
} | ||
|
||
testWithProfile('cannot reserve existing package', fn: () async { | ||
await expectApiException( | ||
_reserve('oxygen'), | ||
code: 'InvalidInput', | ||
status: 400, | ||
message: 'Package `oxygen` exists.', | ||
); | ||
}); | ||
|
||
testWithProfile('cannot reserve ModeratedPackage', fn: () async { | ||
await dbService.commit(inserts: [ | ||
ModeratedPackage() | ||
..id = 'pkg' | ||
..name = 'pkg' | ||
..moderated = clock.now() | ||
..uploaders = <String>[] | ||
..versions = <String>['1.0.0'] | ||
]); | ||
await expectApiException( | ||
_reserve('pkg'), | ||
code: 'InvalidInput', | ||
status: 400, | ||
message: 'ModeratedPackage `pkg` exists.', | ||
); | ||
}); | ||
|
||
testWithProfile('prevents non-whitelisted publishing', fn: () async { | ||
await _reserve('pkg'); | ||
|
||
final pubspecContent = generatePubspecYaml('pkg', '1.0.0'); | ||
final bytes = await packageArchiveBytes(pubspecContent: pubspecContent); | ||
await expectApiException( | ||
createPubApiClient(authToken: adminClientToken) | ||
.uploadPackageBytes(bytes), | ||
code: 'PackageRejected', | ||
status: 400, | ||
message: 'Package name pkg is reserved.', | ||
); | ||
}); | ||
|
||
testWithProfile('allows whitelisted publishing', fn: () async { | ||
await _reserve('pkg'); | ||
// update email addresses in a second request | ||
await _reserve('pkg', emails: ['[email protected]']); | ||
|
||
final pubspecContent = generatePubspecYaml('pkg', '1.0.0'); | ||
final bytes = await packageArchiveBytes(pubspecContent: pubspecContent); | ||
await createPubApiClient(authToken: adminClientToken) | ||
.uploadPackageBytes(bytes); | ||
|
||
final rp = await packageBackend.lookupReservedPackage('pkg'); | ||
expect(rp, isNull); | ||
}); | ||
|
||
testWithProfile('no longer allows Dart-team exemption', fn: () async { | ||
await _reserve('pkg'); | ||
|
||
final pubspecContent = generatePubspecYaml('pkg', '1.0.0'); | ||
final bytes = await packageArchiveBytes(pubspecContent: pubspecContent); | ||
await expectApiException( | ||
createPubApiClient(authToken: adminClientToken) | ||
.uploadPackageBytes(bytes), | ||
code: 'PackageRejected', | ||
status: 400, | ||
message: 'Package name pkg is reserved.', | ||
); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an option to delete the entity? Maybe it is not worth it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just add:
package-reserve-list
package-reserve-delete
Life is easier, if we split them into multiple actions. We can still put them all in the same file if want that.
We could even consider naming them:
package-reservation-create
package-reservation-list
package-reservation-delete
naming is a nit, but it's probably more intuitive to have separate options. And it's probably easier to read the code too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like a
--delete
flag?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - probably
--delete=true
as we didn't implement real flags for actions,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a separate command is MUCH easier to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's really just something like:
A
--delete=true
flag requires a lot more explanation in the documentation, it's not very intuitive.