-
Notifications
You must be signed in to change notification settings - Fork 218
feat(shorebird_cli): add patches rollback / rollforward #3750
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
Open
bdero
wants to merge
5
commits into
main
Choose a base branch
from
bdero/cli-rollback-patches
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
534ba90
feat: shorebird patches rollback / rollforward
bdero ab17d1e
chore: add rollforward to cspell dictionary
bdero 70a97e1
chore: dart format new tests
bdero 945e8e7
chore: add 'resends' to cspell dictionary
bdero a2b0be4
Merge branch 'main' into bdero/cli-rollback-patches
eseidel 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
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
155 changes: 155 additions & 0 deletions
155
packages/shorebird_cli/lib/src/commands/patches/rollback_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,155 @@ | ||
| import 'package:collection/collection.dart'; | ||
| import 'package:mason_logger/mason_logger.dart'; | ||
| import 'package:shorebird_cli/src/code_push_client_wrapper.dart'; | ||
| import 'package:shorebird_cli/src/common_arguments.dart'; | ||
| import 'package:shorebird_cli/src/json_output.dart'; | ||
| import 'package:shorebird_cli/src/logging/logging.dart'; | ||
| import 'package:shorebird_cli/src/shorebird_command.dart'; | ||
| import 'package:shorebird_cli/src/third_party/flutter_tools/lib/src/base/process.dart'; | ||
| import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; | ||
|
|
||
| /// {@template rollback_command} | ||
| /// Rolls back a patch on a release. Devices on this release that next call | ||
| /// the patch-check endpoint will receive the patch number in | ||
| /// `rolled_back_patch_numbers`, prompting them to revert to the prior patch | ||
| /// (or the base release if no other patch is available on the channel). | ||
| /// | ||
| /// Sample usage: | ||
| /// ```sh | ||
| /// shorebird patches rollback --release-version=1.0.0+1 --patch-number=1 | ||
| /// ``` | ||
| /// {@endtemplate} | ||
| class RollbackCommand extends ShorebirdCommand { | ||
| /// {@macro rollback_command} | ||
| RollbackCommand() { | ||
| argParser | ||
| ..addOption( | ||
| CommonArguments.releaseVersionArg.name, | ||
| help: CommonArguments.patchReleaseVersionDescription, | ||
| mandatory: true, | ||
| ) | ||
| ..addOption( | ||
| 'patch-number', | ||
| help: 'The patch number to roll back (e.g. "1").', | ||
| mandatory: true, | ||
| ) | ||
| ..addOption( | ||
| CommonArguments.appIdArg.name, | ||
| help: CommonArguments.appIdArg.description, | ||
| ) | ||
| ..addOption( | ||
| CommonArguments.flavorArg.name, | ||
| help: 'The product flavor to use (e.g. "prod").', | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| String get name => 'rollback'; | ||
|
|
||
| @override | ||
| String get description => | ||
| 'Rolls back a patch on a release.\n\n' | ||
| 'Example output:\n' | ||
| ' Patch 1 on release 1.0.0+1 has been rolled back.\n\n' | ||
| '${ShorebirdCommand.jsonHint( | ||
| 'shorebird patches rollback --release-version 1.0.0+1 ' | ||
| '--patch-number 1 --app-id <id> --json', | ||
| )}'; | ||
|
|
||
| @override | ||
| Future<int> run() async { | ||
| final (:appId, :errorCode) = await resolveAppId(); | ||
| if (errorCode != null) return errorCode; | ||
|
|
||
| final releaseVersion = | ||
| results[CommonArguments.releaseVersionArg.name] as String; | ||
| final patchNumber = int.parse(results['patch-number'] as String); | ||
|
|
||
| final Release release; | ||
| final List<ReleasePatch> patches; | ||
| try { | ||
| release = await codePushClientWrapper.getRelease( | ||
| appId: appId, | ||
| releaseVersion: releaseVersion, | ||
| ); | ||
| patches = await codePushClientWrapper.getReleasePatches( | ||
| appId: appId, | ||
| releaseId: release.id, | ||
| ); | ||
| } on ProcessExit catch (e) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.fetchFailed, | ||
| message: 'Failed to fetch patches for release "$releaseVersion".', | ||
| ); | ||
| return e.exitCode; | ||
| } | ||
| rethrow; | ||
| } | ||
|
|
||
| final patch = patches.firstWhereOrNull((p) => p.number == patchNumber); | ||
| if (patch == null) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.usageError, | ||
| message: | ||
| 'No patch found with number $patchNumber ' | ||
| 'for release "$releaseVersion".', | ||
| ); | ||
| return ExitCode.usage.code; | ||
| } | ||
| logger | ||
| ..err('No patch found with number $patchNumber') | ||
| ..info( | ||
| 'Available patches: ${patches.map((p) => p.number).join(', ')}', | ||
| ); | ||
| return ExitCode.usage.code; | ||
| } | ||
|
|
||
| if (patch.isRolledBack) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.usageError, | ||
| message: 'Patch $patchNumber is already rolled back.', | ||
| ); | ||
| return ExitCode.usage.code; | ||
| } | ||
| logger.err('Patch $patchNumber is already rolled back'); | ||
| return ExitCode.usage.code; | ||
| } | ||
|
|
||
| try { | ||
| await codePushClientWrapper.rollbackPatch( | ||
| appId: appId, | ||
| releaseId: release.id, | ||
| patchId: patch.id, | ||
| patchNumber: patch.number, | ||
| ); | ||
| } on ProcessExit catch (e) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.softwareError, | ||
| message: | ||
| 'Failed to roll back patch $patchNumber ' | ||
| 'of release "$releaseVersion".', | ||
| ); | ||
| return e.exitCode; | ||
| } | ||
| rethrow; | ||
| } | ||
|
|
||
| if (isJsonMode) { | ||
| emitJsonSuccess({ | ||
| 'release_version': releaseVersion, | ||
| 'patch_number': patchNumber, | ||
| 'action': 'rollback', | ||
|
Comment on lines
+143
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take it or leave it - Consider printing the patch object similar to patches info on rollback and rollforward. Would reduce the need to do follow up patches info call. |
||
| }); | ||
| return ExitCode.success.code; | ||
| } | ||
|
|
||
| logger.success( | ||
| 'Patch $patchNumber on release $releaseVersion has been rolled back.', | ||
| ); | ||
| return ExitCode.success.code; | ||
| } | ||
| } | ||
156 changes: 156 additions & 0 deletions
156
packages/shorebird_cli/lib/src/commands/patches/rollforward_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,156 @@ | ||
| import 'package:collection/collection.dart'; | ||
| import 'package:mason_logger/mason_logger.dart'; | ||
| import 'package:shorebird_cli/src/code_push_client_wrapper.dart'; | ||
| import 'package:shorebird_cli/src/common_arguments.dart'; | ||
| import 'package:shorebird_cli/src/json_output.dart'; | ||
| import 'package:shorebird_cli/src/logging/logging.dart'; | ||
| import 'package:shorebird_cli/src/shorebird_command.dart'; | ||
| import 'package:shorebird_cli/src/third_party/flutter_tools/lib/src/base/process.dart'; | ||
| import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; | ||
|
|
||
| /// {@template rollforward_command} | ||
| /// Rolls forward (reactivates) a previously rolled-back patch on a release. | ||
| /// The server flips `is_rolled_back` from `true` to `false` on the same | ||
| /// patch row, so the same patch artifact (same hash, same number) becomes | ||
| /// active again. Devices on this release will pick the patch back up on | ||
| /// their next launch's auto-update cycle. | ||
| /// | ||
| /// Sample usage: | ||
| /// ```sh | ||
| /// shorebird patches rollforward --release-version=1.0.0+1 --patch-number=1 | ||
| /// ``` | ||
| /// {@endtemplate} | ||
| class RollforwardCommand extends ShorebirdCommand { | ||
| /// {@macro rollforward_command} | ||
| RollforwardCommand() { | ||
| argParser | ||
| ..addOption( | ||
| CommonArguments.releaseVersionArg.name, | ||
| help: CommonArguments.patchReleaseVersionDescription, | ||
| mandatory: true, | ||
| ) | ||
| ..addOption( | ||
| 'patch-number', | ||
| help: 'The patch number to roll forward (e.g. "1").', | ||
| mandatory: true, | ||
| ) | ||
| ..addOption( | ||
| CommonArguments.appIdArg.name, | ||
| help: CommonArguments.appIdArg.description, | ||
| ) | ||
| ..addOption( | ||
| CommonArguments.flavorArg.name, | ||
| help: 'The product flavor to use (e.g. "prod").', | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| String get name => 'rollforward'; | ||
|
|
||
| @override | ||
| String get description => | ||
| 'Rolls forward (reactivates) a previously rolled-back patch.\n\n' | ||
| 'Example output:\n' | ||
| ' Patch 1 on release 1.0.0+1 has been rolled forward.\n\n' | ||
| '${ShorebirdCommand.jsonHint( | ||
| 'shorebird patches rollforward --release-version 1.0.0+1 ' | ||
| '--patch-number 1 --app-id <id> --json', | ||
| )}'; | ||
|
|
||
| @override | ||
| Future<int> run() async { | ||
| final (:appId, :errorCode) = await resolveAppId(); | ||
| if (errorCode != null) return errorCode; | ||
|
|
||
| final releaseVersion = | ||
| results[CommonArguments.releaseVersionArg.name] as String; | ||
| final patchNumber = int.parse(results['patch-number'] as String); | ||
|
|
||
| final Release release; | ||
| final List<ReleasePatch> patches; | ||
| try { | ||
| release = await codePushClientWrapper.getRelease( | ||
| appId: appId, | ||
| releaseVersion: releaseVersion, | ||
| ); | ||
| patches = await codePushClientWrapper.getReleasePatches( | ||
| appId: appId, | ||
| releaseId: release.id, | ||
| ); | ||
| } on ProcessExit catch (e) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.fetchFailed, | ||
| message: 'Failed to fetch patches for release "$releaseVersion".', | ||
| ); | ||
| return e.exitCode; | ||
| } | ||
| rethrow; | ||
| } | ||
|
|
||
| final patch = patches.firstWhereOrNull((p) => p.number == patchNumber); | ||
| if (patch == null) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.usageError, | ||
| message: | ||
| 'No patch found with number $patchNumber ' | ||
| 'for release "$releaseVersion".', | ||
| ); | ||
| return ExitCode.usage.code; | ||
| } | ||
| logger | ||
| ..err('No patch found with number $patchNumber') | ||
| ..info( | ||
| 'Available patches: ${patches.map((p) => p.number).join(', ')}', | ||
| ); | ||
| return ExitCode.usage.code; | ||
| } | ||
|
|
||
| if (!patch.isRolledBack) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.usageError, | ||
| message: 'Patch $patchNumber is already active (not rolled back).', | ||
| ); | ||
| return ExitCode.usage.code; | ||
| } | ||
| logger.err('Patch $patchNumber is already active (not rolled back)'); | ||
| return ExitCode.usage.code; | ||
| } | ||
|
|
||
| try { | ||
| await codePushClientWrapper.rollforwardPatch( | ||
| appId: appId, | ||
| releaseId: release.id, | ||
| patchId: patch.id, | ||
| patchNumber: patch.number, | ||
| ); | ||
| } on ProcessExit catch (e) { | ||
| if (isJsonMode) { | ||
| emitJsonError( | ||
| code: JsonErrorCode.softwareError, | ||
| message: | ||
| 'Failed to roll forward patch $patchNumber ' | ||
| 'of release "$releaseVersion".', | ||
| ); | ||
| return e.exitCode; | ||
| } | ||
| rethrow; | ||
| } | ||
|
|
||
| if (isJsonMode) { | ||
| emitJsonSuccess({ | ||
| 'release_version': releaseVersion, | ||
| 'patch_number': patchNumber, | ||
| 'action': 'rollforward', | ||
| }); | ||
| return ExitCode.success.code; | ||
| } | ||
|
|
||
| logger.success( | ||
| 'Patch $patchNumber on release $releaseVersion has been rolled forward.', | ||
| ); | ||
| return ExitCode.success.code; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Take it or leave it - End state is the same is the same on success or hitting this route. Returning a non zero exit suggests that something went wrong even though the end state is correct. Consider adding some flag IE
--require-change, I am awful at naming to please feel free to change it, if you want the behavior to be there on default or non default. Changing to this would also match other CLI tool behaviors. This is in line w/ current set-track behavior hence take it or leave it.