|
| 1 | +import json |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from urllib.request import urlopen, Request |
| 6 | + |
| 7 | + |
| 8 | +DIRECTORY_ID = os.environ["MSSTORE_TENANT_ID"] |
| 9 | +CLIENT_ID = os.environ["MSSTORE_CLIENT_ID"] |
| 10 | +CLIENT_SECRET = os.environ["MSSTORE_CLIENT_SECRET"] |
| 11 | +SELLER_ID = os.environ["MSSTORE_SELLER_ID"] |
| 12 | +APP_ID = os.environ["MSSTORE_APP_ID"] |
| 13 | + |
| 14 | +PATCH_JSON = os.environ["PATCH_JSON"] |
| 15 | +with open(PATCH_JSON, "rb") as f: |
| 16 | + patch_data = json.load(f) |
| 17 | + |
| 18 | + |
| 19 | +SERVICE_URL = "https://manage.devcenter.microsoft.com/v1.0/my/" |
| 20 | + |
| 21 | +################################################################################ |
| 22 | +# Get auth token/header |
| 23 | +################################################################################ |
| 24 | + |
| 25 | +OAUTH_URL = f"https://login.microsoftonline.com/{DIRECTORY_ID}/oauth2/v2.0/token" |
| 26 | +SERVICE_SCOPE = "https://manage.devcenter.microsoft.com/.default" |
| 27 | + |
| 28 | +reqAuth = Request( |
| 29 | + OAUTH_URL, |
| 30 | + method="POST", |
| 31 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 32 | + data=(f"grant_type=client_credentials&client_id={CLIENT_ID}&" + |
| 33 | + f"client_secret={CLIENT_SECRET}&scope={SERVICE_SCOPE}").encode("utf-8"), |
| 34 | +) |
| 35 | + |
| 36 | +with urlopen(reqAuth) as r: |
| 37 | + jwt = json.loads(r.read()) |
| 38 | + |
| 39 | +auth = {"Authorization": f"Bearer {jwt['access_token']}"} |
| 40 | + |
| 41 | +################################################################################ |
| 42 | +# Get application data (for current submission) |
| 43 | +################################################################################ |
| 44 | + |
| 45 | +reqApps = Request(f"{SERVICE_URL}applications/{APP_ID}", method="GET", headers=auth) |
| 46 | +print("Getting application data from", reqApps.full_url) |
| 47 | +with urlopen(reqApps) as r: |
| 48 | + app_data = json.loads(r.read()) |
| 49 | + |
| 50 | +submission_url = app_data["pendingApplicationSubmission"]["resourceLocation"] |
| 51 | + |
| 52 | +################################################################################ |
| 53 | +# Get current submission data |
| 54 | +################################################################################ |
| 55 | + |
| 56 | +reqSubmission = Request(f"{SERVICE_URL}{submission_url}", method="GET", headers=auth) |
| 57 | +print("Getting submission data from", reqSubmission.full_url) |
| 58 | +with urlopen(reqSubmission) as r: |
| 59 | + sub_data = json.loads(r.read()) |
| 60 | + |
| 61 | +################################################################################ |
| 62 | +# Patch submission data |
| 63 | +################################################################################ |
| 64 | + |
| 65 | +if patch_data: |
| 66 | + def _patch(target, key, src): |
| 67 | + if key.startswith("#"): |
| 68 | + return |
| 69 | + if isinstance(src, dict): |
| 70 | + for k, v in src.items(): |
| 71 | + _patch(target.setdefault(key, {}), k, v) |
| 72 | + else: |
| 73 | + target[k] = v |
| 74 | + |
| 75 | + for k, v in patch_data.items(): |
| 76 | + _patch(sub_data, k, v) |
| 77 | + |
| 78 | +################################################################################ |
| 79 | +# Update submission data |
| 80 | +################################################################################ |
| 81 | + |
| 82 | +reqUpdate = Request(f"{SERVICE_URL}{submission_url}", method="PUT", |
| 83 | + headers={**auth, "Content-Type": "application/json; charset=utf-8"}, |
| 84 | + data=json.dumps(sub_data).encode("utf-8")) |
| 85 | +print("Updating submission data at", reqUpdate.full_url) |
| 86 | +with urlopen(reqUpdate) as r: |
| 87 | + new_data = r.read() |
| 88 | + |
| 89 | +new_data.pop("fileUploadUrl", None) |
| 90 | +print("Current submission metadata:") |
| 91 | +print(json.dumps(new_data, indent=2)) |
0 commit comments