Skip to content

Commit

Permalink
Adiciona regex para substituir url de assets pela do cache (#372)
Browse files Browse the repository at this point in the history
* Adiciona regex para substituir url de assets pela do cache

* fix eslint

* allow-empty-message

* --alow-empty-message

* adiciona dist

* --allow-empty-message

* Adiciona teste

* Test
  • Loading branch information
raigons authored May 4, 2022
1 parent 14039a4 commit 6bbe27c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 133 deletions.
18 changes: 18 additions & 0 deletions __tests__/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,22 @@ describe('Files', () => {
'/agile/scrum.png': '__mocks__/fixtures/assets/static/agile/scrum-213c790c4129428a74486324d08e78e7.png',
});
});

test('Builds array of assets new hash urls with cached path', async () => {
const assetPath = '__mocks__/fixtures/assets/static/agile/scrum.png';
const assetContent = readMockFile(assetPath);

gitCommands.getBlobContent
.mockResolvedValueOnce(assetContent);

s3.uploadToBucket
.mockResolvedValueOnce('https://s3.bucketname.amazonaws.com/assets.app.betrybe.com/agile/scrum-d1291f436dfe589ba4efe36562ae4db4.png');

const urlHashObj = await files.processAssetContent(assetPath, '__mocks__/fixtures/assets/static');

expect(typeof urlHashObj).toBe('object');
expect(urlHashObj).toEqual({
'/agile/scrum.png': 'https:\\/\\/assets.app.betrybe.com/agile/scrum-d1291f436dfe589ba4efe36562ae4db4.png',
});
});
});
194 changes: 62 additions & 132 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const processAssetContent = async (assetPath) => {
const assetUrlHash = buildAssetHashUrl(assetPath, assetContentMd5);

const location = await s3.uploadToBucket(assetUrlHash, assetPath);
const s3UrlLocation = utils.urlSanitizer(location);
const s3UrlLocation = utils.useCachedUrl(location);
const relativeAssetPath = assetPath.split('static').pop();
core.info(`Asset: ${s3UrlLocation} sucessfully uploaded`);
return { [relativeAssetPath]: s3UrlLocation };
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ const urlSanitizer = (url) => {
return url;
};

const useCachedUrl = (url) => {
const regex = /(https:)(.+?)\/(assets.app.betrybe.com)(.+)/;
return regex.test(url) ? urlSanitizer(url.replace(regex, '$1//$3$4')) : url;
};

module.exports = {
sanitizeExtension,
verifyFileMatching,
sanitizeFilesArray,
urlSanitizer,
useCachedUrl,
};

0 comments on commit 6bbe27c

Please sign in to comment.