parse-github-payload
Advanced tools
Comparing version 0.0.1 to 0.1.0
35
index.js
@@ -18,5 +18,10 @@ var clone = require('clone'); | ||
* delete a file. In this case that file would not be included in | ||
* `_files.added`. | ||
* `_files.added`. This is most useful for webhooks that act on files added or | ||
* modified in push events. | ||
* @param {Object} payload Github webhook | ||
* [payload](https://developer.github.com/v3/activity/events/types/#pushevent) | ||
* @param {Object} [options] | ||
* @param {Regex} [options.matchName] only return files that match this regex. | ||
* @param {Regex} [options.ignoreCommit] ignore files submitted with a commit message | ||
* that matches this regex - useful for avoiding circular webhooks | ||
* @return {parsedPayload} a new parsedPayload with the `_files` | ||
@@ -34,4 +39,8 @@ * property. | ||
* removed. | ||
* | ||
* `_files.added_and_modified` array of files that have been added and | ||
* modified in the commits in a push event, excluding files that were added or | ||
* modified and subsequently deleted. | ||
*/ | ||
function parsePayload(payload) { | ||
function parsePayload(payload, options) { | ||
if (typeof payload != 'object') | ||
@@ -42,2 +51,12 @@ throw new TypeError('must provide a payload object'); | ||
options = options || {}; | ||
matchNameRe = options.matchName || new RegExp('.*'); | ||
ignoreCommitRe = options.ignoreCommit || new RegExp('a^'); | ||
if (!(matchNameRe instanceof RegExp)) | ||
throw new TypeError('options.matchName must be a Regular Expression'); | ||
if (!(ignoreCommitRe instanceof RegExp)) | ||
throw new TypeError('options.ignoreCommit must be a Regular Expression'); | ||
var parsedPayload = clone(payload); | ||
@@ -51,2 +70,3 @@ | ||
.reduce(function(previousCommit, currentCommit) { | ||
if (ignoreCommitRe.test(currentCommit.message)) return previousCommit; | ||
return previousCommit | ||
@@ -62,4 +82,4 @@ .concat(currentCommit.modified) | ||
.filter(function(value, i, arr) { | ||
// Remove duplicates | ||
return arr.indexOf(value) >= i; | ||
// Remove duplicates and only return files that match options.matchName | ||
return arr.indexOf(value) >= i && matchNameRe.test(value); | ||
}); | ||
@@ -75,2 +95,5 @@ | ||
parsedPayload._files[type] = payload.commits | ||
.filter(function(commit) { | ||
return !ignoreCommitRe.test(commit.message); | ||
}) | ||
.map(function(commit) { | ||
@@ -84,4 +107,4 @@ return commit[type]; | ||
.filter(function(value, i, arr) { | ||
// Remove duplicates | ||
return arr.indexOf(value) >= i; | ||
// Remove duplicates and only return files that match options.matchName | ||
return arr.indexOf(value) >= i && matchNameRe.test(value); | ||
}); | ||
@@ -88,0 +111,0 @@ }); |
{ | ||
"name": "parse-github-payload", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Parses a github push event payload adding a list of files added, modified and removed to the payload", | ||
@@ -23,3 +23,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"clone": "^0.2.0" | ||
"clone": "^0.2.0", | ||
"defaults": "^1.0.0" | ||
}, | ||
@@ -26,0 +27,0 @@ "repository": { |
@@ -8,3 +8,3 @@ # parse-github-payload | ||
### `parsePayload(payload)` | ||
### `parsePayload(payload, [options], [options.matchName], [options.ignoreCommit])` | ||
@@ -25,9 +25,13 @@ Parses a [Github webhook | ||
delete a file. In this case that file would not be included in | ||
`_files.added`. | ||
`_files.added`. This is most useful for webhooks that act on files added or | ||
modified in push events. | ||
### Parameters | ||
| parameter | type | description | | ||
| --------- | ------ | ------------------------------------------------------------------------------------------ | | ||
| `payload` | Object | Github webhook [payload](https://developer.github.com/v3/activity/events/types/#pushevent) | | ||
| parameter | type | description | | ||
| ------------------------ | ------ | ------------------------------------------------------------------------------------------------------------------------ | | ||
| `payload` | Object | Github webhook [payload](https://developer.github.com/v3/activity/events/types/#pushevent) | | ||
| `[options]` | Object | _optional:_ | | ||
| `[options.matchName]` | Regex | _optional:_ only return files that match this regex. | | ||
| `[options.ignoreCommit]` | Regex | _optional:_ ignore files submitted with a commit message that matches this regex - useful for avoiding circular webhooks | | ||
@@ -48,2 +52,6 @@ | ||
`_files.added_and_modified` array of files that have been added and | ||
modified in the commits in a push event, excluding files that were added or | ||
modified and subsequently deleted. | ||
## Installation | ||
@@ -50,0 +58,0 @@ |
48
test.js
@@ -90,1 +90,49 @@ var test = require('tape'); | ||
}); | ||
test('Only files that match options.matchName are returned', function(t) { | ||
var payload = { | ||
commits: [{ | ||
added: ['a.js'], | ||
modified: ['b.xml'], | ||
removed: ['c.js'], | ||
},{ | ||
added: ['d.md'], | ||
modified: ['e.js'], | ||
removed: ['f.txt'] | ||
}] | ||
}; | ||
var parsed = parse(payload, { matchName: /.*\.js$/ }); | ||
t.plan(4); | ||
t.deepEqual(parsed._files.added_and_modified.sort(), ['a.js', 'e.js'], 'added_and_modified'); | ||
t.deepEqual(parsed._files.added.sort(), ['a.js'], 'added'); | ||
t.deepEqual(parsed._files.modified.sort(), ['e.js'], 'modified'); | ||
t.deepEqual(parsed._files.removed.sort(), ['c.js'], 'removed'); | ||
}); | ||
test('Only files commits that do not match options.ignoreCommit are returned', function(t) { | ||
var payload = { | ||
commits: [{ | ||
message: '[WEBHOOK] commit by automated webhook', | ||
added: ['a.js'], | ||
modified: ['b.xml'], | ||
removed: ['c.js'], | ||
},{ | ||
message: 'A human commit', | ||
added: ['d.md'], | ||
modified: ['e.js'], | ||
removed: ['f.txt'] | ||
}] | ||
}; | ||
var parsed = parse(payload, { ignoreCommit: /^\[WEBHOOK\]/ }); | ||
t.plan(4); | ||
t.deepEqual(parsed._files.added_and_modified.sort(), ['d.md', 'e.js'], 'added_and_modified'); | ||
t.deepEqual(parsed._files.added.sort(), ['d.md'], 'added'); | ||
t.deepEqual(parsed._files.modified.sort(), ['e.js'], 'modified'); | ||
t.deepEqual(parsed._files.removed.sort(), ['f.txt'], 'removed'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11999
208
69
2
+ Addeddefaults@^1.0.0
+ Addedclone@1.0.4(transitive)
+ Addeddefaults@1.0.4(transitive)