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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358 | class IntegrateSlackAPI(pyblish.api.InstancePlugin):
""" Send message notification to a channel.
Triggers on instances with "slack" family, filled by
'collect_slack_family'.
Expects configured profile in
Project settings > Slack > Publish plugins > Notification to Slack.
If instance contains 'thumbnail' it uploads it. Bot must be present
in the target channel.
If instance contains 'review' it could upload (if configured) or place
link with {review_filepath} placeholder.
Message template can contain {} placeholders from anatomyData.
"""
order = pyblish.api.IntegratorOrder + 0.499
label = "Integrate Slack Api"
families = ["slack"]
settings_category = "slack"
optional = True
def process(self, instance):
if instance.data.get("farm"):
self.log.debug(
"Instance is marked to be processed on farm. Skipping")
return
thumbnail_path = self._get_thumbnail_path(instance)
review_path = self._get_review_path(instance)
publish_files = set()
token = instance.data["slack_token"]
additional_message = instance.data.get("slack_additional_message")
for message_profile in instance.data["slack_channel_message_profiles"]:
message = message_profile["message"]
if additional_message:
message = f"{additional_message} \n {message}"
message = self._get_filled_content(
message, instance, review_path)
if not message:
return
if message_profile["upload_thumbnail"] and thumbnail_path:
publish_files.add(thumbnail_path)
if message_profile["upload_review"] and review_path:
message, publish_files = self._handle_review_upload(
message, message_profile, publish_files, review_path)
for channel in message_profile["channels"]:
channel = self._get_filled_content(
channel, instance, review_path)
client = SlackOperations(token, self.log)
if "@" in message:
cache_key = "__cache_slack_ids"
slack_ids = instance.context.data.get(cache_key, None)
if slack_ids is None:
users, groups = client.get_users_and_groups()
instance.context.data[cache_key] = {}
instance.context.data[cache_key]["users"] = users
instance.context.data[cache_key]["groups"] = groups
else:
users = slack_ids["users"]
groups = slack_ids["groups"]
message = self._translate_users(message, users, groups)
client.send_message(channel, message, publish_files)
def _handle_review_upload(self, message, message_profile, publish_files,
review_path):
"""Check if uploaded file is not too large"""
review_file_size_MB = os.path.getsize(review_path) / 1024 / 1024
file_limit = message_profile.get("review_upload_limit", 50)
if review_file_size_MB > file_limit:
message += "\nReview upload omitted because of file size."
if review_path not in message:
message += "\nFile located at: {}".format(review_path)
else:
publish_files.add(review_path)
return message, publish_files
def _get_filled_content(self, message, instance, review_path=None):
"""Use message and data from instance to get dynamic message content.
Reviews might be large, so allow only adding link to message instead of
uploading only.
"""
fill_data = copy.deepcopy(instance.data["anatomyData"])
# Make sure version is string
# TODO remove when fixed in ayon-core 'prepare_template_data' function
fill_data["version"] = str(fill_data["version"])
if review_path:
fill_data["review_filepath"] = review_path
message = (
message
.replace("{task}", "{task[name]}")
.replace("{Task}", "{Task[name]}")
.replace("{TASK}", "{TASK[NAME]}")
.replace("{asset}", "{folder[name]}")
.replace("{Asset}", "{Folder[name]}")
.replace("{ASSET}", "{FOLDER[NAME]}")
.replace("{subset}", "{product[name]}")
.replace("{Subset}", "{Product[name]}")
.replace("{SUBSET}", "{PRODUCT[NAME]}")
.replace("{family}", "{product[type]}")
.replace("{Family}", "{Product[type]}")
.replace("{FAMILY}", "{PRODUCT[TYPE]}")
)
multiple_case_variants = prepare_template_data(fill_data)
fill_data.update(multiple_case_variants)
try:
message = self._escape_missing_keys(
message, fill_data
).format(**fill_data)
except Exception:
# shouldn't happen
self.log.warning(
"Some keys are missing in {}".format(message),
exc_info=True)
return message
def _get_thumbnail_path(self, instance):
"""Returns abs url for thumbnail if present in instance repres"""
thumbnail_path = None
for repre in instance.data.get("representations", []):
if repre.get("thumbnail") or "thumbnail" in repre.get("tags", []):
repre_thumbnail_path = get_publish_repre_path(
instance, repre, False
)
if os.path.exists(repre_thumbnail_path):
thumbnail_path = repre_thumbnail_path
break
return thumbnail_path
def _get_review_path(self, instance):
"""Returns abs url for review if present in instance repres"""
review_path = None
for repre in instance.data.get("representations", []):
tags = repre.get("tags", [])
if (
repre.get("review")
or "review" in tags
or "burnin" in tags
):
repre_review_path = get_publish_repre_path(
instance, repre, False
)
if repre_review_path and os.path.exists(repre_review_path):
review_path = repre_review_path
if "burnin" in tags: # burnin has precedence if exists
break
return review_path
def _get_user_id(self, users, user_name):
"""Returns internal slack id for user name"""
user_name_lower = user_name.lower()
for user in users:
if user.get("deleted"):
continue
user_profile = user["profile"]
if user_name_lower in (
user["name"].lower(),
user_profile.get("display_name", "").lower(),
user_profile.get("real_name", "").lower(),
):
return user["id"]
return None
def _get_group_id(self, groups, group_name):
"""Returns internal group id for string name"""
for group in groups:
if (
not group.get("date_delete")
and group_name.lower() in (
group["name"].lower(),
group["handle"]
)
):
return group["id"]
return None
def _translate_users(self, message, users, groups):
"""Replace all occurences of @mentions with proper <@name> format."""
matches = re.findall(r"(?<!<)@\S+", message)
in_quotes = re.findall(r"(?<!<)(['\"])(@[^'\"]+)", message)
for item in in_quotes:
matches.append(item[1])
if not matches:
return message
for orig_user in matches:
user_name = orig_user.replace("@", "")
slack_id = self._get_user_id(users, user_name)
mention = None
if slack_id:
mention = "<@{}>".format(slack_id)
else:
slack_id = self._get_group_id(groups, user_name)
if slack_id:
mention = "<!subteam^{}>".format(slack_id)
if mention:
message = message.replace(orig_user, mention)
return message
def _escape_missing_keys(self, message, fill_data):
"""Double escapes placeholder which are missing in 'fill_data'"""
placeholder_keys = re.findall(r"\{([^}]+)\}", message)
fill_keys = []
for key, value in fill_data.items():
fill_keys.append(key)
if isinstance(value, dict):
for child_key in value.keys():
fill_keys.append("{}[{}]".format(key, child_key))
not_matched = set(placeholder_keys) - set(fill_keys)
for not_matched_item in not_matched:
message = message.replace(
f"{not_matched_item}",
f"{{{not_matched_item}}}"
)
return message
|