Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Add support for specifying content type (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
et13 authored Apr 20, 2020
1 parent 3293d26 commit d27de5c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ inputs:
tags:
description: 'Stringified form of a JSON object with the following shape: { [propertyName: string]: string; }'
required: false
contentType:
description: 'Content type associated with the values'
required: false
runs:
using: 'node12'
main: 'lib/index.js'
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/configstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ const userAgentPrefix = "GitHub-AppConfiguration-Sync/1.0.0";
* @param label Label assigned to the modified settings.
* @param prefix Prefix appended to the front of the setting key.
* @param tags Tags applied to the modified settings.
* @param contentType Content type applied to the settings.
*/
export async function syncConfig(config: any, connectionString: string, strict: boolean, label?: string, prefix?: string, tags?: Tags): Promise<void> {
export async function syncConfig(config: any, connectionString: string, strict: boolean, label?: string, prefix?: string, tags?: Tags, contentType?: string): Promise<void> {
const appConfigurationOptions = {
userAgentOptions: {
userAgentPrefix: userAgentPrefix
Expand All @@ -25,7 +26,7 @@ export async function syncConfig(config: any, connectionString: string, strict:
const client = new AppConfigurationClient(connectionString, appConfigurationOptions);

core.info('Determining which keys to sync');
const settingsToAdd = getSettingsToAdd(config, label, prefix, tags);
const settingsToAdd = getSettingsToAdd(config, label, prefix, tags, contentType);
const settingsToDelete = strict ? await getSettingsToDelete(client, settingsToAdd, label, prefix) : [];

const failedDeletes = await deleteSettings(client, settingsToDelete);
Expand All @@ -51,7 +52,7 @@ export async function syncConfig(config: any, connectionString: string, strict:
}
}

function getSettingsToAdd(config: any, label?: string, prefix?: string, tags?: Tags): SetConfigurationSettingParam[] {
function getSettingsToAdd(config: any, label?: string, prefix?: string, tags?: Tags, contentType?: string): SetConfigurationSettingParam[] {
const settings: SetConfigurationSettingParam[] = [];

for (const key in config) {
Expand All @@ -60,6 +61,7 @@ function getSettingsToAdd(config: any, label?: string, prefix?: string, tags?: T
value: getSettingValue(config[key]),
label: label ? label : undefined,
tags: tags ? tags : undefined,
contentType: contentType ? contentType : undefined
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Input {
label?: string;
depth?: number;
tags?: Tags;
contentType?: string;
}

/**
Expand All @@ -40,7 +41,8 @@ export function getInput(): Input {
prefix: getNonRequiredInputString('prefix'),
label: getNonRequiredInputString('label'),
depth: getDepth(),
tags: getTags()
tags: getTags(),
contentType: getNonRequiredInputString('contentType')
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function main(): Promise<void> {
const input = getInput();
const config = await loadConfigFiles(input.workspace, input.configFile, input.format, input.separator, input.depth);

await syncConfig(config, input.connectionString, input.strict, input.label, input.prefix, input.tags);
await syncConfig(config, input.connectionString, input.strict, input.label, input.prefix, input.tags, input.contentType);
} catch (error) {
core.setFailed(getErrorMessage(error));
}
Expand Down
13 changes: 13 additions & 0 deletions tests/configstore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ describe('syncConfig', () => {
expect(core.error).not.toBeCalled();
})

it('adds setting with content type', async () => {
const config = { "Key1": "Value1" };
await configstore.syncConfig(config, "connection string", false, undefined, undefined, undefined, "contentType");

expect(AppConfigurationClient).toBeCalled();
expect(AppConfigurationClient.prototype.listConfigurationSettings).not.toBeCalled();
expect(AppConfigurationClient.prototype.deleteConfigurationSetting).not.toBeCalled();
expect(AppConfigurationClient.prototype.setConfigurationSetting).toHaveBeenCalledTimes(1);
expect(AppConfigurationClient.prototype.setConfigurationSetting).toHaveBeenCalledWith({ key: "Key1", value: "Value1", contentType: "contentType" });
expect(core.setFailed).not.toBeCalled();
expect(core.error).not.toBeCalled();
})

function setListConfigurationSettingsMock(...configurationSettings: ConfigurationSetting[]) {
AppConfigurationClient.prototype.listConfigurationSettings = jest.fn((options?: ListConfigurationSettingsOptions) => {
const iter = getConfigurationSettingIterator(configurationSettings);
Expand Down
16 changes: 16 additions & 0 deletions tests/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ describe('input', () => {
expect(input.tags).toEqual({ A: "foo", B: "foo" });
})

it('validation succeeds with missing content type', () => {
mockInput = getDefaultInput();
mockInput.contentType = undefined;

const input = getInput();
expect(input.contentType).toBe(undefined);
})

it('validation succeeds with content type', () => {
mockInput = getDefaultInput();
mockInput.contentType = "contentType";

const input = getInput();
expect(input.contentType).toBe("contentType");
})

function verifySeparator(separator: string) {
mockInput = getDefaultInput();
mockInput.separator = separator
Expand Down

0 comments on commit d27de5c

Please sign in to comment.