Send comments to Slack

🚧

Pre-requisites

Users should have a custom field SlackID

📘

How to apply this rule?

🚧

Warning: sample URLs!

Please make sure to replace https://hooks.slack.com/services/xxxxx/yyyyyy/zzzzzzzzztGRAKwHLfm4kw with your Slack Webhook URL and https://youraccount.tpondemand.com/ with your Targetprocess URL

Filter by General.Project.Name is optional

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "Comment"
    ],
    "modifications": {
      "created": true,
      "deleted": false,
      "updated": [
        "Description"
      ]
    }
  },
  {
    "type": "filter:Relational",
    "or": [
      {
        "and": [
          {
            "target": {
              "type": "field",
              "name": "Name",
              "target": {
                "type": "field",
                "name": "Project",
                "target": {
                  "type": "field",
                  "name": "General",
                  "target": {
                    "type": "pipelineBlockOutput"
                  }
                }
              }
            },
            "value": {
              "type": "constant",
              "value": "Mobile"
            },
            "operator": {
              "type": "is"
            }
          }
        ]
      }
    ]
  },
  {
    "type": "action:JavaScript",
    "script": "const MARKDOWN_MARK = \"<!--markdown-->\";\n\nconst isMarkdown = desc => desc.startsWith(MARKDOWN_MARK);\nconst preprocessMarkdown = desc => desc.replace(MARKDOWN_MARK, \"\");\nconst preprocessHtml = desc => decodeHtmlText(removeHtmlTags(desc));\nconst removeHtmlTags = desc => desc.replace(/<\\/?[^>]+(>|$)/g, \"\");\nconst decodeHtmlText = desc => desc.replace(/&#(\\d+);/g, (_, code) => String.fromCharCode(code));\n\nconst rawDescription = args.Current.Description;\nconst description = isMarkdown(rawDescription) ?\n  preprocessMarkdown(rawDescription) :\n  preprocessHtml(rawDescription);\n\nfunction findLogins(desc) {\n  const result = [];\n  const regex = /@user:(.[^[]+)/g;\n\n  let match;\n  while ((match = regex.exec(desc)) !== null) {\n    const login = match[1];\n    if (!result.includes(login)) {\n      result.push(login);\n    }\n  }\n\n  return result;\n}\n\nconst logins = findLogins(description);\n\nconst api = context.getService(\"targetprocess/api/v2\");\nconst quote = x => `\"${x}\"`;\nconst users = await api.queryAsync(\"Users\", {\n  select: \"{ login, slackID }\",\n  where: `login in [${logins.map(quote)}]`\n});\n\nconst getSlackIdByLogin = login => users.find(x => x.login === login).slackID;\nconst link = `<https://youraccount.tpondemand.com/entity/${args.Current.General.Id}|${args.Current.General.Name}>`;\nconst message = args.Author.FullName + \" has commented on \" + link + \": \" + description\n  .replace(/@user:(.[^[]+)\\[.*?\\]/g, (_, login) => `<@${getSlackIdByLogin(login)}>`);\n\nreturn {\n  command: \"SendHttpRequest\",\n  payload: {\n    url: \"https://hooks.slack.com/services/xxxxx/yyyyyy/zzzzzzzzztGRAKwHLfm4kw\",\n    body: {\n      \"text\": message,\n      \"icon_emoji\": \":bee:\",\n      \"username\": \"Targetprocess\",\n      \"channel\": \"#slack-test\"\n    }\n  }\n};"
  }
]