Skip to content

[dev] Improve path #8

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

Merged
merged 64 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
80906e6
Merge pull request #7 from mudlabs/master
mudlabs Oct 29, 2020
14de335
Update action.js
mudlabs Oct 29, 2020
e4bd692
Update test.yaml
mudlabs Oct 29, 2020
66cf864
Update action.js
mudlabs Oct 29, 2020
28fd661
Update action.js
mudlabs Oct 29, 2020
bf88bac
Update action.js
mudlabs Oct 29, 2020
8d8e4e0
Update action.js
mudlabs Oct 29, 2020
6123505
Update action.js
mudlabs Oct 29, 2020
46d8bae
Update action.js
mudlabs Oct 29, 2020
43973a3
Update action.js
mudlabs Oct 29, 2020
79201c7
Update action.js
mudlabs Oct 29, 2020
1c3f362
Update action.js
mudlabs Oct 29, 2020
ac83314
Update action.js
mudlabs Oct 29, 2020
ce8f9a9
Update action.js
mudlabs Oct 29, 2020
afb477d
Update action.js
mudlabs Oct 29, 2020
f9fac61
Update action.js
mudlabs Oct 29, 2020
ab5febd
Update action.js
mudlabs Oct 29, 2020
31c02bd
Update action.js
mudlabs Oct 29, 2020
6b82350
Update action.js
mudlabs Oct 29, 2020
f584163
Update action.js
mudlabs Oct 29, 2020
26b2c44
Create temp.yaml
mudlabs Oct 29, 2020
08810c7
Update action.js
mudlabs Oct 29, 2020
fb14da1
Delete temp.yaml
mudlabs Oct 29, 2020
65575f5
Update action.js
mudlabs Oct 29, 2020
50195ee
Update npm.worker.config.yml
mudlabs Oct 29, 2020
6104d7f
Update action.js
mudlabs Oct 29, 2020
e7b5e5e
Rename npm.worker.config.yml to npm.worker.config.yaml
mudlabs Oct 29, 2020
10295f9
Commit from GitHub Actions (NPM Worker)
mudlabs Oct 29, 2020
c07f1e3
Update action.js
mudlabs Oct 29, 2020
74015f5
Update npm.worker.config.yaml
mudlabs Oct 29, 2020
d2cdd32
Commit from GitHub Actions (NPM Worker)
mudlabs Oct 29, 2020
9cba65d
Update action.js
mudlabs Oct 29, 2020
80f0067
Update test.yaml
mudlabs Oct 29, 2020
dd63fe4
Update README.md
mudlabs Oct 29, 2020
90358b0
Update test.yaml
mudlabs Oct 29, 2020
b4dc3ec
Update README.md
mudlabs Oct 29, 2020
5da01e9
Update test.yaml
mudlabs Oct 29, 2020
76d6073
Update action.js
mudlabs Oct 29, 2020
0dc7344
Update action.js
mudlabs Oct 29, 2020
e12d85c
Update action.js
mudlabs Oct 29, 2020
e42dfda
Update README.md
mudlabs Oct 29, 2020
6ddc5ed
Update action.js
mudlabs Oct 29, 2020
19a98f2
Update README.md
mudlabs Oct 29, 2020
89423c5
Update action.js
mudlabs Oct 29, 2020
2aaf6ef
Update README.md
mudlabs Oct 29, 2020
2c8aa65
Update test.yaml
mudlabs Oct 29, 2020
896a044
Update action.js
mudlabs Oct 29, 2020
0b6d7b8
Update README.md
mudlabs Oct 29, 2020
f354d0d
Update test.yaml
mudlabs Oct 29, 2020
328ee3d
Update README.md
mudlabs Oct 29, 2020
a26acb6
Update action.js
mudlabs Oct 29, 2020
1e10458
Update npm.worker.config.yaml
mudlabs Oct 30, 2020
54d7aef
Commit from GitHub Actions (NPM Worker)
mudlabs Oct 30, 2020
02475c9
Update action.js
mudlabs Oct 30, 2020
373008d
Update test.yaml
mudlabs Oct 30, 2020
1494401
Update README.md
mudlabs Oct 30, 2020
bd4e9ec
Update action.js
mudlabs Oct 30, 2020
b8958d5
Update README.md
mudlabs Oct 30, 2020
32f5294
Update action.js
mudlabs Oct 30, 2020
6d3bec4
Update README.md
mudlabs Oct 30, 2020
f5c0ac9
Update action.js
mudlabs Oct 30, 2020
90411f6
Update README.md
mudlabs Oct 30, 2020
4a73fb9
Update test.yaml
mudlabs Oct 30, 2020
fe9c003
Update README.md
mudlabs Oct 30, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
4 changes: 3 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Test Diff

on:
push:
paths:
- '*.md'

jobs:
test:
Expand All @@ -14,8 +16,8 @@ jobs:
id: diff
uses: ./
with:
path: README.md
strict: false
path: 'READ*.md'
- run: |
echo "Added? ${{ steps.diff.outputs.added }}"
echo "Modified? ${{ steps.diff.outputs.modified }}"
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ Very simple, you provide the action with a `path` to a _file_ or _folder_, and i
## Usage
```yaml
- name: Simple Diff
uses: mudlabs/simple-diff@v1.1.0
uses: mudlabs/simple-diff@v1.2.0
with:
path: path/to/file
```

## Inputs
| Input | Description | Default |
| --- | :--- | --- |
| `path` | Specifies a path from _root_ to the file or folder you want to check. | |
| `path` | Specifies a path from the _root_ of your repository to the file or folder you want to check. If not provided the action will try to find a path specification from the workflow file itself. The path can be a `glob` string. | |
| `strict` | Specifies the action should fail if the `path` is not in the commits diff tree. | `true` |

## Outputs
Expand All @@ -42,6 +42,7 @@ Very simple, you provide the action with a `path` to a _file_ or _folder_, and i

## Example Case
You have a workflow that only runs on a push event to a file path. But you don't want it to run if the file was `removed` _(deleted)_.
- _Note:_ In this example we do not specify the path property. If your workflow is conditioned to only run when changes to a given path occure, you don't need to provide the action with the file path. _(assuming that's the file path you want to check)_.

```yaml
name: My File Workflow
Expand All @@ -57,10 +58,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Simple Diff
uses: mudlabs/simple-diff@v1.2.0
id: diff
uses: mudlabs/simple-diff@v1.1.0
with:
path: path/to/my/file.ext
- run: exit 1
if: steps.diff.outputs.removed == true

Expand Down
44 changes: 39 additions & 5 deletions action.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const fs = require("fs");
const yaml = require("js-yaml");
const core = require("@actions/core");
const minimatch = require("minimatch");
const github = require("@actions/github");

const unsupportedEvent = name => name !== "pull_request" && name !== "push" ? true : false;
Expand All @@ -7,9 +10,40 @@ const getHead = name => data => name === "pull_request" ? data.pull_request.head
const normalise = path => path.split("/").filter(item => item !== "" && item !== ".").join("/");
const toBoolean = value => value.toLowerCase() == "true";

const setFromPath = owner => repo => async octokit => {
try {
const input_path = core.getInput("path");
if (input_path) return normalise(input_path);

console.warn("No path provided. Attempting to discern a path from the workflow file.");

const event_name = github.context.eventName;
const workflows = await octokit.request("GET /repos/:owner/:repo/actions/workflows", { owner, repo });
const workflow = workflows.data.workflows.find(workflow => workflow.name === process.env.GITHUB_WORKFLOW);
const file = await fs.promises.readFile(workflow.path);
const data = yaml.safeLoad(file);
const path = data.on[event_name] && data.on[event_name].paths ? data.on[event_name].paths[0] : undefined;

if (!path) throw new Error(`No path specified within the workflow file for this (${event_name}) event.`);

console.log(`Using the specified path (${path}), for this ${event_name} event`);
return normalise(path);
} catch(error) {
console.error(error.message);
return undefined;
}
}

const contentsUrlDoesMatch = file => target => {
const contents_url = decodeURIComponent(file.contents_url);
const start = contents_url.indexOf("contents/");
const end = contents_url.indexOf("?ref=");
const contents_path = contents_url.substring(start, end);
return minimatch(contents_path, `contents/${target}`);
};

(async function(){
try {
const path = core.getInput("path");
const token = core.getInput("token");
const repo = github.context.repo.repo;
const owner = github.context.repo.owner;
Expand All @@ -26,9 +60,9 @@ const toBoolean = value => value.toLowerCase() == "true";
if (response.status !== 200) throw `The API request for this ${github.context.eventName} event returned ${response.status}, expected 200.`;
if (response.data.status !== "ahead") throw `The head commit for this ${github.context.eventName} event is not ahead of the base commit.`;

const target = normalise(path);
const files = response.data.files;
const file = files.find(file => decodeURIComponent(file.contents_url).indexOf(`contents/${target}`) !== -1);
const target = await setFromPath(owner)(repo)(octokit);
const file = files.find(file => contentsUrlDoesMatch(file)(target));

core.setOutput("name", file ? file.filename : target);
core.setOutput("added", file ? file.status === "added" : false);
Expand All @@ -38,8 +72,8 @@ const toBoolean = value => value.toLowerCase() == "true";
core.setOutput("previous", file ? file.previous_filename || file.filename : target);

if (file) return;
if (strict) throw `None of the files in this commits diff tree match the provided file (${path}).`;
console.log(`None of the files in this commits diff tree match the provided file (${path}).`);
if (strict) throw `None of the files in this commits diff tree match the provided file (${target}).`;
console.log(`None of the files in this commits diff tree match the provided file (${target}).`);

} catch (error) {
core.setFailed(error);
Expand Down
1 change: 1 addition & 0 deletions node_modules/.bin/esparse

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/esvalidate

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/js-yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node_modules/@actions/core/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 185 additions & 0 deletions node_modules/argparse/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/argparse/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading