Amal Jose commited on
Commit ·
fc50dd8
1
Parent(s): 1dfbe94
feat: added new github check run features
Browse filesFiles changed (5)
42 unmodified lines434445464647484913 unmodified lines636465666766686970715 unmodified lines7778798081828384858687888990919293949596979899100781011021031043 unmodified lines10810911011111211311411511611742 unmodified lines const { body } = req; const eventType = String(req.headers["x-github-event"]); switch (eventType) { case "push": case "push": { const repository = body.repository; const sender = body.sender; const installationId = body.installation.id;13 unmodified lines deployResult = await this.webhookService.webhookCodePushEvent(repository, meta); console.log({ deployResult }); break; }
case "installation": case "installation": { const action = body.action; switch (action) { case "created":5 unmodified lines break; } break; }
case "check_run": { const checkRunAction = body.action; switch (checkRunAction) { case "rerequested": { const repository = body.repository; const sender = body.sender; const installationId = body.installation.id;
const headCommitId = body.check_run.head_sha; const meta = { sender, installationId, headCommitId };
deployResult = await this.webhookService.webhookCheckRunReRequestEvent(repository, meta); console.log({ deployResult }); break; } } break; }
case "installation_repositories": case "installation_repositories": { const repoActions = body.action; switch (repoActions) { case "added":3 unmodified lines await this.webhookService.webhookRepositoryAddRemove(body.repositories_added, body.repositories_removed); break; } } default: { console.log("Github webhook req: ", body.action, body.repository.id, eventType); } }
// console.log("\n---------------------\n", JSON.stringify(body, null, 2), "\n---------------------\n",17 unmodified lines181920212223242526272829303117 unmodified lines deployRequired: boolean; }, ): Promise<{ status: string; reason: string }>; webhookCheckRunReRequestEvent( repo: GithubRepoResponse, meta: { sender: GithubRepositoryOwner; installationId: number; headCommitId: string; }, ): Promise<{ status: string; reason: string }>;
githubSecondaryEvents(installationId: string, userId: string): Promise<string>; removeGhbInstallation(installationId: string, userId: string, skipUserCheck?: boolean): Promise<void>;21 unmodified lines2223242526272821 unmodified lines}export enum DeploymentTriggers { GIT_PUSH = "GIT_PUSH", GITHUB_CHECKS_RERUN = "GITHUB_CHECKS_RERUN", MANUAL = "MANUAL", REDEPLOY = "REDEPLOY",}38 unmodified lines394041424243444548 unmodified lines9495969797989910044 unmodified lines1451461471481491501511481491503 unmodified lines15415515616115715815916016116216316438 unmodified lines private githubSvcs: IGithubService;
private USER_CONCURRENCY_KEY_PREFIX = "build:cncrncy"; private USER_CONCURRENCY_TTL_SECONDS = 60 * 10; private USER_CONCURRENCY_TTL_SECONDS = 60 * 20;
constructor( deploymentRepo: IDeploymentRepository,48 unmodified lines
deploymentData.status = DeploymentStatus.QUEUED; deploymentData.overWrite = false; deploymentData.commit_hash = "------||------"; deploymentData.commit_hash = "----||----"; deploymentData.publicId = nanoid(DEPLOYMENT_ID_LENGTH); deploymentData.identifierSlug = generateSlug(3); deploymentData.project = new Types.ObjectId(correspondindProject._id);44 unmodified lines ]); const correspondindProject = project;
if (problems?.affectedModules?.includes("github")) { await this.createNewFailedDeployment(deploymentData, project, DEPLOYMENT_ERRORS.DEPLOY_FAILED + "; Deployments are currently disabled"); }
if (installationId && canDeploy.user.githubInstallationId && canDeploy.user.githubInstallationId !== installationId) { return { status: "ignored", reason: "installation mismatch" }; }3 unmodified lines deploymentData.identifierSlug = generateSlug(3); deploymentData.project = new Types.ObjectId(correspondindProject._id); deploymentData.user = correspondindProject.user; deploymentData.triggerEvent = DeploymentTriggers.GIT_PUSH;
if (problems?.affectedModules?.includes("github") || problems?.affectedModules?.includes("deployments")) { await this.createNewFailedDeployment(deploymentData, project, DEPLOYMENT_ERRORS.DEPLOY_FAILED + "; Deployments are currently disabled"); return { status: "cancelled", reason: DEPLOYMENT_ERRORS.CREATE_FAILED }; } if (!canDeploy.allowed) { await this.createNewFailedDeployment(deploymentData, project, "User daily deploy limit reached."); return { status: "cancelled", reason: DEPLOYMENT_ERRORS.DAILY_DEPLOYMENT_LIMIT };5 unmodified lines6789910111298 unmodified lines1111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461475 unmodified linesimport { IProjectService } from "@/interfaces/service/IProjectService.js";import { IUserSerivce } from "@/interfaces/service/IUserService.js";import { IWebhookService } from "@/interfaces/service/IWebhookService.js";import { IDeployment } from "@/models/Deployment.js";import { DeploymentTriggers, IDeployment } from "@/models/Deployment.js";import AppError, { WebhookError } from "@/utils/AppError.js";import dispatchRequestService from "@/utils/dispatchRequest.js";import { generateTokenForGithubAppInstallation } from "@/utils/generateToken.js";98 unmodified lines triggeredBy: `${sender.id}||${sender.login}`, commit_hash: `${headCommit.id}||${headCommit.message}`, branch: project.branch, triggerEvent: DeploymentTriggers.GIT_PUSH, }; return await this.deploySvs.newPushDeployment(deployData, project, installationId); return { reason: "", status: "" }; }
async webhookCheckRunReRequestEvent( repo: GithubRepoResponse, meta: { sender: GithubRepositoryOwner; installationId: number; headCommitId: string; }, ): Promise<{ status: string; reason: string }> { const { installationId, sender, headCommitId } = meta;
const project = await this.projectSvcs.__getProjectByRepository(repo.id);
if (!project) { console.log("No Project found"); return { status: "ignored", reason: PROJECT_ERRORS.NOT_FOUND }; } if (project.isDisabled || project.isDeleted) { return { status: "ignored", reason: "project inactive" }; }
const deployData: Partial<IDeployment> = { triggeredBy: `${sender.id}||${sender.login}`, commit_hash: `${headCommitId}||----`, branch: project.branch, triggerEvent: DeploymentTriggers.GITHUB_CHECKS_RERUN, }; return await this.deploySvs.newPushDeployment(deployData, project, installationId); return { reason: "", status: "" };src/controllers/webhookController.ts+30-3
src/interfaces/service/IWebhookService.ts+8
src/models/Deployment.ts+1
src/services/deployment.service.ts+6-7
src/services/webhook.service.ts+32-1