Skip to content

Commit e7ce37d

Browse files
authored
Merge pull request #8 from mudlabs/dev
[dev] Improve path
2 parents 99ce048 + fe9c003 commit e7ce37d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+23408
-12
lines changed
File renamed without changes.

Diff for: .github/workflows/test.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Test Diff
22

33
on:
44
push:
5+
paths:
6+
- '*.md'
57

68
jobs:
79
test:
@@ -14,8 +16,8 @@ jobs:
1416
id: diff
1517
uses: ./
1618
with:
17-
path: README.md
1819
strict: false
20+
path: 'READ*.md'
1921
- run: |
2022
echo "Added? ${{ steps.diff.outputs.added }}"
2123
echo "Modified? ${{ steps.diff.outputs.modified }}"

Diff for: README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ Very simple, you provide the action with a `path` to a _file_ or _folder_, and i
1919
## Usage
2020
```yaml
2121
- name: Simple Diff
22-
uses: mudlabs/simple-diff@v1.1.0
22+
uses: mudlabs/simple-diff@v1.2.0
2323
with:
2424
path: path/to/file
2525
```
2626
2727
## Inputs
2828
| Input | Description | Default |
2929
| --- | :--- | --- |
30-
| `path` | Specifies a path from _root_ to the file or folder you want to check. | |
30+
| `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. | |
3131
| `strict` | Specifies the action should fail if the `path` is not in the commits diff tree. | `true` |
3232

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

4343
## Example Case
4444
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)_.
45+
- _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)_.
4546

4647
```yaml
4748
name: My File Workflow
@@ -57,10 +58,8 @@ jobs:
5758
steps:
5859
- uses: actions/checkout@v2
5960
- name: Simple Diff
61+
uses: mudlabs/[email protected]
6062
id: diff
61-
uses: mudlabs/[email protected]
62-
with:
63-
path: path/to/my/file.ext
6463
- run: exit 1
6564
if: steps.diff.outputs.removed == true
6665

Diff for: action.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
const fs = require("fs");
2+
const yaml = require("js-yaml");
13
const core = require("@actions/core");
4+
const minimatch = require("minimatch");
25
const github = require("@actions/github");
36

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

13+
const setFromPath = owner => repo => async octokit => {
14+
try {
15+
const input_path = core.getInput("path");
16+
if (input_path) return normalise(input_path);
17+
18+
console.warn("No path provided. Attempting to discern a path from the workflow file.");
19+
20+
const event_name = github.context.eventName;
21+
const workflows = await octokit.request("GET /repos/:owner/:repo/actions/workflows", { owner, repo });
22+
const workflow = workflows.data.workflows.find(workflow => workflow.name === process.env.GITHUB_WORKFLOW);
23+
const file = await fs.promises.readFile(workflow.path);
24+
const data = yaml.safeLoad(file);
25+
const path = data.on[event_name] && data.on[event_name].paths ? data.on[event_name].paths[0] : undefined;
26+
27+
if (!path) throw new Error(`No path specified within the workflow file for this (${event_name}) event.`);
28+
29+
console.log(`Using the specified path (${path}), for this ${event_name} event`);
30+
return normalise(path);
31+
} catch(error) {
32+
console.error(error.message);
33+
return undefined;
34+
}
35+
}
36+
37+
const contentsUrlDoesMatch = file => target => {
38+
const contents_url = decodeURIComponent(file.contents_url);
39+
const start = contents_url.indexOf("contents/");
40+
const end = contents_url.indexOf("?ref=");
41+
const contents_path = contents_url.substring(start, end);
42+
return minimatch(contents_path, `contents/${target}`);
43+
};
44+
1045
(async function(){
1146
try {
12-
const path = core.getInput("path");
1347
const token = core.getInput("token");
1448
const repo = github.context.repo.repo;
1549
const owner = github.context.repo.owner;
@@ -26,9 +60,9 @@ const toBoolean = value => value.toLowerCase() == "true";
2660
if (response.status !== 200) throw `The API request for this ${github.context.eventName} event returned ${response.status}, expected 200.`;
2761
if (response.data.status !== "ahead") throw `The head commit for this ${github.context.eventName} event is not ahead of the base commit.`;
2862

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

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

4074
if (file) return;
41-
if (strict) throw `None of the files in this commits diff tree match the provided file (${path}).`;
42-
console.log(`None of the files in this commits diff tree match the provided file (${path}).`);
75+
if (strict) throw `None of the files in this commits diff tree match the provided file (${target}).`;
76+
console.log(`None of the files in this commits diff tree match the provided file (${target}).`);
4377

4478
} catch (error) {
4579
core.setFailed(error);

Diff for: node_modules/.bin/esparse

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: node_modules/.bin/esvalidate

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: node_modules/.bin/js-yaml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: node_modules/@actions/core/package.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: node_modules/argparse/CHANGELOG.md

+185
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: node_modules/argparse/LICENSE

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)