Add 'fail_on_unmatched_files' input (#55)

This commit is contained in:
David Kramer 2020-06-24 23:11:41 -07:00 committed by GitHub
parent 9439581056
commit 4fb86a77e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 17 deletions

View File

@ -172,15 +172,16 @@ jobs:
The following are optional as `step.with` keys
| Name | Type | Description |
|-------------|---------|-----------------------------------------------------------------|
| `body` | String | Text communicating notable changes in this release |
| `body_path` | String | Path to load text communicating notable changes in this release |
| `draft` | Boolean | Indicator of whether or not this release is a draft |
| `prerelease`| Boolean | Indicator of whether or not is a prerelease |
| `files` | String | Newline-delimited globs of paths to assets to upload for release|
| `name` | String | Name of the release. defaults to tag name |
| `tag_name` | String | Name of a tag. defaults to `github.ref` |
| Name | Type | Description |
|---------------------------|---------|-----------------------------------------------------------------------|
| `body` | String | Text communicating notable changes in this release |
| `body_path` | String | Path to load text communicating notable changes in this release |
| `draft` | Boolean | Indicator of whether or not this release is a draft |
| `prerelease` | Boolean | Indicator of whether or not is a prerelease |
| `files` | String | Newline-delimited globs of paths to assets to upload for release |
| `name` | String | Name of the release. defaults to tag name |
| `tag_name` | String | Name of a tag. defaults to `github.ref` |
| `fail_on_unmatched_files` | Boolean | Indicator of whether to fail if any of the `files` globs match nothing|
💡When providing a `body` and `body_path` at the same time, `body_path` will be attempted first, then falling back on `body` if the path can not be read from.

View File

@ -3,7 +3,8 @@ import {
isTag,
paths,
parseConfig,
parseInputFiles
parseInputFiles,
unmatchedPatterns
} from "../src/util";
import * as assert from "assert";
@ -87,7 +88,8 @@ describe("util", () => {
input_prerelease: false,
input_files: [],
input_name: undefined,
input_tag_name: undefined
input_tag_name: undefined,
input_fail_on_unmatched_files: false
});
});
});
@ -102,9 +104,19 @@ describe("util", () => {
describe("paths", () => {
it("resolves files given a set of paths", async () => {
assert.deepStrictEqual(paths(["tests/data/**/*"]), [
"tests/data/foo/bar.txt"
]);
assert.deepStrictEqual(
paths(["tests/data/**/*", "tests/data/does/not/exist/*"]),
["tests/data/foo/bar.txt"]
);
});
});
describe("unmatchedPatterns", () => {
it("returns the patterns that don't match any files", async () => {
assert.deepStrictEqual(
unmatchedPatterns(["tests/data/**/*", "tests/data/does/not/exist/*"]),
["tests/data/does/not/exist/*"]
);
});
});
});

View File

@ -24,6 +24,9 @@ inputs:
files:
description: 'Newline-delimited list of path globs for asset files to upload'
required: false
fail_on_unmatched_files:
description: 'Fails if any of the `files` globs match nothing. Defaults to false'
required: false
env:
'GITHUB_TOKEN': 'As provided by Github Actions'
outputs:

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
import { paths, parseConfig, isTag } from "./util";
import { paths, parseConfig, isTag, unmatchedPatterns } from "./util";
import { release, upload, GitHubReleaser } from "./github";
import { setFailed, setOutput } from "@actions/core";
import { GitHub } from "@actions/github";
@ -10,6 +10,15 @@ async function run() {
if (!config.input_tag_name && !isTag(config.github_ref)) {
throw new Error(`⚠️ GitHub Releases requires a tag`);
}
if (config.input_files) {
const patterns = unmatchedPatterns(config.input_files);
patterns.forEach(pattern =>
console.warn(`🤔 Pattern '${pattern}' does not match any files.`)
);
if (patterns.length > 0 && config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ There were unmatched files`);
}
}
GitHub.plugin([
require("@octokit/plugin-throttling"),
require("@octokit/plugin-retry")

View File

@ -13,6 +13,7 @@ export interface Config {
input_files?: string[];
input_draft?: boolean;
input_prerelease?: boolean;
input_fail_on_unmatched_files?: boolean;
}
export const releaseBody = (config: Config): string | undefined => {
@ -47,7 +48,8 @@ export const parseConfig = (env: Env): Config => {
input_body_path: env.INPUT_BODY_PATH,
input_files: parseInputFiles(env.INPUT_FILES || ""),
input_draft: env.INPUT_DRAFT === "true",
input_prerelease: env.INPUT_PRERELEASE == "true"
input_prerelease: env.INPUT_PRERELEASE == "true",
input_fail_on_unmatched_files: env.INPUT_FAIL_ON_UNMATCHED_FILES == "true"
};
};
@ -59,6 +61,16 @@ export const paths = (patterns: string[]): string[] => {
}, []);
};
export const unmatchedPatterns = (patterns: string[]): string[] => {
return patterns.reduce((acc: string[], pattern: string): string[] => {
return acc.concat(
glob.sync(pattern).filter(path => lstatSync(path).isFile()).length == 0
? [pattern]
: []
);
}, []);
};
export const isTag = (ref: string): boolean => {
return ref.startsWith("refs/tags/");
};