diff --git a/packages/server/src/services/application.ts b/packages/server/src/services/application.ts index ba36dcec..fef1457c 100644 --- a/packages/server/src/services/application.ts +++ b/packages/server/src/services/application.ts @@ -51,7 +51,12 @@ import { findPreviewDeploymentById, updatePreviewDeployment, } from "./preview-deployment"; -import { getIssueComment, updateIssueComment } from "./github"; +import { + createPreviewDeploymentComment, + getIssueComment, + issueCommentExists, + updateIssueComment, +} from "./github"; import { type Domain, getDomainHost } from "./domain"; export type Application = typeof applications.$inferSelect; @@ -401,6 +406,27 @@ export const deployPreviewApplication = async ({ githubId: application?.githubId || "", }; try { + const commentExists = await issueCommentExists({ + ...issueParams, + }); + if (!commentExists) { + const result = await createPreviewDeploymentComment({ + ...issueParams, + previewDomain, + appName: previewDeployment.appName, + githubId: application?.githubId || "", + previewDeploymentId, + }); + + if (!result) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Pull request comment not found", + }); + } + + issueParams.comment_id = Number.parseInt(result?.pullRequestCommentId); + } const buildingComment = getIssueComment( application.name, "running", @@ -487,6 +513,27 @@ export const deployRemotePreviewApplication = async ({ githubId: application?.githubId || "", }; try { + const commentExists = await issueCommentExists({ + ...issueParams, + }); + if (!commentExists) { + const result = await createPreviewDeploymentComment({ + ...issueParams, + previewDomain, + appName: previewDeployment.appName, + githubId: application?.githubId || "", + previewDeploymentId, + }); + + if (!result) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Pull request comment not found", + }); + } + + issueParams.comment_id = Number.parseInt(result?.pullRequestCommentId); + } const buildingComment = getIssueComment( application.name, "running", diff --git a/packages/server/src/services/github.ts b/packages/server/src/services/github.ts index 17ec7d72..d60c8353 100644 --- a/packages/server/src/services/github.ts +++ b/packages/server/src/services/github.ts @@ -7,6 +7,7 @@ import { import { TRPCError } from "@trpc/server"; import { eq } from "drizzle-orm"; import { authGithub } from "../utils/providers/github"; +import { updatePreviewDeployment } from "./preview-deployment"; export type Github = typeof github.$inferSelect; export const createGithub = async ( @@ -97,7 +98,31 @@ export const getIssueComment = ( return finished; }; - +interface CommentExists { + owner: string; + repository: string; + comment_id: number; + githubId: string; +} +export const issueCommentExists = async ({ + owner, + repository, + comment_id, + githubId, +}: CommentExists) => { + const github = await findGithubById(githubId); + const octokit = authGithub(github); + try { + await octokit.rest.issues.getComment({ + owner: owner || "", + repo: repository || "", + comment_id: comment_id, + }); + return true; + } catch (error) { + return false; + } +}; interface Comment { owner: string; repository: string; @@ -106,7 +131,6 @@ interface Comment { comment_id: number; githubId: string; } - export const updateIssueComment = async ({ owner, repository, @@ -126,3 +150,43 @@ export const updateIssueComment = async ({ comment_id: comment_id, }); }; + +interface CommentCreate { + appName: string; + owner: string; + repository: string; + issue_number: string; + previewDomain: string; + githubId: string; + previewDeploymentId: string; +} + +export const createPreviewDeploymentComment = async ({ + owner, + repository, + issue_number, + previewDomain, + appName, + githubId, + previewDeploymentId, +}: CommentCreate) => { + const github = await findGithubById(githubId); + const octokit = authGithub(github); + + const runningComment = getIssueComment( + appName, + "initializing", + previewDomain, + ); + + const issue = await octokit.rest.issues.createComment({ + owner: owner || "", + repo: repository || "", + issue_number: Number.parseInt(issue_number), + body: `### Dokploy Preview Deployment\n\n${runningComment}`, + }); + + return await updatePreviewDeployment(previewDeploymentId, { + pullRequestCommentId: `${issue.data.id}`, + }).then((response) => response[0]); +}; diff --git a/packages/server/src/services/preview-deployment.ts b/packages/server/src/services/preview-deployment.ts index 59788adc..e52f4553 100644 --- a/packages/server/src/services/preview-deployment.ts +++ b/packages/server/src/services/preview-deployment.ts @@ -205,8 +205,6 @@ export const createPreviewDeployment = async ( application.appName = appName; - console.log(application); - await manageDomain(application, newDomain); await db