Skip to content

Commit 4137715

Browse files
committed
feat(gh-inputs): filter comments and blank lines in multiline inputs
1 parent 28e581d commit 4137715

File tree

29 files changed

+1089
-945
lines changed

29 files changed

+1089
-945
lines changed

b2-workflow/dist/index.js

Lines changed: 41 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

b2-workflow/dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

boost-clone/dist/index.js

Lines changed: 105 additions & 104 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

boost-clone/dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmake-workflow/dist/index.js

Lines changed: 113 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmake-workflow/dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/gh-inputs/src/index.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,100 @@ describe('gh-inputs', () => {
133133

134134
expect(result).toEqual(['default']);
135135
});
136+
137+
it('should filter comment lines by default', () => {
138+
mockedCore.getMultilineInput.mockReturnValue([
139+
'value1',
140+
'# this is a comment',
141+
'value2',
142+
' # indented comment',
143+
'value3'
144+
]);
145+
146+
const result = ghInputs.getMultilineInput('test');
147+
148+
expect(result).toEqual(['value1', 'value2', 'value3']);
149+
});
150+
151+
it('should filter blank lines by default', () => {
152+
mockedCore.getMultilineInput.mockReturnValue([
153+
'value1',
154+
'',
155+
'value2',
156+
' ',
157+
'value3'
158+
]);
159+
160+
const result = ghInputs.getMultilineInput('test');
161+
162+
expect(result).toEqual(['value1', 'value2', 'value3']);
163+
});
164+
165+
it('should filter both comments and blank lines', () => {
166+
mockedCore.getMultilineInput.mockReturnValue([
167+
'# Header comment',
168+
'',
169+
'key1:value1',
170+
' ',
171+
'# Section divider',
172+
'key2:value2',
173+
'# Trailing comment'
174+
]);
175+
176+
const result = ghInputs.getMultilineInput('test');
177+
178+
expect(result).toEqual(['key1:value1', 'key2:value2']);
179+
});
180+
181+
it('should preserve comments when filterComments is false', () => {
182+
mockedCore.getMultilineInput.mockReturnValue([
183+
'value1',
184+
'# comment',
185+
'value2'
186+
]);
187+
188+
const result = ghInputs.getMultilineInput('test', { filterComments: false });
189+
190+
expect(result).toEqual(['value1', '# comment', 'value2']);
191+
});
192+
193+
it('should preserve blank lines when filterBlankLines is false', () => {
194+
mockedCore.getMultilineInput.mockReturnValue([
195+
'value1',
196+
'',
197+
'value2'
198+
]);
199+
200+
const result = ghInputs.getMultilineInput('test', { filterBlankLines: false });
201+
202+
expect(result).toEqual(['value1', '', 'value2']);
203+
});
204+
205+
it('should use custom comment prefix', () => {
206+
mockedCore.getMultilineInput.mockReturnValue([
207+
'value1',
208+
'// js comment',
209+
'# not a comment',
210+
'value2'
211+
]);
212+
213+
const result = ghInputs.getMultilineInput('test', { commentPrefix: '//' });
214+
215+
expect(result).toEqual(['value1', '# not a comment', 'value2']);
216+
});
217+
218+
it('should return empty array when all lines are comments or blank', () => {
219+
mockedCore.getMultilineInput.mockReturnValue([
220+
'# comment only',
221+
'',
222+
' # another comment',
223+
' '
224+
]);
225+
226+
const result = ghInputs.getMultilineInput('test');
227+
228+
expect(result).toEqual([]);
229+
});
136230
});
137231

138232
describe('getArray', () => {

common/gh-inputs/src/index.ts

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const defaultOptions: InputOptions = {
88
required: false,
99
trimWhitespace: true,
1010
fallbackEnv: undefined,
11-
defaultValue: ''
11+
defaultValue: '',
12+
filterComments: true,
13+
commentPrefix: '#',
14+
filterBlankLines: true
1215
};
1316

1417
const defaultSplitRegex = /[,; ]/;
@@ -78,15 +81,40 @@ export function getRegex(name: string | string[], options: InputOptions = {}): R
7881
return new RegExp(getInput(name, options));
7982
}
8083

84+
/**
85+
* Filters an array of lines based on comment and blank line options.
86+
*
87+
* Only full-line comments are filtered (lines where the first non-whitespace
88+
* character is the comment prefix). Inline comments are intentionally not
89+
* supported to avoid the complexity of escape sequences and quoted strings.
90+
*
91+
* @param lines - Array of lines to filter
92+
* @param opts - Options containing filterComments, commentPrefix, and filterBlankLines
93+
* @returns Filtered array of lines
94+
*/
95+
function filterLines(lines: string[], opts: InputOptions): string[] {
96+
return lines.filter(line => {
97+
const trimmed = line.trim();
98+
if (opts.filterBlankLines && trimmed === '') {
99+
return false;
100+
}
101+
if (opts.filterComments && opts.commentPrefix && trimmed.startsWith(opts.commentPrefix)) {
102+
return false;
103+
}
104+
return true;
105+
});
106+
}
107+
81108
/**
82109
* Retrieves a multiline GitHub Actions input as an array of strings.
83110
*
84111
* Each line of the input becomes a separate element in the returned array.
85112
* Supports multiple input name aliases and environment variable fallbacks.
113+
* By default, filters out comment lines (starting with '#') and blank lines.
86114
*
87115
* @param name - The input name or array of name aliases to retrieve
88116
* @param options - Configuration options including required flag, trimWhitespace,
89-
* fallbackEnv, and defaultValue (can be string or string[])
117+
* fallbackEnv, defaultValue, filterComments, commentPrefix, and filterBlankLines
90118
* @returns Array of strings, one per line of input, or defaultValue if not found
91119
* @throws {Error} When input is marked as required but no value is available
92120
*/
@@ -96,9 +124,12 @@ export function getMultilineInput(name: string | string[], options: InputOptions
96124

97125
for (const n of nameArr) {
98126
const coreOptions = { ...opts, required: false };
99-
const str = core.getMultilineInput(n, coreOptions);
100-
if (str && str.length > 0) {
101-
return str;
127+
const lines = core.getMultilineInput(n, coreOptions);
128+
if (lines && lines.length > 0) {
129+
const filtered = filterLines(lines, opts);
130+
if (filtered.length > 0) {
131+
return filtered;
132+
}
102133
}
103134
}
104135

@@ -107,13 +138,13 @@ export function getMultilineInput(name: string | string[], options: InputOptions
107138
for (const env of envArray) {
108139
const envVal = process.env[env];
109140
if (envVal) {
110-
if (opts.trimWhitespace) {
111-
const trimmed = envVal.trim();
112-
if (trimmed) {
113-
return [trimmed];
114-
}
115-
} else {
116-
return [envVal];
141+
const envLines = envVal.split('\n');
142+
const filtered = filterLines(
143+
opts.trimWhitespace ? envLines.map(l => l.trim()) : envLines,
144+
opts
145+
);
146+
if (filtered.length > 0) {
147+
return filtered;
117148
}
118149
}
119150
}

common/gh-inputs/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export interface InputOptions {
1010
fallbackEnv?: string | string[];
1111
/** Default value if input is not provided */
1212
defaultValue?: string | boolean | string[];
13+
/** Whether to filter out comment lines (lines starting with commentPrefix). Defaults to true. */
14+
filterComments?: boolean;
15+
/** The prefix that identifies comment lines. Defaults to '#'. */
16+
commentPrefix?: string;
17+
/** Whether to filter out blank/whitespace-only lines. Defaults to true. */
18+
filterBlankLines?: boolean;
1319
}
1420

1521
/**

cpp-matrix/dist/index.js

Lines changed: 59 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)