-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow any SDK branch to be specified in
global_config.sdkVersion
.
This PR allows any SDK branch, not just official releases, to be specified in `global_config.sdkVersion`. To support this, a new HBS helper was added to the Theme: `sdkAssetUrl`. This helper takes in the branch (or release version), locale, and asset name, and provides the correct URL for it. With the addition of this helper, we were able to remove the `sdk-url` partial. The logic for computing the URL is now sufficiently complicated that we should do it in JS instead of HBS. J=SLAP-924 TEST=auto, manual Added unit tests for the new helper. For the following scenarios, I tested with both an English and French locale, and verified the correct asset URLs were provided: - Released SDK version. - Release branch of the SDK. - Feature branch of the SDK. - Dev branch of the SDK.
- Loading branch information
1 parent
d9a59a4
commit 279130b
Showing
8 changed files
with
112 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const RELEASE_BRANCH_REGEX = /^release\/v[0-9.]+$/; | ||
const HOTFIX_BRANCH_REGEX = /^hotfix\/v[0-9.]+$/; | ||
const SEM_VER_REGEX = /^[1-9]+$|^[1-9]+\.[0-9]+$|^[1-9]+\.[0-9]+\.[0-9]+$/; | ||
|
||
/** | ||
* Given a branch (or release) of the SDK and a locale, this helper provides the correct | ||
* URL in the CDN for the asset. | ||
* | ||
* @param {string} branch The branch (or release) of the SDK. | ||
* @param {string} locale The locale to use. | ||
* @param {string} assetName The name of the desired asset. | ||
* @returns {string} The CDN URL of the localized asset. | ||
*/ | ||
module.exports = function sdkAssetUrl(branch, locale, assetName) { | ||
const isReleasedBranch = SEM_VER_REGEX.test(branch); | ||
|
||
let parsedBranch; | ||
if (isReleasedBranch) { | ||
parsedBranch = `v${branch}`; | ||
} else { | ||
parsedBranch = `dev/${branch.replace(/\//g, '-')}`; | ||
} | ||
|
||
const isPreReleaseBranch = | ||
RELEASE_BRANCH_REGEX.test(branch) || HOTFIX_BRANCH_REGEX.test(branch); | ||
const isLocalizationSupported = | ||
(isReleasedBranch || isPreReleaseBranch) && | ||
!(locale.startsWith('en') || assetName === 'answers.css') ; | ||
|
||
const parsedAssetName = isLocalizationSupported ? | ||
`${locale}-${assetName}` : | ||
assetName; | ||
|
||
return `https://assets.sitescdn.net/answers/${parsedBranch}/${parsedAssetName}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import sdkAssetUrl from '../../hbshelpers/sdkAssetUrl'; | ||
|
||
describe('URLs are computed properly for released versions', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('1.8', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('1.8', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/v1.8/fr-answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('1.8', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('1.8', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); | ||
|
||
describe('URLs are computed properly for release branches', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('release/v1.8', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('release/v1.8', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/fr-answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('release/v1.8', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('release/v1.8', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); | ||
|
||
describe('URLs are computed properly for hotfix branches', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.css' | ||
|
||
expect(sdkAssetUrl('hotfix/v1.8.1', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('hotfix/v1.8.1', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/fr-answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.css' | ||
|
||
expect(sdkAssetUrl('hotfix/v1.8.1', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('hotfix/v1.8.1', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); | ||
|
||
describe('URLs are computed properly for all other branches', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.css' | ||
|
||
expect(sdkAssetUrl('feature/foo', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('feature/foo', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.css' | ||
|
||
expect(sdkAssetUrl('feature/foo', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('feature/foo', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.