smbcloud-ascapi-frontend 0.5.0

Shared operations behind the asc command line and MCP surfaces: credential resolution, result shaping, certificate issuance, and altool uploads.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Google Play listing operations shared by `asc play` and the MCP tools.
//!
//! Play does everything through edits: open one, change things, `validate`,
//! then `commit`. Every function here opens exactly one edit and closes it
//! again, so a failure or a dry run leaves nothing behind. Reads open an edit
//! too (the API has no edit-less read for listings) and delete it after.

use std::path::{Path, PathBuf};

use serde::Serialize;
use sha2::{Digest, Sha256};
use smbcloud_ascapi_play::{
    png_dimensions, Image, ImageType, Listing, ListingFields, PlayClient, ServiceAccount, Track,
};

/// Resolve the Play client. `path` wins; then `PLAY_SERVICE_ACCOUNT_JSON_PATH`;
/// then the key contents inline in `PLAY_SERVICE_ACCOUNT_JSON`. Per call, like
/// the App Store Connect credentials, so an unconfigured MCP server still
/// lists its tools. The key itself never appears in an error.
pub fn play_client(path: Option<&Path>) -> Result<PlayClient, String> {
    let account = if let Some(path) = path {
        ServiceAccount::from_json_file(path)
    } else if let Ok(path) = std::env::var("PLAY_SERVICE_ACCOUNT_JSON_PATH") {
        ServiceAccount::from_json_file(path)
    } else if let Ok(json) = std::env::var("PLAY_SERVICE_ACCOUNT_JSON") {
        ServiceAccount::from_json(&json)
    } else {
        return Err(
            "no Google Play credentials: pass --play-service-account, or set \
                    PLAY_SERVICE_ACCOUNT_JSON_PATH (or PLAY_SERVICE_ACCOUNT_JSON)"
                .to_string(),
        );
    };
    account.map(PlayClient::new).map_err(|e| e.to_string())
}

/// Parse an image bucket name given as `phone-screenshots` or
/// `phoneScreenshots`.
pub fn image_type_from_str(value: &str) -> Result<ImageType, String> {
    match value.replace('-', "").to_ascii_lowercase().as_str() {
        "phonescreenshots" => Ok(ImageType::PhoneScreenshots),
        "seveninchscreenshots" => Ok(ImageType::SevenInchScreenshots),
        "teninchscreenshots" => Ok(ImageType::TenInchScreenshots),
        "featuregraphic" => Ok(ImageType::FeatureGraphic),
        "icon" => Ok(ImageType::Icon),
        other => Err(format!(
            "unknown image type {other:?}; use phone-screenshots, seven-inch-screenshots, \
             ten-inch-screenshots, feature-graphic or icon"
        )),
    }
}

fn err(error: impl std::fmt::Display) -> String {
    error.to_string()
}

/// Open an edit, run the body inside it, then commit (after `validate`) when
/// `commit` is set, or delete the edit otherwise. A failure anywhere deletes
/// the edit and returns the first error. A macro rather than a function so the
/// body can borrow from the caller's scope.
macro_rules! in_edit {
    ($client:expr, $package:expr, $commit:expr, |$c:ident, $id:ident| $body:expr) => {{
        let $c: &PlayClient = $client;
        let edit = $c.insert_edit($package).await.map_err(err)?;
        let $id: &str = edit.id.as_str();
        let outcome: Result<_, String> = $body.await;
        finish($c, $package, $id, $commit, outcome).await
    }};
}

async fn finish<T>(
    client: &PlayClient,
    package: &str,
    edit_id: &str,
    commit: bool,
    outcome: Result<T, String>,
) -> Result<T, String> {
    let outcome = match outcome {
        Ok(value) if commit => {
            async {
                client.validate_edit(package, edit_id).await.map_err(err)?;
                client.commit_edit(package, edit_id).await.map_err(err)?;
                Ok(value)
            }
            .await
        }
        other => other,
    };
    if !(commit && outcome.is_ok()) {
        // Best effort: an edit that fails to delete simply expires.
        let _ = client.delete_edit(package, edit_id).await;
    }
    outcome
}

pub async fn read_listings(client: &PlayClient, package: &str) -> Result<Vec<Listing>, String> {
    in_edit!(client, package, false, |c, id| {
        Box::pin(async move { c.list_listings(package, id).await.map_err(err) })
    })
}

pub async fn read_images(
    client: &PlayClient,
    package: &str,
    locale: &str,
    image_type: ImageType,
) -> Result<Vec<Image>, String> {
    in_edit!(client, package, false, |c, id| {
        Box::pin(async move {
            c.list_images(package, id, locale, image_type)
                .await
                .map_err(err)
        })
    })
}

pub async fn read_track(client: &PlayClient, package: &str, track: &str) -> Result<Track, String> {
    in_edit!(client, package, false, |c, id| {
        Box::pin(async move { c.get_track(package, id, track).await.map_err(err) })
    })
}

/// The result of a listing update.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ListingUpdate {
    pub dry_run: bool,
    /// What was (or, on a dry run, would be) sent.
    pub sent: ListingFields,
    /// The listing before the change, if the locale existed.
    pub before: Option<Listing>,
    /// The listing read back after commit. `None` on a dry run.
    pub after: Option<Listing>,
}

pub async fn update_listing(
    client: &PlayClient,
    package: &str,
    locale: &str,
    fields: ListingFields,
    dry_run: bool,
) -> Result<ListingUpdate, String> {
    fields.validate()?;
    if fields.is_empty() {
        return Err("nothing to update: pass at least one listing field".to_string());
    }
    let sent = fields.clone();
    let before = in_edit!(client, package, !dry_run, |c, id| {
        Box::pin(async move {
            let before = c.get_listing(package, id, locale).await.ok();
            if !dry_run {
                c.patch_listing(package, id, locale, &fields)
                    .await
                    .map_err(err)?;
            }
            Ok(before)
        })
    })?;
    let after = if dry_run {
        None
    } else {
        let stored = in_edit!(client, package, false, |c, id| {
            Box::pin(async move { c.get_listing(package, id, locale).await.map_err(err) })
        })?;
        let wrong = sent.diff(&stored);
        if !wrong.is_empty() {
            return Err(format!(
                "committed, but the read-back differs in: {}",
                wrong.join(", ")
            ));
        }
        Some(stored)
    };
    Ok(ListingUpdate {
        dry_run,
        sent,
        before,
        after,
    })
}

/// One local file picked for upload.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageFile {
    pub path: PathBuf,
    pub bytes: usize,
    pub sha256: String,
    pub width: Option<u32>,
    pub height: Option<u32>,
    #[serde(skip)]
    content_type: &'static str,
    #[serde(skip)]
    data: Vec<u8>,
}

/// Read and lint a local image: PNG or JPEG, and for PNGs the pixel size
/// against the bucket's rules.
pub fn load_image(path: &Path, image_type: ImageType) -> Result<ImageFile, String> {
    let data = std::fs::read(path).map_err(|e| format!("cannot read {}: {e}", path.display()))?;
    let extension = path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    let content_type = match extension.as_deref() {
        Some("png") => "image/png",
        Some("jpg") | Some("jpeg") => "image/jpeg",
        _ => return Err(format!("{}: Play accepts only PNG or JPEG", path.display())),
    };
    let dims = png_dimensions(&data);
    if let Some((w, h)) = dims {
        image_type
            .check_dimensions(w, h)
            .map_err(|m| format!("{}: {m}", path.display()))?;
    } else if content_type == "image/png" {
        return Err(format!("{}: not a valid PNG", path.display()));
    }
    Ok(ImageFile {
        path: path.to_path_buf(),
        bytes: data.len(),
        sha256: hex(&Sha256::digest(&data)),
        width: dims.map(|d| d.0),
        height: dims.map(|d| d.1),
        content_type,
        data,
    })
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImagesReplaced {
    pub dry_run: bool,
    pub image_type: String,
    pub locale: String,
    pub files: Vec<ImageFile>,
    /// How many images the bucket held before the replace.
    pub previously: usize,
    /// The bucket read back after commit. `None` on a dry run.
    pub after: Option<Vec<Image>>,
}

/// Delete the bucket's images and upload `paths` in order, inside one edit,
/// so a failed upload leaves the old set in place.
pub async fn replace_images(
    client: &PlayClient,
    package: &str,
    locale: &str,
    image_type: ImageType,
    paths: &[PathBuf],
    dry_run: bool,
) -> Result<ImagesReplaced, String> {
    if paths.is_empty() {
        return Err("no image files given".to_string());
    }
    let files = paths
        .iter()
        .map(|p| load_image(p, image_type))
        .collect::<Result<Vec<_>, _>>()?;
    let previously = in_edit!(client, package, !dry_run, |c, id| {
        let files = &files;
        Box::pin(async move {
            let previously = c
                .list_images(package, id, locale, image_type)
                .await
                .map_err(err)?
                .len();
            if !dry_run {
                c.delete_all_images(package, id, locale, image_type)
                    .await
                    .map_err(err)?;
                for file in files {
                    c.upload_image(
                        package,
                        id,
                        locale,
                        image_type,
                        file.content_type,
                        file.data.clone(),
                    )
                    .await
                    .map_err(|e| format!("{}: {e}", file.path.display()))?;
                }
            }
            Ok(previously)
        })
    })?;
    let after = if dry_run {
        None
    } else {
        let images = read_images(client, package, locale, image_type).await?;
        if images.len() != files.len() {
            return Err(format!(
                "committed, but the bucket holds {} images; expected {}",
                images.len(),
                files.len()
            ));
        }
        for (image, file) in images.iter().zip(&files) {
            if image.sha256.as_deref().is_some_and(|s| s != file.sha256) {
                return Err(format!(
                    "committed, but the stored copy of {} has a different sha256",
                    file.path.display()
                ));
            }
        }
        Some(images)
    };
    Ok(ImagesReplaced {
        dry_run,
        image_type: image_type.api_name().to_string(),
        locale: locale.to_string(),
        files,
        previously,
        after,
    })
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NotesSet {
    pub dry_run: bool,
    /// The track as it will be (dry run) or is (read back after commit).
    pub track: Track,
}

pub async fn set_track_notes(
    client: &PlayClient,
    package: &str,
    track_name: &str,
    version_code: &str,
    locale: &str,
    notes: &str,
    dry_run: bool,
) -> Result<NotesSet, String> {
    let planned = in_edit!(client, package, !dry_run, |c, id| {
        Box::pin(async move {
            let mut track = c.get_track(package, id, track_name).await.map_err(err)?;
            track.set_release_notes(version_code, locale, notes)?;
            if !dry_run {
                c.update_track(package, id, &track).await.map_err(err)?;
            }
            Ok(track)
        })
    })?;
    if dry_run {
        return Ok(NotesSet {
            dry_run,
            track: planned,
        });
    }
    let track = read_track(client, package, track_name).await?;
    let stored = track
        .releases
        .iter()
        .filter(|r| r.version_codes.iter().any(|c| c == version_code))
        .flat_map(|r| &r.release_notes)
        .any(|n| n.language == locale && n.text == notes);
    if !stored {
        return Err("committed, but the read-back release notes differ".to_string());
    }
    Ok(NotesSet { dry_run, track })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn image_types_parse_in_both_spellings() {
        assert_eq!(
            image_type_from_str("phone-screenshots"),
            Ok(ImageType::PhoneScreenshots)
        );
        assert_eq!(
            image_type_from_str("featureGraphic"),
            Ok(ImageType::FeatureGraphic)
        );
        assert!(image_type_from_str("tv").is_err());
    }

    #[test]
    fn load_image_rejects_other_formats() {
        let dir = std::env::temp_dir();
        let path = dir.join("ascapi-play-test.gif");
        std::fs::write(&path, b"GIF89a").unwrap();
        let error = load_image(&path, ImageType::PhoneScreenshots).unwrap_err();
        assert!(error.contains("PNG or JPEG"));
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn load_image_lints_png_size_for_the_bucket() {
        let mut png = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A, 0, 0, 0, 13];
        png.extend_from_slice(b"IHDR");
        png.extend_from_slice(&1000u32.to_be_bytes());
        png.extend_from_slice(&500u32.to_be_bytes());
        let path = std::env::temp_dir().join("ascapi-play-test.png");
        std::fs::write(&path, &png).unwrap();
        assert!(load_image(&path, ImageType::FeatureGraphic).is_err());
        assert!(load_image(&path, ImageType::PhoneScreenshots).is_ok());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn missing_credentials_name_the_options_without_a_key() {
        // Only meaningful when the variables are unset, as in CI.
        if std::env::var_os("PLAY_SERVICE_ACCOUNT_JSON_PATH").is_none()
            && std::env::var_os("PLAY_SERVICE_ACCOUNT_JSON").is_none()
        {
            let error = play_client(None).err().unwrap();
            assert!(error.contains("PLAY_SERVICE_ACCOUNT_JSON_PATH"));
        }
    }
}