Skip to content

Commit

Permalink
🏗 Add opt-in option to nightly release channel (#27188)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrozenberg committed Apr 6, 2020
1 parent b477236 commit 7f6a191
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions tools/experiments/experiments.js
Expand Up @@ -55,6 +55,7 @@ let ExperimentDef;
*/
const EXPERIMENTAL_CHANNEL_ID = 'experimental-channel';
const BETA_CHANNEL_ID = 'beta-channel';
const NIGHTLY_CHANNEL_ID = 'nightly-channel';
const RTV_CHANNEL_ID = 'rtv-channel';

/**
Expand All @@ -64,36 +65,28 @@ const AMP_OPT_IN_COOKIE = {
DISABLED: '0',
EXPERIMENTAL: 'experimental',
BETA: 'beta',
// NIGHTLY: 'nightly', // TODO(#25616): add when CDN supports nightly builds.
};

/**
* Legacy values for __Host-AMP_OPT_IN cookie.
* TODO(#25205): remove this once the CDN stops supporting these values.
*/
const _LEGACY_AMP_OPT_IN_COOKIE = {
DISABLED: '0',
EXPERIMENTAL: '1',
BETA: '2',
NIGHTLY: 'nightly',
};

/** @const {!Array<!ExperimentDef>} */
const CHANNELS = [
// Experimental Channel
{
id: EXPERIMENTAL_CHANNEL_ID,
name: 'AMP Experimental Channel (more info)',
spec:
'https://github.com/ampproject/amphtml/blob/master/' +
'contributing/release-schedule.md#amp-experimental-and-beta-channels',
'https://github.com/ampproject/amphtml/blob/master/contributing/release-schedule.md#amp-experimental-and-beta-channels',
},
// Beta Channel
{
id: BETA_CHANNEL_ID,
name: 'AMP Beta Channel (more info)',
spec:
'https://github.com/ampproject/amphtml/blob/master/' +
'contributing/release-schedule.md#amp-experimental-and-beta-channels',
'https://github.com/ampproject/amphtml/blob/master/contributing/release-schedule.md#amp-experimental-and-beta-channels',
},
{
id: NIGHTLY_CHANNEL_ID,
name: 'AMP Nightly Channel (more info)',
spec:
'https://github.com/ampproject/amphtml/blob/master/contributing/release-schedule.md#amp-experimental-and-beta-channels',
},
];

Expand Down Expand Up @@ -282,14 +275,11 @@ function isExperimentOn_(id) {
const optInCookieValue = getCookie(window, '__Host-AMP_OPT_IN');
switch (id) {
case EXPERIMENTAL_CHANNEL_ID:
return [
AMP_OPT_IN_COOKIE.EXPERIMENTAL,
_LEGACY_AMP_OPT_IN_COOKIE.EXPERIMENTAL,
].includes(optInCookieValue);
return optInCookieValue == AMP_OPT_IN_COOKIE.EXPERIMENTAL;
case BETA_CHANNEL_ID:
return [AMP_OPT_IN_COOKIE.BETA, _LEGACY_AMP_OPT_IN_COOKIE.BETA].includes(
optInCookieValue
);
return optInCookieValue == AMP_OPT_IN_COOKIE.BETA;
case NIGHTLY_CHANNEL_ID:
return optInCookieValue == AMP_OPT_IN_COOKIE.NIGHTLY;
case RTV_CHANNEL_ID:
return RTV_PATTERN.test(optInCookieValue);
default:
Expand All @@ -298,8 +288,8 @@ function isExperimentOn_(id) {
}

/**
* Opts in to / out of the "beta" or "experimental" channels or a specific RTV
* by setting the __Host-AMP_OPT_IN cookie.
* Opts in to / out of the pre-release channels or a specific RTV by setting the
* __Host-AMP_OPT_IN cookie.
* @param {string} cookieState One of the AMP_OPT_IN_COOKIE enum values, or a
* 15-digit RTV.
*/
Expand Down Expand Up @@ -344,16 +334,24 @@ function toggleExperiment_(id, name, opt_on) {
: 'Do you really want to deactivate the AMP experiment?';

showConfirmation_(`${confirmMessage}: "${name}"`, () => {
if (id == EXPERIMENTAL_CHANNEL_ID) {
setAmpOptInCookie_(
on ? AMP_OPT_IN_COOKIE.EXPERIMENTAL : AMP_OPT_IN_COOKIE.DISABLED
);
} else if (id == BETA_CHANNEL_ID) {
setAmpOptInCookie_(
on ? AMP_OPT_IN_COOKIE.BETA : AMP_OPT_IN_COOKIE.DISABLED
);
} else {
toggleExperiment(window, id, on);
switch (id) {
case EXPERIMENTAL_CHANNEL_ID:
setAmpOptInCookie_(
on ? AMP_OPT_IN_COOKIE.EXPERIMENTAL : AMP_OPT_IN_COOKIE.DISABLED
);
break;
case BETA_CHANNEL_ID:
setAmpOptInCookie_(
on ? AMP_OPT_IN_COOKIE.BETA : AMP_OPT_IN_COOKIE.DISABLED
);
break;
case NIGHTLY_CHANNEL_ID:
setAmpOptInCookie_(
on ? AMP_OPT_IN_COOKIE.NIGHTLY : AMP_OPT_IN_COOKIE.DISABLED
);
break;
default:
toggleExperiment(window, id, on);
}
update();
});
Expand Down Expand Up @@ -388,8 +386,9 @@ function showConfirmation_(message, callback) {

/**
* Loads the AMP_CONFIG objects from whatever the v0.js is that the user has
* (depends on whether they opted into beta, experimental, or a specific RTV) so
* that experiment state can reflect the default activated experiments.
* (depends on whether they opted into one of the pre-release channels or into a
* specific RTV) so that experiment state can reflect the default activated
* experiments.
* @return {Promise<JSON>} the active AMP_CONFIG, parsed as a JSON object
*/
function getAmpConfig() {
Expand Down

0 comments on commit 7f6a191

Please sign in to comment.