From b352f91a43717ad3159bc4a3449f87ac9d1d7564 Mon Sep 17 00:00:00 2001 From: vishnuoruphones Date: Sat, 13 Jul 2024 01:38:00 +0530 Subject: [PATCH] feat : added org hooks too --- client/src/Components/Home.tsx | 1 - .../src/controllers/webhook/createWebhook.ts | 34 +++++++++++-------- server/src/services/github.ts | 2 +- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/client/src/Components/Home.tsx b/client/src/Components/Home.tsx index 24e73f7..711cf27 100644 --- a/client/src/Components/Home.tsx +++ b/client/src/Components/Home.tsx @@ -82,7 +82,6 @@ export default function Home() { currentToken ); if (!repoSub.ok) { - toast.error("Error Subscribing to Repo"); if (repoSub != "Webhook Created") { setRepoSubscribeErrorMessage(repoSub.message); } diff --git a/server/src/controllers/webhook/createWebhook.ts b/server/src/controllers/webhook/createWebhook.ts index 98b6f2d..67b3f18 100644 --- a/server/src/controllers/webhook/createWebhook.ts +++ b/server/src/controllers/webhook/createWebhook.ts @@ -18,23 +18,27 @@ export const createWebHook = async ( if (!req.body.repository) { return res.status(400).json({ message: 'Invalid request' }); } - const parts = req.body.repository.html_url.split('/'); - const baseGithubUrl = parts.slice(0, 4).join('/'); - const user = await getUserByGithubUrl(baseGithubUrl); - const repoName = extractRepoNameFromUrl(req.body.repository.html_url); + if (req.body.pull_request || req.body.pusher) { + const parts = req.body.repository.html_url.split('/'); + const baseGithubUrl = parts.slice(0, 4).join('/'); + const user = await getUserByGithubUrl(baseGithubUrl); + const repoName = extractRepoNameFromUrl(req.body.repository.html_url); - await sendPushNotification( - user.gcmToken, - req.body.action == 'opened' - ? `${req.body.sender.login} created a new pull request in ${repoName}` - : `${req.body.pusher.name} added new commit in ${repoName}`, - req.body.action == 'opened' - ? `Please take a look: A new pull request has been created in the repository ${repoName}. Click here to view: ${req.body.repository.html_url}` - : `New changes have been pushed to the repository ${repoName}. Click here to review: ${req.body.repository.html_url}`, + await sendPushNotification( + user.gcmToken, + req.body.action == 'opened' + ? `${req.body.sender.login} created a new pull request in ${repoName}` + : `${req.body.pusher.name} added new commit in ${repoName}`, + req.body.action == 'opened' + ? `Please take a look: A new pull request has been created in the repository ${repoName}. Click here to view: ${req.body.repository.html_url}` + : `New changes have been pushed to the repository ${repoName}. Click here to review: ${req.body.repository.html_url}`, - { url: req.body.repository.html_url }, - ); - res.status(200).json({ message: 'Push Notification sent succesfully' }); + { url: req.body.repository.html_url }, + ); + res.status(200).json({ message: 'Push Notification sent succesfully' }); + } else { + res.status(400).json({ message: 'Invalid request' }); + } } catch (error) { if (error instanceof z.ZodError) { res diff --git a/server/src/services/github.ts b/server/src/services/github.ts index 30d9e94..ff8b1af 100644 --- a/server/src/services/github.ts +++ b/server/src/services/github.ts @@ -129,7 +129,7 @@ export const addWebhookForOrganizationRepo = async ( ) => { try { const response = await fetch( - `https://api.github.com/repos/${organization}/${repo}/hooks`, + `https://api.github.com/orgs/${organization}/hooks`, { method: 'POST', headers: {