Skip to content

Commit

Permalink
fix: Add basic support for search-config-overrides(-v2).
Browse files Browse the repository at this point in the history
  • Loading branch information
Standard8 committed Jan 5, 2024
1 parent fc9b2a1 commit e307c37
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 26 deletions.
6 changes: 4 additions & 2 deletions extension/content/byEngineView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class ByEngineView extends HTMLElement {
const byLength = allBy.length;
// Pre-filter the config for just the engine id to reduce the amount of
// processing to do.
const configUrl = this.#filterConfig(config, engineId);
const configData = this.#filterConfig(config, engineId);

let count = 0;
const results = new Map();
Expand All @@ -133,7 +133,9 @@ export default class ByEngineView extends HTMLElement {
const itemResults = new Set();
for (const subItem of allSub) {
const { engines } = await browser.experiments.searchengines.getEngines({
configUrl,
configData,
// No need to apply the overrides for this view.
configOverridesData: '{"data":[]}',
locale: byLocale ? item : subItem,
region: byLocale ? subItem : item,
distroID: "",
Expand Down
6 changes: 4 additions & 2 deletions extension/content/byEngineViewOld.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class ByEngineViewOld extends HTMLElement {
const byLength = allBy.length;
// Pre-filter the config for just the engine id to reduce the amount of
// processing to do.
const configUrl = this.#filterConfig(config, engineId);
const configData = this.#filterConfig(config, engineId);

let count = 0;
const results = new Map();
Expand All @@ -133,7 +133,9 @@ export default class ByEngineViewOld extends HTMLElement {
const itemResults = new Set();
for (const subItem of allSub) {
const { engines } = await browser.experiments.searchengines.getEngines({
configUrl,
configData,
// No need to apply the overrides for this view.
configOverridesData: '{"data":[]}',
locale: byLocale ? item : subItem,
region: byLocale ? subItem : item,
distroID: "",
Expand Down
16 changes: 14 additions & 2 deletions extension/content/configController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export default class ConfigController extends HTMLElement {
return fetchCached(await this.#getEngineUrl(buttonValue));
}

async fetchConfigOverrides() {
const buttonValue =
this.shadowRoot.getElementById(`primary-config`).selected;
if (buttonValue == "local-text") {
return '{"data":[]}';
}
return fetchCached(await this.#getEngineUrl(buttonValue, true));
}

async getAttachmentBaseUrl() {
const buttonValue =
this.shadowRoot.getElementById(`primary-config`).selected;
Expand Down Expand Up @@ -143,8 +152,11 @@ export default class ConfigController extends HTMLElement {
textarea.scrollTop = line * parseInt(lineHeight, 10);
}

async #getEngineUrl(server) {
let url = this.#getConfigUrl(server) + "collections/search-config";
async #getEngineUrl(server, overrides) {
let url =
this.#getConfigUrl(server) +
"collections/search-config" +
(overrides ? "-overrides" : "");

if (
(await browser.experiments.searchengines.getCurrentConfigFormat()) == "2"
Expand Down
26 changes: 22 additions & 4 deletions extension/content/enginesView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Utils from "./utils.mjs";
import { getLocales, getRegions, validateConfiguration } from "./loader.mjs";
import {
getLocales,
getRegions,
validateConfiguration,
validateConfigurationOverrides,
} from "./loader.mjs";

const enginesSelectionElements = [
"region-select",
Expand All @@ -15,6 +20,7 @@ const enginesSelectionElements = [

export default class EnginesView extends HTMLElement {
#config = null;
#configOverrides = null;
#attachmentBaseUrl = null;
#iconConfig = null;
#initializedPromise = null;
Expand Down Expand Up @@ -71,20 +77,31 @@ export default class EnginesView extends HTMLElement {
}
}

async loadEngines(event, config, attachmentBaseUrl, iconConfig) {
async loadEngines(
event,
config,
configOverrides,
attachmentBaseUrl,
iconConfig
) {
if (event) {
event.preventDefault();
}
await this.initialize();

if (config) {
if (!(await validateConfiguration(JSON.parse(config)))) {
if (
!(await validateConfiguration(JSON.parse(config))) ||
!(await validateConfigurationOverrides(JSON.parse(configOverrides)))
) {
this.#config = null;
this.#configOverrides = null;
this.#iconConfig = null;
this.#attachmentBaseUrl = null;
throw new Error("Invalid Config");
}
this.#config = config;
this.#configOverrides = configOverrides;
this.#iconConfig = JSON.parse(iconConfig);
this.#attachmentBaseUrl = attachmentBaseUrl;
}
Expand All @@ -93,7 +110,8 @@ export default class EnginesView extends HTMLElement {
}

let options = {
configUrl: this.#config,
configData: this.#config,
configOverridesData: this.#configOverrides,
locale: this.shadowRoot.getElementById("locale-select").value,
region: this.shadowRoot.getElementById("region-select").value,
distroID: this.shadowRoot.getElementById("distro-id").value,
Expand Down
20 changes: 16 additions & 4 deletions extension/content/enginesViewOld.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Utils from "./utils.mjs";
import { getLocales, getRegions, validateConfiguration } from "./loader.mjs";
import {
getLocales,
getRegions,
validateConfiguration,
validateConfigurationOverrides,
} from "./loader.mjs";

const enginesSelectionElements = [
"region-select",
Expand All @@ -15,6 +20,7 @@ const enginesSelectionElements = [

export default class EnginesViewOld extends HTMLElement {
#config = null;
#configOverrides = null;
#initializedPromise = null;

constructor() {
Expand Down Expand Up @@ -69,25 +75,31 @@ export default class EnginesViewOld extends HTMLElement {
}
}

async loadEngines(event, config) {
async loadEngines(event, config, configOverrides) {
if (event) {
event.preventDefault();
}
await this.initialize();

if (config) {
if (!(await validateConfiguration(JSON.parse(config)))) {
if (
!(await validateConfiguration(JSON.parse(config))) ||
!(await validateConfigurationOverrides(JSON.parse(configOverrides)))
) {
this.#config = null;
this.#configOverrides = null;
throw new Error("Invalid Config");
}
this.#config = config;
this.#configOverrides = configOverrides;
}
if (!this.#config) {
return;
}

let options = {
configUrl: this.#config,
configData: this.#config,
configOverridesData: this.#configOverrides,
locale: this.shadowRoot.getElementById("locale-select").value,
region: this.shadowRoot.getElementById("region-select").value,
distroID: this.shadowRoot.getElementById("distro-id").value,
Expand Down
18 changes: 16 additions & 2 deletions extension/content/loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,30 @@ export async function validateConfiguration(config) {
(await browser.experiments.searchengines.getCurrentConfigFormat()) == "2"
? validate.validateWithSchemaV2
: validate.validateWithSchemaV1;

return validateCollectionToSchema(validator, config);
}

export async function validateConfigurationOverrides(overrides) {
let validator =
(await browser.experiments.searchengines.getCurrentConfigFormat()) == "2"
? validate.validateWithOverridesSchemaV2
: validate.validateWithOverridesSchemaV1;

return validateCollectionToSchema(validator, overrides);
}

function validateCollectionToSchema(validator, collection) {
let valid = true;

if (!("data" in config)) {
if (!("data" in collection)) {
console.error("Could not find top-level 'data' property in config.");
valid = false;
return false;
}

try {
for (let item of config.data) {
for (let item of collection.data) {
if (!validator(item)) {
for (let error of validator.errors) {
console.warn(error);
Expand Down
1 change: 1 addition & 0 deletions extension/content/script.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ async function setupEnginesView() {
await $("#engines-view").loadEngines(
null,
await $("#config-controller").fetchPrimaryConfig(),
await $("#config-controller").fetchConfigOverrides(),
await $("#config-controller").getAttachmentBaseUrl(),
await $("#config-controller").fetchIconConfig()
);
Expand Down
13 changes: 9 additions & 4 deletions extension/experiments/searchengines/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ async function getEngines(options) {
}

engineSelector.getEngineConfiguration = async () => {
const result = JSON.parse(options.configUrl).data;
let config = JSON.parse(options.configData).data;
if (!usingV2) {
result.sort((a, b) => a.id.localeCompare(b.id));
config.sort((a, b) => a.id.localeCompare(b.id));
}
engineSelector._configuration = result;
return result;
engineSelector._configuration = config;

engineSelector._configurationOverrides = JSON.parse(
options.configOverridesData
).data;

return config;
};
return engineSelector.fetchEngineConfiguration(options);
}
Expand Down
8 changes: 6 additions & 2 deletions extension/experiments/searchengines/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@
"type": "object",
"name": "getEnginesProperties",
"properties": {
"configUrl": {
"configData": {
"type": "string",
"description": "The configuration"
"description": "The configuration to use (a JSON string)"
},
"configOverridesData": {
"type": "string",
"description": "The configuration overrides to use (a JSON string)"
},
"locale": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "searchengine-devtools",
"version": "2.1.0",
"version": "2.2.0",
"description": "A tool to help test search engine configuration changes",
"homepage_url": "https://github.com/mozilla/searchengine-devtools",
"browser_specific_settings": {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "searchengine-devtools",
"version": "2.1.0",
"version": "2.2.0",
"description": "A tool to help test search engine configuration changes",
"homepage_url": "https://github.com/mozilla/searchengine-devtools",
"webExt": {
Expand Down
13 changes: 12 additions & 1 deletion schemas/generateValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ let schemaV2 = JSON.parse(
fs.readFileSync(path.join(__dirname, "search-config-v2-schema.json"))
);

let schemaOverridesV1 = JSON.parse(
fs.readFileSync(path.join(__dirname, "search-config-overrides-schema.json"))
);
let schemaOverridesV2 = JSON.parse(
fs.readFileSync(
path.join(__dirname, "search-config-overrides-v2-schema.json")
)
);

schemaV1.$id = "validateWithSchemaV1";
schemaV2.$id = "validateWithSchemaV2";
schemaOverridesV1.$id = "validateWithOverridesSchemaV1";
schemaOverridesV2.$id = "validateWithOverridesSchemaV2";

const ajv = new Ajv({
schemas: [schemaV1, schemaV2],
schemas: [schemaV1, schemaV2, schemaOverridesV1, schemaOverridesV2],
code: { source: true },
});
addFormats(ajv);
Expand Down
64 changes: 64 additions & 0 deletions schemas/search-config-overrides-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"title": "Search Engine Overrides Schema",
"description": "This schema contains the details for overriding application provided search engines defined in search-config. The associated remote settings collection is search-config-overrides.",
"type": "object",
"required": ["telemetryId"],
"properties": {
"telemetryId": {
"type": "string",
"title": "Telemetry Id",
"description": "The telemetry Id used to match the engine that this record will override.",
"pattern": "^[a-zA-Z0-9-$_]{0,100}$"
},
"params": {
"$ref": "#/definitions/params"
},
"clickUrl": {
"type": "string",
"format": "uri",
"description": "The url used to for reporting clicks."
},
"telemetrySuffix": {
"type": "string",
"title": "Telemetry Suffix",
"description": "Suffix that is appended to the search engine identifier following a dash, i.e. `<identifier>-<suffix>`.",
"pattern": "^[a-zA-Z0-9-]*$"
}
},
"definitions": {
"searchUrlCodes": {
"type": "array",
"title": "Codes",
"description": "A array of objects - map of parameter name to the parameter value.",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name",
"pattern": "^[a-zA-Z0-9.-]{0,100}$",
"description": "Name of the parameter that will be used in the query"
},
"value": {
"type": "string",
"title": "Value",
"pattern": "^[a-zA-Z0-9_{}:/.-]{0,100}$",
"description": "The value of parameter (pref or purpose)"
}
}
}
},
"params": {
"type": "object",
"title": "Parameters",
"description": "Various parameters for the search engines",
"properties": {
"searchUrlGetParams": {
"title": "Search URL GET Parameters",
"description": "Extra parameters for search URLs (e.g. 'pc=foo').",
"$ref": "#/definitions/searchUrlCodes"
}
}
}
}
}
Loading

1 comment on commit e307c37

@firefoxci-taskcluster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! Looks like an error! Details

Taskcluster-GitHub attempted to create a task for this event with the following scopes:

["assume:repo:github.com/mozilla-extensions/searchengine-devtools:tag:2.2.0-build2","queue:route:checks","queue:scheduler-id:taskcluster-github"]

The expansion of these scopes is not sufficient to create the task, leading to the following:

Client ID static/taskcluster/github does not have sufficient scopes and is missing the following scopes:

assume:repo:github.com/mozilla-extensions/searchengine-devtools:branch:2.2.0-build2

This request requires the client to satisfy the following scope expression:

{
  "AllOf": [
    "assume:repo:github.com/mozilla-extensions/searchengine-devtools:branch:2.2.0-build2",
    "queue:route:checks",
    "queue:route:index.xpi.v2.searchengine-devtools.revision.e307c37866cafec17835a394abc1ede1271ffa41.taskgraph.decision",
    "queue:create-task:project:none",
    "queue:scheduler-id:xpi-level-1",
    {
      "AnyOf": [
        "queue:create-task:highest:xpi-1/decision-gcp",
        "queue:create-task:very-high:xpi-1/decision-gcp",
        "queue:create-task:high:xpi-1/decision-gcp",
        "queue:create-task:medium:xpi-1/decision-gcp",
        "queue:create-task:low:xpi-1/decision-gcp",
        "queue:create-task:very-low:xpi-1/decision-gcp",
        "queue:create-task:lowest:xpi-1/decision-gcp"
      ]
    }
  ]
}

  • method: createTask
  • errorCode: InsufficientScopes
  • statusCode: 403
  • time: 2024-01-09T10:45:30.264Z

Please sign in to comment.