Merge pull request #177 from github/joshmgross/environment-case-sensitivity

Support case-insensitive value providers
This commit is contained in:
Josh Gross
2023-03-09 12:30:12 -05:00
committed by GitHub
4 changed files with 68 additions and 2 deletions
+2
View File
@@ -20,10 +20,12 @@ export function valueProviders(
return {
"job-environment": {
kind: ValueProviderKind.AllowedValues,
caseInsensitive: true,
get: (_: WorkflowContext) => getEnvironments(client, cache, repo.owner, repo.name)
},
"job-environment-name": {
kind: ValueProviderKind.AllowedValues,
caseInsensitive: true,
get: (_: WorkflowContext) => getEnvironments(client, cache, repo.owner, repo.name)
},
"runs-on": {
+62
View File
@@ -3,6 +3,7 @@ import {createDocument} from "./test-utils/document";
import {validate} from "./validate";
import {defaultValueProviders} from "./value-providers/default";
import {clearCache} from "./utils/workflow-cache";
import {ValueProvider, ValueProviderConfig, ValueProviderKind} from "./value-providers/config";
beforeEach(() => {
clearCache();
@@ -193,4 +194,65 @@ jobs:
}
} as Diagnostic);
});
describe("value provider case sensitivity", () => {
it("value with a different case and case sensitive provider", async () => {
const workflow = `
on: push
jobs:
build:
runs-on: ubuntu-latest
environment: TEST
steps:
- run: echo
`;
const valueProviderConfig: ValueProviderConfig = {
"job-environment": {
kind: ValueProviderKind.AllowedValues,
get: async () => [{label: "test"}],
caseInsensitive: false
}
};
const result = await validate(createDocument("wf.yaml", workflow), {valueProviderConfig});
expect(result).toEqual([
{
message: "Value 'TEST' is not valid",
severity: DiagnosticSeverity.Error,
range: {
start: {
line: 5,
character: 19
},
end: {
line: 5,
character: 23
}
}
}
]);
});
it("value with a different case and case insensitive provider", async () => {
const workflow = `
on: push
jobs:
build:
runs-on: ubuntu-latest
environment: TEST
steps:
- run: echo
`;
const valueProviderConfig: ValueProviderConfig = {
"job-environment": {
kind: ValueProviderKind.AllowedValues,
get: async () => [{label: "test"}],
caseInsensitive: true
}
};
const result = await validate(createDocument("wf.yaml", workflow), {valueProviderConfig});
expect(result).toEqual([]);
});
});
});
+3 -2
View File
@@ -130,10 +130,11 @@ async function additionalValidations(
if (valueProvider) {
const customValues = await valueProvider.get(getProviderContext(documentUri, template, root, token));
const customValuesMap = new Set(customValues.map(x => x.label));
const caseInsensitive = valueProvider.caseInsensitive ?? false;
const customValuesMap = new Set(customValues.map(x => (caseInsensitive ? x.label.toLowerCase() : x.label)));
if (isString(token)) {
if (!customValuesMap.has(token.value)) {
if (!customValuesMap.has(caseInsensitive ? token.value.toLowerCase() : token.value)) {
invalidValue(diagnostics, token, valueProvider.kind);
}
}
@@ -21,6 +21,7 @@ export enum ValueProviderKind {
export type ValueProvider = {
kind: ValueProviderKind;
caseInsensitive?: boolean;
get: (context: WorkflowContext, existingValues?: Set<string>) => Promise<Value[]>;
};