mirror of
https://github.com/actions/deploy-pages.git
synced 2025-12-08 16:16:16 +00:00
Wrap Twirp responses like Octokit responses for consistency
This commit is contained in:
9
package-lock.json
generated
9
package-lock.json
generated
@@ -11,7 +11,9 @@
|
||||
"dependencies": {
|
||||
"@actions/artifact": "^2.0.0",
|
||||
"@actions/core": "^1.10.1",
|
||||
"@actions/github": "^6.0.0"
|
||||
"@actions/github": "^6.0.0",
|
||||
"@octokit/request-error": "^5.0.1",
|
||||
"http-status-messages": "^1.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
@@ -4670,6 +4672,11 @@
|
||||
"integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/http-status-messages": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/http-status-messages/-/http-status-messages-1.1.0.tgz",
|
||||
"integrity": "sha512-zq9mKkNX6w6qYtNE0aAiH+urvEenUUIyoq8eAWQh2prhA5o03NETCOm/D2GIVt/qCFItt+23Sm1E7HyelPvi6w=="
|
||||
},
|
||||
"node_modules/human-signals": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz",
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
"dependencies": {
|
||||
"@actions/artifact": "^2.0.0",
|
||||
"@actions/core": "^1.10.1",
|
||||
"@actions/github": "^6.0.0"
|
||||
"@actions/github": "^6.0.0",
|
||||
"@octokit/request-error": "^5.0.1",
|
||||
"http-status-messages": "^1.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
|
||||
@@ -1,16 +1,80 @@
|
||||
const core = require('@actions/core')
|
||||
const github = require('@actions/github')
|
||||
const { DefaultArtifactClient } = require('@actions/artifact')
|
||||
const { RequestError } = require('@octokit/request-error')
|
||||
const HttpStatusMessages = require('http-status-messages')
|
||||
|
||||
function wrapTwirpResponseLikeOctokit(twirpResponse, requestOptions) {
|
||||
// Specific response shape aligned with Octokit
|
||||
const response = {
|
||||
url: requestOptions.url,
|
||||
status: 200,
|
||||
headers: {
|
||||
...requestOptions.headers
|
||||
},
|
||||
data: twirpResponse
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
// Mimic the errors thrown by Octokit for consistency.
|
||||
function wrapTwirpErrorLikeOctokit(twirpError, requestOptions) {
|
||||
const rawErrorMsg = twirpError?.message || twirpError?.toString() || ''
|
||||
const statusCodeMatch = rawErrorMsg.match(/Failed request: \((?<statusCode>\d+)\)/)
|
||||
const statusCode = statusCodeMatch?.groups?.statusCode ?? 500
|
||||
|
||||
// Try to provide the best error message
|
||||
const errorMsg =
|
||||
rawErrorMsg ||
|
||||
// Fallback to the HTTP status message based on the status code
|
||||
HttpStatusMessages[statusCode] ||
|
||||
// Or if the status code is unexpected...
|
||||
`Unknown error (${statusCode})`
|
||||
|
||||
// RequestError is an Octokit-specific class
|
||||
return new RequestError(errorMsg, statusCode, {
|
||||
response: {
|
||||
url: requestOptions.url,
|
||||
status: statusCode,
|
||||
headers: {
|
||||
...requestOptions.headers
|
||||
},
|
||||
data: rawErrorMsg ? { message: rawErrorMsg } : ''
|
||||
},
|
||||
request: requestOptions
|
||||
})
|
||||
}
|
||||
|
||||
function getArtifactsServiceOrigin() {
|
||||
const resultsUrl = process.env.ACTIONS_RESULTS_URL
|
||||
return resultsUrl ? new URL(resultsUrl).origin : ''
|
||||
}
|
||||
|
||||
async function getArtifactMetadata({ artifactName }) {
|
||||
const artifactClient = new DefaultArtifactClient()
|
||||
|
||||
// Primarily for debugging purposes, accuracy is not critical
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
url: `${getArtifactsServiceOrigin()}/twirp/github.actions.results.api.v1.ArtifactService/ListArtifacts`,
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
core.info(`Fetching artifact metadata for ${artifactName} in this workflow run`)
|
||||
|
||||
const response = await artifactClient.listArtifacts()
|
||||
let response
|
||||
try {
|
||||
const twirpResponse = await artifactClient.listArtifacts()
|
||||
response = wrapTwirpResponseLikeOctokit(twirpResponse, requestOptions)
|
||||
} catch (twirpError) {
|
||||
const octokitError = wrapTwirpErrorLikeOctokit(twirpError, requestOptions)
|
||||
throw octokitError
|
||||
}
|
||||
|
||||
const filteredArtifacts = response.artifacts.filter(artifact => artifact.name === artifactName)
|
||||
const filteredArtifacts = response.data.artifacts.filter(artifact => artifact.name === artifactName)
|
||||
|
||||
const artifactCount = filteredArtifacts.length
|
||||
core.debug(`List artifact count: ${artifactCount}`)
|
||||
|
||||
@@ -62,12 +62,8 @@ class Deployment {
|
||||
core.debug(`Action ID: ${this.actionsId}`)
|
||||
core.debug(`Actions Workflow Run ID: ${this.workflowRun}`)
|
||||
|
||||
let artifactData
|
||||
try {
|
||||
artifactData = await getArtifactMetadata({ artifactName: this.artifactName })
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to create deployment: ${error.message}`)
|
||||
}
|
||||
const artifactData = await getArtifactMetadata({ artifactName: this.artifactName })
|
||||
|
||||
if (artifactData?.size > ONE_GIGABYTE) {
|
||||
core.warning(
|
||||
@@ -75,7 +71,6 @@ class Deployment {
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const deployment = await createPagesDeployment({
|
||||
githubToken: this.githubToken,
|
||||
artifactId: artifactData.id,
|
||||
|
||||
Reference in New Issue
Block a user