From 8645d954ddb04847be9d4214034d75e8c6d65aa7 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 25 Jul 2022 19:42:58 +0000 Subject: [PATCH] fix: pr webhook --- apps/api/src/routes/webhooks/github/handlers.ts | 12 +++++------- apps/api/src/routes/webhooks/github/types.ts | 10 +++++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/apps/api/src/routes/webhooks/github/handlers.ts b/apps/api/src/routes/webhooks/github/handlers.ts index 3f691d7e8..d8b4e7be8 100644 --- a/apps/api/src/routes/webhooks/github/handlers.ts +++ b/apps/api/src/routes/webhooks/github/handlers.ts @@ -75,16 +75,14 @@ export async function gitHubEvents(request: FastifyRequest): Promi if (!allowedGithubEvents.includes(githubEvent)) { throw { status: 500, message: 'Event not allowed.' } } - let repository, projectId, branch; + let projectId, branch; const body = request.body if (githubEvent === 'push') { - repository = body.repository; - projectId = repository.id; + projectId = body.repository.id; branch = body.ref.includes('/') ? body.ref.split('/')[2] : body.ref; } else if (githubEvent === 'pull_request') { - repository = body.pull_request.head.repo; - projectId = repository.id; - branch = body.pull_request.head.ref.includes('/') ? body.pull_request.head.ref.split('/')[2] : body.pull_request.head.ref; + projectId = body.pull_request.base.repo.id; + branch = body.pull_request.base.ref.includes('/') ? body.pull_request.base.ref.split('/')[2] : body.pull_request.base.ref; } if (!projectId || !branch) { throw { status: 500, message: 'Cannot parse projectId or branch from the webhook?!' } @@ -156,7 +154,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi } else if (githubEvent === 'pull_request') { const pullmergeRequestId = body.number; const pullmergeRequestAction = body.action; - const sourceBranch = body.pull_request.head.ref; + const sourceBranch = body.pull_request.base.ref.includes('/') ? body.pull_request.base.ref.split('/')[2] : body.pull_request.base.ref; if (!allowedActions.includes(pullmergeRequestAction)) { throw { status: 500, message: 'Action not allowed.' } } diff --git a/apps/api/src/routes/webhooks/github/types.ts b/apps/api/src/routes/webhooks/github/types.ts index c7502ec6d..a8f5d3817 100644 --- a/apps/api/src/routes/webhooks/github/types.ts +++ b/apps/api/src/routes/webhooks/github/types.ts @@ -8,12 +8,16 @@ export interface GitHubEvents { Body: { number: string, action: string, - repository: string, + repository: { + id: string, + }, ref: string, pull_request: { - head: { + base: { ref: string, - repo: string + repo: { + id: string, + } } } }