Co-authored-by: Daniel Walsh <walshydev@gmail.com>
Fix undefined
This commit is contained in:
Willow (GHOST) 2022-12-20 17:30:56 +00:00 committed by GitHub
parent 226d2ba38e
commit b3c3fa6f93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 34 deletions

View file

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

View file

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