|
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 |
| - }) |
| 1 | +const request = require('request') |
| 2 | + |
| 3 | +exports.handleSubmit = (req, res) => { |
| 4 | + const pullRequest = req.body.pull_request |
| 5 | + if (shouldHandlePullRequestChange(req)) { |
| 6 | + const result = handlePuRequest(pullRequest) |
| 7 | + res.json({ message: result }) |
| 8 | + } |
| 9 | + res.json({ message: "Can't handle this pull request" }) |
| 10 | +} |
| 11 | + |
| 12 | +const handlePuRequest = (pullRequest) => { |
| 13 | + request(pullRequest.diff_url, (error, response, body) => { |
| 14 | + logStatusAndErrors(error, response) |
| 15 | + if (isChangeInContributorsFile(body)) { |
| 16 | + return mergePullRequest(pullRequest) |
19 | 17 | }
|
20 |
| - res.json({ message: 'Awesome' }) |
| 18 | + |
| 19 | + return `diff check failed ${error}` |
| 20 | + }) |
21 | 21 | }
|
22 | 22 |
|
| 23 | +const mergePullRequest = (pullRequest) => { |
| 24 | + const options = getPostRequestOptions(pullRequest.user.login, `${pullRequest.url}/merge`) |
| 25 | + request.put(options, (error, response, body) => { |
| 26 | + logStatusAndErrors(error, response) |
| 27 | + if (response.statusCode === 200) { |
| 28 | + return postComment(pullRequest) |
| 29 | + } |
| 30 | + return `Merge failed ${error}` |
| 31 | + }) |
| 32 | +} |
23 | 33 |
|
| 34 | +const postComment = (pullRequest) => { |
| 35 | + const options = getPostRequestOptions(pullRequest.user.login, `${pullRequest.issue_url}/comments`) |
| 36 | + request.post(options, (error, response, body) => { |
| 37 | + logStatusAndErrors(error, response) |
| 38 | + return !error ? 'Awesome': `Commenting failed ${error}` |
| 39 | + }) |
| 40 | +} |
24 | 41 |
|
25 | 42 | const shouldHandlePullRequestChange = req =>
|
26 |
| - req.body.action === 'opened' && isSingleLineChange(req.body.pull_request) |
| 43 | + req.body.action === 'opened' && isSingleLineChange(req.body.pull_request) |
27 | 44 |
|
28 | 45 | const isChangeInContributorsFile = diff =>
|
29 |
| - (diff.match(/Contributors\.md/g) || []).length === 4 |
| 46 | + (diff.match(/Contributors\.md/g) || []).length === 4 |
30 | 47 |
|
31 | 48 | 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 |
| - } |
| 49 | + (pullRequest.additions === 1 || pullRequest.additions === 2 && |
| 50 | + pullRequest.additions - pullRequest.deletions === 1 ) && |
| 51 | + pullRequest.changed_files === 1 |
| 52 | + |
| 53 | +const getPostRequestOptions = (user, url) => ({ |
| 54 | + url: url, |
| 55 | + json: { |
| 56 | + body: getMergeMessage(user) |
| 57 | + }, |
| 58 | + headers: { |
| 59 | + 'Authorization': `token ${process.env.GITHUB_SECRET}`, |
| 60 | + 'User-Agent': 'request' |
| 61 | + } |
45 | 62 | })
|
46 | 63 |
|
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? |
| 64 | +const getMergeMessage = username => `Hello @${username}, congratulations! You've successfully submitted a pull request. 🎉 |
| 65 | + **Next steps** |
| 66 | + - Continue contributing: If you're looking for projects to contribute to, checkout our [webapp](https://firstcontributions.github.io). |
| 67 | + - Join our slack group: We have a community to help/support contributors. [Join slack group](https://join.slack.com/t/firstcontributors/shared_invite/enQtNjkxNzQwNzA2MTMwLTVhMWJjNjg2ODRlNWZhNjIzYjgwNDIyZWYwZjhjYTQ4OTBjMWM0MmFhZDUxNzBiYzczMGNiYzcxNjkzZDZlMDM). |
| 68 | + - Share on social media: You can share this content to help more people. [Share](https://firstcontributions.github.io/#social-share). |
51 | 69 |
|
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.` |
| 70 | +We'd love to hear your thoughts about this project. Let us know how we can improve my commenting or opening an issue here.`; |
55 | 71 |
|
56 | 72 | 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 |
| 73 | + console.error('error:', error) // Print the error if one occurred |
| 74 | + console.error('statusCode:', response && response.statusCode) // Print the response status code if a response was received |
59 | 75 | }
|
| 76 | + |
0 commit comments