From b184a964e539cf40b00bb4b6f24a254adf598d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=92=9F?= Date: Mon, 4 Dec 2023 08:35:00 +0800 Subject: [PATCH] Update sync_version_to_contentful.py --- build/sync_version_to_contentful.py | 58 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/build/sync_version_to_contentful.py b/build/sync_version_to_contentful.py index 3f6e9be1a..3d859df92 100644 --- a/build/sync_version_to_contentful.py +++ b/build/sync_version_to_contentful.py @@ -8,8 +8,6 @@ ACCESS_TOKEN = os.environ['CONTENTFUL_ACCESS_TOKEN'] SPACE_ID = "ffrhttfighww" -print(f"ACCESS_TOKEN: {ACCESS_TOKEN}") - # 设置请求头 headers = { "Authorization": f"Bearer {ACCESS_TOKEN}", @@ -18,22 +16,26 @@ def update_contentful(product_name, editions): # 获取 Contentful 中的 Product entry - response = requests.get( - f"{CONTENTFUL_MANAGEMENT_API}/spaces/{SPACE_ID}/environments/master/entries?content_type=product&fields.key={product_name}", - headers=headers - ) - response.raise_for_status() - entries = response.json() + try: + response = requests.get( + f"{CONTENTFUL_MANAGEMENT_API}/spaces/{SPACE_ID}/environments/master/entries?content_type=product&fields.key={product_name}", + headers=headers + ) + response.raise_for_status() + except requests.exceptions.HTTPError as e: + print(f"Failed to get entries: {e.response.text}") + return + entries = response.json() print(f"entries: {entries}") - + # 假设只有一个匹配的 entry if entries['total'] > 0: entry_id = entries['items'][0]['sys']['id'] version = entries['items'][0]['sys']['version'] - + # 准备更新的数据 - distribution = [{"dist": edition['dist'], "version": ",".join(edition['version'])} for edition in editions] + distribution = [{"id": edition['dist'], "key": "version", "value": edition['version']} for edition in editions] update_data = { "fields": { "distribution": { @@ -42,21 +44,29 @@ def update_contentful(product_name, editions): } } print(f"update_data: {update_data}") - + # 更新 Contentful entry - update_response = requests.put( - f"{CONTENTFUL_MANAGEMENT_API}/spaces/{SPACE_ID}/environments/master/entries/{entry_id}", - headers={**headers, "X-Contentful-Version": str(version)}, - data=json.dumps(update_data) - ) - update_response.raise_for_status() - + try: + update_response = requests.put( + f"{CONTENTFUL_MANAGEMENT_API}/spaces/{SPACE_ID}/environments/master/entries/{entry_id}", + headers={**headers, "X-Contentful-Version": str(version)}, + data=json.dumps(update_data) + ) + update_response.raise_for_status() + except requests.exceptions.HTTPError as e: + print(f"Failed to update entry: {e.response.text}") + return + # 发布更新 - publish_response = requests.put( - f"{CONTENTFUL_MANAGEMENT_API}/spaces/{SPACE_ID}/environments/master/entries/{entry_id}/published", - headers={**headers, "X-Contentful-Version": str(update_response.json()['sys']['version'])} - ) - publish_response.raise_for_status() + try: + publish_response = requests.put( + f"{CONTENTFUL_MANAGEMENT_API}/spaces/{SPACE_ID}/environments/master/entries/{entry_id}/published", + headers={**headers, "X-Contentful-Version": str(update_response.json()['sys']['version'])} + ) + publish_response.raise_for_status() + except requests.exceptions.HTTPError as e: + print(f"Failed to publish entry: {e.response.text}") + return # 遍历 apps 文件夹中的 variables.json 文件 apps_path = Path('apps')