Skip to content

Commit

Permalink
fix(e2e): utlise new backend endpoint to reset e2e repos (#1536)
Browse files Browse the repository at this point in the history
* fix(e2e): utlise new backend endpoint to reset e2e repos

* fix(e2e): avoid printing actual content of env var
  • Loading branch information
dcshzj authored Oct 4, 2023
1 parent 16203d5 commit 947b817
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 63 deletions.
12 changes: 0 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
"@typescript-eslint/parser": "^5.55.0",
"auto-changelog": "^2.4.0",
"babel-jest": "^29.5.0",
"btoa": "^1.2.1",
"chromatic": "^6.5.6",
"cypress": "^12.8.0",
"cypress-pipe": "^2.0.0",
Expand Down
90 changes: 44 additions & 46 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
const { spawn } = require("child_process")

const axios = require("axios")
const btoa = require("btoa")

// login credentials
const { PERSONAL_ACCESS_TOKEN, USERNAME } = process.env
if (!PERSONAL_ACCESS_TOKEN || !USERNAME) {
// Login credentials
const {
CYPRESS_COOKIE_NAME,
CYPRESS_COOKIE_VALUE,
CYPRESS_BACKEND_URL,
} = process.env
if (!CYPRESS_COOKIE_NAME || !CYPRESS_COOKIE_VALUE) {
throw new Error(
`E2E tests require env vars PERSONAL_ACCESS_TOKEN (${PERSONAL_ACCESS_TOKEN}) and USERNAME (${USERNAME}) to be set.`
`E2E tests require env vars CYPRESS_COOKIE_NAME and CYPRESS_COOKIE_VALUE to be set.`
)
}

const CREDENTIALS = `${USERNAME}:${PERSONAL_ACCESS_TOKEN}`
const headers = {
authorization: `basic ${btoa(CREDENTIALS)}`,
accept: "application/json",
const emailHeaders = {
Cookie: `${CYPRESS_COOKIE_NAME}=${CYPRESS_COOKIE_VALUE}; e2eUserType=Email admin; site=e2e email test site; [email protected]`,
}

// cypress commands
const githubHeaders = {
Cookie: `${CYPRESS_COOKIE_NAME}=${CYPRESS_COOKIE_VALUE}; e2eUserType=Github user`,
}

// Cypress test runner
const cypressCommand = process.argv[2] // either `run` or `open`
const baseCypressCommand = `npx cypress ${cypressCommand}`
// if we are running tests, record them on the dashboard so that github will show the corresponding status
Expand All @@ -27,49 +32,40 @@ const runCypressCommand =
? `${baseCypressCommand} --record`
: baseCypressCommand

// reset e2e-test-repo
const { E2E_COMMIT_HASH } = process.env
if (!E2E_COMMIT_HASH) {
throw new Error(
`E2E tests require env var E2E_COMMIT_HASH (${E2E_COMMIT_HASH}) to be set.`
)
// E2E test repositories
const e2eTestRepositoriesWithHashes = {
"e2e-test-repo": "bcfe46da1288b3302c5bb5f72c5c58b50574f26c",
"e2e-email-test-repo": "93593ceb8ee8af690267e49ea787701fc73baed8",
"e2e-notggs-test-repo": "1ccc5253dd06e06a088d1e6ec86a38c870c0a3d6",
}
const GITHUB_ORG_NAME = "isomerpages"
const E2E_GITHUB_REPO_NAME = "e2e-test-repo"
const E2E_EMAIL_REPO_NAME = "e2e-email-test-repo"
const E2E_EMAIL_COMMIT_HASH = "93593ceb8ee8af690267e49ea787701fc73baed8"

const resetRepo = async (repo, hash, branch) => {
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${repo}/git/refs/heads/${branch}`
await axios.patch(
// Reset test repositories
const resetRepository = async (repository, branchName, userType) => {
const endpoint = `${CYPRESS_BACKEND_URL}/sites/${repository}/admin/resetRepo`
const headers = userType === "github" ? githubHeaders : emailHeaders
await axios.post(
endpoint,
{
sha: hash,
force: true,
commitSha: e2eTestRepositoriesWithHashes[repository],
branchName,
},
{
headers,
}
{ headers },
{ withCredentials: true }
)
console.log(
`Successfully reset ${repository} (${branchName}) to ${e2eTestRepositoriesWithHashes[repository]}}`
)
console.log(`Successfully reset ${repo} to ${hash}`)
}

const resetGithubE2eTestRepo = () =>
resetRepo(E2E_GITHUB_REPO_NAME, E2E_COMMIT_HASH, "staging")

const resetEmailE2eTestRepo = () => {
resetRepo(E2E_EMAIL_REPO_NAME, E2E_EMAIL_COMMIT_HASH, "staging")
resetRepo(E2E_EMAIL_REPO_NAME, E2E_EMAIL_COMMIT_HASH, "master")
}

const resetE2eTestRepo = async () => {
await resetGithubE2eTestRepo()
await resetEmailE2eTestRepo()
const resetE2ETestRepositories = async () => {
resetRepository("e2e-test-repo", "staging", "github")
resetRepository("e2e-notggs-test-repo", "staging", "github")
resetRepository("e2e-email-test-repo", "staging", "email")
resetRepository("e2e-email-test-repo", "master", "email")
}

// resets the e2e repo then runs the corresponding cypress command
const runE2eTests = async () =>
resetE2eTestRepo()
const runE2ETests = async () => {
resetE2ETestRepositories()
.then(() => {
const child = spawn(runCypressCommand, { shell: true })
child.stderr.on("data", (data) => {
Expand All @@ -83,7 +79,8 @@ const runE2eTests = async () =>
// NOTE: Cypress uses the exit code to show the number of failed tests.
// If we do not exit here, the child process (that's running the test) will not be able to feedback
// to Github Actions if the tests pass.
process.exit(exitCode)
// If exitCode is null, means that the child process was killed by a signal.
process.exit(exitCode || 1)
})
})
.catch((err) => {
Expand All @@ -92,8 +89,9 @@ const runE2eTests = async () =>
// This also prevents CI from recording this as a successful step.
process.exit(1)
})
}

module.exports = {
runE2eTests,
resetE2eTestRepo,
runE2ETests,
resetE2ETestRepositories,
}
4 changes: 2 additions & 2 deletions scripts/reset-e2e.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { resetE2eTestRepo } = require("./build")
const { resetE2ETestRepositories } = require("./build")

resetE2eTestRepo()
resetE2ETestRepositories()
4 changes: 2 additions & 2 deletions scripts/run-e2e.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { runE2eTests } = require("./build")
const { runE2ETests } = require("./build")

runE2eTests()
runE2ETests()

0 comments on commit 947b817

Please sign in to comment.