Skip to content

Commit 85b299e

Browse files
committed
better error handling
1 parent 32980e8 commit 85b299e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

.github/comment-close-old-discussions.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import requests
3-
from datetime import datetime
3+
from datetime import datetime, UTC
44

55
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
66
REPO = os.environ['GITHUB_REPOSITORY']
@@ -39,26 +39,40 @@ def get_comments(discussion_number):
3939
return resp.json()
4040

4141

42+
def safe_post(url, json_data):
43+
resp = requests.post(url, headers=HEADERS, json=json_data)
44+
if resp.status_code >= 400:
45+
print(f"POST {url} failed: {resp.status_code} {resp.text}")
46+
return resp
47+
48+
49+
def safe_patch(url, json_data):
50+
resp = requests.patch(url, headers=HEADERS, json=json_data)
51+
if resp.status_code >= 400:
52+
print(f"PATCH {url} failed: {resp.status_code} {resp.text}")
53+
return resp
54+
55+
4256
def add_label(discussion_number, label):
4357
print(f"Adding label '{label}' to discussion #{discussion_number}")
44-
url = f"{API_URL}/{discussion_number}/labels"
45-
requests.post(url, headers=HEADERS, json={"labels": [label]})
58+
url = f"https://api.github.com/repos/{REPO}/issues/{discussion_number}/labels"
59+
safe_post(url, {"labels": [label]})
4660

4761

4862
def post_comment(discussion_number, body):
4963
print(f"Posting comment to discussion #{discussion_number}")
5064
url = f"{API_URL}/{discussion_number}/comments"
51-
requests.post(url, headers=HEADERS, json={"body": body})
65+
safe_post(url, {"body": body})
5266

5367

5468
def close_and_lock(discussion_number):
5569
print(f"Closing and locking discussion #{discussion_number}")
5670
url = f"{API_URL}/{discussion_number}"
57-
requests.patch(url, headers=HEADERS, json={"state": "closed", "locked": True})
71+
safe_patch(url, {"state": "closed", "locked": True})
5872

5973

6074
def main():
61-
now = datetime.utcnow()
75+
now = datetime.now(UTC)
6276
discussions = get_discussions()
6377
for d in discussions:
6478
number = d['number']

0 commit comments

Comments
 (0)