forked from benwaffle/twithub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
54 lines (50 loc) · 1.72 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const twitter = require('./twit.js');
const githubToTwitter = {
'benwaffle': 'benwafflez',
'khayyamsaleem': 'KhayyamSaleem',
};
app.use(bodyParser.json());
app.post('/', (req, res) => {
let data = req.body;
let event = req.headers['x-github-event'];
if (event) {
let project = data.repository.name;
let user = data.sender.login;
if (event == "issues") {
let action = data.action;
let title = data.issue.title;
let url = data.issue.html_url;
let twitterUser = githubToTwitter[user];
let tweet = `@${twitterUser} ${project}: ${user} ${action} issue '${title}'\n${url}`;
console.log(`Tweeting: ${tweet}`);
twitter.tweet(tweet, (err) => {
if (err)
console.log(err);
});
} else if (event == "issue_comment") {
let title = data.issue.title;
let url = data.issue.html_url;
let comment = data.comment.body;
let twitterUser = githubToTwitter[user];
let tweet = `@${twitterUser} ${project}: ${user} commented on issue '${title}'\n${comment}\n${url}`;
console.log(`Tweeting: ${tweet}`);
twitter.tweet(tweet, (err) => {
if (err)
console.log(err);
});
} else {
console.log(`unknown github event: ${event}`);
}
} else {
console.log("Post request without github event");
}
console.log('\n');
res.send('ok');
});
app.listen(3000, () => {
console.log("listening on port 3000");
});