Skip to content

Commit

Permalink
Update sync_version_to_contentful.py
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojing1987 authored Dec 4, 2023
1 parent 785daaf commit b184a96
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions build/sync_version_to_contentful.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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": {
Expand All @@ -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')
Expand Down

0 comments on commit b184a96

Please sign in to comment.