-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for github service, and move all github deployments logic t…
…o be inside of github.ts
- Loading branch information
1 parent
d96ce63
commit ef98565
Showing
8 changed files
with
211 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
import { setupServer } from "msw/node"; | ||
import { createGitHubDeployment, createJobSummary } from "./github"; | ||
import { getOctokit } from "@actions/github"; | ||
import { mockGithubDeployments } from "../test/mocks"; | ||
import { getTestConfig } from "../test/test-utils"; | ||
import mockfs from "mock-fs"; | ||
import { readFile } from "fs/promises"; | ||
|
||
afterEach(() => { | ||
mockfs.restore(); | ||
}); | ||
|
||
describe("github", () => { | ||
it("Calls createGitHubDeployment successfully", async () => { | ||
const githubUser = "mock-user"; | ||
const githubRepoName = "wrangler-action"; | ||
const server = setupServer( | ||
...mockGithubDeployments({ githubUser, githubRepoName }).handlers, | ||
); | ||
server.listen({ onUnhandledRequest: "error" }); | ||
vi.stubEnv("GITHUB_REPOSITORY", `${githubUser}/${githubRepoName}`); | ||
|
||
const testConfig = getTestConfig(); | ||
const octokit = getOctokit(testConfig.GITHUB_TOKEN, { request: fetch }); | ||
await createGitHubDeployment({ | ||
config: testConfig, | ||
octokit, | ||
productionBranch: "production-branch", | ||
deploymentId: "fake-deployment-id", | ||
projectName: "fake-project-name", | ||
deploymentUrl: "https://fake-deployment-url.com", | ||
environment: "production", | ||
}); | ||
server.close(); | ||
}); | ||
it("Calls createJobSummary successfully", async () => { | ||
vi.stubEnv("GITHUB_STEP_SUMMARY", "summary"); | ||
mockfs({ | ||
summary: mockfs.file(), | ||
}); | ||
await createJobSummary({ | ||
commitHash: "fake-commit-hash", | ||
deploymentUrl: "https://fake-deployment-url.com", | ||
aliasUrl: "https://fake-alias-url.com", | ||
}); | ||
expect((await readFile("summary")).toString()).toMatchInlineSnapshot(` | ||
" | ||
# Deploying with Cloudflare Pages | ||
| Name | Result | | ||
| ----------------------- | - | | ||
| **Last commit:** | fake-commit-hash | | ||
| **Preview URL**: | https://fake-deployment-url.com | | ||
| **Branch Preview URL**: | https://fake-alias-url.com | | ||
" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { summary } from "@actions/core"; | ||
import { context, getOctokit } from "@actions/github"; | ||
import { env } from "process"; | ||
import { WranglerActionConfig } from "../wranglerAction"; | ||
import { OutputEntryPagesDeployment } from "../wranglerArtifactManager"; | ||
|
||
type Octokit = ReturnType<typeof getOctokit>; | ||
|
||
export async function createGitHubDeployment({ | ||
config, | ||
octokit, | ||
productionBranch, | ||
environment, | ||
deploymentId, | ||
projectName, | ||
deploymentUrl, | ||
}: { | ||
config: WranglerActionConfig; | ||
octokit: Octokit; | ||
productionBranch: string; | ||
environment: string; | ||
deploymentId: string | null; | ||
projectName: string; | ||
deploymentUrl?: string; | ||
}) { | ||
const githubBranch = env.GITHUB_HEAD_REF || env.GITHUB_REF_NAME; | ||
const productionEnvironment = githubBranch === productionBranch; | ||
|
||
const deployment = await octokit.rest.repos.createDeployment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: githubBranch || context.ref, | ||
auto_merge: false, | ||
description: "Cloudflare Pages", | ||
required_contexts: [], | ||
environment, | ||
production_environment: productionEnvironment, | ||
}); | ||
|
||
if (deployment.status === 201) { | ||
await octokit.rest.repos.createDeploymentStatus({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
deployment_id: deployment.data.id, | ||
environment, | ||
environment_url: deploymentUrl, | ||
production_environment: productionEnvironment, | ||
// don't have project_name or deployment_id I think | ||
log_url: `https://dash.cloudflare.com/${config.CLOUDFLARE_ACCOUNT_ID}/pages/view/${projectName}/${deploymentId}`, | ||
description: "Cloudflare Pages", | ||
state: "success", | ||
auto_inactive: false, | ||
}); | ||
} | ||
} | ||
|
||
export async function createJobSummary({ | ||
commitHash, | ||
deploymentUrl, | ||
aliasUrl, | ||
}: { | ||
commitHash: string; | ||
deploymentUrl?: string; | ||
aliasUrl?: string; | ||
}) { | ||
await summary | ||
.addRaw( | ||
` | ||
# Deploying with Cloudflare Pages | ||
| Name | Result | | ||
| ----------------------- | - | | ||
| **Last commit:** | ${commitHash} | | ||
| **Preview URL**: | ${deploymentUrl} | | ||
| **Branch Preview URL**: | ${aliasUrl} | | ||
`, | ||
) | ||
.write(); | ||
} | ||
|
||
/** | ||
* Create github deployment, if GITHUB_TOKEN is present in config | ||
*/ | ||
export async function createGitHubDeploymentAndJobSummary( | ||
config: WranglerActionConfig, | ||
pagesArtifactFields: OutputEntryPagesDeployment, | ||
) { | ||
if ( | ||
config.GITHUB_TOKEN && | ||
pagesArtifactFields.production_branch && | ||
pagesArtifactFields.project_name && | ||
pagesArtifactFields.deployment_trigger && | ||
pagesArtifactFields.stages | ||
) { | ||
const octokit = getOctokit(config.GITHUB_TOKEN); | ||
await Promise.all([ | ||
createGitHubDeployment({ | ||
config, | ||
octokit, | ||
deploymentUrl: pagesArtifactFields.url, | ||
productionBranch: pagesArtifactFields.production_branch, | ||
environment: pagesArtifactFields.environment, | ||
deploymentId: pagesArtifactFields.deployment_id, | ||
projectName: pagesArtifactFields.project_name, | ||
}), | ||
createJobSummary({ | ||
commitHash: | ||
pagesArtifactFields.deployment_trigger.metadata.commit_hash.substring( | ||
0, | ||
8, | ||
), | ||
deploymentUrl: pagesArtifactFields.url, | ||
aliasUrl: pagesArtifactFields.alias, | ||
}), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters