Amal Jose commited on
Commit ·
fc50dd8
1
Parent(s): 1dfbe94
feat: added new github check run features
Browse filesFiles changed (5)
42 unmodified lines4344454647484913 unmodified lines636465666768695 unmodified lines757677787980813 unmodified lines85868788899042 unmodified linesconst { body } = req;const eventType = String(req.headers["x-github-event"]);switch (eventType) {case "push":const repository = body.repository;const sender = body.sender;const installationId = body.installation.id;13 unmodified linesdeployResult = await this.webhookService.webhookCodePushEvent(repository, meta);console.log({ deployResult });break;case "installation":const action = body.action;switch (action) {case "created":5 unmodified linesbreak;}break;case "installation_repositories":const repoActions = body.action;switch (repoActions) {case "added":3 unmodified linesawait this.webhookService.webhookRepositoryAddRemove(body.repositories_added, body.repositories_removed);break;}}// console.log("\n---------------------\n", JSON.stringify(body, null, 2), "\n---------------------\n",42 unmodified lines4344454647484913 unmodified lines6364656667686970715 unmodified lines77787980818283848586878889909192939495969798991001011021031043 unmodified lines10810911011111211311411511611742 unmodified linesconst { body } = req;const eventType = String(req.headers["x-github-event"]);switch (eventType) {case "push": {const repository = body.repository;const sender = body.sender;const installationId = body.installation.id;13 unmodified linesdeployResult = await this.webhookService.webhookCodePushEvent(repository, meta);console.log({ deployResult });break;}case "installation": {const action = body.action;switch (action) {case "created":5 unmodified linesbreak;}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": {const repoActions = body.action;switch (repoActions) {case "added":3 unmodified linesawait 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 lines18192021222317 unmodified linesdeployRequired: boolean;},): Promise<{ status: string; reason: string }>;githubSecondaryEvents(installationId: string, userId: string): Promise<string>;removeGhbInstallation(installationId: string, userId: string, skipUserCheck?: boolean): Promise<void>;17 unmodified lines181920212223242526272829303117 unmodified linesdeployRequired: 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 lines22232425262721 unmodified lines}export enum DeploymentTriggers {GIT_PUSH = "GIT_PUSH",MANUAL = "MANUAL",REDEPLOY = "REDEPLOY",}21 unmodified lines2223242526272821 unmodified lines}export enum DeploymentTriggers {GIT_PUSH = "GIT_PUSH",GITHUB_CHECKS_RERUN = "GITHUB_CHECKS_RERUN",MANUAL = "MANUAL",REDEPLOY = "REDEPLOY",}
38 unmodified lines3940414243444548 unmodified lines94959697989910044 unmodified lines1451461471481491501511521531543 unmodified lines15815916016116216316416538 unmodified linesprivate githubSvcs: IGithubService;private USER_CONCURRENCY_KEY_PREFIX = "build:cncrncy";private USER_CONCURRENCY_TTL_SECONDS = 60 * 10;constructor(deploymentRepo: IDeploymentRepository,48 unmodified linesdeploymentData.status = DeploymentStatus.QUEUED;deploymentData.overWrite = false;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 linesdeploymentData.identifierSlug = generateSlug(3);deploymentData.project = new Types.ObjectId(correspondindProject._id);deploymentData.user = correspondindProject.user;deploymentData.triggerEvent = DeploymentTriggers.GIT_PUSH;if (!canDeploy.allowed) {await this.createNewFailedDeployment(deploymentData, project, "User daily deploy limit reached.");return { status: "cancelled", reason: DEPLOYMENT_ERRORS.DAILY_DEPLOYMENT_LIMIT };38 unmodified lines3940414243444548 unmodified lines94959697989910044 unmodified lines1451461471481491503 unmodified lines15415515615715815916016116216316438 unmodified linesprivate githubSvcs: IGithubService;private USER_CONCURRENCY_KEY_PREFIX = "build:cncrncy";private USER_CONCURRENCY_TTL_SECONDS = 60 * 20;constructor(deploymentRepo: IDeploymentRepository,48 unmodified linesdeploymentData.status = DeploymentStatus.QUEUED;deploymentData.overWrite = false;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 (installationId && canDeploy.user.githubInstallationId && canDeploy.user.githubInstallationId !== installationId) {return { status: "ignored", reason: "installation mismatch" };}3 unmodified linesdeploymentData.identifierSlug = generateSlug(3);deploymentData.project = new Types.ObjectId(correspondindProject._id);deploymentData.user = correspondindProject.user;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 lines678910111298 unmodified lines1111121131141151165 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 AppError, { WebhookError } from "@/utils/AppError.js";import dispatchRequestService from "@/utils/dispatchRequest.js";import { generateTokenForGithubAppInstallation } from "@/utils/generateToken.js";98 unmodified linestriggeredBy: `${sender.id}||${sender.login}`,commit_hash: `${headCommit.id}||${headCommit.message}`,branch: project.branch,};return await this.deploySvs.newPushDeployment(deployData, project, installationId);return { reason: "", status: "" };5 unmodified lines678910111298 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 { 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 linestriggeredBy: `${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