Skip to content

Commit a52ed85

Browse files
committed
github webhooks pull_request
1 parent d40591a commit a52ed85

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

docs/webhooks.md

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
API webhooks available data, which depends on the event that generated it.
44

5+
The intention behind custom mappings was to reduce the huge amount of data that GitHub generates,
6+
and retrieve only the one that fits our needs.<br>
7+
Most of the resulting mappings are just reduced versions of the [GitHub's API v3](https://developer.github.com/v3/).
8+
59
## GitHub
610

711
### issue_comment
@@ -61,6 +65,37 @@ Example output:
6165
}
6266
```
6367

68+
### pull_request
69+
70+
Type: `github`<br>
71+
Event: `pull_request`
72+
73+
Example output:
74+
75+
```
76+
"_id": "1234abcd",
77+
"type": "github",
78+
"uid": "1234-abcd-1234-abcd",
79+
"github": {
80+
"event": "pull_request",
81+
"action": "opened",
82+
"repository": "octocat/Hello_world",
83+
"pull_request": {
84+
"number": "1",
85+
"state": "open",
86+
"locked": false,
87+
"title": "Changes",
88+
"user": "octocat",
89+
"body": "A lot of changes",
90+
"merged": false,
91+
"commits": 2,
92+
"additions": 1000,
93+
"deletions": 0,
94+
"changed_files": 0
95+
}
96+
}
97+
```
98+
6499
### push
65100

66101
Type: `github`<br>

src/models/Webhook.js

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ const webhookSchema = new Schema({
2828
body: String, // 'i dun know'
2929
},
3030

31+
pull_request: {
32+
number: Number,
33+
state: String, // 'open'
34+
locked: Boolean,
35+
title: String,
36+
user: String, // 'octocat'
37+
body: String,
38+
merged: Boolean,
39+
commits: Number,
40+
additions: Number,
41+
deletions: Number,
42+
changed_files: Number,
43+
},
44+
3145
commits: [{
3246
id: String,
3347
tree_id: String,

src/services/webhook.js

+21
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ export function githubWebhook(event, uid, data) {
5050
};
5151
break;
5252
}
53+
case 'pull_request': {
54+
webhook.github = {
55+
event,
56+
action: data.action,
57+
repository: data.repository.full_name,
58+
pull_request: {
59+
number: data.pull_request.number,
60+
state: data.pull_request.state,
61+
locked: data.pull_request.locked,
62+
title: data.pull_request.title,
63+
user: data.pull_request.user.login,
64+
body: data.pull_request.body,
65+
merged: data.pull_request.merged,
66+
commits: data.pull_request.commits,
67+
additions: data.pull_request.additions,
68+
deletions: data.pull_request.deletions,
69+
changed_files: data.pull_request.changed_files,
70+
},
71+
};
72+
break;
73+
}
5374
case 'push': {
5475
webhook.github = {
5576
event,

test/api/webhook.js

+27
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ describe('Webhook', () => {
8686
});
8787
});
8888

89+
it('should create a Webhook entry if the request is valid (pull_request test)', (done) => {
90+
request
91+
.post(`${server}/webhooks/github`)
92+
.set('X-Forwarded-For', GITHUB_VALID_IP)
93+
.set('X-GitHub-Delivery', '1111')
94+
.set('X-GitHub-Event', 'pull_request')
95+
.send({
96+
repository: { full_name: 'github/repo' },
97+
pull_request: {
98+
number: 1,
99+
user: { login: 'octocat' },
100+
},
101+
})
102+
.end((err, res) => {
103+
expect(res.status).to.equal(200);
104+
expect(res.body).to.eql({ message: 'OK' });
105+
Webhook.findOne({}).exec().then((webhook) => {
106+
expect(webhook.type).to.equal('github');
107+
expect(webhook.uid).to.equal('1111');
108+
expect(webhook.github.repository).to.equal('github/repo');
109+
expect(webhook.github.pull_request.number).to.equal(1);
110+
expect(webhook.github.pull_request.user).to.equal('octocat');
111+
done();
112+
});
113+
});
114+
});
115+
89116
it('should create a Webhook entry if the request is valid (push test)', (done) => {
90117
request
91118
.post(`${server}/webhooks/github`)

0 commit comments

Comments
 (0)