From 8cffc048a544595bed20a5c30d6d68c0491dc5a1 Mon Sep 17 00:00:00 2001 From: Keith Thompson Date: Thu, 6 May 2021 20:50:59 +0100 Subject: [PATCH] Fix --- .github/workflows/main.yml | 2 ++ action.yml | 4 ++++ dist/index.js | 32 +++++++++++--------------------- index.js | 32 +++++++++++--------------------- 4 files changed, 28 insertions(+), 42 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index aec082a..f377bed 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,6 +11,8 @@ jobs: uses: ./ id: generate_build_matrix with: + build-all: true + target-directory: .github repo-token: ${{ secrets.MY_GITHUB_TOKEN }} - name: Print the output run: | diff --git a/action.yml b/action.yml index 50b73e1..bf47f76 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,10 @@ inputs: target-directory: description: 'An optional target directory to listen to changes in' required: false + build-all: + description: 'Set to true to build all sub directories in the target directory' + required: false + default: false outputs: build_matrix: description: 'The files changed between the default branch and the sha being built' diff --git a/dist/index.js b/dist/index.js index 622093e..f83835e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7,7 +7,7 @@ module.exports = const core = __webpack_require__(127); const github = __webpack_require__(134); -const readdirSync = __webpack_require__(747) +const fs = __webpack_require__(747) try { const context = github.context.payload; @@ -23,24 +23,17 @@ try { const token = core.getInput('repo-token'); const octokit = github.getOctokit(token); const depth = core.getInput('depth'); + const buildAll = core.getInput('build-all'); const targetDirectory = core.getInput('target-directory') if (targetDirectory && depth == 1) { depth = 2; } - var labels; - if (github.context.eventName === 'pull_request') { - labels = context['pull_request']['labels']; - } else { - labels = []; - } - if (shouldBuildAll(labels)) { - const directories = targetDirectory => readdirSync(targetDirectory, { withFileTypes: true }) - .filter(dirent => dirent.isDirectory()) - .map(dirent => dirent.name) + if (buildAll) { + const directories = getDirectories(targetDirectory) const result = buildMatrix(directories) - core.setOutput("is_empty", result['include'].length < 1) - core.setOutput("build_matrix", JSON.stringify(result)); + core.setOutput("is_empty", result['include'].length < 1) + core.setOutput("build_matrix", JSON.stringify(result)); } else { octokit.repos.compareCommits({owner, repo, base, head}) .then(data => { @@ -95,14 +88,11 @@ function buildMatrix(changedDirectories) { return {'include': include}; } -function shouldBuildAll(labels) { - for (label of labels) { - if (label['name'] === "build-all") { - return true - } - } - return false -} +function getDirectories(path) { + const isDirectory = fs.lstatSync(path).isDirectory() + return fs.readdirSync(path, { withFileTypes: true}) + .filter(directory => fs.lstatSync(path + "/" + directory.name).isDirectory()) + } /***/ }), diff --git a/index.js b/index.js index c82c778..0cf3ae8 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const core = require('@actions/core'); const github = require('@actions/github'); -const readdirSync = require('fs') +const fs = require('fs') try { const context = github.context.payload; @@ -16,24 +16,17 @@ try { const token = core.getInput('repo-token'); const octokit = github.getOctokit(token); const depth = core.getInput('depth'); + const buildAll = core.getInput('build-all'); const targetDirectory = core.getInput('target-directory') if (targetDirectory && depth == 1) { depth = 2; } - var labels; - if (github.context.eventName === 'pull_request') { - labels = context['pull_request']['labels']; - } else { - labels = []; - } - if (shouldBuildAll(labels)) { - const directories = targetDirectory => readdirSync(targetDirectory, { withFileTypes: true }) - .filter(dirent => dirent.isDirectory()) - .map(dirent => dirent.name) + if (buildAll) { + const directories = getDirectories(targetDirectory) const result = buildMatrix(directories) - core.setOutput("is_empty", result['include'].length < 1) - core.setOutput("build_matrix", JSON.stringify(result)); + core.setOutput("is_empty", result['include'].length < 1) + core.setOutput("build_matrix", JSON.stringify(result)); } else { octokit.repos.compareCommits({owner, repo, base, head}) .then(data => { @@ -88,11 +81,8 @@ function buildMatrix(changedDirectories) { return {'include': include}; } -function shouldBuildAll(labels) { - for (label of labels) { - if (label['name'] === "build-all") { - return true - } - } - return false -} \ No newline at end of file +function getDirectories(path) { + const isDirectory = fs.lstatSync(path).isDirectory() + return fs.readdirSync(path, { withFileTypes: true}) + .filter(directory => fs.lstatSync(path + "/" + directory.name).isDirectory()) + } \ No newline at end of file