Skip to content

Commit

Permalink
Wrap everything in classes, fix RSS
Browse files Browse the repository at this point in the history
  • Loading branch information
reconman committed Sep 25, 2022
1 parent 3524829 commit 5ea3da4
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 241 deletions.
2 changes: 1 addition & 1 deletion etc/config.example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let config = {
const config = {
interval: 60000, // Feed check interval, in miliseconds
userAgent: 'Mozilla/5.0 (Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0', // Experimental: User agent string to bypass possible fetching limits on GitHub
/**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alerthub",
"version": "2.1.1",
"version": "2.2.1",
"description": "A tool to send notifications when an update to a repository is released",
"main": "./src/index.js",
"type": "module",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */

// First, let's require the libraries
import RssFeedEmitter from 'rss-feed-emitter'
import http from 'http'
import RssFeedEmitter from 'rss-feed-emitter';
import http from 'http';

import * as alertHubUtils from './utils/alertHub.js';
import alertHubUtils from './utils/alertHub.js';
import pushBulletUtils from './utils/pushBullet.js';
import pushOverUtils from './utils/pushOver.js';
import emailUtils from './utils/email.js';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/rss-braider/addReleaseNameToTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
}]
*/
// We need a plug-in to add release name to the title.
import * as utils from '../../utils/alertHub.js'
import utils from '../../utils/alertHub.js';

export default (_item, itemOptions, /* source */) => {
if (
Expand Down
111 changes: 52 additions & 59 deletions src/utils/alertHub.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,70 @@
import URL from 'url';
import querystring from 'querystring';

// Because there are also feeds,
// we need this method to check whether the feed is from GitHub or not
function isFeedFromGitHub(item) {
if (item.guid !== undefined && item.guid !== null && typeof item.guid === 'string') {
if (item.guid.substring(0, 14) === 'tag:github.com') {
export default class AlertHubUtils {
// Because there are also feeds,
// we need this method to check whether the feed is from GitHub or not
static isFeedFromGitHub(item) {
if (item.guid !== undefined && item.guid !== null && typeof item.guid === 'string') {
if (item.guid.substring(0, 14) === 'tag:github.com') {
return true;
}
}
return false;
}

static isFeedFromGitLab(item) {
const parsedURL = URL.parse(item.url);
if (parsedURL.hostname === 'gitlab.com') {
return true;
}
return false;
}
return false;
}

function isFeedFromGitLab(item) {
const parsedURL = URL.parse(item.url);
if (parsedURL.hostname === 'gitlab.com') {
return true;
// Because there's no release name in GitHub feed, we steal from URL
static getReleaseNameFromGitHubAndGitLabFeedLink(link) {
const parts = link.split('/');
return `${parts[3]}/${parts[4]}`;
}
return false;
}

// Because there's no release name in GitHub feed, we steal from URL
function getReleaseNameFromGitHubAndGitLabFeedLink(link) {
const parts = link.split('/');
return `${parts[3]}/${parts[4]}`;
}
// Standardize the new feed element
static parseFeedData(feedData) {
const parsedFeed = {
title: feedData.title,
link: feedData.link,
description: feedData.description,
// summary: feedData.summary,
date: feedData.date,
};

// Standardize the new feed element
function parseFeedData(feedData) {
const parsedFeed = {
title: feedData.title,
link: feedData.link,
description: feedData.description,
// summary: feedData.summary,
date: feedData.date,
};
// We need to prepend release name to the title
if (this.isFeedFromGitHub(feedData) === true || this.isFeedFromGitLab(feedData) === true) {
parsedFeed.title = `${this.getReleaseNameFromGitHubAndGitLabFeedLink(feedData.link)} - ${feedData.title}`;
}

// We need to prepend release name to the title
if (isFeedFromGitHub(feedData) === true || isFeedFromGitLab(feedData) === true) {
parsedFeed.title = `${getReleaseNameFromGitHubAndGitLabFeedLink(feedData.link)} - ${feedData.title}`;
return parsedFeed;
}

return parsedFeed;
}

// Generates the RSS feed URL with given parameters honoring the configuration
function generateURLForTheFeed(options, config) {
if (options.resource === 'github') {
const optionalGitHubAccessToken = config.githubToken !== null ? `?token=${config.githubToken}` : '';
if (options.type === 'issues') {
return `https://issue-tracker-rss.now.sh/${options.repository}?${querystring.encode(options.params)}`;
// Generates the RSS feed URL with given parameters honoring the configuration
static generateURLForTheFeed(options, config) {
if (options.resource === 'github') {
const optionalGitHubAccessToken = config.githubToken !== null ? `?token=${config.githubToken}` : '';
if (options.type === 'issues') {
return `https://issue-tracker-rss.now.sh/${options.repository}?${querystring.encode(options.params)}`;
}
if (Object.prototype.hasOwnProperty.call(options, 'subType')) {
return `https://www.github.com/${options.repository}/${options.type}/${options.subType}.atom${optionalGitHubAccessToken}`;
}
return `https://www.github.com/${options.repository}/${options.type}.atom${optionalGitHubAccessToken}`;
}
if (Object.prototype.hasOwnProperty.call(options, 'subType')) {
return `https://www.github.com/${options.repository}/${options.type}/${options.subType}.atom${optionalGitHubAccessToken}`;
}
return `https://www.github.com/${options.repository}/${options.type}.atom${optionalGitHubAccessToken}`;
}

if (options.resource === 'gitlab') {
if (Object.prototype.hasOwnProperty.call(options, 'subType')) {
return `https://gitlab.com/${options.repository}/-/${options.type}/${options.subType}?format=atom`;
if (options.resource === 'gitlab') {
if (Object.prototype.hasOwnProperty.call(options, 'subType')) {
return `https://gitlab.com/${options.repository}/-/${options.type}/${options.subType}?format=atom`;
}
return `https://gitlab.com/${options.repository}/-/${options.type}?format=atom`;
}
return `https://gitlab.com/${options.repository}/-/${options.type}?format=atom`;
}

return '';
return '';
}
}

export {
parseFeedData,
generateURLForTheFeed,
// These bottom methods are for rss braider plugin to prepend release name to feed title
isFeedFromGitHub,
isFeedFromGitLab,
getReleaseNameFromGitHubAndGitLabFeedLink,
};
56 changes: 29 additions & 27 deletions src/utils/email.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import mail from 'nodemailer';
import striptags from 'striptags';

// Sends and e-mail notification to provided user
export default async function sendEmailNotification(config, feedData) {
const transporter = mail.createTransport(config.notifications.email.config);
export default class Email {
// Sends and e-mail notification to provided user
static async sendEmailNotification(config, feedData) {
const transporter = mail.createTransport(config.notifications.email.config);

/* await smtp.verify((error, success) => {
if (error) {
// console.log(error);
return false;
}
// successful smtp
}); */
// E-mail options that will be used by nodemailer
const mailOptions = {
from: config.notifications.email.mailOptions.from,
to: config.notifications.email.mailOptions.to,
subject: config.notifications.email.mailOptions.subjectPrefix.length > 0 ? `${config.notifications.email.mailOptions.subjectPrefix} - ${feedData.title}` : feedData.title,
text: `${feedData.link}\n${striptags(feedData.description)}`,
html: `<a target="_blank" href="${feedData.link}">${feedData.title}</a><br><br>${feedData.description}`,
};
/* await smtp.verify((error, success) => {
if (error) {
// console.log(error);
return false;
}
// successful smtp
}); */
// E-mail options that will be used by nodemailer
const mailOptions = {
from: config.notifications.email.mailOptions.from,
to: config.notifications.email.mailOptions.to,
subject: config.notifications.email.mailOptions.subjectPrefix.length > 0 ? `${config.notifications.email.mailOptions.subjectPrefix} - ${feedData.title}` : feedData.title,
text: `${feedData.link}\n${striptags(feedData.description)}`,
html: `<a target="_blank" href="${feedData.link}">${feedData.title}</a><br><br>${feedData.description}`,
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
// console.log(error);
return false;
}
// console.log('Message sent: %s', info.messageId);
return info;
});
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
// console.log(error);
return false;
}
// console.log('Message sent: %s', info.messageId);
return info;
});
}
}
31 changes: 17 additions & 14 deletions src/utils/pushBullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import striptags from 'striptags';

// Send the push notification.
// Todo: why bother with async / await at all ?
export default async function sendPushBulletNotification(config, feedData) {
const pusher = new PushBullet(config.notifications.pushbullet.accessToken);
await pusher.link(
{},
feedData.title,
feedData.link,
striptags(feedData.description),
(error, response) => {
if (error) {
return false;
export default class PushBulletUtils {
static async sendPushBulletNotification(config, feedData) {
const pusher = new PushBullet(config.notifications.pushbullet.accessToken);
// @ts-ignore
await pusher.link(
{},
feedData.title,
feedData.link,
striptags(feedData.description),
(error, response) => {
if (error) {
return false;
}
// console.log('Push notification sent successfully!');
return response;
}
// console.log('Push notification sent successfully!');
return response;
}
);
);
}
}
44 changes: 23 additions & 21 deletions src/utils/pushOver.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import PushOver from 'pushover-notifications';
import striptags from 'striptags';

// Send the push notification.
// Todo: why bother with async / await at all ?
export default async function sendPushOverNotification(config, feedData) {
return new Promise((resolve, reject) => {
const pusher = new PushOver({
user: config.notifications.pushover.config.user,
token: config.notifications.pushover.config.token,
export default class PushOverUtils {
// Send the push notification.
// Todo: why bother with async / await at all ?
static async sendPushOverNotification(config, feedData) {
return new Promise((resolve, reject) => {
const pusher = new PushOver({
user: config.notifications.pushover.config.user,
token: config.notifications.pushover.config.token,
});
const msg = {
message: `${striptags(feedData.description)}\n\n${feedData.link}`,
title: feedData.title,
};
pusher.send(msg, (err, result) => {
if (err) {
reject(err);
// throw err;
return false;
}
resolve(result);
return result;
});
});
const msg = {
message: `${striptags(feedData.description)}\n\n${feedData.link}`,
title: feedData.title,
};
pusher.send(msg, (err, result) => {
if (err) {
reject(err);
// throw err;
return false;
}
resolve(result);
return result;
});
});
}
}
Loading

0 comments on commit 5ea3da4

Please sign in to comment.