Skip to content

Commit

Permalink
add api to fetch existing test reports from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mozrokafor committed Dec 19, 2023
1 parent 2239c75 commit 61f5585
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/e2e/global-teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import https from "https";
import { readFileSync, writeFileSync } from "fs";
import { CustomTestResult } from "../../E2EReporter";

function globalTearDown() {
async function globalTearDown() {
// check env for cron, teardown is only needed for teardown run
if (process.env.IS_CRON) {
// get data from e2e-reports branch files -- these are api calls
Expand All @@ -16,7 +17,7 @@ function globalTearDown() {
newFullReportJson,
newFailReportJson,
newFlakeReportJson,
} = getExistingAndNewReports();
} = await getExistingAndNewReports();

// merge new and existing reports
existingFullReports.push(newFullReportJson as unknown as CustomTestResult);
Expand Down Expand Up @@ -46,7 +47,7 @@ function globalTearDown() {
}
}

function getExistingAndNewReports() {
async function getExistingAndNewReports() {
let existingFullReports: CustomTestResult[] = [];
let existingFlakeReports: CustomTestResult[] = [];
let existingFailedReports: CustomTestResult[] = [];
Expand All @@ -55,10 +56,16 @@ function getExistingAndNewReports() {
let newFailReportJson: CustomTestResult[] = [];

try {
// existing files
const fullData = readFileSync("./full_test_report.json", "utf-8");
const flakeData = readFileSync("./flake_test_report.json", "utf-8");
const failData = readFileSync("./failed_test_report.json", "utf-8");
// fetch existing reports from repo
existingFullReports = (await fetchExistingReport(
"https://raw.githubusercontent.com/mozilla/blurts-server/e2e-report/full_test_report.json",
)) as CustomTestResult[];
existingFlakeReports = (await fetchExistingReport(
"https://raw.githubusercontent.com/mozilla/blurts-server/e2e-report/flake_test_report.json",
)) as CustomTestResult[];
existingFailedReports = (await fetchExistingReport(
"https://raw.githubusercontent.com/mozilla/blurts-server/e2e-report/failed_test_report.json",
)) as CustomTestResult[];

// new test generated files
const newFullReport = readFileSync("./new_full_test_report.json", "utf-8");
Expand All @@ -71,9 +78,7 @@ function getExistingAndNewReports() {
"utf-8",
);

existingFullReports = JSON.parse(fullData ?? "[]");
existingFlakeReports = JSON.parse(flakeData ?? "[]");
existingFailedReports = JSON.parse(failData ?? "[]");
// parse new test reports
newFullReportJson = JSON.parse(newFullReport ?? "[]");
newFlakeReportJson = JSON.parse(newFlakeReport ?? "[]");
newFailReportJson = JSON.parse(newFailReport ?? "[]");
Expand All @@ -99,4 +104,29 @@ function getExistingAndNewReports() {
}
}

async function fetchExistingReport(url: string) {
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
let data = "";

response.on("data", (chunk) => {
data += chunk;
});

response.on("end", () => {
try {
const parsedData = JSON.parse(data);
resolve(parsedData);
} catch (err) {
reject(err);
}
});
})
.on("error", (err) => {
reject(err);
});
});
}

export default globalTearDown;

0 comments on commit 61f5585

Please sign in to comment.