-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
357 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import GeorgeQueue from "../../../src/queues/george-queue"; | ||
import crypto from "crypto"; | ||
import request from "supertest"; | ||
import app from "../../../src/app"; | ||
|
||
const mockApp = request(app); | ||
|
||
describe("WebhooksController", () => { | ||
describe("githubRelease", () => { | ||
const georgeSendReleaseAnnouncementMock = vi.spyOn( | ||
GeorgeQueue, | ||
"sendReleaseAnnouncement" | ||
); | ||
const timingSafeEqualMock = vi.spyOn(crypto, "timingSafeEqual"); | ||
|
||
beforeEach(() => { | ||
vi.stubEnv("GITHUB_WEBHOOK_SECRET", "GITHUB_WEBHOOK_SECRET"); | ||
|
||
georgeSendReleaseAnnouncementMock.mockReset(); | ||
timingSafeEqualMock.mockReset().mockReturnValue(true); | ||
}); | ||
|
||
it("should announce release", async () => { | ||
//WHEN | ||
const { body } = await mockApp | ||
.post("/webhooks/githubRelease") | ||
.set("x-hub-signature-256", "the-signature") | ||
.send({ action: "published", release: { id: 1 } }) | ||
.expect(200); | ||
|
||
//THEN | ||
expect(body).toEqual({ | ||
message: "Added release announcement task to queue", | ||
data: null, | ||
}); | ||
|
||
expect(georgeSendReleaseAnnouncementMock).toHaveBeenCalledWith("1"); | ||
expect(timingSafeEqualMock).toHaveBeenCalledWith( | ||
Buffer.from( | ||
"sha256=ff0f3080539e9df19153f6b5b5780f66e558d61038e6cf5ecf4efdc7266a7751" | ||
), | ||
Buffer.from("the-signature") | ||
); | ||
}); | ||
it("should ignore non-published actions", async () => { | ||
//WHEN | ||
const { body } = await mockApp | ||
.post("/webhooks/githubRelease") | ||
.set("x-hub-signature-256", "the-signature") | ||
.send({ action: "created" }) | ||
.expect(200); | ||
|
||
//THEN | ||
expect(body.message).toEqual("No action taken"); | ||
expect(georgeSendReleaseAnnouncementMock).not.toHaveBeenCalled(); | ||
}); | ||
it("should ignore additional properties", async () => { | ||
//WHEN | ||
await mockApp | ||
.post("/webhooks/githubRelease") | ||
.set("x-hub-signature-256", "the-signature") | ||
.send({ | ||
action: "published", | ||
extra: "value", | ||
release: { id: 1, extra2: "value" }, | ||
}) | ||
.expect(200); | ||
}); | ||
it("should fail with missing releaseId", async () => { | ||
//WHEN | ||
const { body } = await mockApp | ||
.post("/webhooks/githubRelease") | ||
.set("x-hub-signature-256", "the-signature") | ||
.send({ action: "published" }) | ||
.expect(422); | ||
|
||
//THEN | ||
expect(body.message).toEqual('Missing property "release.id".'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
import { MonkeyResponse } from "../../utils/monkey-response"; | ||
import { PostGithubReleaseRequest } from "@monkeytype/contracts/webhooks"; | ||
import GeorgeQueue from "../../queues/george-queue"; | ||
import { MonkeyResponse2 } from "../../utils/monkey-response"; | ||
import MonkeyError from "../../utils/error"; | ||
|
||
export async function githubRelease( | ||
req: MonkeyTypes.Request | ||
): Promise<MonkeyResponse> { | ||
req: MonkeyTypes.Request2<undefined, PostGithubReleaseRequest> | ||
): Promise<MonkeyResponse2> { | ||
const action = req.body.action; | ||
|
||
if (action === "published") { | ||
const releaseId = req.body.release.id; | ||
const releaseId = req.body.release?.id; | ||
if (releaseId === undefined) | ||
throw new MonkeyError(422, 'Missing property "release.id".'); | ||
|
||
await GeorgeQueue.sendReleaseAnnouncement(releaseId); | ||
return new MonkeyResponse("Added release announcement task to queue"); | ||
return new MonkeyResponse2( | ||
"Added release announcement task to queue", | ||
null | ||
); | ||
} | ||
return new MonkeyResponse("No action taken"); | ||
return new MonkeyResponse2("No action taken", null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
// import joi from "joi"; | ||
import { Router } from "express"; | ||
import { authenticateGithubWebhook } from "../../middlewares/auth"; | ||
import { asyncHandler } from "../../middlewares/utility"; | ||
import { webhookLimit } from "../../middlewares/rate-limit"; | ||
import { githubRelease } from "../controllers/webhooks"; | ||
import { webhooksContract } from "@monkeytype/contracts/webhooks"; | ||
import { initServer } from "@ts-rest/express"; | ||
import * as WebhooksController from "../controllers/webhooks"; | ||
import { callController } from "../ts-rest-adapter"; | ||
|
||
const router = Router(); | ||
|
||
router.post( | ||
"/githubRelease", | ||
webhookLimit, | ||
authenticateGithubWebhook(), | ||
asyncHandler(githubRelease) | ||
); | ||
|
||
export default router; | ||
const s = initServer(); | ||
export default s.router(webhooksContract, { | ||
postGithubRelease: { | ||
handler: async (r) => callController(WebhooksController.githubRelease)(r), | ||
}, | ||
}); |
Oops, something went wrong.