From 79da63ff9f4ea6407da4234acfe03bd1b375f05b Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 17 Jul 2023 23:00:45 -0500 Subject: [PATCH] refactor(code_push_client)!: use new patches endpoint (#865) --- .../lib/src/code_push_client_wrapper.dart | 18 +++++-- .../src/code_push_client_wrapper_test.dart | 45 ++++++++++++++--- .../patch/patch_android_command_test.dart | 1 + .../patch/patch_ios_command_test.dart | 1 + .../shorebird_cli/test/src/java_test.dart | 12 +++-- .../example/main.dart | 9 +++- .../lib/src/code_push_client.dart | 13 +++-- .../test/src/code_push_client_test.dart | 49 +++++++++++++------ 8 files changed, 116 insertions(+), 32 deletions(-) diff --git a/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart b/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart index 06db9032..cda55586 100644 --- a/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart +++ b/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart @@ -513,10 +513,16 @@ aar artifact already exists, continuing...''', } @visibleForTesting - Future createPatch({required int releaseId}) async { + Future createPatch({ + required String appId, + required int releaseId, + }) async { final createPatchProgress = logger.progress('Creating patch'); try { - final patch = await codePushClient.createPatch(releaseId: releaseId); + final patch = await codePushClient.createPatch( + appId: appId, + releaseId: releaseId, + ); createPatchProgress.complete(); return patch; } catch (error) { @@ -527,6 +533,7 @@ aar artifact already exists, continuing...''', @visibleForTesting Future createPatchArtifacts({ + required String appId, required Patch patch, required String platform, required Map patchArtifactBundles, @@ -535,6 +542,7 @@ aar artifact already exists, continuing...''', for (final artifact in patchArtifactBundles.values) { try { await codePushClient.createPatchArtifact( + appId: appId, patchId: patch.id, artifactPath: artifact.path, arch: artifact.arch, @@ -551,6 +559,7 @@ aar artifact already exists, continuing...''', @visibleForTesting Future promotePatch({ + required String appId, required int patchId, required Channel channel, }) async { @@ -559,6 +568,7 @@ aar artifact already exists, continuing...''', ); try { await codePushClient.promotePatch( + appId: appId, patchId: patchId, channelId: channel.id, ); @@ -577,10 +587,12 @@ aar artifact already exists, continuing...''', required Map patchArtifactBundles, }) async { final patch = await createPatch( + appId: appId, releaseId: releaseId, ); await createPatchArtifacts( + appId: appId, patch: patch, platform: platform, patchArtifactBundles: patchArtifactBundles, @@ -595,7 +607,7 @@ aar artifact already exists, continuing...''', name: channelName, ); - await promotePatch(patchId: patch.id, channel: channel); + await promotePatch(appId: appId, patchId: patch.id, channel: channel); } Future getUsage() async { diff --git a/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart b/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart index 0208a35a..64323602 100644 --- a/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart +++ b/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart @@ -1588,12 +1588,16 @@ Please bump your version number and try again.''', test('exits with code 70 when creating patch fails', () async { const error = 'something went wrong'; when( - () => codePushClient.createPatch(releaseId: releaseId), + () => codePushClient.createPatch( + appId: appId, + releaseId: releaseId, + ), ).thenThrow(error); await expectLater( () async => runWithOverrides( () => codePushClientWrapper.createPatch( + appId: appId, releaseId: releaseId, ), ), @@ -1603,11 +1607,16 @@ Please bump your version number and try again.''', }); test('returns patch when patch is successfully created', () async { - when(() => codePushClient.createPatch(releaseId: releaseId)) - .thenAnswer((_) async => patch); + when( + () => codePushClient.createPatch( + appId: appId, + releaseId: releaseId, + ), + ).thenAnswer((_) async => patch); final result = await runWithOverrides( () => codePushClientWrapper.createPatch( + appId: appId, releaseId: releaseId, ), ); @@ -1622,6 +1631,7 @@ Please bump your version number and try again.''', const error = 'something went wrong'; when( () => codePushClient.promotePatch( + appId: any(named: 'appId'), patchId: any(named: 'patchId'), channelId: any(named: 'channelId'), ), @@ -1630,6 +1640,7 @@ Please bump your version number and try again.''', await expectLater( () async => runWithOverrides( () => codePushClientWrapper.promotePatch( + appId: appId, patchId: patchId, channel: channel, ), @@ -1642,6 +1653,7 @@ Please bump your version number and try again.''', test('completes progress when patch is promoted', () async { when( () => codePushClient.promotePatch( + appId: any(named: 'appId'), patchId: any(named: 'patchId'), channelId: any(named: 'channelId'), ), @@ -1649,6 +1661,7 @@ Please bump your version number and try again.''', await runWithOverrides( () => codePushClientWrapper.promotePatch( + appId: appId, patchId: patchId, channel: channel, ), @@ -1665,6 +1678,7 @@ Please bump your version number and try again.''', const error = 'something went wrong'; when( () => codePushClient.createPatchArtifact( + appId: any(named: 'appId'), patchId: any(named: 'patchId'), artifactPath: any(named: 'artifactPath'), arch: any(named: 'arch'), @@ -1676,6 +1690,7 @@ Please bump your version number and try again.''', await expectLater( () async => runWithOverrides( () => codePushClientWrapper.createPatchArtifacts( + appId: appId, patch: patch, platform: platformName, patchArtifactBundles: patchArtifactBundles, @@ -1691,6 +1706,7 @@ Please bump your version number and try again.''', test('creates artifacts successfully', () async { when( () => codePushClient.createPatchArtifact( + appId: any(named: 'appId'), patchId: any(named: 'patchId'), artifactPath: any(named: 'artifactPath'), arch: any(named: 'arch'), @@ -1701,6 +1717,7 @@ Please bump your version number and try again.''', await runWithOverrides( () => codePushClientWrapper.createPatchArtifacts( + appId: appId, patch: patch, platform: platformName, patchArtifactBundles: patchArtifactBundles, @@ -1710,6 +1727,7 @@ Please bump your version number and try again.''', verify(() => progress.complete()).called(1); verify( () => codePushClient.createPatchArtifact( + appId: appId, artifactPath: partchArtifactBundle.path, patchId: patchId, arch: arch.name, @@ -1723,10 +1741,14 @@ Please bump your version number and try again.''', group('publishPatch', () { setUp(() { when( - () => codePushClient.createPatch(releaseId: releaseId), + () => codePushClient.createPatch( + appId: any(named: 'appId'), + releaseId: any(named: 'releaseId'), + ), ).thenAnswer((_) async => patch); when( () => codePushClient.createPatchArtifact( + appId: any(named: 'appId'), patchId: any(named: 'patchId'), artifactPath: any(named: 'artifactPath'), arch: any(named: 'arch'), @@ -1739,6 +1761,7 @@ Please bump your version number and try again.''', ).thenAnswer((_) async => [channel]); when( () => codePushClient.promotePatch( + appId: any(named: 'appId'), patchId: any(named: 'patchId'), channelId: any(named: 'channelId'), ), @@ -1757,10 +1780,14 @@ Please bump your version number and try again.''', ); verify( - () => codePushClient.createPatch(releaseId: releaseId), + () => codePushClient.createPatch( + appId: appId, + releaseId: releaseId, + ), ).called(1); verify( () => codePushClient.createPatchArtifact( + appId: appId, artifactPath: partchArtifactBundle.path, patchId: patchId, arch: arch.name, @@ -1777,6 +1804,7 @@ Please bump your version number and try again.''', ); verify( () => codePushClient.promotePatch( + appId: appId, patchId: patchId, channelId: channel.id, ), @@ -1806,10 +1834,14 @@ Please bump your version number and try again.''', ); verify( - () => codePushClient.createPatch(releaseId: releaseId), + () => codePushClient.createPatch( + appId: appId, + releaseId: releaseId, + ), ).called(1); verify( () => codePushClient.createPatchArtifact( + appId: appId, artifactPath: partchArtifactBundle.path, patchId: patchId, arch: arch.name, @@ -1826,6 +1858,7 @@ Please bump your version number and try again.''', ).called(1); verify( () => codePushClient.promotePatch( + appId: appId, patchId: patchId, channelId: channel.id, ), diff --git a/packages/shorebird_cli/test/src/commands/patch/patch_android_command_test.dart b/packages/shorebird_cli/test/src/commands/patch/patch_android_command_test.dart index 5c784d41..deb3a17f 100644 --- a/packages/shorebird_cli/test/src/commands/patch/patch_android_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/patch/patch_android_command_test.dart @@ -604,6 +604,7 @@ https://github.com/shorebirdtech/shorebird/issues/472 verify(() => logger.confirm('Continue anyways?')).called(1); verifyNever( () => codePushClientWrapper.createPatch( + appId: any(named: 'appId'), releaseId: any(named: 'releaseId'), ), ); diff --git a/packages/shorebird_cli/test/src/commands/patch/patch_ios_command_test.dart b/packages/shorebird_cli/test/src/commands/patch/patch_ios_command_test.dart index 9b6fce49..5175b709 100644 --- a/packages/shorebird_cli/test/src/commands/patch/patch_ios_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/patch/patch_ios_command_test.dart @@ -428,6 +428,7 @@ https://github.com/shorebirdtech/shorebird/issues/472 expect(exitCode, equals(ExitCode.success.code)); verifyNever( () => codePushClientWrapper.createPatch( + appId: any(named: 'appId'), releaseId: any(named: 'releaseId'), ), ); diff --git a/packages/shorebird_cli/test/src/java_test.dart b/packages/shorebird_cli/test/src/java_test.dart index 8e1065fb..3b8a02db 100644 --- a/packages/shorebird_cli/test/src/java_test.dart +++ b/packages/shorebird_cli/test/src/java_test.dart @@ -100,7 +100,9 @@ void main() { 'PROGRAMFILES(X86)': tempDir.path, }); await expectLater( - runWithOverrides(() => java.home), equals(jbrDir.path)); + runWithOverrides(() => java.home), + equals(jbrDir.path), + ); }); test('returns correct path on MacOS', () async { @@ -125,7 +127,9 @@ void main() { when(() => platform.isLinux).thenReturn(false); when(() => platform.environment).thenReturn({'HOME': tempDir.path}); await expectLater( - runWithOverrides(() => java.home), equals(jbrDir.path)); + runWithOverrides(() => java.home), + equals(jbrDir.path), + ); }); test('returns correct path on Linux', () async { @@ -144,7 +148,9 @@ void main() { when(() => platform.isLinux).thenReturn(true); when(() => platform.environment).thenReturn({'HOME': tempDir.path}); await expectLater( - runWithOverrides(() => java.home), equals(jbrDir.path)); + runWithOverrides(() => java.home), + equals(jbrDir.path), + ); }); }); }); diff --git a/packages/shorebird_code_push_client/example/main.dart b/packages/shorebird_code_push_client/example/main.dart index d71e3bfe..7c27bd87 100644 --- a/packages/shorebird_code_push_client/example/main.dart +++ b/packages/shorebird_code_push_client/example/main.dart @@ -39,10 +39,11 @@ Future main() async { ); // Create a new patch. - final patch = await client.createPatch(releaseId: release.id); + final patch = await client.createPatch(appId: app.id, releaseId: release.id); // Create a patch artifact. await client.createPatchArtifact( + appId: app.id, patchId: patch.id, artifactPath: '', // e.g. 'libapp.so' platform: '', // e.g. 'android' @@ -51,7 +52,11 @@ Future main() async { ); // Promote a patch to a channel. - await client.promotePatch(patchId: patch.id, channelId: channel.id); + await client.promotePatch( + appId: app.id, + patchId: patch.id, + channelId: channel.id, + ); // Close the client. client.close(); diff --git a/packages/shorebird_code_push_client/lib/src/code_push_client.dart b/packages/shorebird_code_push_client/lib/src/code_push_client.dart index bbc0f24a..d48a581d 100644 --- a/packages/shorebird_code_push_client/lib/src/code_push_client.dart +++ b/packages/shorebird_code_push_client/lib/src/code_push_client.dart @@ -135,6 +135,7 @@ class CodePushClient { /// Create a new artifact for a specific [patchId]. Future createPatchArtifact({ required String artifactPath, + required String appId, required int patchId, required String arch, required String platform, @@ -142,7 +143,7 @@ class CodePushClient { }) async { final request = http.MultipartRequest( 'POST', - Uri.parse('$_v1/patches/$patchId/artifacts'), + Uri.parse('$_v1/apps/$appId/patches/$patchId/artifacts'), ); final file = await http.MultipartFile.fromPath('file', artifactPath); request.fields.addAll({ @@ -265,9 +266,12 @@ class CodePushClient { } /// Create a new patch for the given [releaseId]. - Future createPatch({required int releaseId}) async { + Future createPatch({ + required String appId, + required int releaseId, + }) async { final response = await _httpClient.post( - Uri.parse('$_v1/patches'), + Uri.parse('$_v1/apps/$appId/patches'), body: json.encode({'release_id': releaseId}), ); @@ -492,11 +496,12 @@ class CodePushClient { /// Promote the [patchId] to the [channelId]. Future promotePatch({ + required String appId, required int patchId, required int channelId, }) async { final response = await _httpClient.post( - Uri.parse('$_v1/patches/promote'), + Uri.parse('$_v1/apps/$appId/patches/promote'), body: json.encode({'patch_id': patchId, 'channel_id': channelId}), ); diff --git a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart index accf98db..c5b1e826 100644 --- a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart +++ b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart @@ -231,6 +231,7 @@ void main() { try { await codePushClient.createPatchArtifact( + appId: appId, artifactPath: fixture.path, patchId: patchId, arch: arch, @@ -243,7 +244,10 @@ void main() { .captured .single as http.BaseRequest; expect(request.method, equals('POST')); - expect(request.url, equals(v1('patches/$patchId/artifacts'))); + expect( + request.url, + equals(v1('apps/$appId/patches/$patchId/artifacts')), + ); expect(request.hasStandardHeaders, isTrue); }); @@ -261,6 +265,7 @@ void main() { expect( codePushClient.createPatchArtifact( + appId: appId, artifactPath: fixture.path, patchId: patchId, arch: arch, @@ -291,6 +296,7 @@ void main() { expect( codePushClient.createPatchArtifact( + appId: appId, artifactPath: fixture.path, patchId: patchId, arch: arch, @@ -345,6 +351,7 @@ void main() { await expectLater( codePushClient.createPatchArtifact( + appId: appId, artifactPath: fixture.path, patchId: patchId, arch: arch, @@ -407,6 +414,7 @@ void main() { await expectLater( codePushClient.createPatchArtifact( + appId: appId, artifactPath: fixture.path, patchId: patchId, arch: arch, @@ -423,7 +431,7 @@ void main() { expect( request.url, codePushClient.hostedUri.replace( - path: '/api/v1/patches/$patchId/artifacts', + path: '/api/v1/apps/$appId/patches/$patchId/artifacts', ), ); }); @@ -939,12 +947,12 @@ void main() { const releaseId = 0; test('makes the correct request', () async { - codePushClient.createPatch(releaseId: releaseId).ignore(); + codePushClient.createPatch(appId: appId, releaseId: releaseId).ignore(); final request = verify(() => httpClient.send(captureAny())) .captured .single as http.BaseRequest; expect(request.method, equals('POST')); - expect(request.url, equals(v1('patches'))); + expect(request.url, equals(v1('apps/$appId/patches'))); expect(request.hasStandardHeaders, isTrue); }); @@ -957,7 +965,7 @@ void main() { ); expect( - codePushClient.createPatch(releaseId: releaseId), + codePushClient.createPatch(appId: appId, releaseId: releaseId), throwsA( isA().having( (e) => e.message, @@ -977,7 +985,7 @@ void main() { ); expect( - codePushClient.createPatch(releaseId: releaseId), + codePushClient.createPatch(appId: appId, releaseId: releaseId), throwsA( isA().having( (e) => e.message, @@ -1003,7 +1011,7 @@ void main() { ); await expectLater( - codePushClient.createPatch(releaseId: releaseId), + codePushClient.createPatch(appId: appId, releaseId: releaseId), completion( equals( isA() @@ -1019,7 +1027,7 @@ void main() { expect( request.url, - codePushClient.hostedUri.replace(path: '/api/v1/patches'), + codePushClient.hostedUri.replace(path: '/api/v1/apps/$appId/patches'), ); }); }); @@ -2055,13 +2063,13 @@ void main() { test('makes the correct request', () async { codePushClient - .promotePatch(patchId: patchId, channelId: channelId) + .promotePatch(appId: appId, patchId: patchId, channelId: channelId) .ignore(); final request = verify(() => httpClient.send(captureAny())) .captured .single as http.BaseRequest; expect(request.method, equals('POST')); - expect(request.url, equals(v1('patches/promote'))); + expect(request.url, equals(v1('apps/$appId/patches/promote'))); expect(request.hasStandardHeaders, isTrue); }); @@ -2074,7 +2082,11 @@ void main() { ); expect( - codePushClient.promotePatch(patchId: patchId, channelId: channelId), + codePushClient.promotePatch( + appId: appId, + patchId: patchId, + channelId: channelId, + ), throwsA( isA().having( (e) => e.message, @@ -2094,7 +2106,11 @@ void main() { ); expect( - codePushClient.promotePatch(patchId: patchId, channelId: channelId), + codePushClient.promotePatch( + appId: appId, + patchId: patchId, + channelId: channelId, + ), throwsA( isA().having( (e) => e.message, @@ -2114,7 +2130,11 @@ void main() { ); await expectLater( - codePushClient.promotePatch(patchId: patchId, channelId: channelId), + codePushClient.promotePatch( + appId: appId, + patchId: patchId, + channelId: channelId, + ), completes, ); @@ -2124,7 +2144,8 @@ void main() { expect( request.url, - codePushClient.hostedUri.replace(path: '/api/v1/patches/promote'), + codePushClient.hostedUri + .replace(path: '/api/v1/apps/$appId/patches/promote'), ); }); });