Mathias Rönnlund
Mathias Rönnlund

Reputation: 4855

Google play api's not returning all version visible in App bundle explorer

In App bundle explorer I can see releases that are not returned by the below code. E.g. a few versions that have been uploaded to the internal track but later superseeded by newer versions.

The problem is that when we trigger the release pipeline for external testing track or production track, it is checking if there is already a release with the same versioncode. Since some releases are not returned, the pipeline tries to upload the new bundle, which fails.

For clarity, the below code is how I've been testing the api's, not what the release pipeline uses.

import json
import sys
from google.oauth2 import service_account
from googleapiclient.discovery import build

def main():
    service_account_file = "abc.json"
    package_name = "com.mycompany.mypackage"
    
    # Define the scopes
    SCOPES = ['https://www.googleapis.com/auth/androidpublisher']

    # Authenticate using the service account
    credentials = service_account.Credentials.from_service_account_file(
        service_account_file, scopes=SCOPES)

    # Build the Google Play Developer API service
    service = build('androidpublisher', 'v3', credentials=credentials)

    # Insert a new edit
    print("Inserting a new edit...")
    edit_response = service.edits().insert(body={}, packageName=package_name).execute()
    edit_id = edit_response['id']
    print(f"Edit inserted. Response: {json.dumps(edit_response, indent=2)}")

    # Retrieve uploaded bundles (as seen in App bundle explorer)
    print("\nRetrieving uploaded bundles...")
    bundles_response = service.edits().bundles().list(
        editId=edit_id, packageName=package_name).execute()
    bundles = bundles_response.get('bundles', [])
    bundle_version_codes = []
    for bundle in bundles:
        vc = bundle.get('versionCode')
        bundle_version_codes.append(vc)
        print(f"Bundle - Version code: {vc}, File size: {bundle.get('fileSize')}")

    # Retrieve track releases
    print("\nRetrieving track releases...")
    tracks_response = service.edits().tracks().list(
        editId=edit_id, packageName=package_name).execute()
    # Debug: print full tracks response to verify all releases are included
    print("Full tracks response:")
    print(json.dumps(tracks_response, indent=2))
    
    track_version_codes = []
    for track in tracks_response.get('tracks', []):
        track_name = track.get('track')
        print(f"\nTrack: {track_name}")
        for release in track.get('releases', []):
            vcs = release.get('versionCodes', [])
            # Added debug: print each version code with its type
            for vc in vcs:
                print(f"  Debug: release '{release.get('name')}' version code {vc} (type: {type(vc)})")
            track_version_codes.extend(vcs)
            print(f"  Release '{release.get('name')}' with versionCodes: {vcs} (status: {release.get('status')})")

    # Aggregate and list all unique version codes
    all_version_codes = set(bundle_version_codes)
    all_version_codes.update(track_version_codes)
    # Convert all version codes to integers for proper numeric sorting
    try:
        all_version_codes_int = [int(vc) for vc in all_version_codes]
    except Exception as e:
        print("Error converting version codes to int:", e)
        all_version_codes_int = list(all_version_codes)
    print("\nAll version codes aggregated:")
    print(sorted(all_version_codes_int))

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 19

Answers (0)

Related Questions