mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 16:34:53 +01:00
client/#75 - Added Slack module
Added collector Added integrator
This commit is contained in:
parent
9d055e9adb
commit
841124e87c
7 changed files with 214 additions and 1 deletions
|
|
@ -0,0 +1,55 @@
|
|||
from avalon import io
|
||||
import pyblish.api
|
||||
|
||||
from openpype.lib.profiles_filtering import filter_profiles
|
||||
|
||||
|
||||
class CollectSlackFamilies(pyblish.api.InstancePlugin):
|
||||
"""Collect family for Slack notification
|
||||
|
||||
Expects configured profile in
|
||||
Project settings > Slack > Publish plugins > Notification to Slack
|
||||
|
||||
Add Slack family to those instance that should be messaged to Slack
|
||||
"""
|
||||
order = pyblish.api.CollectorOrder + 0.4999
|
||||
label = 'Collect Slack family'
|
||||
|
||||
profiles = None
|
||||
|
||||
def process(self, instance):
|
||||
task_name = io.Session.get("AVALON_TASK")
|
||||
family = self.main_family_from_instance(instance)
|
||||
|
||||
key_values = {
|
||||
"families": family,
|
||||
"tasks": task_name,
|
||||
"hosts": instance.data["anatomyData"]["app"],
|
||||
}
|
||||
self.log.debug("key_values {}".format(key_values))
|
||||
profile = filter_profiles(self.profiles, key_values,
|
||||
logger=self.log)
|
||||
|
||||
# make slack publishable
|
||||
if profile:
|
||||
if instance.data.get('families'):
|
||||
instance.data['families'].append('slack')
|
||||
else:
|
||||
instance.data['families'] = ['slack']
|
||||
|
||||
instance.data["slack_channel"] = profile["channel"]
|
||||
instance.data["slack_message"] = profile["message"]
|
||||
|
||||
slack_token = (instance.context.data["project_settings"]
|
||||
["slack"]
|
||||
["publish"]
|
||||
["CollectSlackFamilies"]
|
||||
["token"])
|
||||
instance.data["slack_token"] = slack_token
|
||||
|
||||
def main_family_from_instance(self, instance): # TODO yank from integrate
|
||||
"""Returns main family of entered instance."""
|
||||
family = instance.data.get("family")
|
||||
if not family:
|
||||
family = instance.data["families"][0]
|
||||
return family
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
from slack_sdk import WebClient
|
||||
from slack_sdk.errors import SlackApiError
|
||||
|
||||
import pyblish.api
|
||||
from openpype.lib.plugin_tools import prepare_template_data
|
||||
|
||||
|
||||
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
|
||||
|
||||
Message template can contain {} placeholders from anatomyData.
|
||||
"""
|
||||
order = pyblish.api.IntegratorOrder+0.499
|
||||
label = "Integrate Slack Api"
|
||||
families = ["slack"]
|
||||
|
||||
optional = True
|
||||
|
||||
def process(self, instance):
|
||||
message_templ = instance.data["slack_message"]
|
||||
|
||||
fill_pairs = set()
|
||||
for key, value in instance.data["anatomyData"].items():
|
||||
if not isinstance(value, str):
|
||||
continue
|
||||
fill_pairs.add((key, value))
|
||||
self.log.debug("fill_pairs:: {}".format(fill_pairs))
|
||||
|
||||
message = message_templ.format(**prepare_template_data(fill_pairs))
|
||||
|
||||
self.log.debug("message:: {}".format(message))
|
||||
if '{' in message:
|
||||
self.log.warning(
|
||||
"Missing values to fill message properly {}".format(message))
|
||||
|
||||
return
|
||||
|
||||
for channel in instance.data["slack_channel"]:
|
||||
try:
|
||||
client = WebClient(token=instance.data["slack_token"])
|
||||
_response = client.chat_postMessage(
|
||||
channel=channel,
|
||||
text=message
|
||||
)
|
||||
except SlackApiError as e:
|
||||
# You will get a SlackApiError if "ok" is False
|
||||
self.log.warning("Error happened {}".format(e.response[
|
||||
"error"]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue