Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[tool] version-check publish-check commands can check against pub #3840

Merged
merged 18 commits into from
May 11, 2021

Conversation

cyanglaz
Copy link
Contributor

@cyanglaz cyanglaz commented Apr 30, 2021

Add a PubVersionFinder class to easily fetch the version from pub.

Add an against-pub flag to check-version command, which allows it to check the version against pub server

Make the 'publish-check' command to check against pub to determine if the specific versions of packages need to be published.
Add a log-status flag, which allows the publish-check command to log the final status of the result. This helps other ci tools to easily grab the results and use it to determine what to do next. See option 3 in flutter/flutter#81444

This PR also fixes some tests.

partially flutter/flutter#81444

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart format. See plugin_tool format)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@google-cla google-cla bot added the cla: yes label Apr 30, 2021
@cyanglaz cyanglaz changed the title [tool] version check command can check against pub [tool] version-check publish-check commands can check against pub May 1, 2021
help:
'Logs the check-publish final status to a defined string as the last line of the command output.\n'
'The possible values are:\n'
' $_resultNeedsPublish: There is at least one package need to be published. They also passed all publish checks. \n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/need to/that needs to be/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

help:
'Logs the check-publish final status to a defined string as the last line of the command output.\n'
'The possible values are:\n'
' $_resultNeedsPublish: There is at least one package need to be published. They also passed all publish checks. \n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove training space since there's a \n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

'Logs the check-publish final status to a defined string as the last line of the command output.\n'
'The possible values are:\n'
' $_resultNeedsPublish: There is at least one package need to be published. They also passed all publish checks. \n'
' $_resultNoPublish: There are no packages need to be published. Either no pubspec change detected or the version has already been published. \n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/the version/all versions/, since there can be multiple packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -26,9 +29,22 @@ class PublishCheckCommand extends PluginCommand {
'the SDK constraint is a pre-release version, is ignored.',
defaultsTo: false,
);
argParser.addFlag(_logStatusFlag,
help:
'Logs the check-publish final status to a defined string as the last line of the command output.\n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing output for humans and output for machines is generally not a good idea; I would rather we make a --machine flag that switches entirely to an output meant for parsing.

final PubVersionFinder pubVersionFinder = PubVersionFinder(
package: packageName, httpClient: httpClient ?? http.Client());
final PubVersionFinderResponse pubVersionFinderResponse =
await pubVersionFinder.getPackageVersion();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want a try/catch here to deal with issues reaching pub?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need try-catch, but we should check the status of pubVersionFinderResponse. Done.

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuartmorgan Updated per review comments. PTAL

help:
'Logs the check-publish final status to a defined string as the last line of the command output.\n'
'The possible values are:\n'
' $_resultNeedsPublish: There is at least one package need to be published. They also passed all publish checks. \n'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

help:
'Logs the check-publish final status to a defined string as the last line of the command output.\n'
'The possible values are:\n'
' $_resultNeedsPublish: There is at least one package need to be published. They also passed all publish checks. \n'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

'Logs the check-publish final status to a defined string as the last line of the command output.\n'
'The possible values are:\n'
' $_resultNeedsPublish: There is at least one package need to be published. They also passed all publish checks. \n'
' $_resultNoPublish: There are no packages need to be published. Either no pubspec change detected or the version has already been published. \n'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

final PubVersionFinder pubVersionFinder = PubVersionFinder(
package: packageName, httpClient: httpClient ?? http.Client());
final PubVersionFinderResponse pubVersionFinderResponse =
await pubVersionFinder.getPackageVersion();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need try-catch, but we should check the status of pubVersionFinderResponse. Done.

@cyanglaz cyanglaz requested a review from stuartmorgan May 4, 2021 21:05
## NEXT

- Add `against-pub` flag for version-check.
- Add `log-status` flag for publish-check.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/// The pub host url, defaults to `https://pub.dev`.
final String pubHost;

/// The http client, can override for testing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's required, then it's not really an override for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

script/tool/lib/src/common.dart Show resolved Hide resolved
final http.Response httpResponse;
}

/// An enum represents the result of [PubVersionFinder].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: representing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -549,6 +550,97 @@ class ProcessRunner {
}
}

/// Finding version of [package] that is published on pub.
///
/// Note: you should manually close the [httpClient] when done using the finder.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems worth noting on the constructor rather than the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

final http.Client httpClient;

/// Get the package version on pub.
Future<PubVersionFinderResponse> getPackageVersion() async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the class would make more sense as a class if package were an argument here rather than on the constructor. I.e., you configure it for a given server/network setup, then you can query packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!


/// The version finder failed to locate the package.
///
/// This is usually OK when the package is new.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "usually"? When would it not be okay for a new package not to be found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworded, this was not what I meant.

final Colorize passedMessage =
Colorize('All packages passed publish check!')..green();
print(passedMessage);
// This has to be last output of this command because it is promised in the help section.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

published = pubVersionFinderResponse.versions.contains(version);
break;
case PubVersionFinderResult.fail:
printErrorAndExit(errorMessage: '''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break the --machine case, because the final message will never print?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking this should return something, rather than bailing completely. Is the reason for this that we assume that all the other checks will fail the same way because it's likely a network issue?

If we do want to keep this construction, this line (and the line above) need comments explaining why they are doing what they are doing, since it's non-obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, I updated to print the message and handled the error later

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated per your comments, PTAL @stuartmorgan

## NEXT

- Add `against-pub` flag for version-check.
- Add `log-status` flag for publish-check.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -549,6 +550,97 @@ class ProcessRunner {
}
}

/// Finding version of [package] that is published on pub.
///
/// Note: you should manually close the [httpClient] when done using the finder.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/// The pub host url, defaults to `https://pub.dev`.
final String pubHost;

/// The http client, can override for testing.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

final http.Client httpClient;

/// Get the package version on pub.
Future<PubVersionFinderResponse> getPackageVersion() async {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

script/tool/lib/src/common.dart Show resolved Hide resolved
final http.Response httpResponse;
}

/// An enum represents the result of [PubVersionFinder].
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/// The version finder failed to locate the package.
///
/// This is usually OK when the package is new.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworded, this was not what I meant.

final Colorize passedMessage =
Colorize('All packages passed publish check!')..green();
print(passedMessage);
// This has to be last output of this command because it is promised in the help section.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

published = pubVersionFinderResponse.versions.contains(version);
break;
case PubVersionFinderResult.fail:
printErrorAndExit(errorMessage: '''
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@cyanglaz cyanglaz requested a review from stuartmorgan May 5, 2021 18:12
published = pubVersionFinderResponse.versions.contains(version);
break;
case PubVersionFinderResult.fail:
printErrorAndExit(errorMessage: '''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking this should return something, rather than bailing completely. Is the reason for this that we assume that all the other checks will fail the same way because it's likely a network issue?

If we do want to keep this construction, this line (and the line above) need comments explaining why they are doing what they are doing, since it's non-obvious.

Error fetching version on pub for $packageName.
HTTP Status ${pubVersionFinderResponse.httpResponse.statusCode}
HTTP response: ${pubVersionFinderResponse.httpResponse.body}
''');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm realizing that failures here are going to be almost impossible to debug, because we won't have any information in the log.

Perhaps we should consider doing something like a JSON dictionary of output, with status and detailed log messages as separate keys? That would mean we'd need to consume the result with something like a Dart script that can parse the JSON, rather than just a one-liner in the GitHub Action. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Idea, updated the code to do exactly that

throw ToolExit(1);
} else {
final Colorize passedMessage =
Colorize('All packages passed publish check!')..green();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the new tests, it's very strange to have terminal color commands in the --machine output. I think we should replace all the Colorize calls with a utility method like:

_printImportantStatusMessage(String message, {@required bool isError}) {
  if (argResults[_machineFlag] as bool) {
    print('${isError ? 'ERROR' : 'SUCCESS'}: $message');
  } else {
    final Colorize colorizedMessage = Colorize(message);
    if (isError) {
      colorizedMessage.red();
    } else {
      colorizedMessage.green();
    }
    print(colorizedMessage);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated so that both machine mesasge and human message include the error and success prefix.

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuartmorgan Updated the status message bit. PTAL

throw ToolExit(1);
} else {
final Colorize passedMessage =
Colorize('All packages passed publish check!')..green();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated so that both machine mesasge and human message include the error and success prefix.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cyanglaz cyanglaz merged commit 0e0c75b into flutter:master May 11, 2021
@cyanglaz cyanglaz deleted the version_check_pub branch May 11, 2021 19:48
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 11, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 11, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 11, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 11, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 19, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 19, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 19, 2021
fotiDim pushed a commit to fotiDim/plugins that referenced this pull request Sep 13, 2021
…utter#3840)


Add a PubVersionFinder class to easily fetch the version from pub.

Add an against-pub flag to check-version command, which allows it to check the version against pub server

Make the 'publish-check' command to check against pub to determine if the specific versions of packages need to be published.
Add a log-status flag, which allows the publish-check command to log the final status of the result. This helps other ci tools to easily grab the results and use it to determine what to do next. See option 3 in flutter/flutter#81444

This PR also fixes some tests.

partially flutter/flutter#81444
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants