summaryrefslogtreecommitdiffstats
path: root/browser/components/asrouter/bin/import-rollouts.js
blob: d29a31a0689e50ef591706d87f4d00a35d2acb66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This is a script to import Nimbus experiments from a given collection into
 * browser/components/asrouter/tests/NimbusRolloutMessageProvider.sys.mjs. By
 * default, it only imports messaging rollouts. This is done so that the content
 * of off-train rollouts can be easily searched. That way, when we are cleaning
 * up old assets (such as Fluent strings), we don't accidentally delete strings
 * that live rollouts are using because it was too difficult to find whether
 * they were in use.
 *
 * This works by fetching the message records from the Nimbus collection and
 * then writing them to the file. The messages are converted from JSON to JS.
 * The file is structured like this:
 * export const NimbusRolloutMessageProvider = {
 *   getMessages() {
 *     return [
 *       { ...message1 },
 *       { ...message2 },
 *     ];
 *   },
 * };
 */

/* eslint-disable no-console */
const chalk = require("chalk");
const https = require("https");
const path = require("path");
const { pathToFileURL } = require("url");
const fs = require("fs");
const util = require("util");
const prettier = require("prettier");
const jsonschema = require("../../../../third_party/js/cfworker/json-schema.js");

const DEFAULT_COLLECTION_ID = "nimbus-desktop-experiments";
const BASE_URL =
  "https://firefox.settings.services.mozilla.com/v1/buckets/main/collections/";
const EXPERIMENTER_URL = "https://experimenter.services.mozilla.com/nimbus/";
const OUTPUT_PATH = "./tests/NimbusRolloutMessageProvider.sys.mjs";
const LICENSE_STRING = `/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */`;

function fetchJSON(url) {
  return new Promise((resolve, reject) => {
    https
      .get(url, resp => {
        let data = "";
        resp.on("data", chunk => {
          data += chunk;
        });
        resp.on("end", () => resolve(JSON.parse(data)));
      })
      .on("error", reject);
  });
}

function isMessageValid(validator, obj) {
  if (validator) {
    const result = validator.validate(obj);
    return result.valid && result.errors.length === 0;
  }
  return true;
}

async function getMessageValidators(skipValidation) {
  if (skipValidation) {
    return { experimentValidator: null, messageValidators: {} };
  }

  async function getSchema(filePath) {
    const file = await util.promisify(fs.readFile)(filePath, "utf8");
    return JSON.parse(file);
  }

  async function getValidator(filePath, { common = false } = {}) {
    const schema = await getSchema(filePath);
    const validator = new jsonschema.Validator(schema);

    if (common) {
      const commonSchema = await getSchema(
        "./content-src/schemas/FxMSCommon.schema.json"
      );
      validator.addSchema(commonSchema);
    }

    return validator;
  }

  const experimentValidator = await getValidator(
    "./content-src/schemas/MessagingExperiment.schema.json"
  );

  const messageValidators = {
    cfr_doorhanger: await getValidator(
      "./content-src/templates/CFR/templates/ExtensionDoorhanger.schema.json",
      { common: true }
    ),
    cfr_urlbar_chiclet: await getValidator(
      "./content-src/templates/CFR/templates/CFRUrlbarChiclet.schema.json",
      { common: true }
    ),
    infobar: await getValidator(
      "./content-src/templates/CFR/templates/InfoBar.schema.json",
      { common: true }
    ),
    pb_newtab: await getValidator(
      "./content-src/templates/PBNewtab/NewtabPromoMessage.schema.json",
      { common: true }
    ),
    spotlight: await getValidator(
      "./content-src/templates/OnboardingMessage/Spotlight.schema.json",
      { common: true }
    ),
    toast_notification: await getValidator(
      "./content-src/templates/ToastNotification/ToastNotification.schema.json",
      { common: true }
    ),
    toolbar_badge: await getValidator(
      "./content-src/templates/OnboardingMessage/ToolbarBadgeMessage.schema.json",
      { common: true }
    ),
    update_action: await getValidator(
      "./content-src/templates/OnboardingMessage/UpdateAction.schema.json",
      { common: true }
    ),
    whatsnew_panel_message: await getValidator(
      "./content-src/templates/OnboardingMessage/WhatsNewMessage.schema.json",
      { common: true }
    ),
    feature_callout: await getValidator(
      // For now, Feature Callout and Spotlight share a common schema
      "./content-src/templates/OnboardingMessage/Spotlight.schema.json",
      { common: true }
    ),
  };

  messageValidators.milestone_message = messageValidators.cfr_doorhanger;

  return { experimentValidator, messageValidators };
}

function annotateMessage({ message, slug, minVersion, maxVersion, url }) {
  const comments = [];
  if (slug) {
    comments.push(`// Nimbus slug: ${slug}`);
  }
  let versionRange = "";
  if (minVersion) {
    versionRange = minVersion;
    if (maxVersion) {
      versionRange += `-${maxVersion}`;
    } else {
      versionRange += "+";
    }
  } else if (maxVersion) {
    versionRange = `0-${maxVersion}`;
  }
  if (versionRange) {
    comments.push(`// Version range: ${versionRange}`);
  }
  if (url) {
    comments.push(`// Recipe: ${url}`);
  }
  return JSON.stringify(message, null, 2).replace(
    /^{/,
    `{ ${comments.join("\n")}`
  );
}

async function format(content) {
  const config = await prettier.resolveConfig("./.prettierrc.js");
  return prettier.format(content, { ...config, filepath: OUTPUT_PATH });
}

async function main() {
  const { default: meow } = await import("meow");
  const { MESSAGING_EXPERIMENTS_DEFAULT_FEATURES } = await import(
    "../modules/MessagingExperimentConstants.sys.mjs"
  );

  const fileUrl = pathToFileURL(__filename);

  const cli = meow(
    `
    Usage
      $ node bin/import-rollouts.js [options]
  
    Options
      -c ID, --collection ID   The Nimbus collection ID to import from
                               default: ${DEFAULT_COLLECTION_ID}
      -e, --experiments        Import all messaging experiments, not just rollouts
      -s, --skip-validation    Skip validation of experiments and messages
      -h, --help               Show this help message
  
    Examples
      $ node bin/import-rollouts.js --collection nimbus-preview
      $ ./mach npm run import-rollouts --prefix=browser/components/newtab -- -e
  `,
    {
      description: false,
      // `pkg` is a tiny optimization. It prevents meow from looking for a package
      // that doesn't technically exist. meow searches for a package and changes
      // the process name to the package name. It resolves to the newtab
      // package.json, which would give a confusing name and be wasteful.
      pkg: {
        name: "import-rollouts",
        version: "1.0.0",
      },
      // `importMeta` is required by meow 10+. It was added to support ESM, but
      // meow now requires it, and no longer supports CJS style imports. But it
      // only uses import.meta.url, which can be polyfilled like this:
      importMeta: { url: fileUrl },
      flags: {
        collection: {
          type: "string",
          alias: "c",
          default: DEFAULT_COLLECTION_ID,
        },
        experiments: {
          type: "boolean",
          alias: "e",
          default: false,
        },
        skipValidation: {
          type: "boolean",
          alias: "s",
          default: false,
        },
      },
    }
  );

  const RECORDS_URL = `${BASE_URL}${cli.flags.collection}/records`;

  console.log(`Fetching records from ${chalk.underline.yellow(RECORDS_URL)}`);

  const { data: records } = await fetchJSON(RECORDS_URL);

  if (!Array.isArray(records)) {
    throw new TypeError(
      `Expected records to be an array, got ${typeof records}`
    );
  }

  const recipes = records.filter(
    record =>
      record.application === "firefox-desktop" &&
      record.featureIds.some(id =>
        MESSAGING_EXPERIMENTS_DEFAULT_FEATURES.includes(id)
      ) &&
      (record.isRollout || cli.flags.experiments)
  );

  const importItems = [];
  const { experimentValidator, messageValidators } = await getMessageValidators(
    cli.flags.skipValidation
  );
  for (const recipe of recipes) {
    const { slug: experimentSlug, branches, targeting } = recipe;
    if (!(experimentSlug && Array.isArray(branches) && branches.length)) {
      continue;
    }
    console.log(
      `Processing ${recipe.isRollout ? "rollout" : "experiment"}: ${chalk.blue(
        experimentSlug
      )}${
        branches.length > 1
          ? ` with ${chalk.underline(`${String(branches.length)} branches`)}`
          : ""
      }`
    );
    const recipeUrl = `${EXPERIMENTER_URL}${experimentSlug}/summary`;
    const [, minVersion] =
      targeting?.match(/\(version\|versionCompare\(\'([0-9]+)\.!\'\) >= 0/) ||
      [];
    const [, maxVersion] =
      targeting?.match(/\(version\|versionCompare\(\'([0-9]+)\.\*\'\) <= 0/) ||
      [];
    let branchIndex = branches.length > 1 ? 1 : 0;
    for (const branch of branches) {
      const { slug: branchSlug, features } = branch;
      console.log(
        `  Processing branch${
          branchIndex > 0 ? ` ${branchIndex} of ${branches.length}` : ""
        }: ${chalk.blue(branchSlug)}`
      );
      branchIndex += 1;
      const url = `${recipeUrl}#${branchSlug}`;
      if (!Array.isArray(features)) {
        continue;
      }
      for (const feature of features) {
        if (
          feature.enabled &&
          MESSAGING_EXPERIMENTS_DEFAULT_FEATURES.includes(feature.featureId) &&
          feature.value &&
          typeof feature.value === "object" &&
          feature.value.template
        ) {
          if (!isMessageValid(experimentValidator, feature.value)) {
            console.log(
              `    ${chalk.red(
                "✗"
              )} Skipping invalid value for branch: ${chalk.blue(branchSlug)}`
            );
            continue;
          }
          const messages = (
            feature.value.template === "multi" &&
            Array.isArray(feature.value.messages)
              ? feature.value.messages
              : [feature.value]
          ).filter(m => m && m.id);
          let msgIndex = messages.length > 1 ? 1 : 0;
          for (const message of messages) {
            let messageLogString = `message${
              msgIndex > 0 ? ` ${msgIndex} of ${messages.length}` : ""
            }: ${chalk.italic.green(message.id)}`;
            if (!isMessageValid(messageValidators[message.template], message)) {
              console.log(
                `    ${chalk.red("✗")} Skipping invalid ${messageLogString}`
              );
              continue;
            }
            console.log(`    Importing ${messageLogString}`);
            let slug = `${experimentSlug}:${branchSlug}`;
            if (msgIndex > 0) {
              slug += ` (message ${msgIndex} of ${messages.length})`;
            }
            msgIndex += 1;
            importItems.push({ message, slug, minVersion, maxVersion, url });
          }
        }
      }
    }
  }

  const content = `${LICENSE_STRING}

/**
 * This file is generated by browser/components/asrouter/bin/import-rollouts.js
 * Run the following from the repository root to regenerate it:
 * ./mach npm run import-rollouts --prefix=browser/components/asrouter
 */

export const NimbusRolloutMessageProvider = {
  getMessages() {
    return [${importItems.map(annotateMessage).join(",\n")}];
  },
};
`;

  const formattedContent = await format(content);

  await util.promisify(fs.writeFile)(OUTPUT_PATH, formattedContent);

  console.log(
    `${chalk.green("✓")} Wrote ${chalk.underline.green(
      `${String(importItems.length)} ${
        importItems.length === 1 ? "message" : "messages"
      }`
    )} to ${chalk.underline.yellow(path.resolve(OUTPUT_PATH))}`
  );
}

main();