Github feed component

This commit is contained in:
Kristian Polso 2018-05-12 13:03:21 +03:00
parent 07a7609502
commit badf059aec
5 changed files with 215 additions and 0 deletions

View file

@ -0,0 +1,112 @@
<template>
<div id="github-feed">
<div class="last-updated" v-on:click="updateFeed">Last updated: {{ formatDate(lastUpdated) }}</div>
<div v-for="event in events" class="event">
<a v-bind:href="event.actor.url" target="_blank">
<img v-bind:src="event.actor.avatar_url" class="avatar"> {{ event.actor.display_login }}
</a>
<template v-if="event.type == 'CommitCommentEvent'">
commented on <a v-bind:href="event.payload.comment.url">commit</a>
</template>
<template v-else-if="event.type == 'CreateEvent'">
created {{ event.payload.ref_type }} '{{ event.payload.ref }}'
</template>
<template v-else-if="event.type == 'DeleteEvent'">
deleted {{ event.payload.ref_type }} '{{ event.payload.ref }}'
</template>
<template v-else-if="event.type == 'ForkEvent'">
forked <a v-bind:href="event.repo.url" target="_blank">{{ event.repo.name }}</a> to <a v-bind:href="event.payload.forkee.url" target="_blank">{{ event.payload.forkee.name }}</a>
</template>
<template v-else-if="event.type == 'IssueCommentEvent'">
commented on <template v-if="event.payload.issue.pull_request">pull request</template><template v-else>issue</template> <a v-bind:href="event.payload.issue.url" target="_blank">{{ event.payload.issue.title }}</a>
</template>
<template v-else-if="event.type == 'IssuesEvent'">
{{ event.payload.action }} issue <a v-bind:href="event.payload.issue.url" target="_blank">{{ event.payload.issue.title }}</a>
</template>
<template v-else-if="event.type == 'PullRequestEvent'">
{{ event.payload.action }} pull request <a v-bind:href="event.payload.pull_request.url" target="_blank">{{ event.payload.pull_request.title }}</a>
</template>
<template v-else-if="event.type == 'PushEvent'">
pushed to <span v-html="refToLink(event.payload.ref, event.repo.name)"></span>
</template>
<template v-else-if="event.type == 'ReleaseEvent'">
released <a v-bind:href="event.payload.release.url" target="_blank">{{ event.payload.release.tag_name }}</a>
</template>
<template v-else-if="event.type == 'WatchEvent'">
starred the repo
</template>
in <a v-bind:href="event.repo.url" target="_blank">{{ event.repo.name }}</a>
</div>
</div>
</template>
<script>
export default {
data () {
return {
events: [],
updateInterval: {},
lastUpdated: new Date()
}
},
name: 'GithubFeed',
mounted () {
this.updateFeed();
this.updateInterval = setInterval(this.updateFeed, 60*1000);
},
methods: {
updateFeed () {
var component = this;
component.$http.get('//beta.lbry.tech/github-feed').then(function(response) {
component.events = response.body;
});
component.lastUpdated = new Date();
},
refToLink (ref, repo) {
return "<a target='_blank' href='https://github.com/" + repo + "/tree/" + ref.replace('refs/heads/','') + "'>" + ref.replace('refs/heads/','') + "</a>";
},
formatDate (date) {
return date.toLocaleString('en-US');
}
}
};
</script>
<style lang="scss">
#github-feed {
.last-updated {
text-align: right;
color: #777;
font-style: italic;
}
.event {
padding: 0.5rem 0;
border-bottom: 1px solid #ccc;
}
.avatar {
width: 2rem;
height: 2rem;
border-radius: 1rem;
vertical-align: middle;
margin-right: 0.5rem;
}
}
</style>

5
github-feed.md Normal file
View file

@ -0,0 +1,5 @@
---
title: Github Feed Example
---
<GithubFeed></GithubFeed>

View file

@ -10,7 +10,9 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@octokit/rest": "^15.4.0", "@octokit/rest": "^15.4.0",
"async": "^2.6.0",
"cors": "^2.8.4", "cors": "^2.8.4",
"cron": "^1.3.0",
"dotenv": "^5.0.1", "dotenv": "^5.0.1",
"express": "^4.16.3", "express": "^4.16.3",
"heroku-ssl-redirect": "0.0.4", "heroku-ssl-redirect": "0.0.4",

View file

@ -1,11 +1,23 @@
// server.js // server.js
// env variables
require('dotenv').config();
// Async
var async = require("async");
// Express etc
var sslRedirect = require('heroku-ssl-redirect'); var sslRedirect = require('heroku-ssl-redirect');
var express = require('express'); var express = require('express');
var path = require('path'); var path = require('path');
var serveStatic = require('serve-static'); var serveStatic = require('serve-static');
var request = require('request'); var request = require('request');
var cors = require('cors'); var cors = require('cors');
// Cron
var CronJob = require('cron').CronJob;
// Github API
var octokit = require('@octokit/rest')();
// Redis
var redis = require("redis"),
redisClient = redis.createClient(process.env.REDISCLOUD_URL);
app = express(); app = express();
app.use(serveStatic(__dirname + "/.vuepress/dist")); app.use(serveStatic(__dirname + "/.vuepress/dist"));
@ -39,7 +51,65 @@ app.get('/forward', function(req, res) {
}); });
app.get('/github-feed', function(req, res) {
redisClient.zrevrange('events', 0, 9, function(err, reply) {
var events = [];
reply.forEach(function(item) {
events.push(JSON.parse(item));
});
res.json(events);
});
});
var port = process.env.PORT || 8080; var port = process.env.PORT || 8080;
app.listen(port); app.listen(port);
console.log('server started '+ port); console.log('server started '+ port);
function updateGithubFeed() {
octokit.activity.getEventsForOrg({
org: 'lbryio',
per_page: 20,
page: 1
}).then(function({data}) {
async.eachSeries(data, function(item, callback) {
var eventString = JSON.stringify(item);
redisClient.zrank('events', eventString, function(err, reply) {
if(reply == null) {
redisClient.zadd('events', item.id, eventString, callback);
} else {
callback();
}
});
}, function() {
// Keep the latest 50 events
redisClient.zremrangebyrank('events', 0, -51);
console.log('Updated Github feed');
});
});
}
// Update Github feed every minute
new CronJob("0 * * * * *", updateGithubFeed, null, true, 'America/Los_Angeles');

View file

@ -297,6 +297,12 @@ async@^1.5.2:
version "1.5.2" version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
async@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4"
dependencies:
lodash "^4.14.0"
asynckit@^0.4.0: asynckit@^0.4.0:
version "0.4.0" version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@ -1114,6 +1120,12 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4:
safe-buffer "^5.0.1" safe-buffer "^5.0.1"
sha.js "^2.4.8" sha.js "^2.4.8"
cron@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/cron/-/cron-1.3.0.tgz#7e459968eaf94e1a445be796ce402166c234659d"
dependencies:
moment-timezone "^0.5.x"
cross-spawn@^3.0.0: cross-spawn@^3.0.0:
version "3.0.1" version "3.0.1"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-3.0.1.tgz#1256037ecb9f0c5f79e3d6ef135e30770184b982" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-3.0.1.tgz#1256037ecb9f0c5f79e3d6ef135e30770184b982"
@ -3156,6 +3168,10 @@ lodash@^4.0.0, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@~4.17.4:
version "4.17.5" version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
lodash@^4.14.0:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
log-symbols@^2.1.0: log-symbols@^2.1.0:
version "2.2.0" version "2.2.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a" resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a"
@ -3470,6 +3486,16 @@ mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkd
dependencies: dependencies:
minimist "0.0.8" minimist "0.0.8"
moment-timezone@^0.5.x:
version "0.5.17"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.17.tgz#3c8fef32051d84c3af174d91dc52977dcb0ad7e5"
dependencies:
moment ">= 2.9.0"
"moment@>= 2.9.0":
version "2.22.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad"
move-concurrently@^1.0.1: move-concurrently@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"