Skip to content

Commit

Permalink
fix: Add routes for intentionally throwing an error to test behavior …
Browse files Browse the repository at this point in the history
…of sentry

Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com>
  • Loading branch information
yorinasub17 committed Oct 20, 2023
1 parent 5afe363 commit 362e887
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
35 changes: 35 additions & 0 deletions web/mgmt_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import {
handleSubscriptionEvent,
} from "../mgmt/mod.ts";
import {
enqueueMsg,
FensakConfigSource,
getComputedFensakConfig,
getSubscription,
MessageType,
mustGetGitHubOrgWithSubscription,
waitForHealthCheckResult,
} from "../svcdata/mod.ts";
import type { GitHubOrgWithSubscription } from "../svcdata/mod.ts";
import { isOrgManager } from "../ghstd/mod.ts";
import { getRandomString } from "../xtd/mod.ts";

interface APIOrganization {
slug: string;
Expand All @@ -38,6 +42,8 @@ export function attachMgmtAPIRoutes(router: Router): void {
const corsMW = oakCors({ origin: corsOrigins });

router
.get("/healthz", healthCheck)
.get("/sentry-test", testSentry)
.post(
"/hooks/mgmt",
middlewares.assertMgmtEvent,
Expand All @@ -59,6 +65,35 @@ export function attachMgmtAPIRoutes(router: Router): void {
);
}

async function healthCheck(ctx: Context): Promise<void> {
const requestID = getRandomString(6);
await enqueueMsg({
type: MessageType.HealthCheck,
payload: {
requestID: requestID,
},
});
const result = await waitForHealthCheckResult(requestID);
if (!result) {
ctx.response.status = Status.InternalServerError;
ctx.response.body = {
status: Status.InternalServerError,
msg: "timed out waiting for worker health result",
};
return;
}

ctx.response.status = Status.OK;
ctx.response.body = {
status: Status.OK,
msg: "system ok",
};
}

function testSentry(_ctx: Context): void {
throw new Error("Test error to ensure sentry is working");
}

async function handleGetOrganizations(ctx: Context): Promise<void> {
const token = ctx.state.apiToken;
const octokit = new Octokit({ auth: token });
Expand Down
33 changes: 1 addition & 32 deletions web/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,14 @@ import { Context, GitHubWebhookEventName, Router, Status } from "../deps.ts";

import { logger } from "../logging/mod.ts";
import * as middlewares from "../middlewares/mod.ts";
import {
enqueueMsg,
MessageType,
waitForHealthCheckResult,
} from "../svcdata/mod.ts";
import { enqueueMsg, MessageType } from "../svcdata/mod.ts";
import { fastRejectEvent } from "../ghevent/mod.ts";
import { getRandomString } from "../xtd/mod.ts";

export function attachRoutes(router: Router): void {
router
.get("/healthz", healthCheck)
.post("/hooks/gh", middlewares.assertGitHubWebhook, handleGitHubWebhooks);
}

async function healthCheck(ctx: Context): Promise<void> {
const requestID = getRandomString(6);
await enqueueMsg({
type: MessageType.HealthCheck,
payload: {
requestID: requestID,
},
});
const result = await waitForHealthCheckResult(requestID);
if (!result) {
ctx.response.status = Status.InternalServerError;
ctx.response.body = {
status: Status.InternalServerError,
msg: "timed out waiting for worker health result",
};
return;
}

ctx.response.status = Status.OK;
ctx.response.body = {
status: Status.OK,
msg: "system ok",
};
}

async function handleGitHubWebhooks(ctx: Context): Promise<void> {
const ghEventName = ctx.request.headers.get("X-GitHub-Event");
if (ghEventName == null) {
Expand Down

0 comments on commit 362e887

Please sign in to comment.