Skip to content

Commit

Permalink
Admin actions for minimal package, version and publisher info.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Aug 29, 2024
1 parent 88fc13f commit ea706bf
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/lib/admin/actions/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import 'moderation_case_info.dart';
import 'moderation_case_list.dart';
import 'moderation_case_resolve.dart';
import 'moderation_case_update.dart';
import 'package_info.dart';
import 'package_version_info.dart';
import 'package_version_retraction.dart';
import 'publisher_block.dart';
import 'publisher_create.dart';
import 'publisher_delete.dart';
import 'publisher_info.dart';
import 'publisher_members_list.dart';
import 'publisher_package_remove.dart';
import 'task_bump_priority.dart';
Expand Down Expand Up @@ -94,10 +97,13 @@ final class AdminAction {
moderationCaseList,
moderationCaseResolve,
moderationCaseUpdate,
packageInfo,
packageVersionInfo,
packageVersionRetraction,
publisherBlock,
publisherCreate,
publisherDelete,
publisherInfo,
publisherMembersList,
publisherPackageRemove,
taskBumpPriority,
Expand Down
42 changes: 42 additions & 0 deletions app/lib/admin/actions/package_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 'actions.dart';

final packageInfo = AdminAction(
name: 'package-info',
summary: 'Gets the package information.',
description: '''
Loads and displays the package information.
''',
options: {
'package': 'The package to be loaded.',
},
invoke: (options) async {
final package = options['package'];
InvalidInputException.check(
package != null && package.isNotEmpty,
'`package` must be given',
);

final p = await packageBackend.lookupPackage(package!);
if (p == null) {
throw NotFoundException.resource(package);
}

return {
'package': {
'name': p.name,
'created': p.created?.toIso8601String(),
'publisherId': p.publisherId,
'latestVersion': p.latestVersion,
'isModerated': p.isModerated,
if (p.moderatedAt != null)
'moderatedAt': p.moderatedAt?.toIso8601String(),
},
};
},
);
48 changes: 48 additions & 0 deletions app/lib/admin/actions/package_version_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 'actions.dart';

final packageVersionInfo = AdminAction(
name: 'package-version-info',
summary: 'Gets the package version information.',
description: '''
Loads and displays the package version information.
''',
options: {
'package': 'The package to be loaded.',
'version': 'The version to be loaded.',
},
invoke: (options) async {
final package = options['package'];
InvalidInputException.check(
package != null && package.isNotEmpty,
'`package` must be given',
);

final version = options['version'];
InvalidInputException.check(
version != null && version.isNotEmpty,
'`version` must be given',
);

final pv = await packageBackend.lookupPackageVersion(package!, version!);
if (pv == null) {
throw NotFoundException.resource('$package/$version');
}

return {
'package-version': {
'package': pv.package,
'version': pv.version,
'created': pv.created?.toIso8601String(),
'isModerated': pv.isModerated,
if (pv.moderatedAt != null)
'moderatedAt': pv.moderatedAt?.toIso8601String(),
},
};
},
);
41 changes: 41 additions & 0 deletions app/lib/admin/actions/publisher_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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/publisher/backend.dart';

import 'actions.dart';

final publisherInfo = AdminAction(
name: 'publisher-info',
summary: 'Gets the publisher information.',
description: '''
Loads and displays the publisher information.
''',
options: {
'publisher': 'The publisherId to be loaded.',
},
invoke: (options) async {
final publisherId = options['publisher'];
InvalidInputException.check(
publisherId != null && publisherId.isNotEmpty,
'`publisher` must be given',
);

final p = await publisherBackend.getPublisher(publisherId!);
if (p == null) {
throw NotFoundException.resource(publisherId);
}

return {
'publisher': {
'publisherId': p.publisherId,
'created': p.created?.toIso8601String(),
'contactEmail': p.contactEmail,
'isModerated': p.isModerated,
if (p.moderatedAt != null)
'moderatedAt': p.moderatedAt?.toIso8601String(),
},
};
},
);
33 changes: 33 additions & 0 deletions app/test/admin/package_actions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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:test/test.dart';

import '../shared/test_models.dart';
import '../shared/test_services.dart';

void main() {
// TODO: move package-related tests from api_actions_test.dart
group('package admin actions', () {
testWithProfile('info request', fn: () async {
final client = createPubApiClient(authToken: siteAdminToken);
final rs = await client.adminInvokeAction(
'package-info',
AdminInvokeActionArguments(arguments: {
'package': 'oxygen',
}),
);
expect(rs.output, {
'package': {
'name': 'oxygen',
'created': isNotEmpty,
'publisherId': null,
'latestVersion': '1.2.0',
'isModerated': false,
}
});
});
});
}
33 changes: 33 additions & 0 deletions app/test/admin/package_version_actions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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:test/test.dart';

import '../shared/test_models.dart';
import '../shared/test_services.dart';

void main() {
// TODO: move package-version-related tests from api_actions_test.dart
group('package version admin actions', () {
testWithProfile('info request', fn: () async {
final client = createPubApiClient(authToken: siteAdminToken);
final rs = await client.adminInvokeAction(
'package-version-info',
AdminInvokeActionArguments(arguments: {
'package': 'oxygen',
'version': '1.2.0',
}),
);
expect(rs.output, {
'package-version': {
'package': 'oxygen',
'version': '1.2.0',
'created': isNotEmpty,
'isModerated': false,
}
});
});
});
}
32 changes: 32 additions & 0 deletions app/test/admin/publisher_actions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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:test/test.dart';

import '../shared/test_models.dart';
import '../shared/test_services.dart';

void main() {
// TODO: move publisher-related tests from api_actions_test.dart
group('publisher admin actions', () {
testWithProfile('info request', fn: () async {
final client = createPubApiClient(authToken: siteAdminToken);
final rs = await client.adminInvokeAction(
'publisher-info',
AdminInvokeActionArguments(arguments: {
'publisher': 'example.com',
}),
);
expect(rs.output, {
'publisher': {
'publisherId': 'example.com',
'created': isNotEmpty,
'contactEmail': 'admin@pub.dev',
'isModerated': false
},
});
});
});
}

0 comments on commit ea706bf

Please sign in to comment.