-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (86 loc) · 2.83 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{
const dotenv = require('dotenv');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.json());
if(process.env.NODE_ENV !== 'production')
dotenv.load();
const dsmGithub = require('./dsm-github');
const slack = require('./slack');
_postNewEventsToSlack = (req) => {
dsmGithub.event.getNewEntries(req.body.before, req.body.after)
.then((newEvents) => {
if(newEvents.length > 0) {
slack.postNewEventsToSlack(newEvents);
}
})
.catch((err) => {
console.log(`An error occurred posting events to Slack: ${err}`);
});
};
_postNewJobsToSlack = (req) => {
dsmGithub.job.getNewEntries(req.body.before, req.body.after)
.then((newJobs) => {
if(newJobs.length > 0) {
slack.postNewJobsToSlack(newJobs);
}
})
.catch((err) => {
console.log(`An error occurred posting jobs to Slack: ${err}`);
});
};
_handleEventsSlashCommand = () => {
return new Promise((fulfill, reject) => {
dsmGithub.event.getLatestEntries()
.then((events) => {
fulfill({
text: "Events",
attachments: events.map((event) => slack.buildEventAttachment(event))
});
});
});
};
_handleJobsSlashCommand = () => {
return new Promise((fulfill, reject) => {
dsmGithub.job.getLatestEntries()
.then((jobs) => {
fulfill({
text: "Jobs",
attachments: jobs.map((job) => slack.buildJobAttachment(job))
});
});
});
};
app.post('/', (req, res) => {
if(!req.body || !req.body.before || !req.body.after) {
res.send('Request is missing body.before and/or body.after commit hashes');
return;
}
_postNewEventsToSlack(req);
_postNewJobsToSlack(req);
res.end();
});
app.get('/events', (req, res) => {
_handleEventsSlashCommand()
.then((response) => {
res.send(response);
})
.catch((err) => {
res.send('Oops. Check http://dsmwebcollective.com/events for the current list of events.');
});
});
app.get('/jobs', (req, res) => {
_handleJobsSlashCommand()
.then((response) => {
res.send(response);
})
.catch((err) => {
res.send('Oops. Check http://dsmwebcollective.com/jobs for the current list of jobs.');
});
});
const port = process.argv[2] || process.env.PORT || 8000;
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
}