Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link gh issues to v1 part 2 #9

Merged
merged 10 commits into from Mar 3, 2019
2 changes: 2 additions & 0 deletions .eslintignore
Expand Up @@ -2,3 +2,5 @@
/node_modules
**/node_modules
/scripts
tests
testing
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
"eslint-plugin-prettier": "^3.0.1",
"jest": "^24.1.0",
"jest-cli": "^24.1.0",
"jest-when": "^2.3.1",
"lerna": "^3.13.1",
"node-fetch": "^2.3.0",
"prettier": "^1.16.4",
Expand Down
15 changes: 11 additions & 4 deletions packages/azure-functions/tests/webhooked.test.js
Expand Up @@ -10,12 +10,9 @@ beforeEach(() => {
handle,
});
});

test('configures webhooked and handles request', async () => {
const request = {
headers: {
'x-github-event': 'issues',
},
headers: {},
body: { some: 'json' },
};

Expand All @@ -25,3 +22,13 @@ test('configures webhooked and handles request', async () => {
expect(handle).toBeCalledWith(request);
expect(context.res.status).toEqual(200);
});

test('responds with a 500 status on error', async () => {
const request = {};
webhooked.mockImplementation(() => {
throw new Error('failure');
});
await httpFunction(context, request);

expect(context.res.status).toEqual(500);
});
8 changes: 3 additions & 5 deletions packages/webhooked-github-request-matchers/src/index.js
@@ -1,9 +1,7 @@
const isRequestFromGithub = req => {
return Boolean(req.headers['x-github-event']);
};
const isRequestFromGithub = require('./isRequestFromGithub');

const matchesActions = (req, actions = []) => {
if (!isRequestFromGithub(req)) {
const matchesActions = (req, actions = [], key = '') => {
if (!isRequestFromGithub(req, key)) {
return false;
}
return Boolean(actions.find(action => req.body.action === action));
Expand Down
@@ -0,0 +1,7 @@
const { createHmac } = require('crypto');

module.exports = (req, hmacKey) =>
req.headers['x-hub-signature'] ===
`sha1=${createHmac('sha1', hmacKey)
.update(JSON.stringify(req.body))
.digest('hex')}`;
@@ -1,15 +1,24 @@
const { createHmac } = require('crypto');
const { isRequestFromGithub } = require('../src');

test('can determine whether a request is from a github API event', () => {
const key = 'hmacKey';
const body = {
some: 'payload',
};
const ghRequest = {
headers: {
'x-github-event': 'issues',
'x-hub-signature': `sha1=${createHmac('sha1', key)
.update(JSON.stringify(body))
.digest('hex')}`,
},
body,
};
expect(isRequestFromGithub(ghRequest)).toBeTruthy();
expect(isRequestFromGithub(ghRequest, key)).toBeTruthy();

const nonGHRequest = {
headers: {},
body,
};
expect(isRequestFromGithub(nonGHRequest)).toBeFalsy();
expect(isRequestFromGithub(nonGHRequest, key)).toBeFalsy();
});
@@ -1,22 +1,39 @@
jest.mock('../src/isRequestFromGithub');
const { when } = require('jest-when');
const { matchesActions } = require('../src');
const isRequestFromGithub = require('../src/isRequestFromGithub');
const key = 'key';

test('returns false if the request is not a github api event', () => {
const nonGHRequest = {
headers: {},
};
when(isRequestFromGithub)
.calledWith(nonGHRequest, key)
.mockReturnValue(false);

expect(matchesActions(nonGHRequest)).toBeFalsy();
});

test('returns false if not actions are provided', () => {
expect(matchesActions(createGHRequest())).toBeFalsy();
test('returns false if no actions are provided', () => {
when(isRequestFromGithub)
.calledWith(createGHRequest(), key)
.mockReturnValue(true);
expect(matchesActions(createGHRequest(), key)).toBeFalsy();
});
test('returns false if the action is not matched', () => {
expect(matchesActions(createGHRequest(), ['created'])).toBeFalsy();
when(isRequestFromGithub)
.calledWith(createGHRequest(), key)
.mockReturnValue(true);
expect(matchesActions(createGHRequest(), ['created'], key)).toBeFalsy();
});

test('returns true if the request matches any one of the provided actions', () => {
when(isRequestFromGithub)
.calledWith(createGHRequest(), key)
.mockReturnValue(true);
expect(
matchesActions(createGHRequest(), ['created', 'labeled']),
matchesActions(createGHRequest(), ['created', 'labeled'], key),
).toBeTruthy();
});

Expand Down
38 changes: 0 additions & 38 deletions packages/webhooked-plugin-gh-issue-labeled-to-v1/src/index.js

This file was deleted.

This file was deleted.