Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'fail_on_unmatched_files' input #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Expand Up @@ -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.

Expand Down
22 changes: 17 additions & 5 deletions __tests__/util.test.ts
Expand Up @@ -3,7 +3,8 @@ import {
isTag,
paths,
parseConfig,
parseInputFiles
parseInputFiles,
unmatchedPatterns
} from "../src/util";
import * as assert from "assert";

Expand Down Expand Up @@ -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
});
});
});
Expand All @@ -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/*"]
);
});
});
});
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/main.ts
@@ -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";
Expand All @@ -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")
Expand Down
14 changes: 13 additions & 1 deletion src/util.ts
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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"
};
};

Expand All @@ -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/");
};