feat: add createPullRequesComment

pull/129/head
SengMitnick 1 year ago
parent c3bc38c748
commit 7633e85d95

@ -22141,7 +22141,7 @@ try {
auto_inactive: false auto_inactive: false
}); });
}; };
const createJobSummary = async ({ deployment, aliasUrl }) => { const generateSummaryBody = ({ deployment, aliasUrl }) => {
const deployStage = deployment.stages.find((stage) => stage.name === "deploy"); const deployStage = deployment.stages.find((stage) => stage.name === "deploy");
let status = "\u26A1\uFE0F Deployment in progress..."; let status = "\u26A1\uFE0F Deployment in progress...";
if (deployStage?.status === "success") { if (deployStage?.status === "success") {
@ -22149,8 +22149,7 @@ try {
} else if (deployStage?.status === "failure") { } else if (deployStage?.status === "failure") {
status = "\u{1F6AB} Deployment failed"; status = "\u{1F6AB} Deployment failed";
} }
await import_core.summary.addRaw( return `
`
# ${projectName} Deploying with Cloudflare Pages # ${projectName} Deploying with Cloudflare Pages
| Name | Result | | Name | Result |
@ -22159,16 +22158,30 @@ try {
| **Status**: | ${status} | | **Status**: | ${status} |
| **Preview URL**: | ${deployment.url} | | **Preview URL**: | ${deployment.url} |
| **Branch Preview URL**: | ${aliasUrl} | | **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 () => { (async () => {
const project = await getProject(); const project = await getProject();
const productionEnvironment = githubBranch === project.production_branch || branch === project.production_branch; const productionEnvironment = githubBranch === project.production_branch || branch === project.production_branch;
const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`; const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`;
let gitHubDeployment; let gitHubDeployment;
let octokit = null;
if (gitHubToken && gitHubToken.length) { if (gitHubToken && gitHubToken.length) {
const octokit = (0, import_github.getOctokit)(gitHubToken); octokit = (0, import_github.getOctokit)(gitHubToken);
gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName); gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName);
} }
const pagesDeployment = await createPagesDeployment(); const pagesDeployment = await createPagesDeployment();
@ -22181,15 +22194,18 @@ try {
} }
(0, import_core.setOutput)("alias", alias); (0, import_core.setOutput)("alias", alias);
await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias }); await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias });
if (octokit && import_github.context.eventName === "pull_request") {
await createPullRequesComment(octokit, { deployment: pagesDeployment, aliasUrl: alias });
}
if (gitHubDeployment) { if (gitHubDeployment) {
const octokit = (0, import_github.getOctokit)(gitHubToken); const octokit2 = (0, import_github.getOctokit)(gitHubToken);
await createGitHubDeploymentStatus({ await createGitHubDeploymentStatus({
id: gitHubDeployment.id, id: gitHubDeployment.id,
url: pagesDeployment.url, url: pagesDeployment.url,
deploymentId: pagesDeployment.id, deploymentId: pagesDeployment.id,
environmentName, environmentName,
productionEnvironment, productionEnvironment,
octokit octokit: octokit2
}); });
} }
})(); })();

16
package-lock.json generated

@ -1,17 +1,18 @@
{ {
"name": "pages-action", "name": "pages-action",
"version": "1.5.0", "version": "2.0.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "pages-action", "name": "pages-action",
"version": "1.5.0", "version": "2.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/core": "^1.10.0", "@actions/core": "^1.10.0",
"@actions/github": "^5.1.1", "@actions/github": "^5.1.1",
"@cloudflare/types": "^6.18.16", "@cloudflare/types": "^6.18.16",
"@octokit/webhooks-definitions": "^3.67.3",
"shellac": "^0.7.2", "shellac": "^0.7.2",
"undici": "^5.11.0" "undici": "^5.11.0"
}, },
@ -213,6 +214,12 @@
"@octokit/openapi-types": "^12.11.0" "@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": { "node_modules/@types/node": {
"version": "18.11.3", "version": "18.11.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz",
@ -968,6 +975,11 @@
"@octokit/openapi-types": "^12.11.0" "@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": { "@types/node": {
"version": "18.11.3", "version": "18.11.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz",

@ -25,6 +25,7 @@
"@actions/core": "^1.10.0", "@actions/core": "^1.10.0",
"@actions/github": "^5.1.1", "@actions/github": "^5.1.1",
"@cloudflare/types": "^6.18.16", "@cloudflare/types": "^6.18.16",
"@octokit/webhooks-definitions": "^3.67.3",
"shellac": "^0.7.2", "shellac": "^0.7.2",
"undici": "^5.11.0" "undici": "^5.11.0"
}, },

@ -1,12 +1,14 @@
import { getInput, setOutput, setFailed, summary } from "@actions/core"; import { getInput, setOutput, setFailed, summary } from "@actions/core";
import type { Project, Deployment } from "@cloudflare/types"; import type { Project, Deployment } from "@cloudflare/types";
import { context, getOctokit } from "@actions/github"; import { context, getOctokit } from "@actions/github";
import { PullRequestEvent } from "@octokit/webhooks-definitions/schema";
import shellac from "shellac"; import shellac from "shellac";
import { fetch } from "undici"; import { fetch } from "undici";
import { env } from "process"; import { env } from "process";
import path from "node:path"; import path from "node:path";
type Octokit = ReturnType<typeof getOctokit>; type Octokit = ReturnType<typeof getOctokit>;
type SummaryBodyParams = { deployment: Deployment; aliasUrl: string };
try { try {
const apiToken = getInput("apiToken", { required: true }); 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"); const deployStage = deployment.stages.find((stage) => stage.name === "deploy");
let status = "⚡️ Deployment in progress..."; let status = "⚡️ Deployment in progress...";
@ -117,10 +119,7 @@ try {
} else if (deployStage?.status === "failure") { } else if (deployStage?.status === "failure") {
status = "🚫 Deployment failed"; status = "🚫 Deployment failed";
} }
return `
await summary
.addRaw(
`
# ${projectName} Deploying with Cloudflare Pages # ${projectName} Deploying with Cloudflare Pages
| Name | Result | | Name | Result |
@ -129,9 +128,23 @@ try {
| **Status**: | ${status} | | **Status**: | ${status} |
| **Preview URL**: | ${deployment.url} | | **Preview URL**: | ${deployment.url} |
| **Branch Preview URL**: | ${aliasUrl} | | **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 () => { (async () => {
@ -141,9 +154,10 @@ try {
const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`; const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`;
let gitHubDeployment: Awaited<ReturnType<typeof createGitHubDeployment>>; let gitHubDeployment: Awaited<ReturnType<typeof createGitHubDeployment>>;
let octokit: Octokit | null = null;
if (gitHubToken && gitHubToken.length) { if (gitHubToken && gitHubToken.length) {
const octokit = getOctokit(gitHubToken); octokit = getOctokit(gitHubToken);
gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName); gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName);
} }
@ -160,6 +174,10 @@ try {
await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias }); await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias });
if (octokit && context.eventName === "pull_request") {
await createPullRequesComment(octokit, { deployment: pagesDeployment, aliasUrl: alias });
}
if (gitHubDeployment) { if (gitHubDeployment) {
const octokit = getOctokit(gitHubToken); const octokit = getOctokit(gitHubToken);

@ -0,0 +1,4 @@
export function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message;
return String(error);
}
Loading…
Cancel
Save