diff --git a/index.js b/index.js index e15008d..24a2664 100644 --- a/index.js +++ b/index.js @@ -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 }); } })(); diff --git a/src/index.ts b/src/index.ts index 49c5300..0fd82e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; 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})`; - } - - const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName); + let gitHubDeployment: Awaited>; + if (gitHubToken && gitHubToken.length) { + const octokit = getOctokit(gitHubToken); + gitHubDeployment = await createGitHubDeployment(octokit, 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 }); } })();