Bug 1943650 - Command-line --help output misformatted after --dbus-service. r=emilio
[gecko.git] / toolkit / components / normandy / actions / AddonRollbackAction.sys.mjs
blob1176bff6d9303d90e37267930cb41097082e4f23
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs";
7 const lazy = {};
9 ChromeUtils.defineESModuleGetters(lazy, {
10   ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs",
11   AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
12   AddonRollouts: "resource://normandy/lib/AddonRollouts.sys.mjs",
13   TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs",
14   TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs",
15 });
17 export class AddonRollbackAction extends BaseAction {
18   get schema() {
19     return lazy.ActionSchemas["addon-rollback"];
20   }
22   async _run(recipe) {
23     const { rolloutSlug } = recipe.arguments;
24     const rollout = await lazy.AddonRollouts.get(rolloutSlug);
26     if (!rollout) {
27       this.log.debug(`Rollback ${rolloutSlug} not applicable, skipping`);
28       return;
29     }
31     switch (rollout.state) {
32       case lazy.AddonRollouts.STATE_ACTIVE: {
33         await lazy.AddonRollouts.update({
34           ...rollout,
35           state: lazy.AddonRollouts.STATE_ROLLED_BACK,
36         });
38         const addon = await lazy.AddonManager.getAddonByID(rollout.addonId);
39         if (addon) {
40           try {
41             await addon.uninstall();
42           } catch (err) {
43             lazy.TelemetryEvents.sendEvent(
44               "unenrollFailed",
45               "addon_rollback",
46               rolloutSlug,
47               {
48                 reason: "uninstall-failed",
49               }
50             );
51             throw err;
52           }
53         } else {
54           this.log.warn(
55             `Could not uninstall addon ${rollout.addonId} for rollback ${rolloutSlug}: it is not installed.`
56           );
57         }
59         lazy.TelemetryEvents.sendEvent(
60           "unenroll",
61           "addon_rollback",
62           rolloutSlug,
63           {
64             reason: "rollback",
65           }
66         );
67         lazy.TelemetryEnvironment.setExperimentInactive(rolloutSlug);
68         break;
69       }
71       case lazy.AddonRollouts.STATE_ROLLED_BACK: {
72         return; // Do nothing
73       }
75       default: {
76         throw new Error(
77           `Unexpected state when rolling back ${rolloutSlug}: ${rollout.state}`
78         );
79       }
80     }
81   }