lbry.tech/app/helpers/github.js

382 lines
11 KiB
JavaScript
Raw Normal View History

2018-07-12 23:07:16 +02:00
"use strict";
2018-09-27 18:11:26 +02:00
// P A C K A G E S
2019-01-30 00:29:14 +01:00
import async from "async";
import color from "colorette";
import Octokit from "@octokit/rest";
import redis from "redis";
2018-09-27 18:11:26 +02:00
2018-10-10 19:56:35 +02:00
// U T I L S
2018-09-27 18:11:26 +02:00
2019-01-30 00:29:14 +01:00
import messageSlack from "@helper/slack";
import relativeDate from "@module/relative-date";
let octokit;
2018-09-27 18:11:26 +02:00
String.prototype.escape = function() {
const tagsToReplace = {
"&": "&",
"<": "&lt;",
">": "&gt;"
};
return this.replace(/[&<>]/g, tag => tagsToReplace[tag] || tag);
};
2018-09-27 18:11:26 +02:00
// R E D I S
2018-10-10 19:56:35 +02:00
let client;
2018-09-27 18:11:26 +02:00
if (typeof process.env.GITHUB_OAUTH_TOKEN !== "undefined") {
octokit = new Octokit({
auth: `token ${process.env.GITHUB_OAUTH_TOKEN}`
2018-09-27 18:11:26 +02:00
});
} 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.`) :
2018-11-30 21:46:22 +01:00
messageSlack(
2018-09-27 18:11:26 +02:00
"\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`);
2018-07-12 23:07:16 +02:00
// P R O G R A M
function generateEvent(event) {
2018-09-28 21:09:45 +02:00
switch(event.type) {
2018-07-12 23:07:16 +02:00
case "CommitCommentEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> commented on
<a
href="${generateUrl("comment", event)}"
target="_blank"
title="View this comment on GitHub"
rel="noopener noreferrer"
>commit</a> in
2018-07-12 23:07:16 +02:00
`;
case "CreateEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> created ${event.payload.ref_type}
<code><a
href="${generateUrl("create", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this branch on GitHub"
>${refToBranch(event.payload.ref)}</a></code> in
2018-07-12 23:07:16 +02:00
`;
case "DeleteEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> deleted
2018-07-12 23:07:16 +02:00
${event.payload.ref_type} <code>${event.payload.ref}</code> in
`;
case "ForkEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> forked
<strong><a
href="${generateUrl("repo", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this repo on GitHub"
>${event.repo.name}</a></strong> to
<strong><a
href="${generateUrl("forkee", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this repo fork on GitHub"
>${event.payload.forkee.full_name}</a></strong>
2018-07-12 23:07:16 +02:00
`;
case "IssueCommentEvent":
if (event.payload.issue.pull_request) {
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> commented on pull request
<em><a
href="${generateUrl("issue", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this comment on GitHub"
>${event.payload.issue.title.escape()}</a></em> in
2018-07-12 23:07:16 +02:00
`;
} else {
return `
<strong>${event.actor.display_login}</strong> commented on issue
2018-09-28 21:09:45 +02:00
<em><a
href="${generateUrl("issue", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this comment on GitHub"
>${event.payload.issue.title.escape()}</a></em> in
2018-07-12 23:07:16 +02:00
`;
}
case "IssuesEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> ${event.payload.action} issue
<em><a
href="${generateUrl("issue", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this issue on GitHub"
>${event.payload.issue.title.escape()}</a></em> in
2018-07-12 23:07:16 +02:00
`;
case "PullRequestEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> pull request
<em><a
href="${generateUrl("pull_request", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this pull request on GitHub"
>${event.payload.pull_request.title.escape()}</a></em> in
2018-07-12 23:07:16 +02:00
`;
case "PullRequestReviewCommentEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> commented on pull request
<em><a
href="${generateUrl("pull_request", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this comment on GitHub"
>${event.payload.pull_request.title.escape()}</a></em> in
2018-07-12 23:07:16 +02:00
`;
case "PushEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> pushed to
<code><a
href="${generateUrl("push", event)}"
rel="noopener noreferrer"
target="_blank"
title="View this branch on GitHub"
>${refToBranch(event.payload.ref)}</a></code> in
2018-07-12 23:07:16 +02:00
`;
case "ReleaseEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></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
2018-07-12 23:07:16 +02:00
`;
case "WatchEvent":
return `
2018-09-28 21:09:45 +02:00
<strong><a
href="${generateUrl("actor", event)}"
rel="noopener noreferrer"
target="_blank"
title="Visit ${event.actor.login}'s profile on GitHub"
>${event.actor.display_login}</a></strong> starred the repo
2018-07-12 23:07:16 +02:00
`;
default:
break;
}
}
2018-09-27 18:11:26 +02:00
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) {
2018-09-28 21:09:45 +02:00
const repoName = `
<a href="${generateUrl("repo", event)}" title="View this repo on GitHub" target="_blank" rel="noopener noreferrer"><strong>${event.repo.name}</strong></a>
`;
2018-09-27 18:11:26 +02:00
renderedEvents.push(`
<div class='github-feed__event'>
<a href="${generateUrl("actor", event)}" target="_blank" rel="noopener noreferrer">
2018-09-28 21:09:45 +02:00
<img src="${event.actor.avatar_url}" class="github-feed__event__avatar" alt="${event.actor.login}'s avatar"/>
2018-09-27 18:11:26 +02:00
</a>
<p>
${generateEvent(event)}
2018-09-28 21:09:45 +02:00
${event.type !== "ForkEvent" ? repoName : ""}
2018-09-27 18:11:26 +02:00
<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>
2018-10-06 22:53:01 +02:00
<h5 class="last-updated">Last updated: ${new Date().format("YYYY-MM-DD")
.replace(/-/g, "&middot;")} at ${new Date().add(-4, "hours")
.format("UTC:H:mm:ss A")
.toLowerCase()} EST</h5>
2018-09-27 18:11:26 +02:00
${renderedEvents.join("")}
`);
});
}
}
2018-07-12 23:07:16 +02:00
function generateUrl(type, event) {
2018-09-28 21:09:45 +02:00
switch(type) {
2018-07-12 23:07:16 +02:00
case "actor":
return `https://github.com/${event.actor.display_login}`;
case "comment":
return event.payload.comment.html_url;
2018-09-28 21:09:45 +02:00
case "create":
return `https://github.com/${event.repo.name}/tree/${event.payload.ref}`;
2018-07-12 23:07:16 +02:00
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;
2018-09-28 21:09:45 +02:00
case "push":
return `https://github.com/${event.repo.name}/tree/${event.payload.ref.replace("refs/heads/", "")}`;
2018-07-12 23:07:16 +02:00
case "release":
return event.payload.release.html_url;
2018-09-28 21:09:45 +02:00
case "repo":
return `https://github.com/${event.repo.name}`;
2018-07-12 23:07:16 +02:00
default:
break;
}
}
2018-09-27 18:11:26 +02:00
function updateGithubFeed() {
2018-11-21 00:15:00 +01:00
octokit.activity.listPublicEventsForOrg({
2018-09-27 18:11:26 +02:00
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
2018-10-06 22:53:01 +02:00
})
.catch(err => {
2018-11-30 21:46:22 +01:00
messageSlack(
2018-10-06 22:53:01 +02:00
"\n" +
2018-10-06 22:59:46 +02:00
"> *GITHUB FEED ERROR:* ```" + JSON.parse(JSON.stringify(err)) + "```" + "\n" +
"> _Cause: GitHub feed refresh_\n"
2018-10-06 22:53:01 +02:00
);
});
2018-09-27 18:11:26 +02:00
}
2018-07-12 23:07:16 +02:00
// H E L P E R
function refToBranch(ref) {
2019-01-30 00:29:14 +01:00
if (ref)
return ref.replace("refs/heads/", "");
2018-07-12 23:07:16 +02:00
}
// E X P O R T S
2018-07-12 23:07:16 +02:00
2018-11-30 21:46:22 +01:00
export {
2018-07-12 23:07:16 +02:00
generateEvent,
2018-09-27 18:11:26 +02:00
generateGitHubFeed,
generateUrl,
updateGithubFeed
2018-07-12 23:07:16 +02:00
};