Skip to content

Commit

Permalink
fix: handle malformed events
Browse files Browse the repository at this point in the history
  • Loading branch information
noahonyejese committed Jan 8, 2025
1 parent cdd78f3 commit 79803e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/api/github/events/projects/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { initializeFirebase } from '@/server/firebase/server';
import { projectSync } from '@/server/github/syncs';
import {
projectSync,
validateGithubProjectInputs,
} from '@/server/github/syncs';
import { verifyGitHubRequest } from '@/server/security';
import { createError } from '@/utils/error-handling';
import {
Expand Down Expand Up @@ -36,6 +39,7 @@ export const POST = async (req: NextRequest) => {
initializeFirebase();

if (event !== 'ping' && 'repository' in body) {
validateGithubProjectInputs(body.repository as Repository);
await projectSync(body.repository as Repository);
}

Expand Down
13 changes: 11 additions & 2 deletions server/github/syncs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { TeamProjectFirebase } from '@/types/firebase';
import { createError } from '@/utils/error-handling';
import { Repository } from '@octokit/webhooks-types';
import * as admin from 'firebase-admin';

export const validateGithubProjectInputs = (project: Repository) => {
if (!project.name) {
throw createError({
title: 'Invalid Slack event',
message: 'Event user or channel is invalid',
cause: 'Slack',
});
}
};

export const projectSync = async (project: Repository) => {
const db = admin.database();
const userRef = db.ref(
Expand All @@ -21,8 +32,6 @@ export const projectSync = async (project: Repository) => {
await userRef.set({
title: project.name,
logo: `https://github.com/${project.owner.id}/${project.id}/pinned-icon.svg`,
defaulBranch: project.default_branch,
masterBranch: project.master_branch,
frequency: 1,
lastUpdated: Date.now(),
});
Expand Down
1 change: 0 additions & 1 deletion server/slack/syncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const validateMessageSlackEvent = (event: MessageEvent) => {
) ||
typeof event.channel !== 'string'
) {
console.log(event);
throw createError({
title: 'Invalid Slack event',
message: 'Event user or channel is invalid',
Expand Down

0 comments on commit 79803e2

Please sign in to comment.