From 7633e85d959e9baca80f75ce57708b6ffd3a5574 Mon Sep 17 00:00:00 2001 From: SengMitnick Date: Wed, 29 May 2024 12:34:57 +0800 Subject: [PATCH] feat: add createPullRequesComment --- index.js | 32 ++++++++++++++++++++++++-------- package-lock.json | 16 ++++++++++++++-- package.json | 1 + src/index.ts | 36 +++++++++++++++++++++++++++--------- src/utils.ts | 4 ++++ 5 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 src/utils.ts diff --git a/index.js b/index.js index 7daf13d..87c65f9 100644 --- a/index.js +++ b/index.js @@ -22141,7 +22141,7 @@ try { auto_inactive: false }); }; - const createJobSummary = async ({ deployment, aliasUrl }) => { + const generateSummaryBody = ({ deployment, aliasUrl }) => { const deployStage = deployment.stages.find((stage) => stage.name === "deploy"); let status = "\u26A1\uFE0F Deployment in progress..."; if (deployStage?.status === "success") { @@ -22149,8 +22149,7 @@ try { } else if (deployStage?.status === "failure") { status = "\u{1F6AB} Deployment failed"; } - await import_core.summary.addRaw( - ` + return ` # ${projectName} Deploying with Cloudflare Pages | Name | Result | @@ -22159,16 +22158,30 @@ try { | **Status**: | ${status} | | **Preview URL**: | ${deployment.url} | | **Branch Preview URL**: | ${aliasUrl} | - ` - ).write(); + `; + }; + const createJobSummary = async (params) => { + const body = generateSummaryBody(params); + await import_core.summary.addRaw(body).write(); + }; + const createPullRequesComment = async (octokit, params) => { + const body = generateSummaryBody(params); + const pullRequestPayload = import_github.context.payload; + await octokit.rest.issues.createComment({ + owner: import_github.context.repo.owner, + repo: import_github.context.repo.repo, + issue_number: pullRequestPayload.pull_request.number, + body + }); }; (async () => { const project = await getProject(); const productionEnvironment = githubBranch === project.production_branch || branch === project.production_branch; const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`; let gitHubDeployment; + let octokit = null; if (gitHubToken && gitHubToken.length) { - const octokit = (0, import_github.getOctokit)(gitHubToken); + octokit = (0, import_github.getOctokit)(gitHubToken); gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName); } const pagesDeployment = await createPagesDeployment(); @@ -22181,15 +22194,18 @@ try { } (0, import_core.setOutput)("alias", alias); await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias }); + if (octokit && import_github.context.eventName === "pull_request") { + await createPullRequesComment(octokit, { deployment: pagesDeployment, aliasUrl: alias }); + } if (gitHubDeployment) { - const octokit = (0, import_github.getOctokit)(gitHubToken); + const octokit2 = (0, import_github.getOctokit)(gitHubToken); await createGitHubDeploymentStatus({ id: gitHubDeployment.id, url: pagesDeployment.url, deploymentId: pagesDeployment.id, environmentName, productionEnvironment, - octokit + octokit: octokit2 }); } })(); diff --git a/package-lock.json b/package-lock.json index 92bab55..5f37923 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,18 @@ { "name": "pages-action", - "version": "1.5.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pages-action", - "version": "1.5.0", + "version": "2.0.0", "license": "MIT", "dependencies": { "@actions/core": "^1.10.0", "@actions/github": "^5.1.1", "@cloudflare/types": "^6.18.16", + "@octokit/webhooks-definitions": "^3.67.3", "shellac": "^0.7.2", "undici": "^5.11.0" }, @@ -213,6 +214,12 @@ "@octokit/openapi-types": "^12.11.0" } }, + "node_modules/@octokit/webhooks-definitions": { + "version": "3.67.3", + "resolved": "https://registry.npmjs.org/@octokit/webhooks-definitions/-/webhooks-definitions-3.67.3.tgz", + "integrity": "sha512-do4Z1r2OVhuI0ihJhQ8Hg+yPWnBYEBNuFNCrvtPKoYT1w81jD7pBXgGe86lYuuNirkDHb0Nxt+zt4O5GiFJfgA==", + "deprecated": "Use @octokit/webhooks-types, @octokit/webhooks-schemas, or @octokit/webhooks-examples instead. See https://github.com/octokit/webhooks/issues/447" + }, "node_modules/@types/node": { "version": "18.11.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", @@ -968,6 +975,11 @@ "@octokit/openapi-types": "^12.11.0" } }, + "@octokit/webhooks-definitions": { + "version": "3.67.3", + "resolved": "https://registry.npmjs.org/@octokit/webhooks-definitions/-/webhooks-definitions-3.67.3.tgz", + "integrity": "sha512-do4Z1r2OVhuI0ihJhQ8Hg+yPWnBYEBNuFNCrvtPKoYT1w81jD7pBXgGe86lYuuNirkDHb0Nxt+zt4O5GiFJfgA==" + }, "@types/node": { "version": "18.11.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", diff --git a/package.json b/package.json index f10c184..dd895e9 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@actions/core": "^1.10.0", "@actions/github": "^5.1.1", "@cloudflare/types": "^6.18.16", + "@octokit/webhooks-definitions": "^3.67.3", "shellac": "^0.7.2", "undici": "^5.11.0" }, diff --git a/src/index.ts b/src/index.ts index 3ace872..7dea690 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,14 @@ import { getInput, setOutput, setFailed, summary } from "@actions/core"; import type { Project, Deployment } from "@cloudflare/types"; import { context, getOctokit } from "@actions/github"; +import { PullRequestEvent } from "@octokit/webhooks-definitions/schema"; import shellac from "shellac"; import { fetch } from "undici"; import { env } from "process"; import path from "node:path"; type Octokit = ReturnType; +type SummaryBodyParams = { deployment: Deployment; aliasUrl: string }; try { const apiToken = getInput("apiToken", { required: true }); @@ -108,7 +110,7 @@ try { }); }; - const createJobSummary = async ({ deployment, aliasUrl }: { deployment: Deployment; aliasUrl: string }) => { + const generateSummaryBody = ({ deployment, aliasUrl }: SummaryBodyParams) => { const deployStage = deployment.stages.find((stage) => stage.name === "deploy"); let status = "⚡️ Deployment in progress..."; @@ -117,10 +119,7 @@ try { } else if (deployStage?.status === "failure") { status = "🚫 Deployment failed"; } - - await summary - .addRaw( - ` + return ` # ${projectName} Deploying with Cloudflare Pages | Name | Result | @@ -129,9 +128,23 @@ try { | **Status**: | ${status} | | **Preview URL**: | ${deployment.url} | | **Branch Preview URL**: | ${aliasUrl} | - ` - ) - .write(); + `; + }; + + const createJobSummary = async (params: SummaryBodyParams) => { + const body = generateSummaryBody(params); + await summary.addRaw(body).write(); + }; + + const createPullRequesComment = async (octokit: Octokit, params: SummaryBodyParams) => { + const body = generateSummaryBody(params); + const pullRequestPayload = context.payload as PullRequestEvent; + await octokit.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pullRequestPayload.pull_request.number, + body, + }); }; (async () => { @@ -141,9 +154,10 @@ try { const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`; let gitHubDeployment: Awaited>; + let octokit: Octokit | null = null; if (gitHubToken && gitHubToken.length) { - const octokit = getOctokit(gitHubToken); + octokit = getOctokit(gitHubToken); gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName); } @@ -160,6 +174,10 @@ try { await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias }); + if (octokit && context.eventName === "pull_request") { + await createPullRequesComment(octokit, { deployment: pagesDeployment, aliasUrl: alias }); + } + if (gitHubDeployment) { const octokit = getOctokit(gitHubToken); diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..bb3bc1b --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,4 @@ +export function getErrorMessage(error: unknown) { + if (error instanceof Error) return error.message; + return String(error); +}