236 lines
7.7 KiB
JavaScript
236 lines
7.7 KiB
JavaScript
"use strict";
|
|
|
|
|
|
|
|
// P A C K A G E S
|
|
|
|
const async = require("async");
|
|
const color = require("colorette");
|
|
const local = require("app-root-path").require;
|
|
const octokit = require("@octokit/rest")();
|
|
const redis = require("redis");
|
|
|
|
// V A R I A B L E S
|
|
|
|
const logSlackError = local("app/helpers/slack");
|
|
const relativeDate = local("app/modules/relative-date");
|
|
let client;
|
|
|
|
// R E D I S
|
|
|
|
if (typeof process.env.GITHUB_OAUTH_TOKEN !== "undefined") {
|
|
octokit.authenticate({
|
|
type: "oauth",
|
|
token: process.env.GITHUB_OAUTH_TOKEN
|
|
});
|
|
} else process.stdout.write(`${color.red("[missing]")} GitHub token`);
|
|
|
|
if (typeof process.env.REDISCLOUD_URL !== "undefined") {
|
|
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.`) :
|
|
logSlackError(
|
|
"\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`);
|
|
|
|
|
|
|
|
// P R O G R A M
|
|
|
|
function generateEvent(event) {
|
|
switch (event.type) {
|
|
case "CommitCommentEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> commented on
|
|
<a href="${generateUrl("comment", event)}" title="View this comment on GitHub">commit</a> in
|
|
`;
|
|
|
|
case "CreateEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> created
|
|
${event.payload.ref_type} <code>${event.payload.ref}</code> in
|
|
`;
|
|
|
|
case "DeleteEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> deleted
|
|
${event.payload.ref_type} <code>${event.payload.ref}</code> in
|
|
`;
|
|
|
|
case "ForkEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> forked
|
|
<strong><a href="${generateUrl("repo", event)}" title="View this repo on GitHub" target="_blank" rel="noopener noreferrer">${event.repo.name}</a></strong> to
|
|
<strong><a href="${generateUrl("forkee", event)}" title="View this repo fork on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.forkee.name}</a></strong> in
|
|
`;
|
|
|
|
case "IssueCommentEvent":
|
|
if (event.payload.issue.pull_request) {
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> commented on pull request
|
|
<em><a href="${generateUrl("issue", event)}" title="View this comment on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.issue.title}</a></em> in
|
|
`;
|
|
} else {
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> commented on issue
|
|
<em><a href="${generateUrl("issue", event)}" title="View this comment on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.issue.title}</a></em> in
|
|
`;
|
|
}
|
|
|
|
case "IssuesEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> ${event.payload.action} issue
|
|
<em><a href="${generateUrl("issue", event)}" title="View this issue on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.issue.title}</a></em> in
|
|
`;
|
|
|
|
case "PullRequestEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> ${event.payload.action} pull request
|
|
<em><a href="${generateUrl("pull_request", event)}" title="View this pull request on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.pull_request.title}</a></em> in
|
|
`;
|
|
|
|
case "PullRequestReviewCommentEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> commented on pull request
|
|
<em><a href="${generateUrl("pull_request", event)}" title="View this comment on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.pull_request.title}</a></em> in
|
|
`;
|
|
|
|
case "PushEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> pushed to
|
|
<code><a href="${generateUrl("push", event)}" title="View this branch on GitHub" target="_blank" rel="noopener noreferrer">${refToBranch(event.payload.ref)}</a></code> in
|
|
`;
|
|
|
|
case "ReleaseEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> released
|
|
<em><a href="${generateUrl("release", event)}" title="View this release on GitHub" target="_blank" rel="noopener noreferrer">${event.payload.release.tag_name}</a></em> in
|
|
`;
|
|
|
|
case "WatchEvent":
|
|
return `
|
|
<strong>${event.actor.display_login}</strong> starred the repo
|
|
`;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
function generateGitHubFeed(displayGitHubFeed) {
|
|
if (typeof process.env.REDISCLOUD_URL !== "undefined") {
|
|
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) {
|
|
renderedEvents.push(`
|
|
<div class='github-feed__event'>
|
|
<a href="${generateUrl("actor", event)}" target="_blank" rel="noopener noreferrer">
|
|
<img src="${event.actor.avatar_url}" class="github-feed__event__avatar" alt=""/>
|
|
</a>
|
|
|
|
<p>
|
|
${generateEvent(event)}
|
|
<a href="${generateUrl("repo", event)}" title="View this repo on GitHub" target="_blank" rel="noopener noreferrer"><strong>${event.repo.name}</strong></a>
|
|
<em class="github-feed__event__time">${relativeDate(new Date(event.created_at))}</em>
|
|
</p>
|
|
</div>
|
|
`);
|
|
}
|
|
|
|
updateGithubFeed(); // TODO: Update `.last-updated` every minute
|
|
|
|
displayGitHubFeed(`
|
|
<h3>GitHub</h3>
|
|
<h5 class="last-updated">Last updated: ${new Date().format("YYYY-MM-DD").replace(/-/g, "·")} at ${new Date().add(-4, "hours").format("UTC:H:mm:ss A").toLowerCase()} EST</h5>
|
|
|
|
${renderedEvents.join("")}
|
|
`);
|
|
});
|
|
}
|
|
}
|
|
|
|
function generateUrl(type, event) {
|
|
switch (type) {
|
|
case "actor":
|
|
return `https://github.com/${event.actor.display_login}`;
|
|
|
|
case "comment":
|
|
return event.payload.comment.html_url;
|
|
|
|
case "repo":
|
|
return `https://github.com/${event.repo.name}`;
|
|
|
|
case "forkee":
|
|
return `https://github.com/${event.payload.forkee.full_name}`;
|
|
|
|
case "issue":
|
|
return event.payload.issue.html_url;
|
|
|
|
case "pull_request":
|
|
return event.payload.pull_request.html_url;
|
|
|
|
case "release":
|
|
return event.payload.release.html_url;
|
|
|
|
case "push":
|
|
return `https://github.com/${event.repo.name}/tree/${event.payload.ref.replace("refs/heads/", "")}`;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
function updateGithubFeed() {
|
|
octokit.activity.getEventsForOrg({
|
|
org: "lbryio",
|
|
per_page: 20,
|
|
page: 1
|
|
}).then(({ data }) => {
|
|
async.eachSeries(data, (item, callback) => {
|
|
const eventString = JSON.stringify(item);
|
|
|
|
client.zrank("events", eventString, (err, reply) => {
|
|
if (reply === null) client.zadd("events", item.id, eventString, callback);
|
|
else callback();
|
|
});
|
|
}, () => client.zremrangebyrank("events", 0, -51)); // Keep the latest 50 events
|
|
}).catch(err => {
|
|
logSlackError(
|
|
"\n" +
|
|
"> *GITHUB FEED ERROR:* ```" + JSON.parse(JSON.stringify(err)) + "```" + "\n" +
|
|
"> _Cause: GitHub feed refresh_\n"
|
|
);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
// H E L P E R
|
|
|
|
function refToBranch(ref) {
|
|
return ref.replace("refs/heads/", "");
|
|
}
|
|
|
|
|
|
|
|
// E X P O R T S
|
|
|
|
module.exports = exports = {
|
|
generateEvent,
|
|
generateGitHubFeed,
|
|
generateUrl,
|
|
updateGithubFeed
|
|
};
|