Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f3f11d8658 | |||
| 5359433879 | |||
| a8bfe74256 | |||
| e2c5f1f74a | |||
| 2a203ec742 | |||
| 92960e0093 | |||
| 0fe31c6656 | |||
| 67dd4fbd61 | |||
| 4a7e08774d | |||
| 9ec1c123a8 | |||
| aad3bcd291 | |||
| 248934d513 | |||
| b605cb6582 | |||
| 05debf64b0 | |||
| 228acc3cd9 | |||
| 9f30846fde | |||
| 0ebe1262ee | |||
| 94d7f7b124 | |||
| f439272f69 | |||
| 161574adac | |||
| 44900feff7 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@actions/expressions",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"source": "./src/index.ts",
|
||||
|
||||
@@ -2,6 +2,7 @@ import {DescriptionPair} from "./completion/descriptionDictionary.js";
|
||||
import {Dictionary, isDictionary} from "./data/dictionary.js";
|
||||
import {ExpressionData} from "./data/expressiondata.js";
|
||||
import {Evaluator} from "./evaluator.js";
|
||||
import {FeatureFlags} from "./features.js";
|
||||
import {wellKnownFunctions} from "./funcs.js";
|
||||
import {FunctionDefinition, FunctionInfo} from "./funcs/info.js";
|
||||
import {Lexer, Token, TokenType} from "./lexer.js";
|
||||
@@ -26,13 +27,15 @@ export type CompletionItem = {
|
||||
* @param context Context available for the expression
|
||||
* @param extensionFunctions List of functions available
|
||||
* @param functions Optional map of functions to use during evaluation
|
||||
* @param featureFlags Optional feature flags to control which features are enabled
|
||||
* @returns Array of completion items
|
||||
*/
|
||||
export function complete(
|
||||
input: string,
|
||||
context: Dictionary,
|
||||
extensionFunctions: FunctionInfo[],
|
||||
functions?: Map<string, FunctionDefinition>
|
||||
functions?: Map<string, FunctionDefinition>,
|
||||
featureFlags?: FeatureFlags
|
||||
): CompletionItem[] {
|
||||
// Lex
|
||||
const lexer = new Lexer(input);
|
||||
@@ -63,7 +66,7 @@ export function complete(
|
||||
const result = contextKeys(context);
|
||||
|
||||
// Merge with functions
|
||||
result.push(...functionItems(extensionFunctions));
|
||||
result.push(...functionItems(extensionFunctions, featureFlags));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -88,10 +91,15 @@ export function complete(
|
||||
return contextKeys(result);
|
||||
}
|
||||
|
||||
function functionItems(extensionFunctions: FunctionInfo[]): CompletionItem[] {
|
||||
function functionItems(extensionFunctions: FunctionInfo[], featureFlags?: FeatureFlags): CompletionItem[] {
|
||||
const result: CompletionItem[] = [];
|
||||
const flags = featureFlags ?? new FeatureFlags();
|
||||
|
||||
for (const fdef of [...Object.values(wellKnownFunctions), ...extensionFunctions]) {
|
||||
// Filter out case function if feature is disabled
|
||||
if (fdef.name === "case" && !flags.isEnabled("allowCaseFunction")) {
|
||||
continue;
|
||||
}
|
||||
result.push({
|
||||
label: fdef.name,
|
||||
description: fdef.description,
|
||||
|
||||
@@ -12,6 +12,7 @@ export enum ErrorType {
|
||||
ErrorExceededMaxLength,
|
||||
ErrorTooFewParameters,
|
||||
ErrorTooManyParameters,
|
||||
ErrorEvenParameters,
|
||||
ErrorUnrecognizedContext,
|
||||
ErrorUnrecognizedFunction
|
||||
}
|
||||
@@ -42,6 +43,8 @@ function errorDescription(typ: ErrorType): string {
|
||||
return "Too few parameters supplied";
|
||||
case ErrorType.ErrorTooManyParameters:
|
||||
return "Too many parameters supplied";
|
||||
case ErrorType.ErrorEvenParameters:
|
||||
return "Even number of parameters supplied, requires an odd number of parameters";
|
||||
case ErrorType.ErrorUnrecognizedContext:
|
||||
return "Unrecognized named-value";
|
||||
case ErrorType.ErrorUnrecognizedFunction:
|
||||
|
||||
@@ -54,7 +54,8 @@ describe("FeatureFlags", () => {
|
||||
expect(flags.getEnabledFeatures()).toEqual([
|
||||
"missingInputsQuickfix",
|
||||
"blockScalarChompingWarning",
|
||||
"actionScaffoldingSnippets"
|
||||
"actionScaffoldingSnippets",
|
||||
"allowCaseFunction"
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,6 +35,12 @@ export interface ExperimentalFeatures {
|
||||
* @default false
|
||||
*/
|
||||
actionScaffoldingSnippets?: boolean;
|
||||
|
||||
/**
|
||||
* Enable the case() function in expressions.
|
||||
* @default false
|
||||
*/
|
||||
allowCaseFunction?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,7 +55,8 @@ export type ExperimentalFeatureKey = Exclude<keyof ExperimentalFeatures, "all">;
|
||||
const allFeatureKeys: ExperimentalFeatureKey[] = [
|
||||
"missingInputsQuickfix",
|
||||
"blockScalarChompingWarning",
|
||||
"actionScaffoldingSnippets"
|
||||
"actionScaffoldingSnippets",
|
||||
"allowCaseFunction"
|
||||
];
|
||||
|
||||
export class FeatureFlags {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {ErrorType, ExpressionError} from "./errors.js";
|
||||
import {caseFunc} from "./funcs/case.js";
|
||||
import {contains} from "./funcs/contains.js";
|
||||
import {endswith} from "./funcs/endswith.js";
|
||||
import {format} from "./funcs/format.js";
|
||||
@@ -16,6 +17,7 @@ export type ParseContext = {
|
||||
};
|
||||
|
||||
export const wellKnownFunctions: {[name: string]: FunctionDefinition} = {
|
||||
case: caseFunc,
|
||||
contains: contains,
|
||||
endswith: endswith,
|
||||
format: format,
|
||||
@@ -53,4 +55,9 @@ export function validateFunction(context: ParseContext, identifier: Token, argCo
|
||||
if (argCount > f.maxArgs) {
|
||||
throw new ExpressionError(ErrorType.ErrorTooManyParameters, identifier);
|
||||
}
|
||||
|
||||
// case function requires an odd number of arguments
|
||||
if (name === "case" && argCount % 2 === 0) {
|
||||
throw new ExpressionError(ErrorType.ErrorEvenParameters, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import {ExpressionData, Kind} from "../data/index.js";
|
||||
import {FunctionDefinition} from "./info.js";
|
||||
|
||||
export const caseFunc: FunctionDefinition = {
|
||||
name: "case",
|
||||
description:
|
||||
"`case( pred1, val1, pred2, val2, ..., default )`\n\nEvaluates predicates in order and returns the value corresponding to the first predicate that evaluates to `true`. If no predicate matches, it returns the last argument as the default value.",
|
||||
minArgs: 3,
|
||||
maxArgs: Number.MAX_SAFE_INTEGER,
|
||||
call: (...args: ExpressionData[]): ExpressionData => {
|
||||
// Evaluate predicate-result pairs
|
||||
for (let i = 0; i < args.length - 1; i += 2) {
|
||||
const predicate = args[i];
|
||||
|
||||
// Predicate must be a boolean
|
||||
if (predicate.kind !== Kind.Boolean) {
|
||||
throw new Error("case predicate must evaluate to a boolean value");
|
||||
}
|
||||
|
||||
// If predicate is true, return the corresponding result
|
||||
if (predicate.value) {
|
||||
return args[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
// No predicate matched, return default (last argument)
|
||||
return args[args.length - 1];
|
||||
}
|
||||
};
|
||||
Vendored
+157
@@ -0,0 +1,157 @@
|
||||
{
|
||||
"case": [
|
||||
{
|
||||
"expr": "case(true, 'first', 'default')",
|
||||
"result": { "kind": "String", "value": "first" }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 'first', 'default')",
|
||||
"result": { "kind": "String", "value": "default" }
|
||||
},
|
||||
{
|
||||
"expr": "case(true, 'first', false, 'second', 'default')",
|
||||
"result": { "kind": "String", "value": "first" }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 'first', true, 'second', 'default')",
|
||||
"result": { "kind": "String", "value": "second" }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 'first', false, 'second', 'default')",
|
||||
"result": { "kind": "String", "value": "default" }
|
||||
},
|
||||
{
|
||||
"expr": "case(1 == 1, 'equal', 'not equal')",
|
||||
"result": { "kind": "String", "value": "equal" }
|
||||
},
|
||||
{
|
||||
"expr": "case(1 == 2, 'equal', 'not equal')",
|
||||
"result": { "kind": "String", "value": "not equal" }
|
||||
},
|
||||
{
|
||||
"expr": "case(github.ref == 'refs/heads/main', 'main', github.event_name == 'pull_request', 'pr', 'other')",
|
||||
"contexts": {
|
||||
"github": {
|
||||
"ref": "refs/heads/main",
|
||||
"event_name": "push"
|
||||
}
|
||||
},
|
||||
"result": { "kind": "String", "value": "main" }
|
||||
},
|
||||
{
|
||||
"expr": "case(github.ref == 'refs/heads/main', 'main', github.event_name == 'pull_request', 'pr', 'other')",
|
||||
"contexts": {
|
||||
"github": {
|
||||
"ref": "refs/heads/develop",
|
||||
"event_name": "pull_request"
|
||||
}
|
||||
},
|
||||
"result": { "kind": "String", "value": "pr" }
|
||||
},
|
||||
{
|
||||
"expr": "case(github.ref == 'refs/heads/main', 'main', github.event_name == 'pull_request', 'pr', 'other')",
|
||||
"contexts": {
|
||||
"github": {
|
||||
"ref": "refs/heads/develop",
|
||||
"event_name": "push"
|
||||
}
|
||||
},
|
||||
"result": { "kind": "String", "value": "other" }
|
||||
},
|
||||
{
|
||||
"expr": "case(true, 123, 456)",
|
||||
"result": { "kind": "Number", "value": 123 }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 123, 456)",
|
||||
"result": { "kind": "Number", "value": 456 }
|
||||
},
|
||||
{
|
||||
"expr": "case(github.event == 'pull_request', 0, 1)",
|
||||
"contexts": {
|
||||
"github": {
|
||||
"event": "pull_request"
|
||||
}
|
||||
},
|
||||
"result": { "kind": "Number", "value": 0 }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 0, 1)",
|
||||
"result": { "kind": "Number", "value": 1 }
|
||||
},
|
||||
{
|
||||
"expr": "case(true, false, true)",
|
||||
"result": { "kind": "Boolean", "value": false }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, false, true)",
|
||||
"result": { "kind": "Boolean", "value": true }
|
||||
},
|
||||
{
|
||||
"expr": "case(true, '', 'default')",
|
||||
"result": { "kind": "String", "value": "" }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 'first', '')",
|
||||
"result": { "kind": "String", "value": "" }
|
||||
},
|
||||
{
|
||||
"expr": "case(true, fromJSON('[1,2,3]'), 'default')",
|
||||
"result": { "kind": "Array", "value": [1, 2, 3] }
|
||||
},
|
||||
{
|
||||
"expr": "case(true, fromJSON('{\"key\":\"value\"}'), 'default')",
|
||||
"result": { "kind": "Object", "value": { "key": "value" } }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 'first', false, 'second', false, 'third', false, 'fourth', 'default')",
|
||||
"result": { "kind": "String", "value": "default" }
|
||||
},
|
||||
{
|
||||
"expr": "case(false, 'first', false, 'second', true, 'third', false, 'fourth', 'default')",
|
||||
"result": { "kind": "String", "value": "third" }
|
||||
},
|
||||
{
|
||||
"expr": "case('not a boolean', 'first', 'default')",
|
||||
"err": {
|
||||
"kind": "evaluation",
|
||||
"value": "case predicate must evaluate to a boolean value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"expr": "case(1, 'first', 'default')",
|
||||
"err": {
|
||||
"kind": "evaluation",
|
||||
"value": "case predicate must evaluate to a boolean value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"expr": "case(null, 'first', 'default')",
|
||||
"err": {
|
||||
"kind": "evaluation",
|
||||
"value": "case predicate must evaluate to a boolean value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"expr": "case(fromJSON('[]'), 'first', 'default')",
|
||||
"err": {
|
||||
"kind": "evaluation",
|
||||
"value": "case predicate must evaluate to a boolean value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"expr": "case(fromJSON('{}'), 'first', 'default')",
|
||||
"err": {
|
||||
"kind": "evaluation",
|
||||
"value": "case predicate must evaluate to a boolean value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"expr": "case(true, 'first', false, 'second')",
|
||||
"err": {
|
||||
"kind": "parsing",
|
||||
"value": "Even number of parameters supplied, requires an odd number of parameters: 'case'. Located at position 1 within expression: case(true, 'first', false, 'second')"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@actions/languageserver",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"description": "Language server for GitHub Actions",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
@@ -48,8 +48,8 @@
|
||||
"actions-languageserver": "./bin/actions-languageserver"
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/languageservice": "^0.3.35",
|
||||
"@actions/workflow-parser": "^0.3.35",
|
||||
"@actions/languageservice": "^0.3.39",
|
||||
"@actions/workflow-parser": "^0.3.39",
|
||||
"@octokit/rest": "^21.1.1",
|
||||
"@octokit/types": "^9.0.0",
|
||||
"vscode-languageserver": "^8.0.2",
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
import {documentLinks, getInlayHints, hover, validate, ValidationConfig} from "@actions/languageservice";
|
||||
import {
|
||||
documentLinks,
|
||||
getCodeActions,
|
||||
getInlayHints,
|
||||
hover,
|
||||
validate,
|
||||
ValidationConfig
|
||||
} from "@actions/languageservice";
|
||||
import {registerLogger, setLogLevel} from "@actions/languageservice/log";
|
||||
import {clearCache, clearCacheEntry} from "@actions/languageservice/utils/workflow-cache";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import {
|
||||
CodeAction,
|
||||
CodeActionKind,
|
||||
CodeActionParams,
|
||||
CompletionItem,
|
||||
Connection,
|
||||
DocumentLink,
|
||||
@@ -79,7 +89,10 @@ export function initConnection(connection: Connection) {
|
||||
documentLinkProvider: {
|
||||
resolveProvider: false
|
||||
},
|
||||
inlayHintProvider: true
|
||||
inlayHintProvider: true,
|
||||
codeActionProvider: {
|
||||
codeActionKinds: [CodeActionKind.QuickFix]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -141,7 +154,8 @@ export function initConnection(connection: Connection) {
|
||||
getDocument(documents, textDocument),
|
||||
client,
|
||||
repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri)),
|
||||
cache
|
||||
cache,
|
||||
featureFlags
|
||||
)
|
||||
);
|
||||
});
|
||||
@@ -177,6 +191,17 @@ export function initConnection(connection: Connection) {
|
||||
});
|
||||
});
|
||||
|
||||
connection.onCodeAction((params: CodeActionParams): CodeAction[] => {
|
||||
const document = getDocument(documents, params.textDocument);
|
||||
return getCodeActions({
|
||||
uri: params.textDocument.uri,
|
||||
documentContent: document.getText(),
|
||||
diagnostics: params.context.diagnostics,
|
||||
only: params.context.only,
|
||||
featureFlags
|
||||
});
|
||||
});
|
||||
|
||||
// Make the text document manager listen on the connection
|
||||
// for open, change and close text document events
|
||||
documents.listen(connection);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {complete} from "@actions/languageservice/complete";
|
||||
import type {FeatureFlags} from "@actions/expressions";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import {CompletionItem, Connection, Position} from "vscode-languageserver";
|
||||
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||
@@ -15,11 +16,13 @@ export async function onCompletion(
|
||||
document: TextDocument,
|
||||
client: Octokit | undefined,
|
||||
repoContext: RepositoryContext | undefined,
|
||||
cache: TTLCache
|
||||
cache: TTLCache,
|
||||
featureFlags?: FeatureFlags
|
||||
): Promise<CompletionItem[]> {
|
||||
return await complete(document, position, {
|
||||
valueProviderConfig: repoContext && valueProviders(client, repoContext, cache),
|
||||
contextProviderConfig: repoContext && contextProviders(client, repoContext, cache),
|
||||
featureFlags,
|
||||
fileProvider: getFileProvider(client, cache, repoContext?.workspaceUri, async path => {
|
||||
return await connection.sendRequest(Requests.ReadFile, {path});
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@actions/languageservice",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"description": "Language service for GitHub Actions",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
@@ -47,8 +47,8 @@
|
||||
"watch": "tsc --build tsconfig.build.json --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/expressions": "^0.3.35",
|
||||
"@actions/workflow-parser": "^0.3.35",
|
||||
"@actions/expressions": "^0.3.39",
|
||||
"@actions/workflow-parser": "^0.3.39",
|
||||
"vscode-languageserver-textdocument": "^1.0.7",
|
||||
"vscode-languageserver-types": "^3.17.2",
|
||||
"vscode-uri": "^3.0.8",
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import {FeatureFlags} from "@actions/expressions";
|
||||
import {CodeAction, CodeActionKind, Diagnostic} from "vscode-languageserver-types";
|
||||
import {CodeActionContext, CodeActionProvider} from "./types.js";
|
||||
import {getQuickfixProviders} from "./quickfix/quickfix-providers.js";
|
||||
|
||||
export interface CodeActionParams {
|
||||
uri: string;
|
||||
documentContent: string;
|
||||
diagnostics: Diagnostic[];
|
||||
only?: string[];
|
||||
featureFlags?: FeatureFlags;
|
||||
}
|
||||
|
||||
export function getCodeActions(params: CodeActionParams): CodeAction[] {
|
||||
const actions: CodeAction[] = [];
|
||||
const context: CodeActionContext = {
|
||||
uri: params.uri,
|
||||
documentContent: params.documentContent,
|
||||
featureFlags: params.featureFlags
|
||||
};
|
||||
|
||||
// Build providers map based on feature flags
|
||||
const providersByKind: Map<string, CodeActionProvider[]> = new Map([
|
||||
[CodeActionKind.QuickFix, getQuickfixProviders(params.featureFlags)]
|
||||
// [CodeActionKind.Refactor, getRefactorProviders(params.featureFlags)],
|
||||
// [CodeActionKind.Source, getSourceProviders(params.featureFlags)],
|
||||
// etc
|
||||
]);
|
||||
|
||||
// Filter to requested kinds, or use all if none specified
|
||||
const requestedKinds = params.only;
|
||||
const kindsToCheck = requestedKinds
|
||||
? [...providersByKind.keys()].filter(kind => requestedKinds.some(requested => kind.startsWith(requested)))
|
||||
: [...providersByKind.keys()];
|
||||
|
||||
for (const diagnostic of params.diagnostics) {
|
||||
for (const kind of kindsToCheck) {
|
||||
const providers = providersByKind.get(kind) ?? [];
|
||||
for (const provider of providers) {
|
||||
if (provider.diagnosticCodes.includes(diagnostic.code)) {
|
||||
const action = provider.createCodeAction(context, diagnostic);
|
||||
if (action) {
|
||||
action.kind = kind;
|
||||
action.diagnostics = [diagnostic];
|
||||
actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
export type {CodeActionContext, CodeActionProvider} from "./types.js";
|
||||
@@ -0,0 +1,245 @@
|
||||
import {isMapping} from "@actions/workflow-parser";
|
||||
import {MappingToken} from "@actions/workflow-parser/templates/tokens/mapping-token";
|
||||
import {ScalarToken} from "@actions/workflow-parser/templates/tokens/scalar-token";
|
||||
import {TemplateToken} from "@actions/workflow-parser/templates/tokens/template-token";
|
||||
import {CodeAction, Position, TextEdit} from "vscode-languageserver-types";
|
||||
import {error} from "../../log.js";
|
||||
import {findToken} from "../../utils/find-token.js";
|
||||
import {getOrParseWorkflow} from "../../utils/workflow-cache.js";
|
||||
import {DiagnosticCode, MissingInputsDiagnosticData} from "../../validate-action-reference.js";
|
||||
import {CodeActionContext, CodeActionProvider} from "../types.js";
|
||||
|
||||
/**
|
||||
* Information extracted from a step token needed to generate edits
|
||||
*/
|
||||
interface StepInfo {
|
||||
/** Column where step keys start (1-indexed), e.g., the column of "uses:" */
|
||||
stepKeyColumn: number;
|
||||
/** End line of the step (1-indexed) */
|
||||
stepEndLine: number;
|
||||
/** Detected indent size (spaces per level) */
|
||||
indentSize: number;
|
||||
/** Information about existing with: block, if present */
|
||||
withInfo?: {
|
||||
keyColumn: number;
|
||||
keyEndLine: number;
|
||||
valueEndLine: number;
|
||||
hasChildren: boolean;
|
||||
/** Column of first child input (1-indexed), for indentation detection */
|
||||
firstChildColumn?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export const addMissingInputsProvider: CodeActionProvider = {
|
||||
diagnosticCodes: [DiagnosticCode.MissingRequiredInputs],
|
||||
|
||||
createCodeAction(context: CodeActionContext, diagnostic): CodeAction | undefined {
|
||||
const data = diagnostic.data as MissingInputsDiagnosticData | undefined;
|
||||
if (!data) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Parse the document to get the step token
|
||||
const stepInfo = getStepInfo(context, diagnostic.range.start);
|
||||
if (!stepInfo) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const edits = createInputEdits(data.missingInputs, stepInfo);
|
||||
if (!edits || edits.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const inputNames = data.missingInputs.map(i => i.name).join(", ");
|
||||
|
||||
return {
|
||||
title: `Add missing input${data.missingInputs.length > 1 ? "s" : ""}: ${inputNames}`,
|
||||
edit: {
|
||||
changes: {
|
||||
[context.uri]: edits
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse the document and extract step information needed for generating edits.
|
||||
* Returns undefined if parsing fails or the step token cannot be found.
|
||||
*/
|
||||
function getStepInfo(context: CodeActionContext, diagnosticPosition: Position): StepInfo | undefined {
|
||||
// Parse the document (uses cache if available from validation)
|
||||
const file = {name: context.uri, content: context.documentContent};
|
||||
const parseResult = getOrParseWorkflow(file, context.uri);
|
||||
|
||||
if (!parseResult.value) {
|
||||
error("Failed to parse workflow for missing inputs quickfix");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Find the token at the diagnostic position
|
||||
const {path} = findToken(diagnosticPosition, parseResult.value);
|
||||
|
||||
// Walk up the path to find the step token (regular-step)
|
||||
const stepToken = findStepInPath(path);
|
||||
if (!stepToken) {
|
||||
error("Could not find step token for missing inputs quickfix");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return extractStepInfo(stepToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the step token (regular-step) in the token path
|
||||
*/
|
||||
function findStepInPath(path: TemplateToken[]): MappingToken | undefined {
|
||||
// Walk backwards through path to find the step
|
||||
for (let i = path.length - 1; i >= 0; i--) {
|
||||
if (path[i].definition?.key === "regular-step" && isMapping(path[i])) {
|
||||
return path[i] as MappingToken;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract position and indentation info from a step token
|
||||
*/
|
||||
function extractStepInfo(stepToken: MappingToken): StepInfo | undefined {
|
||||
if (!stepToken.range) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Get the column of the first key in the step
|
||||
let stepKeyColumn = stepToken.range.start.column;
|
||||
if (stepToken.count > 0) {
|
||||
const firstEntry = stepToken.get(0);
|
||||
if (firstEntry?.key.range) {
|
||||
stepKeyColumn = firstEntry.key.range.start.column;
|
||||
}
|
||||
}
|
||||
|
||||
// Find the with: block if present
|
||||
let withKey: ScalarToken | undefined;
|
||||
let withToken: TemplateToken | undefined;
|
||||
for (const {key, value} of stepToken) {
|
||||
if (key.toString() === "with") {
|
||||
withKey = key;
|
||||
withToken = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate indent size
|
||||
let indentSize = 2; // Default
|
||||
let withInfo: StepInfo["withInfo"];
|
||||
|
||||
if (withKey?.range && withToken?.range) {
|
||||
// Has with: block - extract its info
|
||||
const hasChildren = isMapping(withToken) && withToken.count > 0;
|
||||
let firstChildColumn: number | undefined;
|
||||
|
||||
if (hasChildren) {
|
||||
const firstChild = (withToken as MappingToken).get(0);
|
||||
if (firstChild?.key.range) {
|
||||
firstChildColumn = firstChild.key.range.start.column;
|
||||
// Detect indent size from with: children
|
||||
indentSize = firstChildColumn - withKey.range.start.column;
|
||||
}
|
||||
}
|
||||
|
||||
withInfo = {
|
||||
keyColumn: withKey.range.start.column,
|
||||
keyEndLine: withKey.range.end.line,
|
||||
valueEndLine: withToken.range.end.line,
|
||||
hasChildren,
|
||||
firstChildColumn
|
||||
};
|
||||
} else {
|
||||
// No with: block - detect indent size using heuristics
|
||||
// Based on the step key column position, estimate indent size
|
||||
// 2-space indent files typically have step keys at column 7
|
||||
// 4-space indent files typically have step keys at column 15
|
||||
const zeroIndexedCol = stepKeyColumn - 1;
|
||||
if (zeroIndexedCol >= 10) {
|
||||
indentSize = 4;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
stepKeyColumn,
|
||||
stepEndLine: stepToken.range.end.line,
|
||||
indentSize,
|
||||
withInfo
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate text edits to add missing inputs
|
||||
*/
|
||||
function createInputEdits(missingInputs: MissingInputsDiagnosticData["missingInputs"], stepInfo: StepInfo): TextEdit[] {
|
||||
const formatInputLines = (indent: string) =>
|
||||
missingInputs.map(input => {
|
||||
const value = input.default ?? '""';
|
||||
return `${indent}${input.name}: ${value}`;
|
||||
});
|
||||
|
||||
if (stepInfo.withInfo) {
|
||||
// `with:` exists - add inputs to existing block
|
||||
const withIndent = stepInfo.withInfo.keyColumn - 1; // 0-indexed
|
||||
const inputIndentSize = stepInfo.withInfo.firstChildColumn
|
||||
? stepInfo.withInfo.firstChildColumn - stepInfo.withInfo.keyColumn
|
||||
: stepInfo.indentSize;
|
||||
|
||||
const inputIndent = " ".repeat(withIndent + inputIndentSize);
|
||||
const inputLines = formatInputLines(inputIndent);
|
||||
|
||||
// Calculate insert position
|
||||
let insertLine: number;
|
||||
if (stepInfo.withInfo.hasChildren) {
|
||||
// Insert after the last child (at end of with: block)
|
||||
// valueEndLine is 1-indexed, we want 0-indexed for Position
|
||||
insertLine = stepInfo.withInfo.valueEndLine - 1;
|
||||
} else {
|
||||
// Empty with: block - insert on the next line after with:
|
||||
// keyEndLine is 1-indexed, convert to 0-indexed and go to next line
|
||||
insertLine = stepInfo.withInfo.keyEndLine;
|
||||
}
|
||||
|
||||
const insertPosition: Position = {
|
||||
line: insertLine,
|
||||
character: 0
|
||||
};
|
||||
|
||||
return [
|
||||
{
|
||||
range: {start: insertPosition, end: insertPosition},
|
||||
newText: inputLines.map(line => line + "\n").join("")
|
||||
}
|
||||
];
|
||||
} else {
|
||||
// No `with:` key - add `with:` at the same level as other step keys
|
||||
const withKeyIndent = stepInfo.stepKeyColumn - 1; // 0-indexed (columns are 1-based)
|
||||
|
||||
const withIndent = " ".repeat(withKeyIndent);
|
||||
const inputIndent = " ".repeat(withKeyIndent + stepInfo.indentSize);
|
||||
const inputLines = formatInputLines(inputIndent);
|
||||
|
||||
const newText = `${withIndent}with:\n` + inputLines.map(line => `${line}\n`).join("");
|
||||
|
||||
// Insert at end of step
|
||||
// stepEndLine is 1-indexed, we want 0-indexed and insert before the line after
|
||||
const insertPosition: Position = {
|
||||
line: stepInfo.stepEndLine - 1,
|
||||
character: 0
|
||||
};
|
||||
|
||||
return [
|
||||
{
|
||||
range: {start: insertPosition, end: insertPosition},
|
||||
newText
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import {FeatureFlags} from "@actions/expressions";
|
||||
import {CodeActionProvider} from "../types.js";
|
||||
import {addMissingInputsProvider} from "./add-missing-inputs.js";
|
||||
|
||||
export function getQuickfixProviders(featureFlags?: FeatureFlags): CodeActionProvider[] {
|
||||
const providers: CodeActionProvider[] = [];
|
||||
|
||||
if (featureFlags?.isEnabled("missingInputsQuickfix")) {
|
||||
providers.push(addMissingInputsProvider);
|
||||
}
|
||||
|
||||
return providers;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import * as path from "path";
|
||||
import {fileURLToPath} from "url";
|
||||
import {loadTestCases, runTestCase} from "./runner.js";
|
||||
import {ValidationConfig} from "../../validate.js";
|
||||
import {ActionMetadata, ActionReference} from "../../action.js";
|
||||
import {clearCache} from "../../utils/workflow-cache.js";
|
||||
|
||||
// ESM-compatible __dirname
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
// Mock action metadata provider for tests
|
||||
const validationConfig: ValidationConfig = {
|
||||
actionsMetadataProvider: {
|
||||
fetchActionMetadata: (ref: ActionReference): Promise<ActionMetadata | undefined> => {
|
||||
const key = `${ref.owner}/${ref.name}@${ref.ref}`;
|
||||
|
||||
const metadata: Record<string, ActionMetadata> = {
|
||||
"actions/cache@v1": {
|
||||
name: "Cache",
|
||||
description: "Cache dependencies",
|
||||
inputs: {
|
||||
path: {
|
||||
description: "A list of files to cache",
|
||||
required: true
|
||||
},
|
||||
key: {
|
||||
description: "Cache key",
|
||||
required: true
|
||||
},
|
||||
"restore-keys": {
|
||||
description: "Restore keys",
|
||||
required: false
|
||||
}
|
||||
}
|
||||
},
|
||||
"actions/setup-node@v3": {
|
||||
name: "Setup Node",
|
||||
description: "Setup Node.js",
|
||||
inputs: {
|
||||
"node-version": {
|
||||
description: "Node version",
|
||||
required: true,
|
||||
default: "16"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return Promise.resolve(metadata[key]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Point to the source testdata directory
|
||||
const testdataDir = path.join(__dirname, "testdata");
|
||||
|
||||
beforeEach(() => {
|
||||
clearCache();
|
||||
});
|
||||
|
||||
describe("code action golden tests", () => {
|
||||
const testCases = loadTestCases(testdataDir);
|
||||
|
||||
if (testCases.length === 0) {
|
||||
it.todo("no test cases found - add .yml files to testdata/");
|
||||
return;
|
||||
}
|
||||
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, async () => {
|
||||
const result = await runTestCase(testCase, validationConfig);
|
||||
|
||||
if (!result.passed) {
|
||||
let errorMessage = result.error || "Test failed";
|
||||
|
||||
if (result.expected !== undefined && result.actual !== undefined) {
|
||||
errorMessage += "\n\n";
|
||||
errorMessage += "=== EXPECTED (golden file) ===\n";
|
||||
errorMessage += result.expected;
|
||||
errorMessage += "\n\n";
|
||||
errorMessage += "=== ACTUAL ===\n";
|
||||
errorMessage += result.actual;
|
||||
}
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,231 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import {TextEdit} from "vscode-languageserver-types";
|
||||
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||
import {FeatureFlags} from "@actions/expressions";
|
||||
import {validate, ValidationConfig} from "../../validate.js";
|
||||
import {getCodeActions, CodeActionParams} from "../code-actions.js";
|
||||
|
||||
// Marker pattern: # want "diagnostic message" fix="code-action-name"
|
||||
const MARKER_PATTERN = /#\s*want\s+"([^"]+)"(?:\s+fix="([^"]+)")?/;
|
||||
|
||||
export interface TestCase {
|
||||
name: string;
|
||||
inputPath: string;
|
||||
goldenPath: string;
|
||||
input: string;
|
||||
golden: string;
|
||||
markers: Marker[];
|
||||
}
|
||||
|
||||
export interface Marker {
|
||||
line: number;
|
||||
message: string;
|
||||
fix?: string;
|
||||
}
|
||||
|
||||
export interface TestResult {
|
||||
name: string;
|
||||
passed: boolean;
|
||||
error?: string;
|
||||
expected?: string;
|
||||
actual?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse markers from input file content
|
||||
*/
|
||||
export function parseMarkers(content: string): Marker[] {
|
||||
const lines = content.split("\n");
|
||||
const markers: Marker[] = [];
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const match = lines[i].match(MARKER_PATTERN);
|
||||
if (match) {
|
||||
markers.push({
|
||||
line: i,
|
||||
message: match[1],
|
||||
fix: match[2]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return markers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip markers from content (for processing)
|
||||
*/
|
||||
export function stripMarkers(content: string): string {
|
||||
return content
|
||||
.split("\n")
|
||||
.map(line => line.replace(MARKER_PATTERN, "").trimEnd())
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all test cases from a testdata directory
|
||||
*/
|
||||
export function loadTestCases(testdataDir: string): TestCase[] {
|
||||
const testCases: TestCase[] = [];
|
||||
|
||||
function walkDir(dir: string) {
|
||||
const entries = fs.readdirSync(dir, {withFileTypes: true});
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
walkDir(fullPath);
|
||||
} else if (entry.isFile() && entry.name.endsWith(".yml") && !entry.name.endsWith(".golden.yml")) {
|
||||
const goldenPath = fullPath.replace(".yml", ".golden.yml");
|
||||
|
||||
if (fs.existsSync(goldenPath)) {
|
||||
const input = fs.readFileSync(fullPath, "utf-8");
|
||||
const golden = fs.readFileSync(goldenPath, "utf-8");
|
||||
|
||||
testCases.push({
|
||||
name: path.relative(testdataDir, fullPath),
|
||||
inputPath: fullPath,
|
||||
goldenPath,
|
||||
input,
|
||||
golden,
|
||||
markers: parseMarkers(input)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walkDir(testdataDir);
|
||||
return testCases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply text edits to a document
|
||||
*/
|
||||
export function applyEdits(content: string, edits: TextEdit[]): string {
|
||||
// Sort edits in reverse order by position to apply from bottom to top
|
||||
const sortedEdits = [...edits].sort((a, b) => {
|
||||
if (b.range.start.line !== a.range.start.line) {
|
||||
return b.range.start.line - a.range.start.line;
|
||||
}
|
||||
return b.range.start.character - a.range.start.character;
|
||||
});
|
||||
|
||||
const lines = content.split("\n");
|
||||
|
||||
for (const edit of sortedEdits) {
|
||||
const startLine = edit.range.start.line;
|
||||
const startChar = edit.range.start.character;
|
||||
const endLine = edit.range.end.line;
|
||||
const endChar = edit.range.end.character;
|
||||
|
||||
const before = lines[startLine].slice(0, startChar);
|
||||
const after = lines[endLine].slice(endChar);
|
||||
|
||||
const newLines = edit.newText.split("\n");
|
||||
newLines[0] = before + newLines[0];
|
||||
newLines[newLines.length - 1] = newLines[newLines.length - 1] + after;
|
||||
|
||||
lines.splice(startLine, endLine - startLine + 1, ...newLines);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a single test case
|
||||
*/
|
||||
export async function runTestCase(testCase: TestCase, validationConfig: ValidationConfig): Promise<TestResult> {
|
||||
const strippedInput = stripMarkers(testCase.input);
|
||||
const document = TextDocument.create("file:///test.yml", "yaml", 1, strippedInput);
|
||||
|
||||
// 1. Validate and get diagnostics
|
||||
const diagnostics = await validate(document, validationConfig);
|
||||
|
||||
// 2. Verify all expected diagnostics are present
|
||||
const missingDiagnostics: string[] = [];
|
||||
for (const marker of testCase.markers) {
|
||||
const found = diagnostics.find(d => d.range.start.line === marker.line && d.message.includes(marker.message));
|
||||
if (!found) {
|
||||
missingDiagnostics.push(`line ${marker.line}: "${marker.message}"`);
|
||||
}
|
||||
}
|
||||
|
||||
if (missingDiagnostics.length > 0) {
|
||||
return {
|
||||
name: testCase.name,
|
||||
passed: false,
|
||||
error: `Missing expected diagnostics:\n ${missingDiagnostics.join(
|
||||
"\n "
|
||||
)}\n\nActual diagnostics:\n ${diagnostics.map(d => `line ${d.range.start.line}: "${d.message}"`).join("\n ")}`
|
||||
};
|
||||
}
|
||||
|
||||
// 3. Collect all edits from all matching code actions
|
||||
const allEdits: TextEdit[] = [];
|
||||
|
||||
for (const marker of testCase.markers) {
|
||||
if (!marker.fix) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const diagnostic = diagnostics.find(d => d.range.start.line === marker.line && d.message.includes(marker.message));
|
||||
|
||||
if (!diagnostic) {
|
||||
continue; // Already reported above
|
||||
}
|
||||
|
||||
const params: CodeActionParams = {
|
||||
uri: document.uri,
|
||||
documentContent: strippedInput,
|
||||
diagnostics: [diagnostic],
|
||||
featureFlags: new FeatureFlags({all: true})
|
||||
};
|
||||
|
||||
const actions = getCodeActions(params);
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- marker.fix is checked at the start of the loop
|
||||
const matchingAction = actions.find(a => a.title.toLowerCase().includes(marker.fix!.toLowerCase()));
|
||||
|
||||
if (!matchingAction) {
|
||||
return {
|
||||
name: testCase.name,
|
||||
passed: false,
|
||||
error: `Code action "${marker.fix}" not found for diagnostic on line ${marker.line}.\nAvailable actions: ${
|
||||
actions.map(a => a.title).join(", ") || "(none)"
|
||||
}`
|
||||
};
|
||||
}
|
||||
|
||||
if (!matchingAction.edit?.changes) {
|
||||
return {
|
||||
name: testCase.name,
|
||||
passed: false,
|
||||
error: `Code action "${marker.fix}" has no edits`
|
||||
};
|
||||
}
|
||||
|
||||
const edits = matchingAction.edit.changes[document.uri] || [];
|
||||
allEdits.push(...edits);
|
||||
}
|
||||
|
||||
// 4. Apply all edits and compare to golden file
|
||||
const actualOutput = applyEdits(strippedInput, allEdits);
|
||||
const expectedOutput = testCase.golden;
|
||||
|
||||
if (actualOutput.trim() !== expectedOutput.trim()) {
|
||||
return {
|
||||
name: testCase.name,
|
||||
passed: false,
|
||||
error: "Output does not match golden file",
|
||||
expected: expectedOutput,
|
||||
actual: actualOutput
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
name: testCase.name,
|
||||
passed: true
|
||||
};
|
||||
}
|
||||
languageservice/src/code-actions/tests/testdata/quickfix/existing-with-key-without-inputs.golden.yml
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1
|
||||
with:
|
||||
path: ""
|
||||
key: ""
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1
|
||||
with: # want "Missing required inputs: `path`, `key`" fix="Add missing inputs: path, key"
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1
|
||||
with:
|
||||
restore-keys: ${{ runner.os }}-
|
||||
path: ""
|
||||
key: ""
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1
|
||||
with: # want "Missing required inputs: `path`, `key`" fix="Add missing inputs: path, key"
|
||||
restore-keys: ${{ runner.os }}-
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1
|
||||
with:
|
||||
path: ""
|
||||
key: ""
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1 # want "Missing required inputs: `path`, `key`" fix="Add missing inputs: path, key"
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1
|
||||
with:
|
||||
path: ""
|
||||
key: ""
|
||||
@@ -0,0 +1,6 @@
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/cache@v1 # want "Missing required inputs: `path`, `key`" fix="Add missing inputs: path, key"
|
||||
@@ -0,0 +1,23 @@
|
||||
import {FeatureFlags} from "@actions/expressions";
|
||||
import {CodeAction, Diagnostic} from "vscode-languageserver-types";
|
||||
|
||||
export interface CodeActionContext {
|
||||
uri: string;
|
||||
documentContent: string;
|
||||
featureFlags?: FeatureFlags;
|
||||
}
|
||||
|
||||
/**
|
||||
* A provider that can produce a code action for a given diagnostic
|
||||
*/
|
||||
export interface CodeActionProvider {
|
||||
/**
|
||||
* The diagnostic codes this provider handles
|
||||
*/
|
||||
diagnosticCodes: (string | number | undefined)[];
|
||||
|
||||
/**
|
||||
* Create a code action for the diagnostic, if applicable
|
||||
*/
|
||||
createCodeAction(context: CodeActionContext, diagnostic: Diagnostic): CodeAction | undefined;
|
||||
}
|
||||
@@ -265,8 +265,8 @@ runs:
|
||||
const usingCompletion = completions.find(c => c.label === "using");
|
||||
expect(usingCompletion).toBeDefined();
|
||||
|
||||
// It should have a sortText that makes it sort first
|
||||
expect(usingCompletion?.sortText).toBe("0_using");
|
||||
// It should have a sortText that makes it sort after snippets
|
||||
expect(usingCompletion?.sortText).toBe("9_using");
|
||||
});
|
||||
|
||||
it("completes step keys inside composite action steps", async () => {
|
||||
|
||||
@@ -83,12 +83,12 @@ runs:
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_NODEJS_USING = `# For more on JavaScript actions (including @actions/toolkit), see:
|
||||
# https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-javascript-action
|
||||
using: node24
|
||||
main: index.js
|
||||
# Sample index.js (vanilla JS, no build required):
|
||||
#
|
||||
# console.log('Hello World');
|
||||
# https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-javascript-action
|
||||
using: node24
|
||||
main: index.js
|
||||
# Sample index.js (vanilla JS, no build required):
|
||||
#
|
||||
# console.log('Hello World');
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_COMPOSITE_FULL = `name: '\${1:Action Name}'
|
||||
@@ -115,9 +115,9 @@ runs:
|
||||
env:
|
||||
INPUT_NAME: \\\${{ inputs.name }}
|
||||
run: |
|
||||
GREETING="Hello $INPUT_NAME"
|
||||
echo "$GREETING"
|
||||
echo "greeting=$GREETING" >> $GITHUB_OUTPUT
|
||||
GREETING="Hello \\$INPUT_NAME"
|
||||
echo "\\$GREETING"
|
||||
echo "greeting=\\$GREETING" >> \\$GITHUB_OUTPUT
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_COMPOSITE_RUNS = `inputs:
|
||||
@@ -141,17 +141,17 @@ runs:
|
||||
env:
|
||||
INPUT_NAME: \\\${{ inputs.name }}
|
||||
run: |
|
||||
GREETING="Hello $INPUT_NAME"
|
||||
echo "$GREETING"
|
||||
echo "greeting=$GREETING" >> $GITHUB_OUTPUT
|
||||
GREETING="Hello \\$INPUT_NAME"
|
||||
echo "\\$GREETING"
|
||||
echo "greeting=\\$GREETING" >> \\$GITHUB_OUTPUT
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_COMPOSITE_USING = `# For more on composite actions, see:
|
||||
# https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-composite-action
|
||||
using: composite
|
||||
steps:
|
||||
- shell: bash
|
||||
run: echo "Hello World"
|
||||
# https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-composite-action
|
||||
using: composite
|
||||
steps:
|
||||
- shell: bash
|
||||
run: echo "Hello World"
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_DOCKER_FULL = `name: '\${1:Action Name}'
|
||||
@@ -179,9 +179,9 @@ runs:
|
||||
args:
|
||||
- -c
|
||||
- |
|
||||
GREETING="Hello $INPUT_NAME"
|
||||
echo "$GREETING"
|
||||
echo "greeting=$GREETING" >> $GITHUB_OUTPUT
|
||||
GREETING="Hello \\$INPUT_NAME"
|
||||
echo "\\$GREETING"
|
||||
echo "greeting=\\$GREETING" >> \\$GITHUB_OUTPUT
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_DOCKER_RUNS = `inputs:
|
||||
@@ -206,20 +206,20 @@ runs:
|
||||
args:
|
||||
- -c
|
||||
- |
|
||||
GREETING="Hello $INPUT_NAME"
|
||||
echo "$GREETING"
|
||||
echo "greeting=$GREETING" >> $GITHUB_OUTPUT
|
||||
GREETING="Hello \\$INPUT_NAME"
|
||||
echo "\\$GREETING"
|
||||
echo "greeting=\\$GREETING" >> \\$GITHUB_OUTPUT
|
||||
`;
|
||||
|
||||
const ACTION_SNIPPET_DOCKER_USING = `# For more on Docker actions, see:
|
||||
# https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-docker-container-action
|
||||
using: docker
|
||||
# 'docker://image:tag' uses pre-built image, 'Dockerfile' builds locally
|
||||
image: '\${1:docker://alpine:3.20}'
|
||||
entrypoint: '\${2:sh}'
|
||||
args:
|
||||
- -c
|
||||
- echo "Hello World"
|
||||
# https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-docker-container-action
|
||||
using: docker
|
||||
# 'docker://image:tag' uses pre-built image, 'Dockerfile' builds locally
|
||||
image: '\${1:docker://alpine:3.20}'
|
||||
entrypoint: '\${2:sh}'
|
||||
args:
|
||||
- -c
|
||||
- echo "Hello World"
|
||||
`;
|
||||
|
||||
/**
|
||||
@@ -282,7 +282,7 @@ export function filterActionRunsCompletions(values: Value[], path: TemplateToken
|
||||
// No using value set - show all keys but prioritize "using"
|
||||
return values.map(v => {
|
||||
if (v.label.toLowerCase() === "using") {
|
||||
return {...v, sortText: "0_using"}; // Sort first
|
||||
return {...v, sortText: "9_using"}; // Sort after snippets (0_, 1_, 2_)
|
||||
}
|
||||
return v;
|
||||
});
|
||||
@@ -354,21 +354,21 @@ export function getActionScaffoldingSnippets(
|
||||
"Scaffold a Node.js action",
|
||||
ACTION_SNIPPET_NODEJS_USING,
|
||||
position,
|
||||
"1_nodejs"
|
||||
"0_nodejs"
|
||||
),
|
||||
createSnippetCompletion(
|
||||
"Composite Action",
|
||||
"Scaffold a composite action",
|
||||
ACTION_SNIPPET_COMPOSITE_USING,
|
||||
position,
|
||||
"2_composite"
|
||||
"1_composite"
|
||||
),
|
||||
createSnippetCompletion(
|
||||
"Docker Action",
|
||||
"Scaffold a Docker action",
|
||||
ACTION_SNIPPET_DOCKER_USING,
|
||||
position,
|
||||
"3_docker"
|
||||
"2_docker"
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
import {data, DescriptionDictionary} from "@actions/expressions";
|
||||
import {data, DescriptionDictionary, FeatureFlags} from "@actions/expressions";
|
||||
import {CompletionItem, CompletionItemKind} from "vscode-languageserver-types";
|
||||
import {complete, getExpressionInput} from "./complete.js";
|
||||
import {ContextProviderConfig} from "./context-providers/config.js";
|
||||
@@ -68,12 +68,16 @@ describe("expressions", () => {
|
||||
describe("top-level auto-complete", () => {
|
||||
it("single region", async () => {
|
||||
const input = "run-name: ${{ | }}";
|
||||
const result = await complete(...getPositionFromCursor(input));
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"github",
|
||||
"inputs",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
@@ -108,12 +112,16 @@ describe("expressions", () => {
|
||||
|
||||
it("single region with existing input", async () => {
|
||||
const input = "run-name: ${{ g| }}";
|
||||
const result = await complete(...getPositionFromCursor(input), {contextProviderConfig});
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"github",
|
||||
"inputs",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
@@ -126,12 +134,16 @@ describe("expressions", () => {
|
||||
|
||||
it("single region with existing condition", async () => {
|
||||
const input = "run-name: ${{ g| == 'test' }}";
|
||||
const result = await complete(...getPositionFromCursor(input), {contextProviderConfig});
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"github",
|
||||
"inputs",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
@@ -144,12 +156,16 @@ describe("expressions", () => {
|
||||
|
||||
it("multiple regions with partial function", async () => {
|
||||
const input = "run-name: Run a ${{ inputs.test }} one-line script ${{ from|('test') == inputs.name }}";
|
||||
const result = await complete(...getPositionFromCursor(input), {contextProviderConfig});
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"github",
|
||||
"inputs",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
@@ -162,12 +178,16 @@ describe("expressions", () => {
|
||||
|
||||
it("multiple regions - first region", async () => {
|
||||
const input = "run-name: test-${{ git| == 1 }}-${{ github.event }}";
|
||||
const result = await complete(...getPositionFromCursor(input), {contextProviderConfig});
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"github",
|
||||
"inputs",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
@@ -180,12 +200,16 @@ describe("expressions", () => {
|
||||
|
||||
it("multiple regions", async () => {
|
||||
const input = "run-name: test-${{ github }}-${{ | }}";
|
||||
const result = await complete(...getPositionFromCursor(input), {contextProviderConfig});
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"github",
|
||||
"inputs",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
@@ -1126,7 +1150,10 @@ jobs:
|
||||
run: echo hi
|
||||
`;
|
||||
|
||||
const result = await complete(...getPositionFromCursor(input), {contextProviderConfig});
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
contextProviderConfig,
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
expect(result.map(x => x.label)).toEqual([
|
||||
"env",
|
||||
"github",
|
||||
@@ -1139,6 +1166,7 @@ jobs:
|
||||
"steps",
|
||||
"strategy",
|
||||
"vars",
|
||||
"case",
|
||||
"contains",
|
||||
"endsWith",
|
||||
"format",
|
||||
|
||||
@@ -6,6 +6,7 @@ import {getPositionFromCursor} from "./test-utils/cursor-position.js";
|
||||
import {TestLogger} from "./test-utils/logger.js";
|
||||
import {clearCache} from "./utils/workflow-cache.js";
|
||||
import {ValueProviderConfig, ValueProviderKind} from "./value-providers/config.js";
|
||||
import {FeatureFlags} from "@actions/expressions/features";
|
||||
|
||||
registerLogger(new TestLogger());
|
||||
|
||||
@@ -895,4 +896,32 @@ jobs:
|
||||
expect(result.some(x => x.label === "macos-latest")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("expression completions", () => {
|
||||
it("include case function when enabled", async () => {
|
||||
const input = "on: push\njobs:\n build:\n runs-on: ${{ c|";
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: true})
|
||||
});
|
||||
|
||||
expect(result).not.toBeUndefined();
|
||||
// Expression completions starting with 'c': case, contains
|
||||
const labels = result.map(x => x.label);
|
||||
expect(labels).toContain("case");
|
||||
expect(labels).toContain("contains");
|
||||
});
|
||||
|
||||
it("exclude case function when disabled", async () => {
|
||||
const input = "on: push\njobs:\n build:\n runs-on: ${{ c|";
|
||||
const result = await complete(...getPositionFromCursor(input), {
|
||||
featureFlags: new FeatureFlags({allowCaseFunction: false})
|
||||
});
|
||||
|
||||
expect(result).not.toBeUndefined();
|
||||
// Expression completions starting with 'c': contains
|
||||
const labels = result.map(x => x.label);
|
||||
expect(labels).not.toContain("case");
|
||||
expect(labels).toContain("contains");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -132,7 +132,7 @@ export async function complete(
|
||||
Mode.Completion
|
||||
);
|
||||
|
||||
return getExpressionCompletionItems(token, context, newPos);
|
||||
return getExpressionCompletionItems(token, context, newPos, config?.featureFlags);
|
||||
}
|
||||
|
||||
const indentation = guessIndentation(newDoc, 2, true); // Use 2 spaces as default and most common for YAML
|
||||
@@ -521,7 +521,8 @@ export function getExistingValues(token: TemplateToken | null, parent: TemplateT
|
||||
function getExpressionCompletionItems(
|
||||
token: TemplateToken,
|
||||
context: DescriptionDictionary,
|
||||
pos: Position
|
||||
pos: Position,
|
||||
featureFlags?: FeatureFlags
|
||||
): CompletionItem[] {
|
||||
if (!token.range) {
|
||||
return [];
|
||||
@@ -540,7 +541,7 @@ function getExpressionCompletionItems(
|
||||
const expressionInput = (getExpressionInput(currentInput, cursorOffset) || "").trim();
|
||||
|
||||
try {
|
||||
return completeExpression(expressionInput, context, [], validatorFunctions).map(item =>
|
||||
return completeExpression(expressionInput, context, [], validatorFunctions, featureFlags).map(item =>
|
||||
mapExpressionCompletionItem(item, currentInput[cursorOffset])
|
||||
);
|
||||
} catch (e) {
|
||||
|
||||
@@ -6,3 +6,4 @@ export {getInlayHints} from "./inlay-hints.js";
|
||||
export {Logger, LogLevel, registerLogger, setLogLevel} from "./log.js";
|
||||
export {validate, ValidationConfig, ActionsMetadataProvider} from "./validate.js";
|
||||
export {ValueProviderConfig, ValueProviderKind} from "./value-providers/config.js";
|
||||
export {getCodeActions, CodeActionParams} from "./code-actions/code-actions.js";
|
||||
|
||||
@@ -249,7 +249,21 @@ jobs:
|
||||
line: 7
|
||||
}
|
||||
},
|
||||
severity: DiagnosticSeverity.Error
|
||||
severity: DiagnosticSeverity.Error,
|
||||
code: "missing-required-inputs",
|
||||
data: {
|
||||
action: {
|
||||
name: "cache",
|
||||
owner: "actions",
|
||||
ref: "v1"
|
||||
},
|
||||
missingInputs: [
|
||||
{
|
||||
default: undefined,
|
||||
name: "path"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]);
|
||||
});
|
||||
@@ -294,7 +308,25 @@ jobs:
|
||||
line: 7
|
||||
}
|
||||
},
|
||||
severity: DiagnosticSeverity.Error
|
||||
severity: DiagnosticSeverity.Error,
|
||||
code: "missing-required-inputs",
|
||||
data: {
|
||||
action: {
|
||||
name: "cache",
|
||||
owner: "actions",
|
||||
ref: "v1"
|
||||
},
|
||||
missingInputs: [
|
||||
{
|
||||
default: undefined,
|
||||
name: "path"
|
||||
},
|
||||
{
|
||||
default: undefined,
|
||||
name: "key"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]);
|
||||
});
|
||||
@@ -323,7 +355,25 @@ jobs:
|
||||
line: 6
|
||||
}
|
||||
},
|
||||
severity: DiagnosticSeverity.Error
|
||||
severity: DiagnosticSeverity.Error,
|
||||
code: "missing-required-inputs",
|
||||
data: {
|
||||
action: {
|
||||
name: "cache",
|
||||
owner: "actions",
|
||||
ref: "v1"
|
||||
},
|
||||
missingInputs: [
|
||||
{
|
||||
default: undefined,
|
||||
name: "path"
|
||||
},
|
||||
{
|
||||
default: undefined,
|
||||
name: "key"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -4,10 +4,22 @@ import {Step} from "@actions/workflow-parser/model/workflow-template";
|
||||
import {ScalarToken} from "@actions/workflow-parser/templates/tokens/scalar-token";
|
||||
import {TemplateToken} from "@actions/workflow-parser/templates/tokens/template-token";
|
||||
import {Diagnostic, DiagnosticSeverity} from "vscode-languageserver-types";
|
||||
import {parseActionReference} from "./action.js";
|
||||
import {ActionReference, parseActionReference} from "./action.js";
|
||||
import {mapRange} from "./utils/range.js";
|
||||
import {ValidationConfig} from "./validate.js";
|
||||
|
||||
export const DiagnosticCode = {
|
||||
MissingRequiredInputs: "missing-required-inputs"
|
||||
} as const;
|
||||
|
||||
export interface MissingInputsDiagnosticData {
|
||||
action: ActionReference;
|
||||
missingInputs: Array<{
|
||||
name: string;
|
||||
default?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates action references in workflow steps, checking for valid inputs and required inputs.
|
||||
*/
|
||||
@@ -94,10 +106,22 @@ export async function validateActionReference(
|
||||
missingRequiredInputs.length === 1
|
||||
? `Missing required input \`${missingRequiredInputs[0][0]}\``
|
||||
: `Missing required inputs: ${missingRequiredInputs.map(input => `\`${input[0]}\``).join(", ")}`;
|
||||
|
||||
// Build minimal diagnostic data - position calculation happens in the quickfix
|
||||
const diagnosticData: MissingInputsDiagnosticData = {
|
||||
action,
|
||||
missingInputs: missingRequiredInputs.map(([name, input]) => ({
|
||||
name,
|
||||
default: input.default
|
||||
}))
|
||||
};
|
||||
|
||||
diagnostics.push({
|
||||
severity: DiagnosticSeverity.Error,
|
||||
range: mapRange((withKey || stepToken).range), // Highlight the whole step if we don't have a with key
|
||||
message: message
|
||||
range: mapRange((withKey || stepToken).range),
|
||||
message: message,
|
||||
code: DiagnosticCode.MissingRequiredInputs,
|
||||
data: diagnosticData
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,5 +6,5 @@
|
||||
"languageservice",
|
||||
"languageserver"
|
||||
],
|
||||
"version": "0.3.35"
|
||||
"version": "0.3.39"
|
||||
}
|
||||
Generated
+9
-9
@@ -136,7 +136,7 @@
|
||||
},
|
||||
"expressions": {
|
||||
"name": "@actions/expressions",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.0.3",
|
||||
@@ -396,11 +396,11 @@
|
||||
},
|
||||
"languageserver": {
|
||||
"name": "@actions/languageserver",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@actions/languageservice": "^0.3.35",
|
||||
"@actions/workflow-parser": "^0.3.35",
|
||||
"@actions/languageservice": "^0.3.39",
|
||||
"@actions/workflow-parser": "^0.3.39",
|
||||
"@octokit/rest": "^21.1.1",
|
||||
"@octokit/types": "^9.0.0",
|
||||
"vscode-languageserver": "^8.0.2",
|
||||
@@ -940,11 +940,11 @@
|
||||
},
|
||||
"languageservice": {
|
||||
"name": "@actions/languageservice",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@actions/expressions": "^0.3.35",
|
||||
"@actions/workflow-parser": "^0.3.35",
|
||||
"@actions/expressions": "^0.3.39",
|
||||
"@actions/workflow-parser": "^0.3.39",
|
||||
"vscode-languageserver-textdocument": "^1.0.7",
|
||||
"vscode-languageserver-types": "^3.17.2",
|
||||
"vscode-uri": "^3.0.8",
|
||||
@@ -13345,10 +13345,10 @@
|
||||
},
|
||||
"workflow-parser": {
|
||||
"name": "@actions/workflow-parser",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@actions/expressions": "^0.3.35",
|
||||
"@actions/expressions": "^0.3.39",
|
||||
"cronstrue": "^2.21.0",
|
||||
"yaml": "^2.0.0-8"
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@actions/workflow-parser",
|
||||
"version": "0.3.35",
|
||||
"version": "0.3.39",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"source": "./src/index.ts",
|
||||
@@ -48,7 +48,7 @@
|
||||
"watch": "tsc --build tsconfig.build.json --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/expressions": "^0.3.35",
|
||||
"@actions/expressions": "^0.3.39",
|
||||
"cronstrue": "^2.21.0",
|
||||
"yaml": "^2.0.0-8"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user