├── .gitignore ├── test ├── mocha.opts └── basics.js ├── package.json ├── LICENSE ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter spec 2 | --globals cached 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-hook-simplify", 3 | "version": "0.1.0", 4 | "license": "MIT", 5 | "dependencies": {}, 6 | "main": "./index.js", 7 | "devDependencies": { 8 | "expect.js": "^0.3.1", 9 | "mocha": "^2.2.5" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Robin Berjon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # github-hook-simplify 3 | 4 | GitHub is great in that it supports hooks that provide information about a *lot* of things. These 5 | events are very detailed, and de-normalised. 6 | 7 | This is really useful if you need to process them immediately, but it becomes less interesting if 8 | you need to store them. Not only is it a fair bit more data to store, but the de-normalised parts 9 | are likely to have become wrong in the mean time. 10 | 11 | What this library does is that it simplifies and trims down the payload for GitHub hook data so as 12 | to, for instance, replace the user details with just the user name or a full issue description with 13 | just the issue number. 14 | 15 | ## Install 16 | 17 | The usual: 18 | 19 | npm install --save github-hook-simplify 20 | 21 | ## Usage 22 | 23 | It's pretty simple: 24 | 25 | var ghs = require("github-hook-simplify"); 26 | ghs("event-name", eventPayload); 27 | 28 | The first parameter is the event type (the same string provided in the GitHub API), and the second 29 | is the payload. 30 | 31 | The payload is modified in place, so be careful if you want to keep the original around; you will 32 | need to clone it. It is also returned so that one can chain. 33 | 34 | The module will also detect if it's dealing with an already-simplified payload and handle that 35 | gracefully. You therefore don't need to be careful about how many times you apply simplification. 36 | (This is notably useful if the module gets an update in the future such that it supports more 37 | simplifications. You will be able to re-simplify what you already have without breaking it.) 38 | 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | function isStr (str) { 3 | return typeof str === "string" || typeof str === "number"; 4 | } 5 | 6 | // This simplifies the payload in place, taking the event type into account 7 | // It is idempotent in that if you run it twice by mistake it should produce the same result 8 | module.exports = function (event, pl) { 9 | // have the event sender be just the username instead of the full user 10 | if (pl.sender && !isStr(pl.sender)) pl.sender = pl.sender.login; 11 | 12 | // the source organisation is just the origanisation name 13 | if (pl.organization && !isStr(pl.organization)) pl.organization = pl.organization.login; 14 | 15 | // if it's a repository event, simplify its owner; otherwise convert the repository to just its 16 | // name 17 | if (event === "repository") { 18 | if (!isStr(pl.repository.owner)) pl.repository.owner = pl.repository.owner.login; 19 | } 20 | else { 21 | if (pl.repository && !isStr(pl.repository)) pl.repository = pl.repository.full_name; 22 | } 23 | 24 | // simplify forkee owner to just the username 25 | if (pl.forkee && !isStr(pl.forkee.owner)) pl.forkee.owner = pl.forkee.owner.login; 26 | 27 | // pull requests have their own level of extra depth 28 | if (pl.pull_request) { 29 | var pr = pl.pull_request; 30 | // simplify the user to just the username 31 | if (pr.user && !isStr(pr.user)) pr.user = pr.user.login; 32 | 33 | // simplify the assignee to just the username 34 | if (pr.assignee && !isStr(pr.assignee)) pr.assignee = pr.assignee.login; 35 | 36 | // for both head and base, simplify user to username and repo to repo name 37 | if (pr.head) { 38 | if (!isStr(pr.head.user)) pr.head.user = pr.head.user.login; 39 | if (!isStr(pr.head.repo)) pr.head.repo = pr.head.repo.full_name; 40 | } 41 | if (pr.base) { 42 | if (!isStr(pr.base.user)) pr.base.user = pr.base.user.login; 43 | if (!isStr(pr.base.repo)) pr.base.repo = pr.base.repo.full_name; 44 | } 45 | } 46 | 47 | // simplify issue reporter to username 48 | if (pl.issue && pl.issue.user && !isStr(pl.issue.user)) pl.issue.user = pl.issue.user.login; 49 | 50 | // simplify commenter to username 51 | if (pl.comment && !isStr(pl.comment.user)) pl.comment.user = pl.comment.user.login; 52 | 53 | // simplify issue to number in issue comments 54 | if (event === "issue_comment" && !isStr(pl.issue)) pl.issue = pl.issue.number; 55 | 56 | // the data is changed in place, but we return it for chainability 57 | return pl; 58 | }; 59 | -------------------------------------------------------------------------------- /test/basics.js: -------------------------------------------------------------------------------- 1 | /*jshint -W079 */ 2 | 3 | var ghs = require("..") 4 | , expect = require("expect.js") 5 | ; 6 | 7 | describe("Simplify", function () { 8 | it("should simplify sender", function () { 9 | expect(ghs("test", { sender: { login: "ok" }}).sender).to.equal("ok"); 10 | expect(ghs("test", { sender: "ok" }).sender).to.equal("ok"); 11 | }); 12 | it("should simplify organization", function () { 13 | expect(ghs("test", { organization: { login: "ok" }}).organization).to.equal("ok"); 14 | expect(ghs("test", { organization: "ok" }).organization).to.equal("ok"); 15 | }); 16 | it("should simplify repository owner", function () { 17 | expect(ghs("repository", { repository: { owner: { login: "ok" }}}).repository.owner).to.equal("ok"); 18 | expect(ghs("repository", { repository: { owner: "ok"}}).repository.owner).to.equal("ok"); 19 | }); 20 | it("should simplify repository", function () { 21 | expect(ghs("test", { repository: { full_name: "ok" }}).repository).to.equal("ok"); 22 | expect(ghs("test", { repository: "ok" }).repository).to.equal("ok"); 23 | }); 24 | it("should simplify forkee.owner", function () { 25 | expect(ghs("test", { forkee: { owner: { login: "ok" }}}).forkee.owner).to.equal("ok"); 26 | expect(ghs("test", { forkee: { owner: "ok" }}).forkee.owner).to.equal("ok"); 27 | }); 28 | it("should simplify pull_request user", function () { 29 | expect(ghs("test", { pull_request: { user: { login: "ok" }}}).pull_request.user).to.equal("ok"); 30 | expect(ghs("test", { pull_request: { user: "ok" }}).pull_request.user).to.equal("ok"); 31 | }); 32 | it("should simplify pull_request assignee", function () { 33 | expect(ghs("test", { pull_request: { assignee: { login: "ok" }}}).pull_request.assignee).to.equal("ok"); 34 | expect(ghs("test", { pull_request: { assignee: "ok" }}).pull_request.assignee).to.equal("ok"); 35 | }); 36 | it("should simplify pull_request head.{user,repo}", function () { 37 | expect(ghs("test", { pull_request: { head: { user: { login: "ok" }, repo: { full_name: "ok" } }}}).pull_request.head.user).to.equal("ok"); 38 | expect(ghs("test", { pull_request: { head: { user: { login: "ok" }, repo: { full_name: "ok" } }}}).pull_request.head.repo).to.equal("ok"); 39 | expect(ghs("test", { pull_request: { head: { user: "ok", repo: "ok" }}}).pull_request.head.user).to.equal("ok"); 40 | expect(ghs("test", { pull_request: { head: { user: "ok", repo: "ok" }}}).pull_request.head.repo).to.equal("ok"); 41 | }); 42 | it("should simplify pull_request base.{user,repo}", function () { 43 | expect(ghs("test", { pull_request: { base: { user: { login: "ok" }, repo: { full_name: "ok" } }}}).pull_request.base.user).to.equal("ok"); 44 | expect(ghs("test", { pull_request: { base: { user: { login: "ok" }, repo: { full_name: "ok" } }}}).pull_request.base.repo).to.equal("ok"); 45 | expect(ghs("test", { pull_request: { base: { user: "ok", repo: "ok" }}}).pull_request.base.user).to.equal("ok"); 46 | expect(ghs("test", { pull_request: { base: { user: "ok", repo: "ok" }}}).pull_request.base.repo).to.equal("ok"); 47 | }); 48 | it("should simplify issue user", function () { 49 | expect(ghs("test", { issue: { user: { login: "ok" }}}).issue.user).to.equal("ok"); 50 | expect(ghs("test", { issue: { user: "ok" }}).issue.user).to.equal("ok"); 51 | }); 52 | it("should simplify comment user", function () { 53 | expect(ghs("test", { comment: { user: { login: "ok" }}}).comment.user).to.equal("ok"); 54 | expect(ghs("test", { comment: { user: "ok" }}).comment.user).to.equal("ok"); 55 | }); 56 | it("should simplify issue number", function () { 57 | expect(ghs("issue_comment", { issue: { number: 42 }}).issue).to.equal(42); 58 | expect(ghs("issue_comment", { issue: 42 }).issue).to.equal(42); 59 | }); 60 | }); 61 | --------------------------------------------------------------------------------