Skip to content

Commit

Permalink
feat : added org hooks too
Browse files Browse the repository at this point in the history
  • Loading branch information
Vswaroop04 committed Jul 12, 2024
1 parent 65eaf87 commit b352f91
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
1 change: 0 additions & 1 deletion client/src/Components/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
34 changes: 19 additions & 15 deletions server/src/controllers/webhook/createWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit b352f91

Please sign in to comment.