<commit_before>const fs = require('fs');
const https = require('https');
const Slack = require('slack-node');
const slackify = require('slackify-html');

// Retrieve config
const config = require('./config.json');

// Send the slack message to the config's webhook
function sendSlackMessage(text, content, date) {
  const msgParameters = {
    username: config.slackHookName,
    text,
  };
  if (content !== null && date !== null) {
    msgParameters.attachments = [
      {
        color: '#015a9f',
        fields: [
          {
            title: date,
            value: content,
            short: false,
          },
        ],
      },
    ];
  }
  const slack = new Slack();
  slack.setWebhook(config.slackHookUrl);
  slack.webhook(msgParameters, (err, response) => {
    if (response.statusCode !== 200) {
      console.log(err, response);
    }
  });
}

// return filename
function getSaveFilename(type) {
  return `savedID_intra_${type}.json`;
}

// Perform update of the save data file
function updateSavedData(savedData, type, newID) {
  const newSavedData = savedData;
  newSavedData[newID] = true;
  if (fs.existsSync(getSaveFilename(type))) {
    fs.unlinkSync(getSaveFilename(type));
  }
  fs.writeFileSync(getSaveFilename(type), JSON.stringify(newSavedData));
}

function decodeHTMLEntities(text) {
  const entities = [
    ['apos', '\''],
    ['amp', '&'],
    ['lt', '<'],
    ['gt', '>'],
  ];
  let decodedText = text;

  for (let i = 0, max = entities.length; i < max; i += 1) {
    decodedText = decodedText.replace(new RegExp(`&${entities[i][0]};`, 'g'), entities[i][1]);
  }

  return decodedText;
}

// return potential saved data from file
function getSavedData(type) {
  let savedData = {};
  if (fs.existsSync(getSaveFilename(type))) {
    savedData = JSON.parse(fs.readFileSync(getSaveFilename(type), 'utf8'));
  }

  return savedData;
}

// iterate over every messages and send new one to slack
function messageNotifier(json, type) {
  const savedData = getSavedData(type);

  for (let i = 0; i < json.length; i += 1) {
    const msg = json[i];
    const id = msg.id;

    if (!(id in savedData)) {
      let title = decodeHTMLEntities(msg.title);
      title = title.split('href="').join('href="https://intra.epitech.eu');
      title = slackify(title);
      const content = slackify(msg.content);
      const date = slackify(msg.date);
      sendSlackMessage(title, content, date);
      updateSavedData(savedData, type, id);
    }
  }
}

// iterate over every modules and send new one to slack
function moduleNotifier(json, type) {
  const savedData = getSavedData(type);<commit_msg>fix bug with url in atttachment content<commit_after>const fs = require('fs');
const https = require('https');
const Slack = require('slack-node');
const slackify = require('slackify-html');

// Retrieve config
const config = require('./config.json');

// Send the slack message to the config's webhook
function sendSlackMessage(text, content, date) {
  const msgParameters = {
    username: config.slackHookName,
    text,
  };
  if (content !== null && date !== null) {
    msgParameters.attachments = [
      {
        color: '#015a9f',
        fields: [
          {
            title: date,
            value: content,
            short: false,
          },
        ],
      },
    ];
  }
  const slack = new Slack();
  slack.setWebhook(config.slackHookUrl);
  slack.webhook(msgParameters, (err, response) => {
    if (response.statusCode !== 200) {
      console.log(err, response);
    }
  });
}

// return filename
function getSaveFilename(type) {
  return `savedID_intra_${type}.json`;
}

// Perform update of the save data file
function updateSavedData(savedData, type, newID) {
  const newSavedData = savedData;
  newSavedData[newID] = true;
  if (fs.existsSync(getSaveFilename(type))) {
    fs.unlinkSync(getSaveFilename(type));
  }
  fs.writeFileSync(getSaveFilename(type), JSON.stringify(newSavedData));
}

function decodeHTMLEntities(text) {
  const entities = [
    ['apos', '\''],
    ['amp', '&'],
    ['lt', '<'],
    ['gt', '>'],
  ];
  let decodedText = text;

  for (let i = 0, max = entities.length; i < max; i += 1) {
    decodedText = decodedText.replace(new RegExp(`&${entities[i][0]};`, 'g'), entities[i][1]);
  }

  return decodedText;
}

// return potential saved data from file
function getSavedData(type) {
  let savedData = {};
  if (fs.existsSync(getSaveFilename(type))) {
    savedData = JSON.parse(fs.readFileSync(getSaveFilename(type), 'utf8'));
  }

  return savedData;
}

// iterate over every messages and send new one to slack
function messageNotifier(json, type) {
  const savedData = getSavedData(type);

  for (let i = 0; i < json.length; i += 1) {
    const msg = json[i];
    const id = msg.id;

    if (!(id in savedData)) {
      let title = decodeHTMLEntities(msg.title);
      title = title.split('href="').join('href="https://intra.epitech.eu');
      title = slackify(title);
      let content = msg.content.split('href="').join('href="https://intra.epitech.eu');
      content = slackify(content);
      const date = slackify(msg.date);
      sendSlackMessage(title, content, date);
      updateSavedData(savedData, type, id);
    }
  }
}

// iterate over every modules and send new one to slack
function moduleNotifier(json, type) {
  const savedData = getSavedData(type);
{'fraction_non_alphanumeric': 0.15250748502994013, 'fraction_numerical': 0.005239520958083832, 'mean_word_length': 3.262360446570973, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7158556', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1730, 'n_words': 413}