Repository navigation
Expand file tree
/
Copy pathtask.py
More file actions
122 lines (92 loc) · 4.02 KB
/
Copy pathtask.py
File metadata and controls
122 lines (92 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
.. module: dispatch.plugins.google_drive.task
:platform: Unix
:copyright: (c) 2019 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
import re
import logging
from typing import Any, List, Dict
from dispatch.task.models import TaskStatus
from dispatch.plugins.dispatch_google.config import GOOGLE_DOMAIN
from .drive import get_file, list_comments
log = logging.getLogger(__name__)
def get_assignees(content: str) -> List[str]:
"""Gets assignees from comment."""
regex = r"[+@]([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
matches = re.findall(regex, content)
return list(matches)
def parse_comment(content: str) -> Dict:
"""Parses a comment into it's various parts."""
assignees = get_assignees(content)
return {"assignees": assignees}
def get_task_status(task: dict):
"""Gets the current status from a task."""
status = {"status": TaskStatus.open, "resolved_at": None, "resolved_by": None}
if task.get("resolved"):
for r in task["replies"]:
if r.get("action") == "resolve":
status["resolved_by"] = r["author"]["displayName"]
status["resolved_at"] = r["createdTime"]
status["status"] = TaskStatus.resolved
return status
def filter_comments(comments: List[Any]):
"""Filters comments for tasks."""
return [c for c in comments if parse_comment(c["content"])["assignees"]]
def find_urls(text: str) -> List[str]:
"""Finds a url in a text blob."""
# findall() has been used
# with valid conditions for urls in string
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex, text)
return [x[0] for x in url]
def get_tickets(replies: List[dict]):
"""Fetches urls/tickets from task replies."""
tickets = []
for r in replies:
if r.get("content"):
for url in find_urls(r["content"]):
tickets.append({"web_link": url})
return tickets
# NOTE We have to use `displayName` instead of `emailAddress` because it's
# not visible to us. We should ask rcerda about why that might be.
def list_tasks(client: Any, file_id: str):
"""Returns all tasks in file."""
doc = get_file(client, file_id)
document_meta = {"document": {"id": file_id, "name": doc["name"]}}
all_comments = list_comments(client, file_id)
task_comments = filter_comments(all_comments)
tasks = []
for t in task_comments:
status = get_task_status(t)
assignees = [{"individual": {"email": x}} for x in get_assignees(t["content"])]
description = t.get("quotedFileContent", {}).get("value", "")
tickets = get_tickets(t["replies"])
task_meta = {
"task": {
"resource_id": t["id"],
"description": description,
"created_at": t["createdTime"],
"assignees": assignees,
"tickets": tickets,
"weblink": f'https://docs.google.com/a/{GOOGLE_DOMAIN}/document/d/{file_id}/edit?disco={t["id"]}',
}
}
# this is a dirty hack because google doesn't return emailAddresses for comments
# complete with conflicting docs
# https://developers.google.com/drive/api/v2/reference/comments#resource
from dispatch.database import SessionLocal
from dispatch.individual.models import IndividualContact
db_session = SessionLocal()
owner = (
db_session.query(IndividualContact)
.filter(IndividualContact.name == t["author"]["displayName"])
.first()
)
if owner:
task_meta["task"].update({"owner": {"individual": {"email": owner.email}}})
db_session.close()
task_meta["task"].update(status)
tasks.append({**document_meta, **task_meta})
return tasks