rsnext/scripts/reset-project.mjs
Zack Tanner 0cb1c40400
ci: disable deployment protection for e2e test project (#58830)
Since we reset the test project on every e2e CI run, deployment protection is automatically enabled by default.

This adds an option to the reset project workflow to disable deployment protection. Our test runners need to be able to hit these pages from an unauthenticated browser in order for the tests to work. 

Verified tests are running properly in [this run](https://github.com/vercel/next.js/actions/runs/6971348806/job/18971225559) (fixing any failing tests themselves are out of scope for this PR; will evaluate once the run finishes)

Closes NEXT-1732
2023-11-23 09:41:34 -08:00

94 lines
2.3 KiB
JavaScript

import fetch from 'node-fetch'
export const TEST_PROJECT_NAME = 'vtest314-e2e-tests'
export const TEST_TEAM_NAME = process.env.VERCEL_TEST_TEAM
export const TEST_TOKEN = process.env.VERCEL_TEST_TOKEN
export async function resetProject({
teamId = TEST_TEAM_NAME,
projectName = TEST_PROJECT_NAME,
disableDeploymentProtection,
}) {
console.log(`Resetting project ${teamId}/${projectName}`)
// TODO: error/bail if existing deployments are pending
const deleteRes = await fetch(
`https://vercel.com/api/v8/projects/${encodeURIComponent(
projectName
)}?teamId=${teamId}`,
{
method: 'DELETE',
headers: {
Authorization: `Bearer ${TEST_TOKEN}`,
},
}
)
if (!deleteRes.ok && deleteRes.status !== 404) {
throw new Error(
`Failed to delete project got status ${
deleteRes.status
}, ${await deleteRes.text()}`
)
}
const createRes = await fetch(
`https://vercel.com/api/v8/projects?teamId=${teamId}`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({
framework: 'nextjs',
name: projectName,
}),
}
)
if (!createRes.ok) {
throw new Error(
`Failed to create project. Got status: ${
createRes.status
}, ${await createRes.text()}`
)
}
const { id: projectId } = await createRes.json()
if (!projectId) {
throw new Error("Couldn't get projectId from create project response")
}
if (disableDeploymentProtection) {
console.log('Disabling deployment protection...')
const patchRes = await fetch(
`https://vercel.com/api/v8/projects/${encodeURIComponent(
projectId
)}?teamId=${teamId}`,
{
method: 'PATCH',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({
ssoProtection: null,
}),
}
)
if (!patchRes.ok) {
throw new Error(
`Failed to disable deployment protection. Got status: ${
patchRes.status
}, ${await patchRes.text()}`
)
}
}
console.log(
`Successfully created fresh Vercel project ${teamId}/${projectName}`
)
}