"use strict";
// P A C K A G E S
import async from "async";
import color from "colorette";
import Octokit from "@octokit/rest";
import redis from "redis";
// U T I L S
import messageSlack from "~helper/slack";
import relativeDate from "~module/relative-date";
let octokit;
// R E D I S
let client;
if (process.env.GITHUB_OAUTH_TOKEN) {
octokit = new Octokit({
auth: `token ${process.env.GITHUB_OAUTH_TOKEN}`
});
} else process.stdout.write(`${color.red("[missing]")} GitHub token\n`);
if (process.env.REDISCLOUD_URL) {
client = redis.createClient(process.env.REDISCLOUD_URL);
client.on("error", redisError => {
process.env.NODE_ENV === "development" ?
process.stdout.write(`\n${color.yellow("Unable to connect to Redis client.")}\nYou may be missing an .env file or your connection was reset.`) :
messageSlack(
"\n" +
"> *REDIS ERROR:* ```" + JSON.parse(JSON.stringify(redisError)) + "```" + "\n" +
"> _Cause: Someone is trying to run LBRY.tech locally without environment variables OR Heroku is busted_\n"
)
;
});
} else process.stdout.write(`${color.red("[missing]")} Redis client URL\n`);
// P R O G R A M
function generateEvent(event) {
switch(event.type) {
case "CommitCommentEvent":
return `
${event.actor.display_login} commented on
commit in
`;
case "CreateEvent":
return `
${event.actor.display_login} created ${event.payload.ref_type}
${refToBranch(event.payload.ref)}
in
`;
case "DeleteEvent":
return `
${event.actor.display_login} deleted
${event.payload.ref_type} ${event.payload.ref}
in
`;
case "ForkEvent":
return `
${event.actor.display_login} forked
${event.repo.name} to
${event.payload.forkee.full_name}
`;
case "IssueCommentEvent":
if (event.payload.issue.pull_request) {
return `
${event.actor.display_login} commented on pull request
${escapeSpecialCharacters(event.payload.issue.title)} in
`;
} else {
return `
${event.actor.display_login} commented on issue
${escapeSpecialCharacters(event.payload.issue.title)} in
`;
}
case "IssuesEvent":
return `
${event.actor.display_login} ${event.payload.action} issue
${escapeSpecialCharacters(event.payload.issue.title)} in
`;
case "PullRequestEvent":
return `
${event.actor.display_login} pull request
${escapeSpecialCharacters(event.payload.pull_request.title)} in
`;
case "PullRequestReviewEvent":
return `
${event.actor.display_login} reviewed pull request
${escapeSpecialCharacters(event.payload.pull_request.title)} in
`;
case "PullRequestReviewCommentEvent":
return `
${event.actor.display_login} commented on pull request
${escapeSpecialCharacters(event.payload.pull_request.title)} in
`;
case "PushEvent":
return `
${event.actor.display_login} pushed to
${refToBranch(event.payload.ref)}
in
`;
case "ReleaseEvent":
return `
${event.actor.display_login} released
${event.payload.release.tag_name} in
`;
case "WatchEvent":
return `
${event.actor.display_login} starred the repo
`;
default:
break;
}
}
function generateGitHubFeed(displayGitHubFeed) {
if (process.env.REDISCLOUD_URL) {
client.zrevrange("events", 0, 9, (err, reply) => {
if (err) return; // TODO: Render a div with nice error message
const events = [];
const renderedEvents = [];
reply.forEach(item => events.push(JSON.parse(item)));
for (const event of events) {
const repoName = `
${event.repo.name}
`;
renderedEvents.push(`
${generateEvent(event)} ${event.type !== "ForkEvent" ? repoName : ""} ${relativeDate(new Date(event.created_at))}