Skip to content

Commit 4db716b

Browse files
authored
feat: respect working_directory for files globs; add input and tests (#667)
1 parent 14820f2 commit 4db716b

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

__tests__/util.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ describe('util', () => {
128128
github_ref: '',
129129
github_repository: '',
130130
github_token: '',
131+
input_working_directory: undefined,
131132
input_append_body: false,
132133
input_body: undefined,
133134
input_body_path: undefined,
@@ -156,6 +157,7 @@ describe('util', () => {
156157
github_ref: '',
157158
github_repository: '',
158159
github_token: '',
160+
input_working_directory: undefined,
159161
input_append_body: false,
160162
input_body: undefined,
161163
input_body_path: undefined,
@@ -183,6 +185,7 @@ describe('util', () => {
183185
github_ref: '',
184186
github_repository: '',
185187
github_token: '',
188+
input_working_directory: undefined,
186189
input_append_body: false,
187190
input_body: undefined,
188191
input_body_path: undefined,
@@ -211,6 +214,7 @@ describe('util', () => {
211214
github_ref: '',
212215
github_repository: '',
213216
github_token: '',
217+
input_working_directory: undefined,
214218
input_append_body: false,
215219
input_body: undefined,
216220
input_body_path: undefined,
@@ -243,6 +247,7 @@ describe('util', () => {
243247
github_ref: '',
244248
github_repository: '',
245249
github_token: 'env-token',
250+
input_working_directory: undefined,
246251
input_append_body: false,
247252
input_body: undefined,
248253
input_body_path: undefined,
@@ -272,6 +277,7 @@ describe('util', () => {
272277
github_ref: '',
273278
github_repository: '',
274279
github_token: 'input-token',
280+
input_working_directory: undefined,
275281
input_append_body: false,
276282
input_body: undefined,
277283
input_body_path: undefined,
@@ -300,6 +306,7 @@ describe('util', () => {
300306
github_ref: '',
301307
github_repository: '',
302308
github_token: '',
309+
input_working_directory: undefined,
303310
input_append_body: false,
304311
input_body: undefined,
305312
input_body_path: undefined,
@@ -327,6 +334,7 @@ describe('util', () => {
327334
github_ref: '',
328335
github_repository: '',
329336
github_token: '',
337+
input_working_directory: undefined,
330338
input_append_body: false,
331339
input_body: undefined,
332340
input_body_path: undefined,
@@ -354,6 +362,7 @@ describe('util', () => {
354362
github_ref: '',
355363
github_repository: '',
356364
github_token: '',
365+
input_working_directory: undefined,
357366
input_append_body: true,
358367
input_body: undefined,
359368
input_body_path: undefined,
@@ -388,6 +397,10 @@ describe('util', () => {
388397
'tests/data/foo/bar.txt',
389398
]);
390399
});
400+
401+
it('resolves files relative to working_directory', async () => {
402+
assert.deepStrictEqual(paths(['data/**/*'], 'tests'), ['tests/data/foo/bar.txt']);
403+
});
391404
});
392405

393406
describe('unmatchedPatterns', () => {
@@ -397,6 +410,12 @@ describe('util', () => {
397410
['tests/data/does/not/exist/*'],
398411
);
399412
});
413+
414+
it('resolves unmatched relative to working_directory', async () => {
415+
assert.deepStrictEqual(unmatchedPatterns(['data/does/not/exist/*'], 'tests'), [
416+
'data/does/not/exist/*',
417+
]);
418+
});
400419
});
401420

402421
describe('replaceSpacesWithDots', () => {

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ inputs:
2727
files:
2828
description: "Newline-delimited list of path globs for asset files to upload"
2929
required: false
30+
working_directory:
31+
description: "Base directory to resolve 'files' globs against (defaults to job working-directory)"
32+
required: false
3033
overwrite_files:
3134
description: "Overwrite existing files with the same name. Defaults to true"
3235
required: false

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function run() {
1212
throw new Error(`⚠️ GitHub Releases requires a tag`);
1313
}
1414
if (config.input_files) {
15-
const patterns = unmatchedPatterns(config.input_files);
15+
const patterns = unmatchedPatterns(config.input_files, config.input_working_directory);
1616
patterns.forEach((pattern) => {
1717
if (config.input_fail_on_unmatched_files) {
1818
throw new Error(`⚠️ Pattern '${pattern}' does not match any files.`);
@@ -50,7 +50,7 @@ async function run() {
5050
//);
5151
const rel = await release(config, new GitHubReleaser(gh));
5252
if (config.input_files && config.input_files.length > 0) {
53-
const files = paths(config.input_files);
53+
const files = paths(config.input_files, config.input_working_directory);
5454
if (files.length == 0) {
5555
if (config.input_fail_on_unmatched_files) {
5656
throw new Error(`⚠️ ${config.input_files} does not include a valid file.`);

src/util.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as glob from 'glob';
22
import { statSync, readFileSync } from 'fs';
3+
import * as pathLib from 'path';
34

45
export interface Config {
56
github_token: string;
@@ -12,6 +13,7 @@ export interface Config {
1213
input_body?: string;
1314
input_body_path?: string;
1415
input_files?: string[];
16+
input_working_directory?: string;
1517
input_overwrite_files?: boolean;
1618
input_draft?: boolean;
1719
input_preserve_order?: boolean;
@@ -62,6 +64,7 @@ export const parseConfig = (env: Env): Config => {
6264
input_body: env.INPUT_BODY,
6365
input_body_path: env.INPUT_BODY_PATH,
6466
input_files: parseInputFiles(env.INPUT_FILES || ''),
67+
input_working_directory: env.INPUT_WORKING_DIRECTORY || undefined,
6568
input_overwrite_files: env.INPUT_OVERWRITE_FILES
6669
? env.INPUT_OVERWRITE_FILES == 'true'
6770
: undefined,
@@ -84,17 +87,34 @@ const parseMakeLatest = (value: string | undefined): 'true' | 'false' | 'legacy'
8487
return undefined;
8588
};
8689

87-
export const paths = (patterns: string[]): string[] => {
90+
export const paths = (patterns: string[], cwd?: string): string[] => {
8891
return patterns.reduce((acc: string[], pattern: string): string[] => {
89-
return acc.concat(glob.sync(pattern).filter((path) => statSync(path).isFile()));
92+
const matches = glob.sync(pattern, { cwd, dot: true, absolute: false });
93+
const resolved = matches
94+
.map((p) => (cwd ? pathLib.join(cwd, p) : p))
95+
.filter((p) => {
96+
try {
97+
return statSync(p).isFile();
98+
} catch {
99+
return false;
100+
}
101+
});
102+
return acc.concat(resolved);
90103
}, []);
91104
};
92105

93-
export const unmatchedPatterns = (patterns: string[]): string[] => {
106+
export const unmatchedPatterns = (patterns: string[], cwd?: string): string[] => {
94107
return patterns.reduce((acc: string[], pattern: string): string[] => {
95-
return acc.concat(
96-
glob.sync(pattern).filter((path) => statSync(path).isFile()).length == 0 ? [pattern] : [],
97-
);
108+
const matches = glob.sync(pattern, { cwd, dot: true, absolute: false });
109+
const files = matches.filter((p) => {
110+
try {
111+
const full = cwd ? pathLib.join(cwd, p) : p;
112+
return statSync(full).isFile();
113+
} catch {
114+
return false;
115+
}
116+
});
117+
return acc.concat(files.length == 0 ? [pattern] : []);
98118
}, []);
99119
};
100120

0 commit comments

Comments
 (0)