Skip to content

Commit

Permalink
Add changes
Browse files Browse the repository at this point in the history
Signed-off-by: Divya Madala <[email protected]>
  • Loading branch information
Divyaasm committed Jan 4, 2024
1 parent 8c19b83 commit bded301
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
20 changes: 13 additions & 7 deletions resources/cf-url-rewriter/cf-url-rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export async function handler(event: CloudFrontRequestEvent, context: Context, c
if (request.uri.includes("/latest/")) {

const urisplit = request.uri.split("/latest");
const index = urisplit[0].lastIndexOf("/");
const versionNumber = urisplit[0].substring(index + 1).replace(/\./g, '');

const replaceIndexPath = getIndexFilePath(versionNumber, urisplit);
const replaceIndexPath = getIndexFilePath(urisplit);

const indexUri = request.uri.replace(/\/latest\/.*/, replaceIndexPath);

Expand Down Expand Up @@ -74,11 +72,19 @@ function errorResponse() {
};
}

function getIndexFilePath(versionNumber: string, urisplit: string[]): string {
if (versionNumber <= "2111") {
return '/index.json';
} else {
function getIndexFilePath(urisplit: string[]): string {
const version = urisplit[0].substring(urisplit[0].lastIndexOf("/") + 1);
const [major, minor, patch] = version.split('.').map(Number);

if (
(major == 1 && minor == 3 && patch >= 15) ||
(major == 2 && minor >= 12) ||
(major >= 3)
) {
const latestPath = urisplit[1].split("/").slice(0, 4).join("/");
return `/index${latestPath}/index.json`;

} else {
return '/index.json';
}
}
24 changes: 24 additions & 0 deletions test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ test('handler with latest url and /fool(latest)bar/ keyword and ci keyword', asy
);
});

test('handler with latest url and ci keyword for 1.3.x', async () => {
const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.3.15/latest/linux/x64/tar/opensearch.tar.gz');
const context = {} as Context;
const callback = jest.fn() as CloudFrontRequestCallback;

(httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '4678' });

await handler(event, context, callback);

expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.3.15/index/linux/x64/tar/index.json');

expect(callback).toHaveBeenCalledWith(
null,
{
headers: {
'cache-control': [{ key: 'Cache-Control', value: 'max-age=3600' }],
location: [{ key: 'Location', value: '/ci/dbc/bundle-build-dashboards/1.3.15/4678/linux/x64/tar/opensearch.tar.gz' }],
},
status: '302',
statusDescription: 'Moved temporarily',
},
);
});

test('handler without latest url and without ci keyword', async () => {
const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/');
const context = {} as Context;
Expand Down

0 comments on commit bded301

Please sign in to comment.