2024-02-18 10:46:52 +00:00
|
|
|
import pathlib
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import urllib.request
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2024-02-18 12:22:23 +00:00
|
|
|
FORGEJO_URL = os.environ.get("FORGEJO_URL") or "https://git.private.coffee"
|
2024-02-18 10:46:52 +00:00
|
|
|
|
|
|
|
try:
|
2024-02-18 12:13:51 +00:00
|
|
|
TOKEN = os.environ["FORGEJO_TOKEN"]
|
2024-02-18 12:21:39 +00:00
|
|
|
assert TOKEN
|
|
|
|
except (KeyError, AssertionError):
|
2024-02-18 12:13:51 +00:00
|
|
|
logging.error("FORGEJO_TOKEN environment variable is required")
|
2024-02-18 10:46:52 +00:00
|
|
|
exit(1)
|
|
|
|
|
2024-02-18 12:21:39 +00:00
|
|
|
ASSIGNEES = os.environ.get("CHORES_ASSIGNEES").split()
|
|
|
|
REPO_OWNER = os.environ.get("CHORES_REPO_OWNER") or "PrivateCoffee"
|
|
|
|
REPO_NAME = os.environ.get("CHORES_REPO_NAME") or "chores"
|
|
|
|
EXPIRY_DAYS = int(os.environ.get("CHORES_EXPIRY_DAYS") or 2)
|
2024-02-18 10:46:52 +00:00
|
|
|
|
2024-07-28 18:41:30 +00:00
|
|
|
CHORES = (pathlib.Path(__file__).parent.parent / "assets" / "chores.md").read_text()
|
2024-02-18 12:21:39 +00:00
|
|
|
|
2024-07-28 18:41:30 +00:00
|
|
|
due_date = (datetime.utcnow() + timedelta(days=EXPIRY_DAYS)).isoformat().split(".")[
|
|
|
|
0
|
|
|
|
] + "Z"
|
2024-02-18 10:46:52 +00:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
"title": "Weekly Chores",
|
|
|
|
"body": CHORES,
|
|
|
|
"due_date": due_date,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ASSIGNEES:
|
|
|
|
payload["assignees"] = ASSIGNEES
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Accept": "application/json",
|
2024-02-18 12:23:54 +00:00
|
|
|
"Authorization": "token " + TOKEN,
|
2024-02-18 10:46:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 12:25:53 +00:00
|
|
|
print(f"Creating issue with payload:\n{payload}")
|
|
|
|
|
2024-02-18 10:46:52 +00:00
|
|
|
request = urllib.request.Request(
|
|
|
|
f"{FORGEJO_URL}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}/issues",
|
|
|
|
data=json.dumps(payload).encode("utf-8"),
|
|
|
|
headers=headers,
|
|
|
|
method="POST",
|
|
|
|
)
|
|
|
|
|
|
|
|
with urllib.request.urlopen(request) as response:
|
|
|
|
print(response.read().decode("utf-8"))
|