-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[ci] Check repo-level package metadata #5811
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
Merged
auto-submit
merged 17 commits into
flutter:main
from
stuartmorgan-g:tool-package-repo-info-check
Jan 12, 2024
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8d03281
Add a new command to check README entries
stuartmorgan-g 6d2b8df
Unit tests
stuartmorgan-g c24401f
Add missing table entries
stuartmorgan-g 3ac3f36
Add link validation
stuartmorgan-g c76b3d2
Validate table entries
stuartmorgan-g f21320d
Escape :s in table
stuartmorgan-g 25af375
Fix old pub.dartlang.org references
stuartmorgan-g 1e797bc
Fix old pigeon tag usage
stuartmorgan-g 3395545
Add special-cased tags
stuartmorgan-g 770af8f
Minor edge case fixes
stuartmorgan-g 8474a7e
Validate CODEOWNERS
stuartmorgan-g 7dcbd57
Make CODEOWNERS compliant
stuartmorgan-g c1c6128
Add to CI
stuartmorgan-g ad60655
Analysis
stuartmorgan-g df9edb9
Merge branch 'main' into tool-package-repo-info-check
stuartmorgan-g a28b80a
Fix typo
stuartmorgan-g 98d51c4
Merge branch 'main' into tool-package-repo-info-check
stuartmorgan-g 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
205 changes: 205 additions & 0 deletions
205
script/tool/lib/src/repo_package_info_check_command.dart
This file contains hidden or 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,205 @@ | ||
| // Copyright 2013 The Flutter Authors. 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:file/file.dart'; | ||
|
|
||
| import 'common/core.dart'; | ||
| import 'common/output_utils.dart'; | ||
| import 'common/package_looping_command.dart'; | ||
| import 'common/repository_package.dart'; | ||
|
|
||
| const int _exitBadTableEntry = 3; | ||
| const int _exitUknownPackageEntry = 4; | ||
|
|
||
| /// A command to verify repository-level metadata about packages, such as | ||
| /// repo README and CODEOWNERS entries. | ||
| class RepoPackageInfoCheckCommand extends PackageLoopingCommand { | ||
| /// Creates Dependabot check command instance. | ||
| RepoPackageInfoCheckCommand(super.packagesDir, {super.gitDir}); | ||
|
|
||
| late Directory _repoRoot; | ||
|
|
||
| /// Data from the root README.md table of packages. | ||
| final Map<String, List<String>> _readmeTableEntries = | ||
| <String, List<String>>{}; | ||
|
|
||
| /// Packages with entries in CODEOWNERS. | ||
| final List<String> _ownedPackages = <String>[]; | ||
|
|
||
| @override | ||
| final String name = 'repo-package-info-check'; | ||
|
|
||
| @override | ||
| List<String> get aliases => <String>['check-repo-package-info']; | ||
|
|
||
| @override | ||
| final String description = | ||
| 'Checks that all packages are listed correctly in the repo README.'; | ||
|
|
||
| @override | ||
| final bool hasLongOutput = false; | ||
|
|
||
| @override | ||
| Future<void> initializeRun() async { | ||
| _repoRoot = packagesDir.fileSystem.directory((await gitDir).path); | ||
|
|
||
| // Extract all of the README.md table entries. | ||
| final RegExp namePattern = RegExp(r'\[(.*?)\]\('); | ||
| for (final String line | ||
| in _repoRoot.childFile('README.md').readAsLinesSync()) { | ||
| // Find all the table entries, skipping the header. | ||
| if (line.startsWith('|') && | ||
| !line.startsWith('| Package') && | ||
| !line.startsWith('|-')) { | ||
| final List<String> cells = line | ||
| .split('|') | ||
| .map((String s) => s.trim()) | ||
| .where((String s) => s.isNotEmpty) | ||
| .toList(); | ||
| // Extract the name, removing any markdown escaping. | ||
| final String? name = | ||
| namePattern.firstMatch(cells[0])?.group(1)?.replaceAll(r'\_', '_'); | ||
| if (name == null) { | ||
| printError('Unexpected README table line:\n $line'); | ||
| throw ToolExit(_exitBadTableEntry); | ||
| } | ||
| _readmeTableEntries[name] = cells; | ||
|
|
||
| if (!(packagesDir.childDirectory(name).existsSync() || | ||
| thirdPartyPackagesDir.childDirectory(name).existsSync())) { | ||
| printError('Unknown package "$name" in root README.md table.'); | ||
| throw ToolExit(_exitUknownPackageEntry); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Extract all of the CODEOWNERS package entries. | ||
| final RegExp packageOwnershipPattern = | ||
| RegExp(r'^((?:third_party/)?packages/(?:[^/]*/)?([^/]*))/\*\*'); | ||
| for (final String line | ||
| in _repoRoot.childFile('CODEOWNERS').readAsLinesSync()) { | ||
| final RegExpMatch? match = packageOwnershipPattern.firstMatch(line); | ||
| if (match == null) { | ||
| continue; | ||
| } | ||
| final String path = match.group(1)!; | ||
| final String name = match.group(2)!; | ||
| if (!_repoRoot.childDirectory(path).existsSync()) { | ||
| printError('Unknown directory "$path" in CODEOWNERS'); | ||
| throw ToolExit(_exitUknownPackageEntry); | ||
| } | ||
| _ownedPackages.add(name); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| Future<PackageResult> runForPackage(RepositoryPackage package) async { | ||
| final String packageName = package.directory.basename; | ||
| final List<String> errors = <String>[]; | ||
|
|
||
| // All packages should have an owner. | ||
| // Platform interface packages are considered to be owned by the app-facing | ||
| // package owner. | ||
| if (!(_ownedPackages.contains(packageName) || | ||
| package.isPlatformInterface && | ||
| _ownedPackages.contains(package.directory.parent.basename))) { | ||
| printError('${indentation}Missing CODEOWNERS entry.'); | ||
| errors.add('Missing CODEOWNERS entry'); | ||
| } | ||
|
|
||
| // Any published package should be in the README table. | ||
| // For federated plugins, only the app-facing package is listed. | ||
| if (package.isPublishable() && | ||
| (!package.isFederated || package.isAppFacing)) { | ||
| final List<String>? cells = _readmeTableEntries[packageName]; | ||
|
|
||
| if (cells == null) { | ||
| printError('${indentation}Missing repo root README.md table entry'); | ||
| errors.add('Missing repo root README.md table entry'); | ||
| } else { | ||
| // Extract the two parts of a "[label](link)" .md link. | ||
| final RegExp mdLinkPattern = RegExp(r'^\[(.*)\]\((.*)\)$'); | ||
| // Possible link targets. | ||
| for (final String cell in cells) { | ||
| final RegExpMatch? match = mdLinkPattern.firstMatch(cell); | ||
| if (match == null) { | ||
| printError( | ||
| '${indentation}Invalid repo root README.md table entry: "$cell"'); | ||
| errors.add('Invalid root README.md table entry'); | ||
| } else { | ||
| final String encodedIssueTag = | ||
| Uri.encodeComponent(_issueTagForPackage(packageName)); | ||
| final String encodedPRTag = | ||
| Uri.encodeComponent(_prTagForPackage(packageName)); | ||
| final String anchor = match.group(1)!; | ||
| final String target = match.group(2)!; | ||
|
|
||
| // The anchor should be one of: | ||
| // - The package name (optionally with any underscores escaped) | ||
| // - An image with a name-based link | ||
| // - An image with a tag-based link | ||
| final RegExp packageLink = | ||
| RegExp(r'^!\[.*\]\(https://img.shields.io/pub/.*/' | ||
| '$packageName' | ||
| r'(?:\.svg)?\)$'); | ||
| final RegExp issueTagLink = RegExp( | ||
| r'^!\[.*\]\(https://img.shields.io/github/issues/flutter/flutter/' | ||
| '$encodedIssueTag' | ||
| r'\?label=\)$'); | ||
| final RegExp prTagLink = RegExp( | ||
| r'^!\[.*\]\(https://img.shields.io/github/issues-pr/flutter/packages/' | ||
| '$encodedPRTag' | ||
| r'\?label=\)$'); | ||
| if (!(anchor == packageName || | ||
| anchor == packageName.replaceAll('_', r'\_') || | ||
| packageLink.hasMatch(anchor) || | ||
| issueTagLink.hasMatch(anchor) || | ||
| prTagLink.hasMatch(anchor))) { | ||
| printError( | ||
| '${indentation}Incorrect anchor in root README.md table: "$anchor"'); | ||
| errors.add('Incorrect anchor in root README.md table'); | ||
| } | ||
|
|
||
| // The link should be one of: | ||
| // - a relative link to the in-repo package | ||
| // - a pub.dev link to the package | ||
| // - a github label link to the package's label | ||
| final RegExp pubDevLink = | ||
| RegExp('^https://pub.dev/packages/$packageName(?:/score)?\$'); | ||
| final RegExp gitHubIssueLink = RegExp( | ||
| '^https://github.com/flutter/flutter/labels/$encodedIssueTag\$'); | ||
| final RegExp gitHubPRLink = RegExp( | ||
| '^https://github.com/flutter/packages/labels/$encodedPRTag\$'); | ||
| if (!(target == './packages/$packageName/' || | ||
| target == './third_party/packages/$packageName/' || | ||
| pubDevLink.hasMatch(target) || | ||
| gitHubIssueLink.hasMatch(target) || | ||
| gitHubPRLink.hasMatch(target))) { | ||
| printError( | ||
| '${indentation}Incorrect link in root README.md table: "$target"'); | ||
| errors.add('Incorrect link in root README.md table'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errors.isEmpty | ||
| ? PackageResult.success() | ||
| : PackageResult.fail(errors); | ||
| } | ||
|
|
||
| String _prTagForPackage(String packageName) => 'p: $packageName'; | ||
|
|
||
| String _issueTagForPackage(String packageName) { | ||
| switch (packageName) { | ||
| case 'google_maps_flutter': | ||
| return 'p: maps'; | ||
| case 'webview_flutter': | ||
| return 'p: webview'; | ||
| default: | ||
| return 'p: $packageName'; | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.