Skip to content

Commit 623fa04

Browse files
committed
Upload images feature
1 parent ae83716 commit 623fa04

9 files changed

+101
-4
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
branches:
77
- main
88
- dev
9+
- feature/*
910

1011
jobs:
1112
testing:

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ node_modules
44
test-results
55
summary.html
66
playwright-report
7-
coverage
7+
coverage
8+
9+
results.json

playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const config: PlaywrightTestConfig<{}, {}> = {
1212
retries: process.env.CI ? 2 : 2,
1313
workers: process.env.CI ? 1 : 1,
1414
reporter: [
15-
// ["list"],
15+
// ["json", { outputFile: "results.json" }],
1616
[
1717
"./src/index.ts",
1818
<GitHubActionOptions>{

src/models/GitHubActionOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ export interface GitHubActionOptions {
88
showError?: boolean;
99
quiet?: boolean;
1010
includeResults?: DisplayLevel[];
11+
12+
// Azure Storage
13+
azureStorageUrl?: string;
14+
azureStorageSAS?: string;
1115
}

src/utils/getTableRows.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ import { getTestAnnotations } from "./getTestAnnotations";
88
import { getTestDuration } from "./getTestDuration";
99
import { getTestStatusIcon } from "./getTestStatusIcon";
1010
import { DisplayLevel } from "../models";
11+
import { processAttachments } from "./processAttachments";
1112

1213
export const getTableRows = async (
1314
tests: TestCase[],
1415
showAnnotations: boolean,
1516
showTags: boolean,
1617
showError: boolean,
17-
displayLevel: DisplayLevel[]
18+
displayLevel: DisplayLevel[],
19+
azure?: {
20+
azureStorageUrl?: string;
21+
azureStorageSAS?: string;
22+
}
1823
): Promise<SummaryTableRow[]> => {
1924
const convert = new Convert();
2025

@@ -49,6 +54,10 @@ export const getTableRows = async (
4954
data: "Error",
5055
header: true,
5156
});
57+
tableHeaders.push({
58+
data: "Attachments",
59+
header: true,
60+
});
5261
}
5362

5463
const tableRows: SummaryTableRow[] = [];
@@ -70,6 +79,7 @@ export const getTableRows = async (
7079
}
7180
if (showError) {
7281
colLength++;
82+
colLength++;
7383
}
7484

7585
const annotations = await getTestAnnotations(test);
@@ -116,6 +126,22 @@ export const getTableRows = async (
116126
data: convert.toHtml(error),
117127
header: false,
118128
});
129+
130+
const mediaFiles =
131+
(azure &&
132+
(await processAttachments(
133+
azure?.azureStorageUrl,
134+
azure?.azureStorageSAS,
135+
result.attachments
136+
))) ||
137+
[];
138+
139+
tableRow.push({
140+
data: (mediaFiles || [])
141+
.map((m) => `<img src="${m.url}" alt="${m.name}" height: 25px; />`)
142+
.join("\n"),
143+
header: false,
144+
});
119145
}
120146

121147
tableRows.push(tableRow);

src/utils/processAttachments.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { parse } from "path";
2+
import { readFile } from "fs/promises";
3+
4+
export const processAttachments = async (
5+
azureContainerUrl?: string,
6+
azureContainerSas?: string,
7+
attachments?: { name: string; path?: string; contentType: string }[]
8+
) => {
9+
if (
10+
!attachments ||
11+
attachments.length === 0 ||
12+
!azureContainerUrl ||
13+
!azureContainerSas
14+
) {
15+
return;
16+
}
17+
18+
const mediaFiles: { name: string; url: string }[] = [];
19+
20+
if (attachments.length > 0) {
21+
attachments = attachments.filter(
22+
(a) => a.contentType.startsWith("image/") && a.path
23+
);
24+
25+
for (const attachment of attachments) {
26+
try {
27+
if (attachment.path && azureContainerUrl && azureContainerSas) {
28+
const parsedFile = parse(attachment.path);
29+
const fileUrl = `${azureContainerUrl}/${
30+
parsedFile.name
31+
}_${Date.now()}${parsedFile.ext}`;
32+
33+
const putResponse = await fetch(`${fileUrl}?${azureContainerSas}`, {
34+
method: "PUT",
35+
headers: {
36+
"x-ms-blob-type": "BlockBlob",
37+
"x-ms-blob-content-type": attachment.contentType,
38+
},
39+
body: await readFile(attachment.path),
40+
});
41+
42+
if (putResponse.ok) {
43+
mediaFiles.push({
44+
name: attachment.name,
45+
url: fileUrl,
46+
});
47+
} else {
48+
console.log(`Failed to upload attachment: ${attachment.name}`);
49+
console.log(`Response: ${putResponse.statusText}`);
50+
}
51+
}
52+
} catch (e) {
53+
console.log(`Failed to upload attachment: ${attachment.name}`);
54+
console.log((e as Error).message);
55+
}
56+
}
57+
}
58+
59+
return mediaFiles;
60+
};

src/utils/processResults.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export const processResults = async (
7676
options.showAnnotations,
7777
options.showTags,
7878
!!options.showError,
79-
options.includeResults as DisplayLevel[]
79+
options.includeResults as DisplayLevel[],
80+
{
81+
azureStorageSAS: options.azureStorageSAS,
82+
azureStorageUrl: options.azureStorageUrl,
83+
}
8084
);
8185

8286
if (tableRows.length !== 0) {
Loading
Loading

0 commit comments

Comments
 (0)