-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplay_publish.py
More file actions
520 lines (457 loc) · 18 KB
/
Copy pathplay_publish.py
File metadata and controls
520 lines (457 loc) · 18 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
"""Publish Android AAB to Google Play with production->alpha fallback support."""
from __future__ import annotations
import argparse
import glob
import json
import mimetypes
import os
import sys
import tempfile
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any
RESET_ERROR_FRAGMENT = (
"certificate this apk is signed with is not yet valid because it has been recently reset"
)
TRANSIENT_ERROR_FRAGMENTS = (
"eof occurred in violation of protocol",
"connection reset",
"connection aborted",
"timed out",
"temporary failure",
"service unavailable",
)
FAILED_PRECONDITION_MARKERS = (
"failed_precondition",
"precondition check failed",
)
MANUAL_REVIEW_REQUIRED_MARKERS = (
"changes cannot be sent for review automatically",
"changesnotsentforreview",
)
LANG_MAP = {
"en-US": "en-US",
"de-DE": "de-DE",
"pt-BR": "pt-BR",
"ja-JP": "ja-JP",
"ko": "ko-KR",
}
@dataclass
class PublishError(RuntimeError):
"""Structured publish error with response payload for workflow triage."""
message: str
http_status: int | None
response_text: str
attempt: int
def _read_text(path: Path) -> str:
try:
if path.is_file():
return path.read_text(encoding="utf-8").strip()
except Exception:
return ""
return ""
def _write_json(path: Path, payload: dict[str, Any]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2, ensure_ascii=True), encoding="utf-8")
def _mime_for(path: str) -> str:
mime, _ = mimetypes.guess_type(path)
return mime or "application/octet-stream"
def _extract_response_text(error: Exception) -> str:
raw = getattr(error, "content", b"") or b""
if isinstance(raw, (bytes, bytearray)):
return raw.decode("utf-8", errors="ignore").strip()
return str(raw).strip()
def _is_failed_precondition(message: str, response_text: str, http_status: int | None) -> bool:
combined = f"{message}\n{response_text}".lower()
if any(marker in combined for marker in FAILED_PRECONDITION_MARKERS):
return True
return http_status == 400 and "precondition" in combined
def _is_transient_http(http_status: int | None, message: str) -> bool:
if http_status in (429, 500, 502, 503, 504):
return True
lowered = message.lower()
return any(fragment in lowered for fragment in TRANSIENT_ERROR_FRAGMENTS)
def _requires_manual_review_submission(message: str, response_text: str, http_status: int | None) -> bool:
combined = f"{message}\n{response_text}".lower()
return http_status == 400 and any(marker in combined for marker in MANUAL_REVIEW_REQUIRED_MARKERS)
def _load_google_clients(credentials_path: Path):
from google.oauth2 import service_account
from googleapiclient.discovery import build
credentials = service_account.Credentials.from_service_account_file(
str(credentials_path), scopes=["https://www.googleapis.com/auth/androidpublisher"]
)
return build("androidpublisher", "v3", credentials=credentials)
def _upload_images(service: Any, package: str, edit_id: str, language: str, image_type: str, pattern: str) -> None:
from googleapiclient.http import MediaFileUpload
files = sorted(glob.glob(pattern))
if not files:
return
try:
service.edits().images().deleteall(
packageName=package,
editId=edit_id,
language=language,
imageType=image_type,
).execute()
except Exception:
pass
for fp in files:
service.edits().images().upload(
packageName=package,
editId=edit_id,
language=language,
imageType=image_type,
media_body=MediaFileUpload(fp, mimetype=_mime_for(fp)),
).execute()
def _commit_edit(edits_service: Any, package: str, edit_id: str) -> bool:
"""Commit a Play edit.
Returns True when Google requires `changesNotSentForReview=true`, which means
the edit was committed successfully but still needs a manual "Send for review"
action in Play Console.
"""
try:
edits_service.commit(packageName=package, editId=edit_id).execute()
return False
except Exception as error:
response_text = _extract_response_text(error)
status = getattr(getattr(error, "resp", None), "status", None)
if _requires_manual_review_submission(str(error), response_text, status):
try:
edits_service.commit(
packageName=package,
editId=edit_id,
changesNotSentForReview=True,
).execute()
return True
except Exception:
# If retry also fails, changes were likely auto-committed
return False
error_text = f"{error}\n{response_text}".lower()
# Google auto-commits changes — no manual commit needed.
if (
"changesnotsentforreview must not be set" in error_text
or (
"sent for review automatically" in error_text
and "cannot be sent for review automatically" not in error_text
)
):
return False
else:
raise
def _update_listing_and_assets(
service: Any,
package: str,
edit_id: str,
metadata_dir: Path,
ios_support_url_path: Path,
) -> None:
for local_lang, api_lang in LANG_MAP.items():
locale_dir = metadata_dir / local_lang
listing = {}
title = _read_text(locale_dir / "title.txt")
short_desc = _read_text(locale_dir / "short_description.txt")
full_desc = _read_text(locale_dir / "full_description.txt")
video = _read_text(locale_dir / "video.txt")
if title:
listing["title"] = title
if short_desc:
listing["shortDescription"] = short_desc
if full_desc:
listing["fullDescription"] = full_desc
if video:
listing["video"] = video
if not listing:
continue
try:
service.edits().listings().update(
packageName=package,
editId=edit_id,
language=api_lang,
body=listing,
).execute()
except Exception:
continue
details = {"defaultLanguage": "en-US"}
support_url = _read_text(ios_support_url_path)
if support_url:
details["contactWebsite"] = support_url
try:
service.edits().details().patch(
packageName=package,
editId=edit_id,
body=details,
).execute()
except Exception:
pass
_upload_images(
service,
package,
edit_id,
"en-US",
"icon",
str(metadata_dir / "en-US" / "images" / "icon.*"),
)
_upload_images(
service,
package,
edit_id,
"en-US",
"featureGraphic",
str(metadata_dir / "en-US" / "images" / "featureGraphic" / "*.*"),
)
_upload_images(
service,
package,
edit_id,
"en-US",
"phoneScreenshots",
str(metadata_dir / "en-US" / "images" / "phoneScreenshots" / "*.*"),
)
def _release_payload(
version_code: str | int,
release_status: str,
release_notes: str,
user_fraction_raw: str,
) -> dict[str, Any]:
release: dict[str, Any] = {
"versionCodes": [str(version_code)],
"status": release_status,
"name": f"v{version_code}",
}
if release_notes:
release["releaseNotes"] = [{"language": "en-US", "text": release_notes}]
if release_status == "inProgress":
try:
user_fraction = float(user_fraction_raw.strip() or "0.1")
except Exception:
user_fraction = 0.1
user_fraction = max(0.0, min(1.0, user_fraction))
if user_fraction >= 1.0:
user_fraction = 0.1
release["userFraction"] = user_fraction
return release
def _publish_to_track(
*,
package: str,
aab_path: Path,
track: str,
release_status: str,
retry_window_seconds: int,
retry_interval_seconds: int,
metadata_dir: Path,
ios_support_url_path: Path,
changelog_dir: Path,
credentials_path: Path,
user_fraction_raw: str,
) -> dict[str, Any]:
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
service = _load_google_clients(credentials_path)
deadline = time.time() + retry_window_seconds
attempt = 0
while True:
attempt += 1
try:
edit = service.edits().insert(body={}, packageName=package).execute()
edit_id = edit["id"]
bundle = service.edits().bundles().upload(
packageName=package,
editId=edit_id,
media_body=MediaFileUpload(str(aab_path), mimetype="application/octet-stream"),
).execute()
version_code = bundle["versionCode"]
_update_listing_and_assets(
service=service,
package=package,
edit_id=edit_id,
metadata_dir=metadata_dir,
ios_support_url_path=ios_support_url_path,
)
notes_path = changelog_dir / f"{version_code}.txt"
release_notes = _read_text(notes_path)
release = _release_payload(
version_code=version_code,
release_status=release_status,
release_notes=release_notes,
user_fraction_raw=user_fraction_raw,
)
service.edits().tracks().update(
packageName=package,
editId=edit_id,
track=track,
body={"releases": [release]},
).execute()
changes_not_sent_for_review = _commit_edit(service.edits(), package, edit_id)
return {
"version_code": str(version_code),
"attempt": attempt,
"changes_not_sent_for_review": changes_not_sent_for_review,
}
except HttpError as error:
message = str(error)
response_text = _extract_response_text(error)
status = getattr(getattr(error, "resp", None), "status", None)
is_recent_reset = RESET_ERROR_FRAGMENT in f"{message}\n{response_text}".lower()
if (is_recent_reset or _is_transient_http(status, message)) and int(deadline - time.time()) > 0:
remaining = int(deadline - time.time())
sleep_for = min(retry_interval_seconds, remaining)
reason = "key reset propagation" if is_recent_reset else f"transient HTTP {status}"
print(
f"⚠️ Play upload retry due to {reason} (track={track}, attempt={attempt}). "
f"Retrying in {sleep_for}s (remaining window: {remaining}s)...",
file=sys.stderr,
)
time.sleep(sleep_for)
continue
raise PublishError(message=message, http_status=status, response_text=response_text, attempt=attempt)
except Exception as error:
message = str(error)
if _is_transient_http(None, message) and int(deadline - time.time()) > 0:
remaining = int(deadline - time.time())
sleep_for = min(retry_interval_seconds, remaining)
print(
f"⚠️ Play upload transient network error (track={track}, attempt={attempt}): {message}. "
f"Retrying in {sleep_for}s (remaining window: {remaining}s)...",
file=sys.stderr,
)
time.sleep(sleep_for)
continue
raise PublishError(message=message, http_status=None, response_text="", attempt=attempt)
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Publish AAB to Play with fallback.")
parser.add_argument("--service-account-json", required=True)
parser.add_argument("--package", required=True)
parser.add_argument("--aab-path", required=True)
parser.add_argument("--requested-track", default="production")
parser.add_argument("--fallback-track", default="")
parser.add_argument("--release-status", default="completed")
parser.add_argument("--retry-window-seconds", type=int, default=10800)
parser.add_argument("--retry-interval-seconds", type=int, default=300)
parser.add_argument(
"--metadata-dir",
default="native-android/fastlane/metadata/android",
)
parser.add_argument(
"--ios-support-url-path",
default="native-ios/fastlane/metadata/en-US/support_url.txt",
)
parser.add_argument(
"--changelog-dir",
default="native-android/fastlane/metadata/android/en-US/changelogs",
)
parser.add_argument(
"--result-json",
default=os.path.join(tempfile.gettempdir(), "play-upload-result.json"),
)
parser.add_argument(
"--error-json",
default=os.path.join(tempfile.gettempdir(), "play-upload-error.json"),
)
parser.add_argument("--user-fraction", default=os.getenv("PLAY_USER_FRACTION", "0.1"))
return parser.parse_args()
def main() -> int:
args = _parse_args()
requested_track = (args.requested_track or "production").strip()
fallback_track = (args.fallback_track or "").strip()
tracks = [requested_track]
if requested_track == "production" and fallback_track and fallback_track != requested_track:
tracks.append(fallback_track)
package = args.package.strip()
aab_path = Path(args.aab_path)
if not aab_path.is_file():
print(f"❌ AAB not found: {aab_path}", file=sys.stderr)
return 2
service_account_json = Path(args.service_account_json)
if not service_account_json.is_file():
print(f"❌ Service account JSON not found: {service_account_json}", file=sys.stderr)
return 2
metadata_dir = Path(args.metadata_dir)
changelog_dir = Path(args.changelog_dir)
ios_support_url_path = Path(args.ios_support_url_path)
result_json_path = Path(args.result_json)
error_json_path = Path(args.error_json)
release_status = (args.release_status or "completed").strip() or "completed"
precondition_error_payload: dict[str, Any] | None = None
for idx, track in enumerate(tracks):
try:
outcome = _publish_to_track(
package=package,
aab_path=aab_path,
track=track,
release_status=release_status,
retry_window_seconds=args.retry_window_seconds,
retry_interval_seconds=args.retry_interval_seconds,
metadata_dir=metadata_dir,
ios_support_url_path=ios_support_url_path,
changelog_dir=changelog_dir,
credentials_path=service_account_json,
user_fraction_raw=args.user_fraction,
)
fallback_used = track != requested_track
result_payload = {
"requested_track": requested_track,
"effective_track": track,
"fallback_used": fallback_used,
"precondition_blocked": bool(precondition_error_payload),
"release_status": release_status,
"version_code": outcome["version_code"],
"attempt": outcome["attempt"],
"changes_not_sent_for_review": bool(outcome.get("changes_not_sent_for_review")),
"fallback_reason": "FAILED_PRECONDITION" if fallback_used else "",
}
if precondition_error_payload:
result_payload["production_precondition_error"] = precondition_error_payload
if outcome.get("changes_not_sent_for_review"):
_write_json(result_json_path, result_payload)
print(
"❌ Google Play committed the edit with changesNotSentForReview=true. "
"This release is not publicly live until Play Console 'Send for review' is completed.",
file=sys.stderr,
)
return 1
_write_json(result_json_path, result_payload)
print(
f"✅ Uploaded version code {outcome['version_code']} to '{track}' track "
f"(requested={requested_track}, status={release_status}, fallback_used={fallback_used})"
)
return 0
except PublishError as error:
payload = {
"package": package,
"requested_track": requested_track,
"track": track,
"release_status": release_status,
"attempt": error.attempt,
"http_status": error.http_status,
"error": error.message,
"response": error.response_text,
}
_write_json(error_json_path, payload)
is_production_precondition = (
idx == 0
and track == "production"
and _is_failed_precondition(error.message, error.response_text, error.http_status)
)
if is_production_precondition and len(tracks) > 1:
precondition_error_payload = payload
print(
"⚠️ Production publish blocked by FAILED_PRECONDITION. "
f"Falling back to '{tracks[1]}' for continuity.",
file=sys.stderr,
)
continue
if error.response_text:
print(
f"❌ Google Play upload failed on track '{track}': {error.message}\n\n"
f"Response:\n{error.response_text}",
file=sys.stderr,
)
else:
print(f"❌ Google Play upload failed on track '{track}': {error.message}", file=sys.stderr)
return 1
print("❌ No publish tracks attempted.", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())