Skip to content

Commit

Permalink
refactor(code_push_client)!: use new patches endpoint (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Jul 18, 2023
1 parent f891800 commit 79da63f
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 32 deletions.
18 changes: 15 additions & 3 deletions packages/shorebird_cli/lib/src/code_push_client_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,16 @@ aar artifact already exists, continuing...''',
}

@visibleForTesting
Future<Patch> createPatch({required int releaseId}) async {
Future<Patch> 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) {
Expand All @@ -527,6 +533,7 @@ aar artifact already exists, continuing...''',

@visibleForTesting
Future<void> createPatchArtifacts({
required String appId,
required Patch patch,
required String platform,
required Map<Arch, PatchArtifactBundle> patchArtifactBundles,
Expand All @@ -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,
Expand All @@ -551,6 +559,7 @@ aar artifact already exists, continuing...''',

@visibleForTesting
Future<void> promotePatch({
required String appId,
required int patchId,
required Channel channel,
}) async {
Expand All @@ -559,6 +568,7 @@ aar artifact already exists, continuing...''',
);
try {
await codePushClient.promotePatch(
appId: appId,
patchId: patchId,
channelId: channel.id,
);
Expand All @@ -577,10 +587,12 @@ aar artifact already exists, continuing...''',
required Map<Arch, PatchArtifactBundle> patchArtifactBundles,
}) async {
final patch = await createPatch(
appId: appId,
releaseId: releaseId,
);

await createPatchArtifacts(
appId: appId,
patch: patch,
platform: platform,
patchArtifactBundles: patchArtifactBundles,
Expand All @@ -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<GetUsageResponse> getUsage() async {
Expand Down
45 changes: 39 additions & 6 deletions packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
Expand All @@ -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,
),
);
Expand All @@ -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'),
),
Expand All @@ -1630,6 +1640,7 @@ Please bump your version number and try again.''',
await expectLater(
() async => runWithOverrides(
() => codePushClientWrapper.promotePatch(
appId: appId,
patchId: patchId,
channel: channel,
),
Expand All @@ -1642,13 +1653,15 @@ 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'),
),
).thenAnswer((_) async => patch);

await runWithOverrides(
() => codePushClientWrapper.promotePatch(
appId: appId,
patchId: patchId,
channel: channel,
),
Expand All @@ -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'),
Expand All @@ -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,
Expand All @@ -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'),
Expand All @@ -1701,6 +1717,7 @@ Please bump your version number and try again.''',

await runWithOverrides(
() => codePushClientWrapper.createPatchArtifacts(
appId: appId,
patch: patch,
platform: platformName,
patchArtifactBundles: patchArtifactBundles,
Expand All @@ -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,
Expand All @@ -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'),
Expand All @@ -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'),
),
Expand All @@ -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,
Expand All @@ -1777,6 +1804,7 @@ Please bump your version number and try again.''',
);
verify(
() => codePushClient.promotePatch(
appId: appId,
patchId: patchId,
channelId: channel.id,
),
Expand Down Expand Up @@ -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,
Expand All @@ -1826,6 +1858,7 @@ Please bump your version number and try again.''',
).called(1);
verify(
() => codePushClient.promotePatch(
appId: appId,
patchId: patchId,
channelId: channel.id,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
);
Expand Down
12 changes: 9 additions & 3 deletions packages/shorebird_cli/test/src/java_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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),
);
});
});
});
Expand Down
9 changes: 7 additions & 2 deletions packages/shorebird_code_push_client/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ Future<void> 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: '<PATH TO ARTIFACT>', // e.g. 'libapp.so'
platform: '<PLATFORM>', // e.g. 'android'
Expand All @@ -51,7 +52,11 @@ Future<void> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,15 @@ class CodePushClient {
/// Create a new artifact for a specific [patchId].
Future<void> createPatchArtifact({
required String artifactPath,
required String appId,
required int patchId,
required String arch,
required String platform,
required String hash,
}) 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({
Expand Down Expand Up @@ -265,9 +266,12 @@ class CodePushClient {
}

/// Create a new patch for the given [releaseId].
Future<Patch> createPatch({required int releaseId}) async {
Future<Patch> 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}),
);

Expand Down Expand Up @@ -492,11 +496,12 @@ class CodePushClient {

/// Promote the [patchId] to the [channelId].
Future<void> 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}),
);

Expand Down
Loading

0 comments on commit 79da63f

Please sign in to comment.