|
1 | 1 | const core = require('@actions/core');
|
2 | 2 | const github = require('@actions/github');
|
3 | 3 |
|
4 |
| -const main = async () => { |
5 |
| - try { |
6 |
| - const path = core.getInput('path', { required: true }); |
7 |
| - const token = core.getInput('token', { required: true }); |
8 |
| - const regExp = RegExp(path) |
9 |
| - const octokit = new github.getOctokit(token); |
10 |
| - |
11 |
| - const response = await octokit.rest.repos.compareCommits({ |
12 |
| - owner: github.context.repo.owner, |
13 |
| - repo: github.context.repo.repo, |
14 |
| - base: "HEAD^", |
15 |
| - head: "HEAD" |
16 |
| - }) |
17 |
| - |
18 |
| - const filteredFiles = (response.data.files || []).filter(file => { |
19 |
| - let isMatch = regExp.test(file.filename) |
20 |
| - console.log(`[${isMatch && '[** match **]'} ${file.filename}`) |
21 |
| - console.log(`##Path: ${file.filename} matches`) |
22 |
| - return isMatch |
| 4 | +async function setOutputs(files) { |
| 5 | + const pathsChanged = [] |
| 6 | + const filesChanged = [] |
| 7 | + files.forEach(file => { |
| 8 | + pathsChanged.push(file.filename.split("/").slice(0, -1).join("/")) |
| 9 | + filesChanged.push(file.filename) |
23 | 10 | })
|
| 11 | + core.setOutput("paths_changed", JSON.stringify(pathsChanged)) |
| 12 | + core.setOutput("file_changed", JSON.stringify(filesChanged)) |
| 13 | +} |
24 | 14 |
|
25 |
| - if(filteredFiles.length === 0){ |
26 |
| - console.log("No matchs found.") |
27 |
| - console.log(`Raw input: ${directory}`) |
28 |
| - console.log(`Regex: ${regExp.toString()}`) |
29 |
| - core.setOutput("hasChanges", false) |
| 15 | +const main = async () => { |
| 16 | + try { |
| 17 | + const path = core.getInput('path', {required: false}) || "./" |
| 18 | + const token = core.getInput('token', {required: true}) |
| 19 | + const pr = github.context.payload.pull_request |
| 20 | + const regExp = RegExp(path) |
| 21 | + const octokit = new github.getOctokit(token); |
| 22 | + |
| 23 | + const response = await octokit.rest.pulls.listFiles({ |
| 24 | + owner: github.context.repo.owner, |
| 25 | + repo: github.context.repo.repo, |
| 26 | + pull_number: pr.number |
| 27 | + }) |
| 28 | + |
| 29 | + const filteredFiles = (response.data || []).filter(file => { |
| 30 | + let isMatch = regExp.test(file.filename) |
| 31 | + console.log(`- ${isMatch} [** match **] ${file.filename}`) |
| 32 | + return isMatch |
| 33 | + }) |
| 34 | + |
| 35 | + if (filteredFiles.length === 0) { |
| 36 | + console.log("No matchs found.") |
| 37 | + console.log(`Raw input: ${directory}`) |
| 38 | + console.log(`Regex: ${regExp.toString()}`) |
| 39 | + } |
| 40 | + await setOutputs(filteredFiles); |
| 41 | + |
| 42 | + } catch (error) { |
| 43 | + core.setFailed(error.message); |
30 | 44 | }
|
31 |
| - |
32 |
| - console.log(`Found a total of ${filteredFiles.length} matches`) |
33 |
| - |
34 |
| - core.setOutput("hasChanges", true) |
35 |
| - |
36 |
| - } catch (error) { |
37 |
| - core.setFailed(error.message); |
38 |
| - } |
39 | 45 | }
|
40 | 46 |
|
41 | 47 | main();
|
0 commit comments