diff --git a/.github/workflows/BuildJobs.yml b/.github/workflows/BuildJobs.yml index 9173c6ebbf..ab7f7f4321 100644 --- a/.github/workflows/BuildJobs.yml +++ b/.github/workflows/BuildJobs.yml @@ -162,6 +162,8 @@ jobs: - name: Install dependencies run: pnpm install - name: "Executing E2E tests" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: >- node ./automation/run-e2e/bin/run-e2e-in-chunks.mjs diff --git a/automation/run-e2e/lib/ci.mjs b/automation/run-e2e/lib/ci.mjs index 880b59b742..8bb13566f2 100644 --- a/automation/run-e2e/lib/ci.mjs +++ b/automation/run-e2e/lib/ci.mjs @@ -1,15 +1,15 @@ import c from "ansi-colors"; import findFreePort from "find-free-port"; import nodeIp from "ip"; -import fetch from "node-fetch"; import { execSync } from "node:child_process"; import sh from "shelljs"; import parseArgs from "yargs-parser"; import { createDeploymentBundle, prepareImage, startCypress, startRuntime } from "./docker-utils.mjs"; import { setupTestProject } from "./setup-test-project.mjs"; -import { updateWidget } from "./utils.mjs"; +import { fetchWithReport, updateWidget } from "./utils.mjs"; -const MX_VERSION_MAP_URL = "https://raw.githubusercontent.com/mendix/web-widgets/main/automation/run-e2e/mendix-versions.json"; +const MX_VERSION_MAP_URL = + "https://raw.githubusercontent.com/mendix/web-widgets/main/automation/run-e2e/mendix-versions.json"; const { ls, cat } = sh; @@ -89,7 +89,7 @@ async function getMendixVersion(options) { return process.env.MENDIX_VERSION; } - const versionMapResponse = await fetch(MX_VERSION_MAP_URL); + const versionMapResponse = await fetchWithReport(MX_VERSION_MAP_URL); if (versionMapResponse.ok) { const { mxVersion } = options; const versionMap = await versionMapResponse.json(); diff --git a/automation/run-e2e/lib/setup-test-project.mjs b/automation/run-e2e/lib/setup-test-project.mjs index 2a3207e8a6..22f715ab0b 100644 --- a/automation/run-e2e/lib/setup-test-project.mjs +++ b/automation/run-e2e/lib/setup-test-project.mjs @@ -5,10 +5,9 @@ import { mkdtemp } from "node:fs/promises"; import { pipeline } from "node:stream"; import { promisify } from "node:util"; import { join } from "node:path"; -import fetch from "node-fetch"; import sh from "shelljs"; import crossZip from "cross-zip"; -import { packageMeta } from "./utils.mjs"; +import { packageMeta, fetchGithubRestAPI, fetchWithReport } from "./utils.mjs"; const { cp, ls, mkdir, rm, mv } = sh; const streamPipe = promisify(pipeline); @@ -58,7 +57,7 @@ async function downloadTestProject(repository, branch) { try { await streamPipe( ( - await fetch(`${repository}/archive/refs/heads/${branch}.zip`) + await fetchWithReport(`${repository}/archive/refs/heads/${branch}.zip`) ).body, createWriteStream(downloadedArchivePath) ); @@ -81,14 +80,16 @@ async function updateAtlas() { "tests/testProject/themesource/datawidgets" ); - const releasesResponse = await fetch("https://api.github.com/repos/mendix/StarterApp_Blank/releases/latest"); + const releasesResponse = await fetchGithubRestAPI( + "https://api.github.com/repos/mendix/StarterApp_Blank/releases/latest" + ); if (releasesResponse.ok) { const release = await releasesResponse.json(); const [{ browser_download_url }] = release.assets; const downloadedPath = join(await usetmp(), `StarterAppRelease.zip`); const outPath = await usetmp(); try { - await streamPipe((await fetch(browser_download_url)).body, createWriteStream(downloadedPath)); + await streamPipe((await fetchWithReport(browser_download_url)).body, createWriteStream(downloadedPath)); crossZip.unzipSync(downloadedPath, outPath); cp("-r", join(outPath, "theme"), "tests/testProject"); cp("-r", join(outPath, "themesource"), "tests/testProject"); diff --git a/automation/run-e2e/lib/utils.mjs b/automation/run-e2e/lib/utils.mjs index c71d75e1ad..ce945dbeef 100644 --- a/automation/run-e2e/lib/utils.mjs +++ b/automation/run-e2e/lib/utils.mjs @@ -45,18 +45,38 @@ export async function updateWidget() { cp("-f", mpkPath, outDir); } -export async function await200(url = "http://localhost:8080", attempts = 50) { +export async function fetchWithReport(url, init) { + const response = await fetch(url, init); + if (response.ok) { + return response; + } + console.error(`HTTP Error Response: ${response.status} ${response.statusText}`); + const errorBody = await response.text(); + console.error(`Error body: ${errorBody}`); + throw new Error("HTTP Error"); +} + +export async function fetchGithubRestAPI(url, init = {}) { + const token = process.env.GITHUB_TOKEN; + assert.ok(typeof token === "string" && token.length > 0, "GITHUB_TOKEN is missing"); + + return fetchWithReport(url, { + ...init, + headers: { + Accept: "application/vnd.github+json", + Authorization: `Bearer ${token}`, + "X-GitHub-Api-Version": "2022-11-28", + ...init.headers + } + }); +} + +export async function await200(url = "http://127.0.0.1:8080", attempts = 50) { let n = 0; while (++n <= attempts) { console.log(c.cyan(`GET ${url} ${n}`)); - let response; - try { - response = await fetch(url); - } catch { - // ignore - } - - const { ok, status } = response ?? {}; + const response = await fetch(url); + const { ok, status } = response; if (ok && status === 200) { console.log(c.green(`200 OK, continue`));