From b85da17f384b4b30338739dff637797a3c5a5cb6 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Sun, 11 Feb 2018 17:50:24 +0000 Subject: First attempt at story hook --- pysite/views/api/asana.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'pysite') diff --git a/pysite/views/api/asana.py b/pysite/views/api/asana.py index ef213f38..d83abe6c 100644 --- a/pysite/views/api/asana.py +++ b/pysite/views/api/asana.py @@ -12,6 +12,11 @@ from pysite.constants import ErrorCodes ASANA_KEY = os.environ.get("ASANA_KEY") ASANA_WEBHOOK = os.environ.get("ASANA_WEBHOOK") +BASE_URL = "https://app.asana.com/api/1.0" +STORY_URL = f"{BASE_URL}/stories" +TASK_URL = f"{BASE_URL}/tasks" +USER_URL = f"{BASE_URL}/users" + COLOUR_RED = 0xFF0000 COLOUR_GREEN = 0x00FF00 COLOUR_BLUE = 0x0000FF @@ -34,7 +39,7 @@ class IndexView(APIView): events = request.get_json()["events"] for event in events: - func_name = f"asana_{event['type']}_{event['action']}" + func_name = f"asana_{event['type']}" if hasattr(self, func_name): func = getattr(self, func_name) @@ -76,6 +81,41 @@ class IndexView(APIView): } session.post(ASANA_WEBHOOK, json={"embeds": [embed]}) + session.close() + + def asana_story(self, *, resource, parent, created_at, user, action, type): + session = requests.session() + story = session.get(f"{STORY_URL}/{resource}").json() + + if story["type"] == "comment" and action == "added": # New comment! + task = session.get(f"{TASK_URL}/{parent}").json() + user = session.get(f"{USER_URL}/{user}").json() + project = task["projects"][0] # Just use the first project in the list + + if user["photo"]: + photo = user["photo"]["image_128x128"] + else: + photo = None + + self.send_webhook( + title=f"Comment: {project['name']}", + description=story["text"], + color=COLOUR_GREEN, + url=f"https://app.asana.com/0/{project['id']}/{parent}", + author_name=story["created_by"]["name"], + author_icon=photo + ) + else: + pretty_story = json.dumps( + story, + indent=4, + sort_keys=True + ) + + self.send_webhook( + title=f"Unknown story action/type: {action}/{story['type']}", + description=f"```json\n{pretty_story}\n```" + ) def asana_unknown(self, *, resource, parent, created_at, user, action, type): pretty_event = json.dumps( -- cgit v1.2.3