Skip to content

Commit b95219e

Browse files
committed
PR feedback
1 parent 2d9b787 commit b95219e

7 files changed

Lines changed: 59 additions & 16 deletions

File tree

languageservice/src/code-actions/quickfix/add-missing-inputs.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ export const addMissingInputsProvider: CodeActionProvider = {
2929
}
3030
};
3131

32-
function createInputEdits(data: MissingInputsDiagnosticData): TextEdit[] | undefined {
32+
function createInputEdits(data: MissingInputsDiagnosticData): TextEdit[] {
3333
const edits: TextEdit[] = [];
3434

35+
const formatInputLines = (indent: string) =>
36+
data.missingInputs.map(input => {
37+
const value = input.default ?? '""';
38+
return `${indent}${input.name}: ${value}`;
39+
});
40+
3541
if (data.hasWithKey && data.withIndent !== undefined) {
3642
// `with:` exists - use its indentation + 2 for inputs
37-
const inputIndent = " ".repeat(data.withIndent + 2);
38-
39-
const inputLines = data.missingInputs.map(input => {
40-
const value = input.default !== undefined ? input.default : '""';
41-
return `${inputIndent}${input.name}: ${value}`;
42-
});
43+
const inputIndent = " ".repeat(data.withIndent + data.indentSize);
44+
const inputLines = formatInputLines(inputIndent);
4345

4446
edits.push({
4547
range: {start: data.insertPosition, end: data.insertPosition},
@@ -48,12 +50,8 @@ function createInputEdits(data: MissingInputsDiagnosticData): TextEdit[] | undef
4850
} else {
4951
// No `with:` key - `with:` at step indentation, inputs at step indentation + 2
5052
const withIndent = " ".repeat(data.stepIndent);
51-
const inputIndent = " ".repeat(data.stepIndent + 2);
52-
53-
const inputLines = data.missingInputs.map(input => {
54-
const value = input.default !== undefined ? input.default : '""';
55-
return `${inputIndent}${input.name}: ${value}`;
56-
});
53+
const inputIndent = " ".repeat(data.stepIndent + data.indentSize);
54+
const inputLines = formatInputLines(inputIndent);
5755

5856
const newText = [`${withIndent}with:\n`, ...inputLines.map(line => `${line}\n`)].join("");
5957

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
on: push
2+
jobs:
3+
build:
4+
runs-on: ubuntu-latest
5+
steps:
6+
- uses: actions/cache@v1
7+
with:
8+
path: ""
9+
key: ""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
on: push
2+
jobs:
3+
build:
4+
runs-on: ubuntu-latest
5+
steps:
6+
- uses: actions/cache@v1 # want "Missing required inputs: `path`, `key`" fix="Add missing inputs: path, key"

languageservice/src/code-actions/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {CodeAction, Diagnostic} from "vscode-languageserver-types";
22

33
export interface CodeActionContext {
44
uri: string;
5-
// TODO: add things like workflow template, parsed content, etc.
65
}
76

87
/**

languageservice/src/validate-action.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface MissingInputsDiagnosticData {
2222
// Indentation of the `with:` key if present, or the step's base indentation
2323
withIndent?: number;
2424
stepIndent: number;
25+
indentSize: number;
2526
// Position where new content should be inserted
2627
insertPosition: {line: number; character: number};
2728
}
@@ -30,7 +31,8 @@ export async function validateAction(
3031
diagnostics: Diagnostic[],
3132
stepToken: TemplateToken,
3233
step: Step | undefined,
33-
config: ValidationConfig | undefined
34+
config: ValidationConfig | undefined,
35+
indentSize: number
3436
): Promise<void> {
3537
if (!isMapping(stepToken) || !step || !isActionStep(step) || !config?.actionsMetadataProvider) {
3638
return;
@@ -135,6 +137,7 @@ export async function validateAction(
135137
hasWithKey: withKey !== undefined,
136138
withIndent,
137139
stepIndent,
140+
indentSize,
138141
insertPosition
139142
};
140143

languageservice/src/validate.actions.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ jobs:
258258
ref: "v1"
259259
},
260260
hasWithKey: true,
261+
indentSize: 2,
261262
insertPosition: {
262263
character: 0,
263264
line: 9
@@ -324,6 +325,7 @@ jobs:
324325
ref: "v1"
325326
},
326327
hasWithKey: true,
328+
indentSize: 2,
327329
insertPosition: {
328330
character: 0,
329331
line: 9
@@ -378,6 +380,7 @@ jobs:
378380
ref: "v1"
379381
},
380382
hasWithKey: false,
383+
indentSize: 2,
381384
insertPosition: {
382385
character: 0,
383386
line: 7

languageservice/src/validate.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {splitAllowedContext} from "@actions/workflow-parser/templates/allowed-co
88
import {BasicExpressionToken} from "@actions/workflow-parser/templates/tokens/basic-expression-token";
99
import {StringToken} from "@actions/workflow-parser/templates/tokens/string-token";
1010
import {TemplateToken} from "@actions/workflow-parser/templates/tokens/template-token";
11+
import {MappingToken} from "@actions/workflow-parser/templates/tokens/mapping-token";
1112
import {TokenRange} from "@actions/workflow-parser/templates/tokens/token-range";
1213
import {File} from "@actions/workflow-parser/workflows/file";
1314
import {FileProvider} from "@actions/workflow-parser/workflows/file-provider";
@@ -155,7 +156,8 @@ async function additionalValidations(
155156
// Validate action metadata (inputs, required fields) for regular steps
156157
if (token.definition?.key === "regular-step" && token.range) {
157158
const context = getProviderContext(documentUri, template, root, token.range);
158-
await validateAction(diagnostics, token, context.step, config);
159+
const indentSize = detectIndentSize(root as MappingToken);
160+
await validateAction(diagnostics, token, context.step, config, indentSize);
159161
}
160162

161163
// Validate job-level reusable workflow uses field format
@@ -783,3 +785,26 @@ function getStaticConcurrencyGroup(token: TemplateToken | undefined): StringToke
783785

784786
return undefined;
785787
}
788+
789+
function detectIndentSize(rootToken: MappingToken): number {
790+
// Find the "jobs" key in the root mapping to get its column position
791+
let jobsKeyColumn: number | undefined;
792+
let jobsValue: TemplateToken | undefined;
793+
794+
for (const {key, value} of rootToken) {
795+
if (key.toString() === "jobs") {
796+
jobsKeyColumn = key.range?.start.column;
797+
jobsValue = value;
798+
break;
799+
}
800+
}
801+
802+
if (jobsKeyColumn !== undefined && jobsValue && isMapping(jobsValue) && jobsValue.count > 0) {
803+
const firstJob = jobsValue.get(0);
804+
if (firstJob?.key.range) {
805+
return firstJob.key.range.start.column - jobsKeyColumn;
806+
}
807+
}
808+
809+
return 2; // fallback
810+
}

0 commit comments

Comments
 (0)