-
Notifications
You must be signed in to change notification settings - Fork 85
/
edit.ts
186 lines (159 loc) · 5.71 KB
/
edit.ts
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
import { Flags } from '@oclif/core';
import assert from 'assert';
import chalk from 'chalk';
import { selectBranchOnAppAsync } from '../../branch/queries';
import EasCommand from '../../commandUtils/EasCommand';
import { EasNonInteractiveAndJsonFlags } from '../../commandUtils/flags';
import { PublishMutation } from '../../graphql/mutations/PublishMutation';
import { UpdateQuery } from '../../graphql/queries/UpdateQuery';
import Log from '../../log';
import { promptAsync } from '../../prompts';
import { selectUpdateGroupOnBranchAsync } from '../../update/queries';
import {
formatUpdateGroup,
getUpdateGroupDescriptions,
getUpdateJsonInfosForUpdates,
} from '../../update/utils';
import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json';
export default class UpdateEdit extends EasCommand {
static override description = 'edit all the updates in an update group';
static override args = [
{
name: 'groupId',
description: 'The ID of an update group to edit.',
},
];
static override flags = {
'rollout-percentage': Flags.integer({
description: `Rollout percentage to set for a rollout update. The specified number must be an integer between 1 and 100.`,
required: false,
min: 0,
max: 100,
}),
branch: Flags.string({
description: 'Branch for which to list updates to select from',
}),
...EasNonInteractiveAndJsonFlags,
};
static override contextDefinition = {
...this.ContextOptions.ProjectId,
...this.ContextOptions.LoggedIn,
};
async runAsync(): Promise<void> {
const {
args: { groupId: maybeGroupId },
flags: {
'rollout-percentage': rolloutPercentage,
json: jsonFlag,
'non-interactive': nonInteractive,
branch: branchFlag,
},
} = await this.parse(UpdateEdit);
const {
projectId,
loggedIn: { graphqlClient },
} = await this.getContextAsync(UpdateEdit, { nonInteractive });
if (jsonFlag) {
enableJsonOutput();
}
let groupId: string | undefined = maybeGroupId;
if (!groupId) {
let branch = branchFlag;
if (!branch) {
const validationMessage = 'Branch name may not be empty.';
if (nonInteractive) {
throw new Error(validationMessage);
}
const selectedBranch = await selectBranchOnAppAsync(graphqlClient, {
projectId,
promptTitle: 'On which branch would you like search for an update to edit?',
displayTextForListItem: updateBranch => ({
title: updateBranch.name,
}),
paginatedQueryOptions: {
json: jsonFlag,
nonInteractive,
offset: 0,
},
});
branch = selectedBranch.name;
}
const selectedUpdateGroup = await selectUpdateGroupOnBranchAsync(graphqlClient, {
projectId,
branchName: branch,
paginatedQueryOptions: {
json: jsonFlag,
nonInteractive,
offset: 0,
},
});
groupId = selectedUpdateGroup[0].group;
}
const proposedUpdatesToEdit = (
await UpdateQuery.viewUpdateGroupAsync(graphqlClient, { groupId })
).map(u => ({ updateId: u.id, rolloutPercentage: u.rolloutPercentage }));
const updatesToEdit = proposedUpdatesToEdit.filter(
(u): u is { updateId: string; rolloutPercentage: number } =>
u.rolloutPercentage !== null && u.rolloutPercentage !== undefined
);
if (updatesToEdit.length === 0) {
throw new Error('Cannot edit rollout percentage on update group that is not a rollout.');
}
const rolloutPercentagesSet = new Set(updatesToEdit.map(u => u.rolloutPercentage));
if (rolloutPercentagesSet.size !== 1) {
throw new Error(
'Cannot edit rollout percentage for a group with non-equal percentages for updates in the group.'
);
}
const previousPercentage = updatesToEdit[0].rolloutPercentage;
if (nonInteractive && rolloutPercentage === undefined) {
throw new Error('Must specify --rollout-percentage in non-interactive mode');
}
let rolloutPercentageToSet = rolloutPercentage;
if (rolloutPercentageToSet === undefined) {
const { percentage } = await promptAsync({
type: 'number',
message: `New rollout percentage (min: ${previousPercentage}, max: 100)`,
validate: value => {
if (value <= previousPercentage) {
return `Rollout percentage must be greater than previous rollout percentage (${previousPercentage})`;
} else if (value > 100) {
return `Rollout percentage must not be greater than 100`;
} else {
return true;
}
},
name: 'percentage',
});
if (!percentage) {
Log.log('Aborted.');
return;
}
rolloutPercentageToSet = percentage;
}
assert(rolloutPercentageToSet !== undefined);
if (rolloutPercentageToSet < previousPercentage) {
throw new Error(
`Rollout percentage must be greater than previous rollout percentage (${previousPercentage})`
);
} else if (rolloutPercentageToSet > 100) {
throw new Error('Rollout percentage must not be greater than 100');
}
const updatedUpdates = await Promise.all(
updatesToEdit.map(async u => {
return await PublishMutation.setRolloutPercentageAsync(
graphqlClient,
u.updateId,
rolloutPercentageToSet!
);
})
);
if (jsonFlag) {
printJsonOnlyOutput(getUpdateJsonInfosForUpdates(updatedUpdates));
} else {
const [updateGroupDescription] = getUpdateGroupDescriptions([updatedUpdates]);
Log.log(chalk.bold('Update group:'));
Log.log(formatUpdateGroup(updateGroupDescription));
}
}
}