-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parity with pages-action for pages deploy outputs
- Loading branch information
1 parent
cd8a317
commit 3ec7f89
Showing
6 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"wrangler-action": minor | ||
--- | ||
|
||
Support id, environment, url, and alias outputs for Pages deploys. |
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
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,89 @@ | ||
import mock from "mock-fs"; | ||
import { afterEach, describe, expect, it } from "vitest"; | ||
import { | ||
getDetailedPagesDeployOutput, | ||
getWranglerArtifacts, | ||
} from "./wranglerArtifactManager"; | ||
|
||
afterEach(async () => { | ||
mock.restore(); | ||
}); | ||
describe("wranglerArtifactsManager", () => { | ||
describe("getWranglerArtifacts()", async () => { | ||
it("Returns only wrangler output files from a given directory", async () => { | ||
mock({ | ||
testOutputDir: { | ||
"wrangler-output-2024-10-17_18-48-40_463-2e6e83.json": ` | ||
{"version": 1, "type":"wrangler-session", "wrangler_version":"3.81.0", "command_line_args":["what's up"], "log_file_path": "/here"} | ||
{"version": 1, "type":"pages-deploy-detailed", "environment":"production", "alias":"test.com", "deployment_id": "123", "url":"url.com"}`, | ||
"not-wrangler-output.json": "test", | ||
}, | ||
}); | ||
|
||
const artifacts = await getWranglerArtifacts("./testOutputDir"); | ||
|
||
expect(artifacts).toEqual([ | ||
"./testOutputDir/wrangler-output-2024-10-17_18-48-40_463-2e6e83.json", | ||
]); | ||
//mock.restore(); | ||
}); | ||
it("Returns an empty list when the output directory doesn't exist", async () => { | ||
mock({ | ||
notTheDirWeWant: {}, | ||
}); | ||
|
||
const artifacts = await getWranglerArtifacts("./testOutputDir"); | ||
expect(artifacts).toEqual([]); | ||
//mock.restore(); | ||
}); | ||
}); | ||
|
||
describe("getDetailedPagesDeployOutput()", async () => { | ||
it("Returns only detailed pages deploy output from wrangler artifacts", async () => { | ||
mock({ | ||
testOutputDir: { | ||
"wrangler-output-2024-10-17_18-48-40_463-2e6e83.json": ` | ||
{"version": 1, "type":"wrangler-session", "wrangler_version":"3.81.0", "command_line_args":["what's up"], "log_file_path": "/here"} | ||
{"version": 1, "type":"pages-deploy-detailed", "pages_project": "project", "environment":"production", "alias":"test.com", "deployment_id": "123", "url":"url.com"}`, | ||
"not-wrangler-output.json": "test", | ||
}, | ||
}); | ||
|
||
const artifacts = await getDetailedPagesDeployOutput("./testOutputDir"); | ||
|
||
expect(artifacts).toEqual({ | ||
version: 1, | ||
pages_project: "project", | ||
type: "pages-deploy-detailed", | ||
url: "url.com", | ||
environment: "production", | ||
deployment_id: "123", | ||
alias: "test.com", | ||
}); | ||
//mock.restore(); | ||
}), | ||
it("Skips artifact entries that are not parseable", async () => { | ||
mock({ | ||
testOutputDir: { | ||
"wrangler-output-2024-10-17_18-48-40_463-2e6e83.json": ` | ||
this line is invalid json. | ||
{"version": 1, "type":"pages-deploy-detailed", "pages_project": "project", "environment":"production", "alias":"test.com", "deployment_id": "123", "url":"url.com"}`, | ||
"not-wrangler-output.json": "test", | ||
}, | ||
}); | ||
|
||
const artifacts = await getDetailedPagesDeployOutput("./testOutputDir"); | ||
|
||
expect(artifacts).toEqual({ | ||
version: 1, | ||
type: "pages-deploy-detailed", | ||
pages_project: "project", | ||
url: "url.com", | ||
environment: "production", | ||
deployment_id: "123", | ||
alias: "test.com", | ||
}); | ||
//mock.restore(); | ||
}); | ||
}); | ||
}); |
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,86 @@ | ||
import { access, open, readdir } from "fs/promises"; | ||
import { z } from "zod"; | ||
|
||
const OutputEntryBase = z.object({ | ||
version: z.literal(1), | ||
type: z.string(), | ||
}); | ||
|
||
const OutputEntryPagesDeployment = OutputEntryBase.merge( | ||
z.object({ | ||
type: z.literal("pages-deploy-detailed"), | ||
pages_project: z.string().nullable(), | ||
deployment_id: z.string().nullable(), | ||
url: z.string().optional(), | ||
alias: z.string().optional(), | ||
environment: z.enum(["production", "preview"]), | ||
}), | ||
); | ||
|
||
type OutputEntryPagesDeployment = z.infer<typeof OutputEntryPagesDeployment>; | ||
|
||
/** | ||
* Parses file names in a directory to find wrangler artifact files | ||
* | ||
* @param artifactDirectory | ||
* @returns All artifact files from the directory | ||
*/ | ||
export async function getWranglerArtifacts( | ||
artifactDirectory: string, | ||
): Promise<string[]> { | ||
try { | ||
await access(artifactDirectory); | ||
} catch { | ||
return []; | ||
} | ||
|
||
// read files in asset directory | ||
const dirent = await readdir(artifactDirectory, { | ||
withFileTypes: true, | ||
recursive: false, | ||
}); | ||
|
||
// Match files to wrangler-output-<timestamp>-xxxxxx.json | ||
const regex = new RegExp( | ||
/^wrangler-output-[\d]{4}-[\d]{2}-[\d]{2}_[\d]{2}-[\d]{2}-[\d]{2}_[\d]{3}-[A-Fa-f0-9]{6}\.json$/, | ||
); | ||
const artifactFilePaths = dirent | ||
.filter((d) => d.name.match(regex)) | ||
.map((d) => `${artifactDirectory}/${d.name}`); | ||
|
||
return artifactFilePaths; | ||
} | ||
|
||
/** | ||
* Searches for detailed wrangler output from a pages deploy | ||
* | ||
* @param artifactDirectory | ||
* @returns The first pages-output-detailed found within a wrangler artifact directory | ||
*/ | ||
export async function getDetailedPagesDeployOutput( | ||
artifactDirectory: string, | ||
): Promise<OutputEntryPagesDeployment | null> { | ||
const artifactFilePaths = await getWranglerArtifacts(artifactDirectory); | ||
|
||
for (let i = 0; i < artifactFilePaths.length; i++) { | ||
const file = await open(artifactFilePaths[i], "r"); | ||
|
||
for await (const line of file.readLines()) { | ||
try { | ||
const output = JSON.parse(line); | ||
const parsedOutput = OutputEntryPagesDeployment.parse(output); | ||
if (parsedOutput.type === "pages-deploy-detailed") { | ||
// Assume, in the context of the action, the first detailed deploy instance seen will suffice | ||
return parsedOutput; | ||
} | ||
} catch (err) { | ||
// If the line can't be parsed, skip it | ||
continue; | ||
} | ||
} | ||
|
||
await file.close(); | ||
} | ||
|
||
return null; | ||
} |