Co-authored-by: Daniel Walsh <walshydev@gmail.com>
Fix undefined
pull/45/head
Willow (GHOST) 2 years ago committed by GitHub
parent 226d2ba38e
commit b3c3fa6f93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22067,7 +22067,6 @@ try {
const directory = (0, import_core.getInput)("directory", { required: true }); const directory = (0, import_core.getInput)("directory", { required: true });
const gitHubToken = (0, import_core.getInput)("gitHubToken", { required: false }); const gitHubToken = (0, import_core.getInput)("gitHubToken", { required: false });
const branch = (0, import_core.getInput)("branch", { required: false }); const branch = (0, import_core.getInput)("branch", { required: false });
let octokit;
const getProject = async () => { const getProject = async () => {
const response = await (0, import_undici.fetch)( const response = await (0, import_undici.fetch)(
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`, `https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
@ -22094,7 +22093,7 @@ try {
} = await response.json(); } = await response.json();
return deployment; return deployment;
}; };
const createGitHubDeployment = async (productionEnvironment, environment) => { const createGitHubDeployment = async (octokit, productionEnvironment, environment) => {
const deployment = await octokit.rest.repos.createDeployment({ const deployment = await octokit.rest.repos.createDeployment({
owner: import_github.context.repo.owner, owner: import_github.context.repo.owner,
repo: import_github.context.repo.repo, repo: import_github.context.repo.repo,
@ -22114,7 +22113,8 @@ try {
url, url,
deploymentId, deploymentId,
environmentName, environmentName,
productionEnvironment productionEnvironment,
octokit
}) => { }) => {
await octokit.rest.repos.createDeploymentStatus({ await octokit.rest.repos.createDeploymentStatus({
owner: import_github.context.repo.owner, owner: import_github.context.repo.owner,
@ -22129,20 +22129,17 @@ try {
}); });
}; };
(async () => { (async () => {
if (gitHubToken === "") {
return;
}
const project = await getProject(); const project = await getProject();
octokit = (0, import_github.getOctokit)(gitHubToken); if (!project)
throw new Error("Unable to find pages project");
const githubBranch = import_process.env.GITHUB_REF_NAME; const githubBranch = import_process.env.GITHUB_REF_NAME;
const productionEnvironment = githubBranch === project.production_branch; const productionEnvironment = githubBranch === project.production_branch;
let environmentName; const environmentName = productionEnvironment ? "Production" : `Preview: (${githubBranch})`;
if (productionEnvironment) { let gitHubDeployment;
environmentName = "Production"; if (gitHubToken && gitHubToken.length) {
} else { const octokit = (0, import_github.getOctokit)(gitHubToken);
environmentName = `Preview (${githubBranch})`; gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName);
} }
const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName);
const pagesDeployment = await createPagesDeployment(); const pagesDeployment = await createPagesDeployment();
(0, import_core.setOutput)("id", pagesDeployment.id); (0, import_core.setOutput)("id", pagesDeployment.id);
(0, import_core.setOutput)("url", pagesDeployment.url); (0, import_core.setOutput)("url", pagesDeployment.url);
@ -22153,12 +22150,14 @@ try {
} }
(0, import_core.setOutput)("alias", alias); (0, import_core.setOutput)("alias", alias);
if (gitHubDeployment) { if (gitHubDeployment) {
const octokit = (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
}); });
} }
})(); })();

@ -1,9 +1,11 @@
import { getInput, setOutput, setFailed } from "@actions/core"; import { getInput, setOutput, setFailed } from "@actions/core";
import type { Project, Deployment } from '@cloudflare/types';
import { context, getOctokit } from "@actions/github"; import { context, getOctokit } from "@actions/github";
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 type { Project, Deployment } from '@cloudflare/types';
type Octokit = ReturnType<typeof getOctokit>;
try { try {
const apiToken = getInput("apiToken", { required: true }); const apiToken = getInput("apiToken", { required: true });
@ -13,14 +15,12 @@ try {
const gitHubToken = getInput("gitHubToken", { required: false }); const gitHubToken = getInput("gitHubToken", { required: false });
const branch = getInput("branch", { required: false }); const branch = getInput("branch", { required: false });
let octokit;
const getProject = async () => { const getProject = async () => {
const response = await fetch( const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`, `https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
{ headers: { Authorization: `Bearer ${apiToken}` } } { headers: { Authorization: `Bearer ${apiToken}` } }
); );
const { result } = await response.json() as { result: Project }; const { result } = await response.json() as { result: Project | null };
return result; return result;
} }
@ -46,7 +46,7 @@ try {
return deployment; return deployment;
}; };
const createGitHubDeployment = async (productionEnvironment: boolean, environment: string) => { const createGitHubDeployment = async (octokit: Octokit, productionEnvironment: boolean, environment: string) => {
const deployment = await octokit.rest.repos.createDeployment({ const deployment = await octokit.rest.repos.createDeployment({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@ -69,7 +69,9 @@ try {
deploymentId, deploymentId,
environmentName, environmentName,
productionEnvironment, productionEnvironment,
octokit,
}: { }: {
octokit: Octokit,
id: number; id: number;
url: string; url: string;
deploymentId: string; deploymentId: string;
@ -91,28 +93,21 @@ try {
}; };
(async () => { (async () => {
if (gitHubToken === "") {
return;
}
const project = await getProject(); const project = await getProject();
octokit = getOctokit(gitHubToken); if (!project) throw new Error('Unable to find pages project')
const githubBranch = env.GITHUB_REF_NAME; const githubBranch = env.GITHUB_REF_NAME;
const productionEnvironment = githubBranch === project.production_branch; const productionEnvironment = githubBranch === project.production_branch;
const environmentName = productionEnvironment ? "Production" : `Preview: (${githubBranch})`;
let environmentName: string; let gitHubDeployment: Awaited<ReturnType<typeof createGitHubDeployment>>;
if (productionEnvironment) {
environmentName = "Production"
} else {
// Use the branch name
environmentName = `Preview (${githubBranch})`;
}
const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName);
if (gitHubToken && gitHubToken.length) {
const octokit = getOctokit(gitHubToken);
gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName);
}
const pagesDeployment = await createPagesDeployment(); const pagesDeployment = await createPagesDeployment();
setOutput("id", pagesDeployment.id); setOutput("id", pagesDeployment.id);
setOutput("url", pagesDeployment.url); setOutput("url", pagesDeployment.url);
setOutput("environment", pagesDeployment.environment); setOutput("environment", pagesDeployment.environment);
@ -124,12 +119,15 @@ try {
setOutput("alias", alias); setOutput("alias", alias);
if (gitHubDeployment) { if (gitHubDeployment) {
const octokit = 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
}); });
} }
})(); })();

Loading…
Cancel
Save