<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
const config = require('./config');
const { log } = require('./log');

let errorMessageStack = [];

/**
 * Add message to message stack.
 * @param {String} message
 */
function addToEmailStack(message) {
	errorMessageStack.push(message);
}

/**
 * Send an e-mail representing an error message.
 */
async function logMailer() {

	// When there is no error, don't send email
	if (errorMessageStack.length === 0) {
		return;
	}

	// Split array then combined into a string message
	let message = '';
	for (let i = 0; i < errorMessageStack.length; i++) {
		message += errorMessageStack[i] + '\n' + '\n' + '\n';
	}

	let mailOptions = {
		from: config.mailer.from,
		to: config.mailer.to,
		subject: `[OED ${config.mailer.org}] Open Energy Dashboard ERROR`,
		text: message
	};

	let transporter;
	if (config.mailer.method === 'none') {
		return;
	} else if (config.mailer.method === 'mailgun') {
		transporter = nodemailer.createTransport(mg({
			auth: {
				api_key: config.mailer.credential,
				domain: config.mailer.ident
			}
		}));
	} else if (config.mailer.method === 'gmail') {
		transporter = nodemailer.createTransport({
			service: 'gmail',
			auth: {
				user: config.mailer.ident,
				pass: <PASSWORD>
			}
		});
	} else {
		// tslint:disable-next-line no-console
		console.error(`Unable to send e-mail due to unknown mailer method ${config.mailer.method}`, true);
		return;
	}

	transporter.sendMail(mailOptions, (err, info) => {
		if (err) {
			log.err(`\t[EMAIL NOT SENT]: ${err.message}`, err, true);
		} else {
			log.info(`\t[EMAIL SENT]: ${info.response}`);
			// Clear the error message stack when email is sent
			errorMessageStack = [];
		}
	});
}

module.exports = {
	addToEmailStack,
	logMailer
};
<commit_msg>Add highlighting specfic error<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
const config = require('./config');
const { log } = require('./log');

let errorMessageStack = [];

/**
 * Add message to message stack.
 * @param {String} message
 */
function addToEmailStack(message) {
	errorMessageStack.push(message);
}

/**
 * @returns {boolean} if there is a special error in the message stack
 */
function checkIfMessageContainsSpecialError() {
	let isImportant = false;
	for (let i = 0; i < errorMessageStack.length; i++) {
		if (errorMessageStack[i].includes('does not parse to a valid moment object')) {
			isImportant = true;
			break;
		}
	}
	return isImportant;
}

/**
 * @param isImportant if there is a special error in the message stack
 * @returns {String} the subject of the email
 */
function createEmailSubject(isImportant) {
	let subject;
	if (isImportant) {
		subject = `[IMPORTANT] [OED ${config.mailer.org}] Open Energy Dashboard ERROR`
	} else {
		subject = `[OED ${config.mailer.org}] Open Energy Dashboard ERROR`
	}
	return subject;
}

/**
 * Create the body of the email. Color message red it if it is a special error
 * @returns {string} the content of the email
 */
function createEmailBody() {
	// Split array then combined into a string message
	let message = '';
	for (let i = 0; i < errorMessageStack.length; i++) {
		if (errorMessageStack[i].includes('does not parse to a valid moment object')) {
			message += `<p style='color:red;'>${errorMessageStack[i]}</p>`;
		} else {
			message += `<p>${errorMessageStack[i]}</p>`;
		}
	}
	return message;
}

/**
 * Send an e-mail representing an error message.
 */
async function logMailer() {

	// When there is no error, don't send email
	if (errorMessageStack.length === 0) {
		return;
	}

	const isImportant = checkIfMessageContainsSpecialError();

	const emailSubject = createEmailSubject(isImportant);

	const emailBody = createEmailBody();

	let mailOptions = {
		from: config.mailer.from,
		to: config.mailer.to,
		subject: emailSubject,
		text: emailBody
	};

	let transporter;
	if (config.mailer.method === 'none') {
		return;
	} else if (config.mailer.method === 'mailgun') {
		transporter = nodemailer.createTransport(mg({
			auth: {
				api_key: config.mailer.credential,
				domain: config.mailer.ident
			}
		}));
	} else if (config.mailer.method === 'gmail') {
		transporter = nodemailer.createTransport({
			service: 'gmail',
			auth: {
				user: config.mailer.ident,
				pass: <PASSWORD>.mailer.credential
			}
		});
	} else {
		// tslint:disable-next-line no-console
		console.error(`Unable to send e-mail due to unknown mailer method ${config.mailer.method}`, true);
		return;
	}

	transporter.sendMail(mailOptions, (err, info) => {
		if (err) {
			log.err(`\t[EMAIL NOT SENT]: ${err.message}`, err, true);
		} else {
			log.info(`\t[EMAIL SENT]: ${info.response}`);
			// Clear the error message stack when email is sent
			errorMessageStack = [];
		}
	});
}

module.exports = {
	addToEmailStack,
	logMailer
};

{'fraction_non_alphanumeric': 0.14562558795860772, 'fraction_numerical': 0.0024459078080903106, 'mean_word_length': 3.6919682259488087, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '6577596', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1731, 'n_words': 509}