Skip to content

Commit 88089cc

Browse files
authored
fix: use aiohttp and create issue
1 parent 85f8f52 commit 88089cc

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

action.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,22 @@ runs:
8484
8585
if [ ! -f "mirrorchyan_release_note.py" ];then
8686
wget -O mirrorchyan_release_note.py https://raw.githubusercontent.com/MirrorChyan/release-note-action/refs/heads/v1/python/upload.py
87-
python -m pip install requests
87+
python -m pip install aiohttp asyncio
8888
fi
8989
9090
python mirrorchyan_release_note.py '${{ inputs.mirrorchyan_rid }}' '${{ inputs.upload_token }}' body.json
9191
92+
- name: Create issue if failed
93+
if: failure()
94+
uses: actions-cool/issues-helper@v3
95+
with:
96+
actions: "create-issue"
97+
title: "Failed to upload release note to MirrorChyan for ${{ github.repository }}"
98+
body: |
99+
${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
100+
101+
@MistEO @Aliothmoon @dongwlin
102+
92103
- uses: actions/upload-artifact@v4
93104
if: always()
94105
with:

python/upload.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
import sys
2-
import time
3-
import urllib3
4-
import requests
2+
import asyncio
3+
import aiohttp
4+
import json
55
from datetime import datetime
66

7-
urllib3.disable_warnings()
8-
97

108
def log(msg: object) -> None:
119
print(f"{datetime.now()} | {msg}")
1210

1311

14-
def upload(rid: str, data: dict, headers: dict) -> bool:
12+
async def upload(session: aiohttp.ClientSession, rid: str, data: dict, headers: dict) -> bool:
1513
log("start upload")
16-
17-
# step 1
18-
response_1 = requests.put(
19-
f"https://mirrorchyan.com/api/resources/{rid}/versions/release-note",
20-
headers=headers,
21-
data=data,
22-
verify=False,
23-
)
24-
log(f"step 1: {response_1.status_code}")
25-
26-
if response_1.status_code != 200:
27-
log(f"step 1 failed: {response_1.status_code}, {response_1.text}")
14+
url = f"https://mirrorchyan.com/api/resources/{rid}/versions/release-note"
15+
16+
try:
17+
# 发送 PUT 请求
18+
async with session.put(
19+
url,
20+
headers=headers,
21+
json=data,
22+
ssl=False # 等同于 requests 的 verify=False
23+
) as response:
24+
log(f"step 1: {response.status}")
25+
26+
if response.status != 200:
27+
text = await response.text()
28+
log(f"step 1 failed: {response.status}, {text}")
29+
return False
30+
31+
log("uploaded")
32+
return True
33+
34+
except Exception as e:
35+
log(f"Request failed: {str(e)}")
2836
return False
2937

30-
log("uploaded")
31-
return True
32-
3338

34-
def main():
39+
async def main():
3540
_, rid, token, body_file = sys.argv
3641

3742
headers = {
@@ -42,20 +47,21 @@ def main():
4247
}
4348

4449
with open(body_file, "r") as file:
45-
data = file.read()
50+
data = json.load(file)
4651

4752
log(data)
4853

4954
done = False
5055
retries = 3
51-
for i in range(retries):
52-
if upload(rid, data, headers):
53-
done = True
54-
break
55-
elif i + 1 < retries:
56-
delay = (i + 1) * 15
57-
log(f"retry {i + 1} after {delay}s")
58-
time.sleep(delay)
56+
async with aiohttp.ClientSession() as session:
57+
for i in range(retries):
58+
if await upload(session, rid, data, headers):
59+
done = True
60+
break
61+
elif i + 1 < retries:
62+
delay = (i + 1) * 15
63+
log(f"retry {i + 1} after {delay}s")
64+
await asyncio.sleep(delay)
5965

6066
if not done:
6167
log("failed")
@@ -66,4 +72,4 @@ def main():
6672

6773

6874
if __name__ == "__main__":
69-
main()
75+
asyncio.run(main())

0 commit comments

Comments
 (0)