Skip to content

Commit

Permalink
feat(core): bring back github & blog releases (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
leocabeza authored Aug 9, 2020
1 parent 0f8f921 commit 8a92ab3
Show file tree
Hide file tree
Showing 16 changed files with 389 additions and 293 deletions.
5 changes: 3 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ DIALOGFLOW_PROJECT_ID=
GITHUB_ACCESS_TOKEN=
MAIN_GROUP_ID=
NODE_ENV=development
PIPEDREAM_USERNAME=
PIPEDREAM_PASSWORD=
SENTRY_DSN=
SUPERFEEDR_SECRET=
TELEGRAM_BOT_TOKEN=
TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_ACCESS_TOKEN=
TWITTER_ACCESS_TOKEN_SECRET=
TWITTER_WEBHOOK_ENV=
ZAPIER_BASIC_AUTH_USERNAME=
ZAPIER_BASIC_AUTH_PASSWORD=
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ module.exports = {
node: true
},
rules: {
"no-console": "off"
"no-console": "off",
"@typescript-eslint/ban-ts-comment": [
2,
{
"ts-ignore": "allow-with-description"
}
]
}
};
12 changes: 8 additions & 4 deletions api/_utils/zapier-auth.ts → api/_utils/basic-auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Buffer } from 'buffer';

const { ZAPIER_BASIC_AUTH_USERNAME, ZAPIER_BASIC_AUTH_PASSWORD } = process.env;

const isBasicAuthValid = (authorizationHeader = '') => {
const isBasicAuthValid = (
authorizationHeader: string,
matchingUsername: string,
matchingPassword: string,
): boolean => {
const token = authorizationHeader.split(/\s+/).pop() || '';

const auth = Buffer.from(token, 'base64').toString();
Expand All @@ -11,7 +13,9 @@ const isBasicAuthValid = (authorizationHeader = '') => {
const username = parts[0];
const password = parts[1];

return username === ZAPIER_BASIC_AUTH_USERNAME && password === ZAPIER_BASIC_AUTH_PASSWORD
return (
username === matchingUsername && password === matchingPassword
);
};

export default isBasicAuthValid;
38 changes: 38 additions & 0 deletions api/_utils/blog/feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import messages from '../messages';
import { sendMessage } from '../telegram/bot-methods';

const { MAIN_GROUP_ID } = process.env;

interface BlogFeed {
actor: {
displayName: string;
};
permalinkUrl: string;
title: string;
}

/**
* TODO: for the ngVenezuela blog to work we have to wait until this is
* closed: https://github.com/thepracticaldev/dev.to/issues/3363
*/
const handleBlogFeed = async (feeds: BlogFeed[]) => {
if (MAIN_GROUP_ID) {
const promises: Promise<void>[] = [];

feeds.forEach(feed => {
const promise = sendMessage({
chatId: Number(MAIN_GROUP_ID),
text: messages.newBlogPost
.replace('#{author}', feed.actor.displayName)
.replace('#{link}', feed.permalinkUrl)
.replace('#{title}', feed.title),
});

promises.push(promise);
});

await Promise.all(promises);
}
};

export default handleBlogFeed;
62 changes: 42 additions & 20 deletions api/_utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
/**
* About feeds:
* Superfeedr free plan only support up to 10 feeds
* in the free tier: https://superfeedr.com/subscriber#pricing
*/

const config = {
whiteListedDomains: [
"https://github.com",
"https://medium.com",
"https://twitter.com"
'https://github.com',
'https://dev.to',
'https://twitter.com',
],
githubFeeds: [
{
name: "angular",
repo: "angular/angular",
name: 'angular',
hasChangelog: true,
feed: 'https://github.com/angular/angular/releases.atom',
},
{
name: 'ionic',
hasChangelog: true,
feed: "https://github.com/angular/angular/releases.atom"
feed: 'https://github.com/driftyco/ionic/releases.atom',
},
{
name: "ionic",
repo: "driftyco/ionic",
name: 'nativescript',
hasChangelog: true,
feed: "https://github.com/driftyco/ionic/releases.atom"
feed:
'https://github.com/NativeScript/NativeScript/releases.atom',
},
{
name: "nativescript",
repo: "NativeScript/NativeScript",
name: 'wengy-ven',
hasChangelog: true,
feed: "https://github.com/NativeScript/NativeScript/releases.atom"
feed: 'https://github.com/ngVenezuela/wengy-ven/releases.atom',
},
{
name: "wengy-ven",
repo: "ngVenezuela/wengy-ven",
name: 'ngrx',
hasChangelog: true,
feed: "https://github.com/ngVenezuela/wengy-ven/releases.atom"
}
feed: 'https://github.com/ngrx/platform/releases.atom',
},
{
name: 'TypeScript',
hasChangelog: false,
feed: 'https://github.com/Microsoft/TypeScript/releases.atom',
},
{
name: 'nx',
hasChangelog: false,
feed: 'https://github.com/nrwl/nx/releases.atom',
},
],
blogFeeds: [
{
name: "Blog oficial de ngvenezuela",
feed: "https://dev.to/feed/ngvenezuela"
}
]
name: 'Blog oficial de ngvenezuela',
feed: 'https://dev.to/feed/ngvenezuela',
},
{
name: 'Angular blog',
feed: 'https://blog.angular.io/feed',
},
],
};

export default config;
40 changes: 40 additions & 0 deletions api/_utils/github/feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import messages from '../messages';
import { sendMessage } from '../telegram/bot-methods';

const { MAIN_GROUP_ID } = process.env;

interface RepoConfig {
hasChangelog: boolean;
name: string;
feed: string;
}

const handleGithubFeed = async (
repoConfig: RepoConfig,
tags: string[],
) => {
if (MAIN_GROUP_ID) {
const promises: Promise<void>[] = [];

tags.forEach(tag => {
const promise = sendMessage({
chatId: Number(MAIN_GROUP_ID),
text: messages.githubRelease
.replace('#{name}', repoConfig.name)
.replace('#{version}', tag)
.replace(
'#{url}',
repoConfig.hasChangelog
? `https://github.com/${repoConfig.name}/blob/master/CHANGELOG.md`
: `https://github.com/${repoConfig.name}/releases/tag/${tag}`,
),
});

promises.push(promise);
});

await Promise.all(promises);
}
};

export default handleGithubFeed;
22 changes: 22 additions & 0 deletions api/_utils/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NowRequest } from '@vercel/node';
import { Buffer } from 'buffer';

const getRawBody = async (readable: NowRequest): Promise<Buffer> => {
const chunks: any[] = [];
let bytes = 0;

return new Promise((resolve, reject) => {
readable.on('error', reject);

readable.on('data', chunk => {
chunks.push(chunk);
bytes += chunk.length;
});

readable.on('end', () => {
resolve(Buffer.concat(chunks, bytes));
});
});
};

export default getRawBody;
79 changes: 79 additions & 0 deletions api/pipedream/morning-greeting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as Sentry from '@sentry/node';
import { NowRequest, NowResponse } from '@vercel/node';

import messages from '../_utils/messages';
import { sendMessage } from '../_utils/telegram/bot-methods';
import isBasicAuthValid from '../_utils/basic-auth';

const {
MAIN_GROUP_ID,
SENTRY_DSN,
NODE_ENV,
PIPEDREAM_USERNAME,
PIPEDREAM_PASSWORD,
} = process.env;

Sentry.init({ dsn: SENTRY_DSN });

// aparently when eslint is run, it always create a long func
// eslint-disable-next-line max-len
const generateRandomBetween = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;

const getMorningMessage = () => {
const { goodMornings } = messages;

const now = new Date();
const weekday = now.getDay();

const weekDays: { [index: number]: string } = {
0: 'generic',
1: 'mondays',
2: 'generic',
3: 'generic',
4: 'generic',
5: 'fridays',
6: 'generic',
};

const randomIndex = generateRandomBetween(
0,
goodMornings[weekDays[weekday]].length - 1,
);

return goodMornings[weekDays[weekday]][randomIndex];
};

export default async (request: NowRequest, response: NowResponse) => {
try {
const matchingUsername = PIPEDREAM_USERNAME || '';
const matchingPassword = PIPEDREAM_PASSWORD || '';
const authorization = request.headers.authorization || '';

if (
isBasicAuthValid(
authorization,
matchingUsername,
matchingPassword,
)
) {
const text = getMorningMessage();

if (MAIN_GROUP_ID) {
await sendMessage({ chatId: Number(MAIN_GROUP_ID), text });
}

response.status(200).send('ok');
} else {
response.status(401).send('Unauthorized');
}
} catch (error) {
if (NODE_ENV === 'development') {
console.error(error);
} else {
Sentry.captureException(error);
}

response.status(400).send('not ok');
}
};
Loading

0 comments on commit 8a92ab3

Please sign in to comment.