Skip to content

Commit

Permalink
Add support for checking the event payload
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Mar 6, 2022
1 parent e80e129 commit acb300d
Show file tree
Hide file tree
Showing 5 changed files with 6,050 additions and 11 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ try {
}
```

### Matching arbitrary payloads

In addition to checking the event, you can also check for entries in the provided `GITHUB_EVENT`.

Here's an example that will throw if the pull request was not opened by `mheap`:

```javascript
guard({
event: "pull_request.opened",
payload: { user: { login: "mheap" } },
});
```

See https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads for sample payloads

### Matching multiple conditions

You can provide multiple conditions to validate. If all fail, an `Error` will be thrown.
Expand Down
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require("lodash");

module.exports = (guards) => {
if (!Array.isArray(guards)) {
guards = [guards];
Expand Down Expand Up @@ -59,5 +61,31 @@ function runSingle(params) {
};
}

if (params.payload) {
const expectedPayload = params.payload;
if (!payloadIncludes(payload, expectedPayload)) {
return {
success: false,
reason: `Invalid payload. Expected '${JSON.stringify(
expectedPayload
)}', got '${JSON.stringify(payload)}'`,
};
}
}
return { success: true };
}

function payloadIncludes(base, object) {
function changes(object, base) {
return _.transform(object, function (result, value, key) {
if (!_.isEqual(value, base[key])) {
result[key] =
_.isObject(value) && _.isObject(base[key])
? changes(value, base[key])
: value;
}
});
}

return _.isEmpty(changes(object, base));
}
40 changes: 40 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ describe("Action Guard", () => {
});
});

describe("Arbitrary payload conditions", () => {
it("throws if the required fields are different in the payload", () => {
mockEvent("pull_request", { user: { login: "fake_user" } });
expect(() => {
guard({
event: "pull_request",
payload: { user: { login: "mheap" } },
});
}).toThrow(
new Error(
`Invalid payload. Expected '{"user":{"login":"mheap"}}', got '{"user":{"login":"fake_user"}}'`
)
);
});

it("throws if the required fields are not in the payload", () => {
mockEvent("pull_request", { user: { email: "user@example.com" } });
expect(() => {
guard({
event: "pull_request",
payload: { user: { login: "mheap" } },
});
}).toThrow(
new Error(
`Invalid payload. Expected '{"user":{"login":"mheap"}}', got '{"user":{"email":"user@example.com"}}'`
)
);
});

it("does not throw if the payload matches", () => {
mockEvent("pull_request", { user: { login: "mheap" } });
expect(
guard({
event: "pull_request",
payload: { user: { login: "mheap" } },
})
).toBe(undefined);
});
});

describe("Calling formats", () => {
it("string", () => {
mockEvent("pull_request", { action: "opened" });
Expand Down
Loading

0 comments on commit acb300d

Please sign in to comment.