Skip to content

Commit 0e61c67

Browse files
authored
fix(actions): image download/upload scope (#706)
1 parent 7688fa5 commit 0e61c67

File tree

9 files changed

+70
-45
lines changed

9 files changed

+70
-45
lines changed

action/dist/main.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61495,22 +61495,23 @@ async function checkS3PrefixExists(bucketName, prefix) {
6149561495
}
6149661496
}
6149761497
async function downloadS3Directory(bucketName, s3Prefix, localDir) {
61498-
info(`Downloading screenshots from s3://${bucketName}/${s3Prefix}`);
61498+
info(`Downloading base images from s3://${bucketName}/${s3Prefix}`);
6149961499
const command = new ListObjectsV2Command({
6150061500
Bucket: bucketName,
6150161501
Prefix: s3Prefix
6150261502
});
6150361503
const response = await s3Client.send(command);
61504-
const objects = response.Contents ?? [];
61505-
info(`Found ${objects.length} file(s) to download`);
61506-
await (0, import_bluebird.map)(objects, async (object) => {
61507-
if (!object.Key) return;
61508-
const relativePath = object.Key.substring(s3Prefix.length);
61504+
const allObjects = response.Contents ?? [];
61505+
const baseObjects = allObjects.filter((obj2) => obj2.Key?.endsWith("base.png"));
61506+
info(`Found ${baseObjects.length} base image(s) to download`);
61507+
await (0, import_bluebird.map)(baseObjects, async ({ Key }) => {
61508+
if (!Key) return;
61509+
const relativePath = Key.substring(s3Prefix.length);
6150961510
const localFilePath = path5.join(localDir, relativePath);
6151061511
await import_fs5.promises.mkdir(path5.dirname(localFilePath), { recursive: true });
6151161512
const getCommand = new GetObjectCommand({
6151261513
Bucket: bucketName,
61513-
Key: object.Key
61514+
Key
6151461515
});
6151561516
const { Body } = await s3Client.send(getCommand);
6151661517
if (Body instanceof import_stream10.Readable) {
@@ -61520,10 +61521,10 @@ async function downloadS3Directory(bucketName, s3Prefix, localDir) {
6152061521
});
6152161522
}
6152261523
});
61523-
info(`Downloaded ${objects.length} file(s) to ${localDir}`);
61524+
info(`Downloaded ${baseObjects.length} base image(s) to ${localDir}`);
6152461525
}
6152561526
async function uploadLocalDirectory(localDir, bucketName, s3Prefix) {
61526-
const files = await glob("**/*.png", {
61527+
const files = await glob("**/{base,diff,new}.png", {
6152761528
cwd: localDir,
6152861529
nodir: true,
6152961530
absolute: false
@@ -65577,9 +65578,12 @@ var run = async () => {
6557765578
).length;
6557865579
const screenshotsDirectory = getInput("screenshots-directory");
6557965580
const screenshotsPath = path6.join(process.cwd(), screenshotsDirectory);
65580-
const filesInScreenshotDirectory = await glob(`${screenshotsPath}/**/*.png`, {
65581-
absolute: false
65582-
});
65581+
const filesInScreenshotDirectory = await glob(
65582+
`${screenshotsPath}/**/{base,diff,new}.png`,
65583+
{
65584+
absolute: false
65585+
}
65586+
);
6558365587
const diffFilePaths = filesInScreenshotDirectory.filter(
6558465588
(file) => file.endsWith("diff.png")
6558565589
);

action/dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/dist/post.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30784,7 +30784,9 @@ var post = async () => {
3078430784
info("Cleaning up PNG files in screenshots directory...");
3078530785
const screenshotsDirectory = getInput("screenshots-directory");
3078630786
try {
30787-
const pngFiles = await glob(`${screenshotsDirectory}/**/*.png`);
30787+
const pngFiles = await glob(
30788+
`${screenshotsDirectory}/**/{base,diff,new}.png`
30789+
);
3078830790
await (0, import_bluebird.map)(pngFiles, (file) => (0, import_promises2.rm)(file, { force: true }));
3078930791
info(`Removed ${pngFiles.length} PNG file(s) from ${screenshotsDirectory}`);
3079030792
} catch (error) {

action/dist/post.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/src/post.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const post = async () => {
99
const screenshotsDirectory = getInput('screenshots-directory');
1010

1111
try {
12-
const pngFiles = await glob(`${screenshotsDirectory}/**/*.png`);
12+
const pngFiles = await glob(
13+
`${screenshotsDirectory}/**/{base,diff,new}.png`
14+
);
1315
await map(pngFiles, file => rm(file, { force: true }));
1416

1517
info(`Removed ${pngFiles.length} PNG file(s) from ${screenshotsDirectory}`);

action/src/run.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ export const run = async () => {
5454

5555
const screenshotsDirectory = getInput('screenshots-directory');
5656
const screenshotsPath = path.join(process.cwd(), screenshotsDirectory);
57-
const filesInScreenshotDirectory = await glob(`${screenshotsPath}/**/*.png`, {
58-
absolute: false
59-
});
57+
const filesInScreenshotDirectory = await glob(
58+
`${screenshotsPath}/**/{base,diff,new}.png`,
59+
{
60+
absolute: false
61+
}
62+
);
6063
const diffFilePaths = filesInScreenshotDirectory.filter(file =>
6164
file.endsWith('diff.png')
6265
);

action/src/s3-operations.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,30 @@ async function downloadS3Directory(
4242
s3Prefix: string,
4343
localDir: string
4444
): Promise<void> {
45-
info(`Downloading screenshots from s3://${bucketName}/${s3Prefix}`);
45+
info(`Downloading base images from s3://${bucketName}/${s3Prefix}`);
4646

4747
const command = new ListObjectsV2Command({
4848
Bucket: bucketName,
4949
Prefix: s3Prefix
5050
});
5151

5252
const response = await s3Client.send(command);
53-
const objects = response.Contents ?? [];
53+
const allObjects = response.Contents ?? [];
54+
const baseObjects = allObjects.filter(obj => obj.Key?.endsWith('base.png'));
5455

55-
info(`Found ${objects.length} file(s) to download`);
56+
info(`Found ${baseObjects.length} base image(s) to download`);
5657

57-
await map(objects, async object => {
58-
if (!object.Key) return;
58+
await map(baseObjects, async ({ Key }) => {
59+
if (!Key) return;
5960

60-
const relativePath = object.Key.substring(s3Prefix.length);
61+
const relativePath = Key.substring(s3Prefix.length);
6162
const localFilePath = path.join(localDir, relativePath);
6263

6364
await fsPromises.mkdir(path.dirname(localFilePath), { recursive: true });
6465

6566
const getCommand = new GetObjectCommand({
6667
Bucket: bucketName,
67-
Key: object.Key
68+
Key
6869
});
6970

7071
const { Body } = await s3Client.send(getCommand);
@@ -76,15 +77,15 @@ async function downloadS3Directory(
7677
}
7778
});
7879

79-
info(`Downloaded ${objects.length} file(s) to ${localDir}`);
80+
info(`Downloaded ${baseObjects.length} base image(s) to ${localDir}`);
8081
}
8182

8283
async function uploadLocalDirectory(
8384
localDir: string,
8485
bucketName: string,
8586
s3Prefix: string
8687
): Promise<void> {
87-
const files = await glob('**/*.png', {
88+
const files = await glob('**/{base,diff,new}.png', {
8889
cwd: localDir,
8990
nodir: true,
9091
absolute: false

app/backend/src/fetchCurrentPage.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ export const fetchCurrentPage = async ({
1818
});
1919
}
2020
const { keys, title } = currentPage;
21-
const images = (
22-
await Promise.all(
23-
keys.map(async key => {
24-
const result = fileNameSchema.safeParse(parse(key).name);
25-
if (!result.success) {
26-
return;
27-
}
21+
const images = await Promise.all(
22+
keys.map(async key => {
23+
const result = fileNameSchema.safeParse(parse(key).name);
24+
if (!result.success) {
25+
throw new TRPCError({
26+
code: 'BAD_REQUEST',
27+
message: `Invalid file name: ${key}. ${result.error.message}`
28+
});
29+
}
2830

29-
return {
30-
name: result.data,
31-
url: await getTemporaryObjectUrl(key, bucket)
32-
};
33-
})
34-
)
35-
).filter(Boolean);
31+
return {
32+
name: result.data,
33+
url: await getTemporaryObjectUrl(key, bucket)
34+
};
35+
})
36+
);
3637
const nextPage = page < paginatedKeys.length ? page + 1 : undefined;
3738
return {
3839
title,

app/backend/test/fetchCurrentPage.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const listObjectsV2Mock = mock(() => ({
1111
{ Key: `${pathPrefix}/SMALL/srpPage/base.png` },
1212
{ Key: `${pathPrefix}/SMALL/srpPage/diff.png` },
1313
{ Key: `${pathPrefix}/SMALL/srpPage/new.png` },
14-
{ Key: `${pathPrefix}/EXTRA_LARGE/pdpPage/new.png }` }
14+
{ Key: `${pathPrefix}/EXTRA_LARGE/pdpPage/new.png }` },
15+
{ Key: `${pathPrefix}/LARGE/invalidPage/invalid.png` },
16+
{ Key: `${pathPrefix}/LARGE/invalidPage/new.png` }
1517
]
1618
}));
1719
mock.module('../src/s3Client', () => ({
@@ -61,7 +63,7 @@ describe('fetchCurrentPage', () => {
6163
url: 'url'
6264
}
6365
],
64-
nextPage: undefined
66+
nextPage: 3
6567
});
6668
});
6769

@@ -72,6 +74,16 @@ describe('fetchCurrentPage', () => {
7274
bucket: 'bucket',
7375
page: 12
7476
})
75-
).rejects.toThrow('Page 12 does not exist. Only 2 pages were found.');
77+
).rejects.toThrow('Page 12 does not exist. Only 3 pages were found.');
78+
});
79+
80+
it('should throw when a key does not conform to fileNameSchema', async () => {
81+
expect(
82+
fetchCurrentPage({
83+
hash: 'hash',
84+
bucket: 'bucket',
85+
page: 3
86+
})
87+
).rejects.toThrow('Invalid file name');
7688
});
7789
});

0 commit comments

Comments
 (0)