Skip to content

Commit

Permalink
Merge branch '343-adjust-configu-store-for-approval-queue' into 0-mer…
Browse files Browse the repository at this point in the history
…ge-of-tag-and-approval-queue
  • Loading branch information
RichardAkman committed Jan 29, 2024
2 parents 84aa824 + dd6ca86 commit 80f4691
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 30 deletions.
62 changes: 44 additions & 18 deletions ts/package-lock.json

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

4 changes: 2 additions & 2 deletions ts/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
],
"dependencies": {
"@configu/lib": "^0.7.0",
"@configu/node": "^0.20.1",
"@configu/ts": "^0.20.1",
"@configu/node": "^0.20.2-next.5",
"@configu/ts": "^0.20.2-next.5",
"@oclif/core": "^3.14.1",
"@oclif/plugin-autocomplete": "^3.0.3",
"@oclif/plugin-not-found": "^3.0.5",
Expand Down
29 changes: 20 additions & 9 deletions ts/packages/cli/src/commands/upsert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flags } from '@oclif/core';
import _ from 'lodash';
import { ConfigSet, UpsertCommand } from '@configu/node';
import { ConfigSet, ConfiguConfigStore, UpsertCommand } from '@configu/node';
import { extractConfigs } from '@configu/lib';
import { BaseCommand } from '../base';
import { readFile } from '../helpers';
Expand Down Expand Up @@ -77,13 +77,24 @@ export default class Upsert extends BaseCommand<typeof Upsert> {
.value();
}

await new UpsertCommand({
store,
set,
schema,
configs,
pipe,
}).run();
this.print(`Configs upserted successfully`, { symbol: 'success' });
try {
await new UpsertCommand({
store,
set,
schema,
configs,
pipe,
}).run();
this.print(`Configs upserted successfully`, { symbol: 'success' });
} catch (error) {
if (error.name === ConfiguConfigStore.approvalQueueErrorName) {
const queueUrl = error.queueUrl as string;
// * print warning message with queue url highlighted with an underline
const warningMessage = error.message.replace(queueUrl, `\u001B[4m${queueUrl}\u001B[0m`);
this.print(warningMessage, { symbol: 'warning' });
} else {
throw error;
}
}
}
}
21 changes: 20 additions & 1 deletion ts/packages/ts/src/stores/Configu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ export type ConfiguConfigStoreConfiguration = {
tag?: string;
};

const ConfiguConfigStoreApprovalQueueErrorName = 'ConfiguConfigStoreApprovalQueueError';
class ConfiguConfigStoreApprovalQueueError extends Error {
readonly queueUrl: string;
readonly name: string = ConfiguConfigStoreApprovalQueueErrorName;

constructor(protectedSet: string, queueUrl: string) {
super(
`Your recent upsert to the ${protectedSet} ConfigSet is currently pending in its approval queue, as ${protectedSet} is a "protected set". To proceed with these changes, please review and approve them at ${queueUrl}. If you lack the necessary permissions, reach out to an authorized org member.`,
);
this.queueUrl = queueUrl;
}
}

export class ConfiguConfigStore extends ConfigStore {
private client: Axios;
private tag?: string;
public static readonly approvalQueueErrorName = ConfiguConfigStoreApprovalQueueErrorName;
constructor({
credentials,
endpoint = `https://api.configu.com`,
Expand Down Expand Up @@ -45,6 +59,11 @@ export class ConfiguConfigStore extends ConfigStore {
}

async set(configs: Config[]): Promise<void> {
await this.client.put('/config', { configs });
const response = await this.client.put('/config', { configs });
if (response.status === 202) {
const protectedSet = response.data.diff.pending[0].set;
const queueUrl = `${response.data.queueUrl}?set=${protectedSet}`;
throw new ConfiguConfigStoreApprovalQueueError(protectedSet, queueUrl);
}
}
}

0 comments on commit 80f4691

Please sign in to comment.