-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
230 lines (151 loc) · 5.91 KB
/
bot.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
''' bot.py
Grabs the latest count and list of identifiers add to the Arctic Data
Center and pastes them into the #arctic Slack channel. Also creates tickets
in RT for any registry-created objects that don't have tickets.
'''
import os.path
import json
import datetime
from xml.etree import ElementTree
import requests
from dotenv import load_dotenv
import rt
# Dynamic variables
load_dotenv(os.path.join(os.path.dirname(__file__), '.env'))
LASTFILE_PATH = os.environ.get("LASTFILE_PATH")
BASE_URL = os.environ.get("BASE_URL")
SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL")
USERS = os.environ.get("USERS")
RT_URL = os.environ.get("RT_URL")
RT_USER = os.environ.get("RT_USER")
RT_PASS = os.environ.get("RT_PASS")
RT_TICKET_OWNER = os.environ.get("RT_TICKET_OWNER")
# Hard-coded variables
PID_STARTSWITH = "arctic-data."
EML_FMT_ID = "eml://ecoinformatics.org/eml-2.1.1"
# General functions
def now():
return datetime.datetime.utcnow().isoformat()
def get_last_run():
last_run = None
path = os.path.join(os.path.dirname(__file__), LASTFILE_PATH)
if os.path.isfile(path):
with open(path, "r") as f:
last_run = f.read().splitlines()[0]
else:
last_run = now()
return last_run
def save_last_run(to_date):
with open(os.path.join(os.path.dirname(__file__), LASTFILE_PATH), "w") as f:
f.write(to_date)
# Slack functions
def send_message(message):
return requests.post(SLACK_WEBHOOK_URL, data=json.dumps({'text': message}))
def create_list_objects_message(count, url):
url_esc = url.replace('&', '&') # Slack says escape ambersands
message = None
# Deal with plural forms of strings
if count == 1:
objects = "object"
was = "was"
else:
objects = "objects"
was = "were"
template = ("Hey {}, {} {} {} just modified. "
"Just thought I'd let you know. "
"You can see more detail at {}.")
message = template.format(USERS, count, objects, was, url_esc)
return message
def create_tickets_message(tickets):
message = "The following tickets were just created:\n"
for ticket in tickets:
ticket_url = "{}/Ticket/Display.html?id={}".format(RT_URL, ticket)
line = "- {}\n".format(ticket_url)
message += line
return message
def create_list_objects_url(from_date, to_date):
return ("{}/object?fromDate={}&toDate={}").format(BASE_URL,
from_date,
to_date)
# Member Node functions
def list_objects(url):
response = requests.get(url)
try:
xmldoc = ElementTree.fromstring(response.content)
except ElementTree.ParseError as err:
print("Error while parsing list_objects() response.")
print("Error: {}".format(err))
print("Response content:")
print(response.content)
raise
return xmldoc
def get_count(doc):
attrs = doc.findall('.')[0].items()
count = [attr[1] for attr in attrs if attr[0] == 'count'][0]
return int(count)
def get_object_identifiers(doc):
return [o.find('identifier').text for o in doc.findall("objectInfo")]
def get_metadata(doc):
metadata = []
# Filter to EML 2.1.1 objects
for o in doc.findall("objectInfo"):
format_id = o.find('formatId').text
pid = o.find('identifier').text
if format_id == EML_FMT_ID and pid.startswith(PID_STARTSWITH):
metadata.append(o.find('identifier').text)
return metadata
# RT functions
def ticket_find(tracker, pid):
# Strip version stringn from PID
# i.e. arctic-data.X.Y => arctic-data.X
# so a new ticket isn't created for updates
pid_noversion = '.'.join(pid.split('.')[0:2])
title = '{}'.format(pid_noversion)
results = tracker.search(Queue='arcticdata', Subject__like=title)
ids = [t['id'].replace('ticket/', '') for t in results]
if len(ids) > 0:
return ids[0]
else:
return None
def ticket_create(tracker, pid):
ticket = tracker.create_ticket(Queue='arcticdata',
Subject="New submission: {}".format(pid),
Owner=RT_TICKET_OWNER,
Text=create_ticket_text(pid))
return ticket
def create_ticket_text(pid):
template = """A new submission just came in. View it here: https://arcticdata.io/catalog/#view/{}. This ticket was automatically created by the listobjects bot because the PID {} was created/modified. See https://github.nceas.ucsb.edu/KNB/arctic-data/blob/master/docs/handling-submissions.md for more information on what to do.")"""
return template.format(pid, pid)
def ticket_reply(tracker, ticket_id, identifier):
tracker.reply(ticket_id,
text="PID {} was updated and needs moderation.".format(identifier))
def create_or_update_tickets(identifiers):
tickets = []
if len(identifiers) <= 0:
return tickets
tracker = rt.Rt("{}/REST/1.0/".format(RT_URL), RT_USER, RT_PASS)
if tracker.login() is False:
send_message("Hey @bryce, I failed to log into RT. Something's wrong!")
raise Exception("Failed to log in to RT.")
for identifier in identifiers:
ticket = ticket_find(tracker, identifier)
if ticket is None:
tickets.append(ticket_create(tracker, identifier))
else:
ticket_reply(tracker, ticket, identifier)
return tickets
# main()
def main():
from_date = get_last_run()
to_date = now()
url = create_list_objects_url(from_date, to_date)
doc = list_objects(url)
count = get_count(doc)
if count > 0:
send_message(create_list_objects_message(count, url))
tickets = create_or_update_tickets(get_metadata(doc))
if len(tickets) > 0:
send_message(create_tickets_message(tickets))
save_last_run(to_date)
if __name__ == "__main__":
main()