Skip to content

Commit b85c846

Browse files
author
Illia Obukhau
authored
feat(run-e2e): authorize github api requests (#219)
2 parents 2eca449 + cd63963 commit b85c846

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

.github/workflows/BuildJobs.yml

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ jobs:
162162
- name: Install dependencies
163163
run: pnpm install
164164
- name: "Executing E2E tests"
165+
env:
166+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
165167
run: >-
166168
node
167169
./automation/run-e2e/bin/run-e2e-in-chunks.mjs

automation/run-e2e/lib/ci.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import c from "ansi-colors";
22
import findFreePort from "find-free-port";
33
import nodeIp from "ip";
4-
import fetch from "node-fetch";
54
import { execSync } from "node:child_process";
65
import sh from "shelljs";
76
import parseArgs from "yargs-parser";
87
import { createDeploymentBundle, prepareImage, startCypress, startRuntime } from "./docker-utils.mjs";
98
import { setupTestProject } from "./setup-test-project.mjs";
10-
import { updateWidget } from "./utils.mjs";
9+
import { fetchWithReport, updateWidget } from "./utils.mjs";
1110

12-
const MX_VERSION_MAP_URL = "https://raw.githubusercontent.com/mendix/web-widgets/main/automation/run-e2e/mendix-versions.json";
11+
const MX_VERSION_MAP_URL =
12+
"https://raw.githubusercontent.com/mendix/web-widgets/main/automation/run-e2e/mendix-versions.json";
1313

1414
const { ls, cat } = sh;
1515

@@ -89,7 +89,7 @@ async function getMendixVersion(options) {
8989
return process.env.MENDIX_VERSION;
9090
}
9191

92-
const versionMapResponse = await fetch(MX_VERSION_MAP_URL);
92+
const versionMapResponse = await fetchWithReport(MX_VERSION_MAP_URL);
9393
if (versionMapResponse.ok) {
9494
const { mxVersion } = options;
9595
const versionMap = await versionMapResponse.json();

automation/run-e2e/lib/setup-test-project.mjs

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { mkdtemp } from "node:fs/promises";
55
import { pipeline } from "node:stream";
66
import { promisify } from "node:util";
77
import { join } from "node:path";
8-
import fetch from "node-fetch";
98
import sh from "shelljs";
109
import crossZip from "cross-zip";
11-
import { packageMeta } from "./utils.mjs";
10+
import { packageMeta, fetchGithubRestAPI, fetchWithReport } from "./utils.mjs";
1211

1312
const { cp, ls, mkdir, rm, mv } = sh;
1413
const streamPipe = promisify(pipeline);
@@ -58,7 +57,7 @@ async function downloadTestProject(repository, branch) {
5857
try {
5958
await streamPipe(
6059
(
61-
await fetch(`${repository}/archive/refs/heads/${branch}.zip`)
60+
await fetchWithReport(`${repository}/archive/refs/heads/${branch}.zip`)
6261
).body,
6362
createWriteStream(downloadedArchivePath)
6463
);
@@ -81,14 +80,16 @@ async function updateAtlas() {
8180
"tests/testProject/themesource/datawidgets"
8281
);
8382

84-
const releasesResponse = await fetch("https://api.github.com/repos/mendix/StarterApp_Blank/releases/latest");
83+
const releasesResponse = await fetchGithubRestAPI(
84+
"https://api.github.com/repos/mendix/StarterApp_Blank/releases/latest"
85+
);
8586
if (releasesResponse.ok) {
8687
const release = await releasesResponse.json();
8788
const [{ browser_download_url }] = release.assets;
8889
const downloadedPath = join(await usetmp(), `StarterAppRelease.zip`);
8990
const outPath = await usetmp();
9091
try {
91-
await streamPipe((await fetch(browser_download_url)).body, createWriteStream(downloadedPath));
92+
await streamPipe((await fetchWithReport(browser_download_url)).body, createWriteStream(downloadedPath));
9293
crossZip.unzipSync(downloadedPath, outPath);
9394
cp("-r", join(outPath, "theme"), "tests/testProject");
9495
cp("-r", join(outPath, "themesource"), "tests/testProject");

automation/run-e2e/lib/utils.mjs

+29-9
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,38 @@ export async function updateWidget() {
4545
cp("-f", mpkPath, outDir);
4646
}
4747

48-
export async function await200(url = "http://localhost:8080", attempts = 50) {
48+
export async function fetchWithReport(url, init) {
49+
const response = await fetch(url, init);
50+
if (response.ok) {
51+
return response;
52+
}
53+
console.error(`HTTP Error Response: ${response.status} ${response.statusText}`);
54+
const errorBody = await response.text();
55+
console.error(`Error body: ${errorBody}`);
56+
throw new Error("HTTP Error");
57+
}
58+
59+
export async function fetchGithubRestAPI(url, init = {}) {
60+
const token = process.env.GITHUB_TOKEN;
61+
assert.ok(typeof token === "string" && token.length > 0, "GITHUB_TOKEN is missing");
62+
63+
return fetchWithReport(url, {
64+
...init,
65+
headers: {
66+
Accept: "application/vnd.github+json",
67+
Authorization: `Bearer ${token}`,
68+
"X-GitHub-Api-Version": "2022-11-28",
69+
...init.headers
70+
}
71+
});
72+
}
73+
74+
export async function await200(url = "http://127.0.0.1:8080", attempts = 50) {
4975
let n = 0;
5076
while (++n <= attempts) {
5177
console.log(c.cyan(`GET ${url} ${n}`));
52-
let response;
53-
try {
54-
response = await fetch(url);
55-
} catch {
56-
// ignore
57-
}
58-
59-
const { ok, status } = response ?? {};
78+
const response = await fetch(url);
79+
const { ok, status } = response;
6080

6181
if (ok && status === 200) {
6282
console.log(c.green(`200 OK, continue`));

0 commit comments

Comments
 (0)