|
1 | 1 | import os |
2 | 2 | import requests |
3 | | -from datetime import datetime |
| 3 | +from datetime import datetime, UTC |
4 | 4 |
|
5 | 5 | GITHUB_TOKEN = os.environ['GITHUB_TOKEN'] |
6 | 6 | REPO = os.environ['GITHUB_REPOSITORY'] |
@@ -39,26 +39,40 @@ def get_comments(discussion_number): |
39 | 39 | return resp.json() |
40 | 40 |
|
41 | 41 |
|
| 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 | + |
42 | 56 | def add_label(discussion_number, label): |
43 | 57 | 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]}) |
46 | 60 |
|
47 | 61 |
|
48 | 62 | def post_comment(discussion_number, body): |
49 | 63 | print(f"Posting comment to discussion #{discussion_number}") |
50 | 64 | url = f"{API_URL}/{discussion_number}/comments" |
51 | | - requests.post(url, headers=HEADERS, json={"body": body}) |
| 65 | + safe_post(url, {"body": body}) |
52 | 66 |
|
53 | 67 |
|
54 | 68 | def close_and_lock(discussion_number): |
55 | 69 | print(f"Closing and locking discussion #{discussion_number}") |
56 | 70 | 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}) |
58 | 72 |
|
59 | 73 |
|
60 | 74 | def main(): |
61 | | - now = datetime.utcnow() |
| 75 | + now = datetime.now(UTC) |
62 | 76 | discussions = get_discussions() |
63 | 77 | for d in discussions: |
64 | 78 | number = d['number'] |
|
0 commit comments