Created
June 4, 2024 08:54
-
-
Save emirhg/684c370dd5756e23529e316e8745905d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# API is here: https://taskwarrior.org/docs/hooks.html | |
# To be saved at ~/.task/hooks/on-modify.01-gnome-pomodoro to ensure it is | |
# run after the timewarrior hook, which should be saved as | |
# ~/.task/hooks/on-modify.00-timewarrior | |
# Otherwise, this is run before which then runs the Gnome-Pomodoro actions | |
# things get quite messy! | |
import json | |
import os | |
import sys | |
import requests | |
import datetime | |
from requests.auth import HTTPBasicAuth | |
from pytz import timezone | |
from dateutil.tz import tzutc | |
# Make no changes to the task, simply observe. | |
sys.stdin.reconfigure(encoding='iso-8859-1') | |
old = json.loads(sys.stdin.readline()) | |
new = json.loads(sys.stdin.readline()) | |
OPENPROJECT_AUTH_TOKEN = os.environ('OPENPROJECT_AUTH_TOKEN') | |
OPENPROJECT_API_BASE_URL = os.environ('OPENPROJECT_API_BASE_URL') | |
baseURL = OPENPROJECT_API_BASE_URL | |
def closeTask(taskId): | |
STATUS_CLOSED = 12 | |
baseURL = OPENPROJECT_API_BASE_URL | |
endPoint = f"{baseURL}{taskId}" | |
auth = HTTPBasicAuth('apikey', OPENPROJECT_AUTH_TOKEN) | |
json = { "_links": {"status": {"href": f"/api/v3/statuses/{STATUS_CLOSED}"}}} | |
postToAPI(endPoint, json, auth) | |
def postToAPI(endPoint, data, auth): | |
getRes = requests.get(endPoint, auth=auth ) | |
if "lockVersion" in getRes.json(): | |
lockVersion = getRes.json()["lockVersion"] | |
data['lockVersion'] = f"{lockVersion}" | |
return requests.patch(endPoint, json=data, auth=auth ) | |
def updateCalendar(taskId, data): | |
endPoint = f"{baseURL}{taskId}" | |
auth = HTTPBasicAuth('apikey', OPENPROJECT_AUTH_TOKEN) | |
return postToAPI(endPoint, data, auth) | |
def isDifferentDate(date1, date2): | |
return datetime.datetime.strptime(date1, '%Y%m%dT%H%M%SZ').strftime("%Y-%m-%d") != datetime.datetime.strptime(date2, '%Y%m%dT%H%M%SZ').strftime("%Y-%m-%d") | |
if "openprojectid" in new: | |
#localtz = timezone('America/Mexico_City') | |
data = {} | |
if new["status"] == "completed" and "start" in old: | |
closeTask(taskId = int(float(new["openprojectid"]))) | |
if 'scheduled' in new and ( 'scheduled' not in old or isDifferentDate(new['scheduled'], old['scheduled'])) : | |
scheduled = datetime.datetime.strptime(new["scheduled"], '%Y%m%dT%H%M%SZ') | |
data['startDate'] = scheduled.strftime("%Y-%m-%d") | |
elif 'scheduled' in old and 'scheduled' not in new: | |
data['startDate'] = None | |
if 'due' in new and ( 'due' not in old or isDifferentDate(new['due'], old['due'])) : | |
due = datetime.datetime.strptime(new["due"], '%Y%m%dT%H%M%SZ') | |
data['dueDate'] = due.strftime("%Y-%m-%d") | |
elif 'due' in old and 'due' not in new: | |
data['dueDate'] = None | |
if 'openprojectestimatedhours' in new and ( 'openprojectestimatedhours' not in old or new['openprojectestimatedhours'] != old['openprojectestimatedhours']) : | |
data['estimatedTime'] = new['openprojectestimatedhours'] | |
elif 'openprojectestimatedhours' in old and 'openprojectestimatedhours' not in new: | |
data['estimatedTime'] = "PT0H" | |
f = open("/tmp/openproject.log","w") | |
if data: | |
r= updateCalendar(int(new["openprojectid"]) , data) | |
f.write(r.content.decode('utf-8')) | |
#if ('start' not in new or 'end' in new) and 'start' in old: | |
# os.system('post-openproject-time-entry'); | |
print(json.dumps(new)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment