Skip to content

Commit

Permalink
feat: Added Github Project Syncronization
Browse files Browse the repository at this point in the history
  • Loading branch information
noahonyejese committed Jan 8, 2025
1 parent 3285edb commit 2ff089e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 40 deletions.
123 changes: 83 additions & 40 deletions app/api/github/events/projects/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { verifyGitHubRequest } from '@/server/security';
import { createError } from '@/utils/error-handling';
import {
CheckRunEvent,
CreateEvent,
DeleteEvent,
DeploymentEvent,
DeploymentStatusEvent,
PingEvent,
PullRequestEvent,
PullRequestReviewEvent,
PushEvent,
RepositoryEvent,
WebhookEventMap,
WebhookEventName,
} from '@octokit/webhooks-types';
import { NextRequest, NextResponse } from 'next/server';

export const POST = async (req: NextRequest) => {
Expand All @@ -13,85 +27,114 @@ export const POST = async (req: NextRequest) => {
});
}

const body = JSON.parse(rawBody);
const event = req.headers.get('x-github-event');
const body = JSON.parse(rawBody) as WebhookEventMap[WebhookEventName];
const event = req.headers.get('x-github-event') as WebhookEventName;

switch (event) {
case 'ping':
case 'ping': {
const pingEvent = body as PingEvent;
return NextResponse.json({
status: 'success',
message: 'Pong!',
zen: pingEvent.zen,
});
}

case 'create':
case 'create': {
const createEvent = body as CreateEvent;
console.log('Create event:', {
ref_type: body.ref_type,
ref: body.ref,
repository: body.repository.full_name,
ref_type: createEvent.ref_type,
ref: createEvent.ref,
repository: createEvent.repository.full_name,
});
break;
}

case 'delete':
case 'delete': {
const deleteEvent = body as DeleteEvent;
console.log('Delete event:', {
ref_type: body.ref_type,
ref: body.ref,
repository: body.repository.full_name,
ref_type: deleteEvent.ref_type,
ref: deleteEvent.ref,
repository: deleteEvent.repository.full_name,
});
break;
}

case 'check_run':
case 'check_run': {
const checkRunEvent = body as CheckRunEvent;
console.log('Check run event:', {
action: body.action, // created, completed, rerequested
status: body.check_run.status,
conclusion: body.check_run.conclusion,
name: body.check_run.name,
action: checkRunEvent.action,
status: checkRunEvent.check_run.status,
conclusion: checkRunEvent.check_run.conclusion,
name: checkRunEvent.check_run.name,
});
break;
}

case 'deployment_status':
// Deployment status updates
case 'deployment_status': {
const deploymentStatusEvent = body as DeploymentStatusEvent;
console.log('Deployment status event:', {
action: body.action,
state: body.deployment_status.state,
environment: body.deployment.environment,
action: deploymentStatusEvent.action,
state: deploymentStatusEvent.deployment_status.state,
environment: deploymentStatusEvent.deployment.environment,
});
break;
}

case 'pull_request_review':
// PR review events
case 'pull_request_review': {
const pullRequestReviewEvent = body as PullRequestReviewEvent;
console.log('PR review event:', {
action: body.action,
state: body.review.state,
pr_number: body.pull_request.number,
action: pullRequestReviewEvent.action,
state: pullRequestReviewEvent.review.state,
pr_number: pullRequestReviewEvent.pull_request.number,
});
break;
}

case 'pull_request':
case 'pull_request': {
const pullRequestEvent = body as PullRequestEvent;
console.log('Pull request event:', {
action: body.action,
number: body.pull_request.number,
state: body.pull_request.state,
merged: body.pull_request.merged,
action: pullRequestEvent.action,
number: pullRequestEvent.pull_request.number,
state: pullRequestEvent.pull_request.state,
merged: pullRequestEvent.pull_request.merged,
});
break;
}

case 'push':
case 'push': {
const pushEvent = body as PushEvent;
console.log('Push event:', {
ref: body.ref,
commits: body.commits.length,
repository: body.repository.full_name,
ref: pushEvent.ref,
commits: pushEvent.commits.length,
repository: pushEvent.repository.full_name,
});
break;
}

case 'repository':
case 'repository': {
const repositoryEvent = body as RepositoryEvent;
console.log('Repository event:', {
action: body.action,
repository: body.repository.full_name,
action: repositoryEvent.action,
repository: repositoryEvent.repository.full_name,
});
break;
}

case 'deployment': {
const deploymentEvent = body as DeploymentEvent;
console.log('Deployment event:', {
action: deploymentEvent.action,
environment: deploymentEvent.deployment.environment,
repository: deploymentEvent.repository.full_name,
});
break;
}

default:
console.log(`Unhandled event type: ${event}`);
default: {
const eventName = event as WebhookEventName;
console.log(`Unhandled event type: ${eventName}`);
}
}

return NextResponse.json({
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@octokit/webhooks-types": "^7.6.1",
"@types/animejs": "^3.1.12",
"@types/d3": "^7.4.3",
"@types/node": "^20",
Expand Down
30 changes: 30 additions & 0 deletions server/github/syncs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TeamProjectFirebase } from '@/types/firebase';
import { Repository } from '@octokit/webhooks-types';
import * as admin from 'firebase-admin';

export const projectSync = async (project: Repository) => {
const db = admin.database();
const userRef = db.ref(
`data/team-projects/sensors/github/values/${project.id}`
);

const snapshot = await userRef.once('value');
const projectData = snapshot.val() as TeamProjectFirebase;

if (projectData) {
await userRef.update({
...projectData,
frequency: projectData.frequency + 1,
lastUpdated: Date.now(),
});
} else {
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(),
});
}
};

0 comments on commit 2ff089e

Please sign in to comment.