semaphoreui / semaphore

Modern UI for Ansible, Terraform, OpenTofu, Bash, Pulumi.
https://semaphoreui.com
MIT License
9.9k stars 1.01k forks source link

Feature: Rocket.Chat notifications #1091

Open don-rumata opened 1 year ago

don-rumata commented 1 year ago

Hi, can you add notification support to Rocket.Chat?

Ansible doc: https://docs.ansible.com/ansible/latest/collections/community/general/rocketchat_module.html

curl example:

curl -vX POST --header "Content-Type: application/json" --data @"$JSON_FILE" https://rocketchat.example.com/hooks/$ROCKETCHAT_TOKEN

JSON_FILE:

{
  "username": "BOT_USERNAME",
  "channel": "#ROCKETCHAT_CHANNEL",
  "text": "some text"
}

ROCKETCHAT_TOKEN: qqqqqqqqqqqqqqqwwwwwwwwwwwwwweeeeeeeeeeeeee

oussjarrousse commented 4 months ago

It should be very very similar to "slack" integration. Although I am not a Go developer, I might give it a try. AI make such things possible

oussjarrousse commented 4 months ago

Have you tried using a rocket.chat hook url in the slack_url in config.json?

oussjarrousse commented 4 months ago

I managed to get alerts to rocket chat working, by adding a processing script to the rocketchat web hook... no changes necessary to the platform... out of the box integration is still a better idea. so in config.json

...
"web_host": "https://your_ansible_semaphore_host",  # without a trailing /
"slack_alert": true,
"slack_url": "https://your_rocket_chat_host/hooks/hook_id/token",
...

in the dedicated rocket.chat hook add the following script, and make sure the script is enabled:

/* exported Script */
/* globals console, _, s */

/** Global Helpers
 *
 * console - A normal console instance
 * _       - An underscore instance
 * s       - An underscore string instance
 */

class Script {
  /**
   * @param {object} request
   */
  process_incoming_request({ request }) {
    // request.url.hash
    // request.url.search
    // request.url.query
    // request.url.pathname
    // request.url.path
    // request.url_raw
    // request.url_params
    // request.headers
    // request.user._id
    // request.user.name
    // request.user.username
    // request.content_raw
    // request.content

    // console is a global helper to improve debug
    console.log(request.content);

    // Define a mapping for color replacements
    const colorMapping = {
      'good': '#00EE00',
      'danger': '#EE0000'
    };
   const content = request.content;

   // Initialize content.text if it doesn't exist
   if (!content.text) {
      content.text = '';
    }

    // Check if attachments exist
    if (content.attachments && Array.isArray(content.attachments)) {
      // Iterate over attachments and modify them
      content.attachments.forEach(attachment => {
        console.log(attachment);
        // Remove "fields" and "mrkdwn_in" properties
        delete attachment.fields;
        delete attachment.mrkdwn_in;
        // Check and replace attachment.color if necessary
        if (attachment.color && colorMapping[attachment.color]) {
          attachment.color = colorMapping[attachment.color];}
        // Add attachment.text to content.text
        if (attachment.text) {
          // If it's the first addition and content.text is empty, don't prepend "; "
          if (content.text === '' || index === 0) {
            content.text += attachment.text;
          } else {
            content.text += "; " + attachment.text;
          }
        }
      });
    }

    console.log(content);

    // Return the modified content as response
    return {
      content: content // Return the modified content
    };
  }
}

the script will reshape the request.content to the rocket.chat format.

oussjarrousse commented 4 months ago

I also created a fork of the repository and will push a pull_request soon

oussjarrousse commented 4 months ago

And I wrote this blog post about it https://blog.jarrousse.org/2024/03/16/custom-rocket-chat-webhook-to-process-3rd-party-app-messages-designated-for-slack/

oussjarrousse commented 4 months ago

Feature is now available in v2.9.56. and I just successfully tested the new configurations for RocketChat:

"rocketchat_alert": true,
"rocketchat_url": "https://my_rocketchat_domain/webhook/xxxx/yyyy"

The issue can be closed :-)