1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const base_1 = tslib_1.__importDefault(require("../../base"));
|
5 | const utils_1 = require("../../utils");
|
6 | const CustomErrors_1 = require("../../errors/CustomErrors");
|
7 | class UnregisterSecret extends base_1.default {
|
8 | constructor() {
|
9 | super(...arguments);
|
10 | this.unregisterConfirm = async () => {
|
11 | const { team } = this.state.config;
|
12 | const { confirmDelete } = await this.ux.prompt({
|
13 | type: 'confirm',
|
14 | name: 'confirmDelete',
|
15 | message: `Are you sure you want to remove the secret provider of the ${this.ux.colors.multiBlue(team.name)} team?`,
|
16 | });
|
17 | return { confirmDelete };
|
18 | };
|
19 | this.unregisterAPI = async (inputs) => {
|
20 | try {
|
21 | if (!inputs.confirmDelete)
|
22 | return inputs;
|
23 | this.log('\n🗑 Removing secret provider...');
|
24 | await this.services.api.remove(`/private/teams/${this.state.config.team.name}/secrets`, 'unregister', {
|
25 | headers: {
|
26 | Authorization: this.state.config.tokens.accessToken,
|
27 | },
|
28 | });
|
29 | return inputs;
|
30 | }
|
31 | catch (err) {
|
32 | const [{ message, code }] = err.error;
|
33 | if (message === 'team not found') {
|
34 | throw new CustomErrors_1.NoTeamFound(this.state.config.team.name);
|
35 | }
|
36 | if (code === 403 || message === 'team not authorized') {
|
37 | throw new CustomErrors_1.UserUnauthorized(err);
|
38 | }
|
39 | if (code === 404) {
|
40 | throw new CustomErrors_1.NoSecretsProviderFound(err);
|
41 | }
|
42 | throw new CustomErrors_1.APIError(err);
|
43 | }
|
44 | };
|
45 | this.logMessage = (inputs) => {
|
46 | if (!inputs.confirmDelete)
|
47 | return inputs;
|
48 | this.log(`\n⚡️ the secret provider has been ${this.ux.colors.red('deleted')} from the team ${this.ux.colors.multiBlue(this.state.config.team.name)}!`);
|
49 | return inputs;
|
50 | };
|
51 | this.sendAnalytics = async (inputs) => {
|
52 | try {
|
53 | const { user, team, tokens } = this.state.config;
|
54 | this.services.analytics.track({
|
55 | userId: user.email,
|
56 | teamId: team.id,
|
57 | cliEvent: 'Secrets CLI Unregister',
|
58 | event: 'Secrets CLI Unregister',
|
59 | properties: {
|
60 | email: user.email,
|
61 | username: user.username,
|
62 | hasBeenDeleted: inputs.confirmDelete,
|
63 | team: team.name,
|
64 | },
|
65 | }, tokens.accessToken);
|
66 | return inputs;
|
67 | }
|
68 | catch (err) {
|
69 | this.debug('%O', err);
|
70 | throw new CustomErrors_1.AnalyticsError(err);
|
71 | }
|
72 | };
|
73 | }
|
74 | async run() {
|
75 | try {
|
76 | await this.isLoggedIn();
|
77 | const unregisterPipeline = utils_1.asyncPipe(this.unregisterConfirm, this.unregisterAPI, this.sendAnalytics, this.logMessage);
|
78 | await unregisterPipeline();
|
79 | }
|
80 | catch (err) {
|
81 | this.debug('%O', err);
|
82 | this.config.runHook('error', {
|
83 | err,
|
84 | accessToken: this.state.config.tokens.accessToken,
|
85 | });
|
86 | }
|
87 | }
|
88 | }
|
89 | exports.default = UnregisterSecret;
|
90 | UnregisterSecret.description = 'Unregister a secrets provider for a team';
|