Skip to content
This repository was archived by the owner on Aug 6, 2020. It is now read-only.

Commit 953c27f

Browse files
committed
Refactor
1 parent 18c7b96 commit 953c27f

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

index.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,38 @@ const fs = require('fs')
33
// exports
44
module.exports = app => {
55

6-
app.log('The Rusty Git loaded successfully!');
7-
86
app.on('pull_request', async context => {
9-
// Auto invite user
10-
sendInvite(context, app)
7+
checkMembership(context, app)
118
});
129

13-
// Get an express router to expose new HTTP endpoints
10+
// Create a log endpoint
1411
const router = app.route('/')
15-
16-
// Use any middleware
1712
router.use(require('express').static('public'))
18-
19-
// Add a new route
2013
router.get('/logs', (req, res) => {
21-
// res.sendFile('./log.txt');
2214
res.sendFile('log.txt', { root: __dirname });
2315
})
2416
}
2517

26-
// Send user an invite to the org
27-
const sendInvite = (context, app) => {
28-
const details = { username: context.payload.pull_request.user.login, org: context.payload.organization.login, time: context.payload.pull_request.created_at };
18+
checkMembership = (context, app) => {
19+
const details = { username: context.payload.pull_request.user.login, org: context.payload.organization.login};
20+
const time = context.payload.pull_request.created_at;
2921
context.github.orgs.getMembership(details)
30-
.then(res => app.log.error(`${res.data.user.login}'s membership is: ${res.data.state}, holding the role: ${res.data.role}`))
31-
.then(saveLog(details.time, `${res.data.user.login}'s membership is: ${res.data.state}, holding the role: ${res.data.role}`))
22+
.then(res => createLog(time, `${res.data.user.login}'s membership is: ${res.data.state}, holding the role: ${res.data.role}`))
23+
3224
.catch((err) => {
33-
app.log.warn(`${details.username} is not a member, lets send them an invite!`)
34-
saveLog(details.time, `${details.username} is not a member, lets send them an invite!`)
25+
createLog(time, `Sent an invitation to ${details.username}!`)
3526
context.github.orgs.addOrUpdateMembership(details);
3627
const issueComment = context.issue({ body: 'Congrats on making your first Pull Request in the Zero To Mastery Organization! You have been sent an invitation to join the organization, please check your emails' });
3728
return context.github.issues.createComment(issueComment);
3829
});
39-
};
30+
}
4031

41-
saveLog = (time, message) => {
42-
const text = time + message + '\r\n';
32+
createLog = (time, message) => {
33+
// Log to heroku console
34+
app.log.error(message);
4335

36+
// Log to text file
37+
const text = time + message + '\r\n'
4438
fs.appendFile('log.txt', text, function (err) {
4539
if (err) return console.log(err);
4640
console.log('successfully logged "' + text + '"');

0 commit comments

Comments
 (0)