|
| 1 | +module.exports = (req, res) => { |
| 2 | + const pullRequest = req.body.pull_request |
| 3 | + if (shouldHandlePullRequestChange(req)) { |
| 4 | + request(pullRequest.diff_url, (error, response, body) => { |
| 5 | + logStatusAndErrors(error, response) |
| 6 | + if (isChangeInContributorsFile(body)) { |
| 7 | + const options = getPostRequestOptions(pullRequest) |
| 8 | + request.put(options, (error, response, body) => { |
| 9 | + logStatusAndErrors(error, response) |
| 10 | + options.url = `${pullRequest.issue_url}/comments` |
| 11 | + if (response.statusCode === 200) { |
| 12 | + request.post(options, (error, response, body) => { |
| 13 | + logStatusAndErrors(error, response) |
| 14 | + }) |
| 15 | + } |
| 16 | + }) |
| 17 | + } |
| 18 | + }) |
| 19 | + } |
| 20 | + res.json({ message: 'Awesome' }) |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +const shouldHandlePullRequestChange = req => |
| 26 | + req.body.action === 'opened' && isSingleLineChange(req.body.pull_request) |
| 27 | + |
| 28 | +const isChangeInContributorsFile = diff => |
| 29 | + (diff.match(/Contributors\.md/g) || []).length === 4 |
| 30 | + |
| 31 | +const isSingleLineChange = pullRequest => |
| 32 | + (pullRequest.additions === 1 || pullRequest.additions === 2) && |
| 33 | + pullRequest.additions - pullRequest.deletions === 1 && |
| 34 | + pullRequest.changed_files === 1 |
| 35 | + |
| 36 | +const getPostRequestOptions = pullRequest => ({ |
| 37 | + url: `${pullRequest.url}/merge`, |
| 38 | + json: { |
| 39 | + body: getMergeMessage(pullRequest.user.login) |
| 40 | + }, |
| 41 | + headers: { |
| 42 | + Authorization: process.env.GITHUB_SECRET, |
| 43 | + 'User-Agent': 'request' |
| 44 | + } |
| 45 | +}) |
| 46 | + |
| 47 | +const getMergeMessage = userName => `Hi @${userName}, I'm quite elated about your pull request. |
| 48 | + I wanna evolve this project to addresses various problems faced by first-time contributors. |
| 49 | + I'd love to learn about your journey in open source community, the problems, pain points you had etc. |
| 50 | + could you explain how you felt when you went through the tutorial, made a pull request and learned that i merged it? |
| 51 | +
|
| 52 | + We’ve recently added social share to our web app. |
| 53 | + Could you please go to https://firstcontributions.github.ios/#social-share |
| 54 | + and share your first contribution to open source? Also, check out projects with easy issues while you’re there.` |
| 55 | + |
| 56 | +const logStatusAndErrors = (error, response) => { |
| 57 | + console.log('error:', error) // Print the error if one occurred |
| 58 | + console.log('statusCode:', response && response.statusCode) // Print the response status code if a response was received |
| 59 | +} |
0 commit comments