375
Ok(NotesSet { dry_run, track })
376
}
377
378
+/// The manifest every Android App Bundle carries.
379
+const BUNDLE_MANIFEST: &str = "base/manifest/AndroidManifest.xml";
380
+
381
+/// A bundle checked on disk, ready to upload.
382
+#[derive(Debug, Serialize)]
383
+#[serde(rename_all = "camelCase")]
384
+pub struct BundleFile {
385
+ pub path: PathBuf,
386
+ pub bytes: u64,
387
+ /// Lowercase hex SHA-256 of the file, to compare with Play's.
388
+ pub sha256: String,
389
+}
390
+
391
+/// Check a file is an `.aab` (a zip with `base/manifest/AndroidManifest.xml`)
392
+/// and hash it. Nothing is sent.
393
+pub fn load_bundle(path: &Path) -> Result<(BundleFile, Vec<u8>), String> {
394
+ let bytes = std::fs::read(path).map_err(|e| format!("cannot read {}: {e}", path.display()))?;
395
+ let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice()))
396
+ .map_err(|_| format!("{} is not a zip archive, so not an .aab", path.display()))?;
397
+ if archive.by_name(BUNDLE_MANIFEST).is_err() {
398
+ return Err(format!(
399
+ "{} has no {BUNDLE_MANIFEST}; it is a zip but not an Android App Bundle",
400
+ path.display()
401
+ ));
402
+ }
403
+ let file = BundleFile {
404
+ path: path.to_path_buf(),
405
+ bytes: bytes.len() as u64,
406
+ sha256: hex(&Sha256::digest(&bytes)),
407
+ };
408
+ Ok((file, bytes))
409
+}
410
+
411
+fn check_upload_status(status: &str) -> Result<(), String> {
412
+ match status {
413
+ "draft" | "completed" => Ok(()),
414
+ other => Err(format!(
415
+ "status {other:?} is not supported for an upload; use draft or completed"
416
+ )),
417
+ }
418
+}
419
+
420
+/// What `bundles upload --dry-run` reports. A dry run cannot know the version
421
+/// code: Play assigns it on upload.
422
+#[derive(Debug, Serialize)]
423
+#[serde(rename_all = "camelCase")]
424
+pub struct BundlePlan {
425
+ pub dry_run: bool,
426
+ pub package: String,
427
+ pub track: String,
428
+ pub status: String,
429
+ pub bundle: BundleFile,
430
+ pub mapping_file: Option<PathBuf>,
431
+ pub ack_install_warning: bool,
432
+}
433
+
434
+/// The local half of `bundles upload`: everything that can be checked without
435
+/// sending anything.
436
+pub fn plan_bundle_upload(
437
+ package: &str,
438
+ track: &str,
439
+ file: &Path,
440
+ status: &str,
441
+ mapping_file: Option<&Path>,
442
+ ack_install_warning: bool,
443
+) -> Result<BundlePlan, String> {
444
+ check_upload_status(status)?;
445
+ let (bundle, _) = load_bundle(file)?;
446
+ if let Some(mapping) = mapping_file {
447
+ if !mapping.is_file() {
448
+ return Err(format!("mapping file {} does not exist", mapping.display()));
449
+ }
450
+ }
451
+ Ok(BundlePlan {
452
+ dry_run: true,
453
+ package: package.to_string(),
454
+ track: track.to_string(),
455
+ status: status.to_string(),
456
+ bundle,
457
+ mapping_file: mapping_file.map(Path::to_path_buf),
458
+ ack_install_warning,
459
+ })
460
+}
461
+
462
+#[derive(Debug, Serialize)]
463
+#[serde(rename_all = "camelCase")]
464
+pub struct BundleUploaded {
465
+ /// Assigned by Play; use it with `tracks set-notes` or `tracks promote`.
466
+ pub version_code: u64,
467
+ pub sha256: String,
468
+ pub status: String,
469
+ pub mapping_uploaded: bool,
470
+ /// The track as read back after the commit.
471
+ pub track: Track,
472
+}
473
+
474
+/// Whether the read-back track shows `code` on a release at `status`.
475
+fn bundle_landed(track: &Track, code: &str, status: &str) -> bool {
476
+ track
477
+ .releases
478
+ .iter()
479
+ .any(|r| r.version_codes.iter().any(|c| c == code) && r.status.as_deref() == Some(status))
480
+}
481
+
482
+/// Upload an `.aab`, optionally its mapping file, and put it on `track_name`
483
+/// as a new release, in one edit. Play's SHA-256 must match the local one, and
484
+/// the track is read back after the commit. The version code is consumed on
485
+/// upload, so a failure after that point still uses one up.
486
+pub async fn upload_bundle(
487
+ client: &PlayClient,
488
+ package: &str,
489
+ track_name: &str,
490
+ file: &Path,
491
+ status: &str,
492
+ mapping_file: Option<&Path>,
493
+ ack_install_warning: bool,
494
+) -> Result<BundleUploaded, String> {
495
+ check_upload_status(status)?;
496
+ let (bundle, bytes) = load_bundle(file)?;
497
+ let mapping = match mapping_file {
498
+ Some(p) => Some(std::fs::read(p).map_err(|e| format!("cannot read {}: {e}", p.display()))?),
499
+ None => None,
500
+ };
501
+ let mapping_uploaded = mapping.is_some();
502
+ let local_sha = bundle.sha256;
503
+ let expected = local_sha.as_str();
504
+ let version_code = in_edit!(client, package, true, |c, id| {
505
+ Box::pin(async move {
506
+ let uploaded = c
507
+ .upload_bundle(package, id, bytes, ack_install_warning)
508
+ .await
509
+ .map_err(err)?;
510
+ if !uploaded.sha256.eq_ignore_ascii_case(expected) {
511
+ return Err(format!(
512
+ "Play stored version code {} with SHA-256 {}, but the local file is {expected}; \
513
+ nothing was committed",
514
+ uploaded.version_code, uploaded.sha256
515
+ ));
516
+ }
517
+ if let Some(mapping) = mapping {
518
+ c.upload_deobfuscation(package, id, uploaded.version_code, mapping)
519
+ .await
520
+ .map_err(err)?;
521
+ }
522
+ let code = uploaded.version_code.to_string();
523
+ let track = c.get_track(package, id, track_name).await.map_err(err)?;
524
+ let planned = track.place_uploaded(&code, status)?;
525
+ c.update_track(package, id, &planned).await.map_err(err)?;
526
+ Ok(uploaded.version_code)
527
+ })
528
+ })?;
529
+ let track = read_track(client, package, track_name).await?;
530
+ let code = version_code.to_string();
531
+ let landed = bundle_landed(&track, &code, status);
532
+ if !landed {
533
+ return Err(format!(
534
+ "committed, but {track_name} does not show version code {version_code} as {status}"
535
+ ));
536
+ }
537
+ Ok(BundleUploaded {
538
+ version_code,
539
+ sha256: local_sha,
540
+ status: status.to_string(),
541
+ mapping_uploaded,
542
+ track,
543
+ })
544
+}
545
+
546
#[derive(Debug, Serialize)]
547
#[serde(rename_all = "camelCase")]
548
pub struct Promoted {
880
assert!(error.contains("PLAY_SERVICE_ACCOUNT_JSON_PATH"));
881
}
882
}
883
+
884
+ fn zip_with(dir: &Path, name: &str, entries: &[&str]) -> PathBuf {
885
+ let path = dir.join(name);
886
+ let mut zip = zip::ZipWriter::new(std::fs::File::create(&path).unwrap());
887
+ for entry in entries {
888
+ zip.start_file(*entry, zip::write::SimpleFileOptions::default())
889
+ .unwrap();
890
+ std::io::Write::write_all(&mut zip, b"x").unwrap();
891
+ }
892
+ zip.finish().unwrap();
893
+ path
894
+ }
895
+
896
+ #[test]
897
+ fn load_bundle_accepts_a_bundle_and_rejects_the_rest() {
898
+ let dir = std::env::temp_dir().join(format!("asc-aab-{}", std::process::id()));
899
+ std::fs::create_dir_all(&dir).unwrap();
900
+ let good = zip_with(&dir, "ok.aab", &[BUNDLE_MANIFEST]);
901
+ let (file, bytes) = load_bundle(&good).unwrap();
902
+ assert_eq!(file.bytes as usize, bytes.len());
903
+ assert_eq!(file.sha256.len(), 64);
904
+
905
+ let bare = zip_with(&dir, "bare.aab", &["other.txt"]);
906
+ assert!(load_bundle(&bare)
907
+ .unwrap_err()
908
+ .contains("not an Android App Bundle"));
909
+
910
+ let text = dir.join("a.aab");
911
+ std::fs::write(&text, b"not a zip").unwrap();
912
+ assert!(load_bundle(&text).unwrap_err().contains("not a zip"));
913
+ assert!(load_bundle(&dir.join("missing.aab")).is_err());
914
+
915
+ let missing_mapping = dir.join("nope.txt");
916
+ assert!(plan_bundle_upload(
917
+ "p",
918
+ "internal",
919
+ &good,
920
+ "draft",
921
+ Some(&missing_mapping),
922
+ false
923
+ )
924
+ .unwrap_err()
925
+ .contains("mapping file"));
926
+ assert!(plan_bundle_upload("p", "internal", &good, "inProgress", None, false).is_err());
927
+ let plan = plan_bundle_upload("p", "internal", &good, "draft", None, false).unwrap();
928
+ assert!(plan.dry_run);
929
+ std::fs::remove_dir_all(&dir).unwrap();
930
+ }
931
+
932
+ #[test]
933
+ fn read_back_must_show_the_code_at_the_status() {
934
+ let track: Track = serde_json::from_str(
935
+ r#"{"track":"internal","releases":[{"versionCodes":["8"],"status":"draft"}]}"#,
936
+ )
937
+ .unwrap();
938
+ assert!(bundle_landed(&track, "8", "draft"));
939
+ assert!(!bundle_landed(&track, "8", "completed"));
940
+ assert!(!bundle_landed(&track, "9", "draft"));
941
+ }
942
}