refactor(code_push_client)!: use new patches endpoint (#865)

This commit is contained in:
Felix Angelov
2023-07-17 23:00:45 -05:00
committed by GitHub
parent f891800419
commit 79da63ff9f
8 changed files with 116 additions and 32 deletions
@@ -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) {
@@ -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,
@@ -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<void> 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<Arch, PatchArtifactBundle> 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<GetUsageResponse> getUsage() async {
@@ -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,
),
@@ -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'),
),
);
@@ -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'),
),
);
@@ -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),
);
});
});
});
@@ -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'
@@ -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();
@@ -135,6 +135,7 @@ 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,
@@ -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<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}),
);
@@ -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}),
);
@@ -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<CodePushException>().having(
(e) => e.message,
@@ -977,7 +985,7 @@ void main() {
);
expect(
codePushClient.createPatch(releaseId: releaseId),
codePushClient.createPatch(appId: appId, releaseId: releaseId),
throwsA(
isA<CodePushException>().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<Patch>()
@@ -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<CodePushException>().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<CodePushException>().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'),
);
});
});