From e712d3dbf03963c97bd024d10cb937968cbb3b89 Mon Sep 17 00:00:00 2001 From: Keith Thompson Date: Thu, 6 May 2021 20:37:44 +0100 Subject: [PATCH] add build all feature --- dist/index.js | 53 +++++++++++++++++++++++++++++++++++++-------------- index.js | 53 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 28 deletions(-) diff --git a/dist/index.js b/dist/index.js index a91707d..622093e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7,6 +7,7 @@ module.exports = const core = __webpack_require__(127); const github = __webpack_require__(134); +const readdirSync = __webpack_require__(747) try { const context = github.context.payload; @@ -26,21 +27,36 @@ try { if (targetDirectory && depth == 1) { depth = 2; } + var labels; + if (github.context.eventName === 'pull_request') { + labels = context['pull_request']['labels']; + } else { + labels = []; + } - octokit.repos.compareCommits({owner, repo, base, head}) - .then(data => { - const changedDirectories = data['data']['files'] - .map(file => file['filename']) - .filter(file => reduceFile(file, targetDirectory)) - .map(file => parseDirectories(file, depth, targetDirectory)) - .filter(directory => directory.length > 0); - const result = buildMatrix(changedDirectories) - core.setOutput("is_empty", result['include'].length < 1) - core.setOutput("build_matrix", JSON.stringify(result)); - }).catch(error => { - console.log(error); - core.setFailed(error.message); - }); + if (shouldBuildAll(labels)) { + const directories = targetDirectory => readdirSync(targetDirectory, { withFileTypes: true }) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name) + const result = buildMatrix(directories) + 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 => { + const changedDirectories = data['data']['files'] + .map(file => file['filename']) + .filter(file => reduceFile(file, targetDirectory)) + .map(file => parseDirectories(file, depth, targetDirectory)) + .filter(directory => directory.length > 0); + const result = buildMatrix(changedDirectories) + core.setOutput("is_empty", result['include'].length < 1) + core.setOutput("build_matrix", JSON.stringify(result)); + }).catch(error => { + console.log(error); + core.setFailed(error.message); + }); + } } catch (error) { console.log(error); @@ -79,6 +95,15 @@ function buildMatrix(changedDirectories) { return {'include': include}; } +function shouldBuildAll(labels) { + for (label of labels) { + if (label['name'] === "build-all") { + return true + } + } + return false +} + /***/ }), /***/ 604: diff --git a/index.js b/index.js index 27ba10a..c82c778 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const core = require('@actions/core'); const github = require('@actions/github'); +const readdirSync = require('fs') try { const context = github.context.payload; @@ -19,21 +20,36 @@ try { if (targetDirectory && depth == 1) { depth = 2; } + var labels; + if (github.context.eventName === 'pull_request') { + labels = context['pull_request']['labels']; + } else { + labels = []; + } - octokit.repos.compareCommits({owner, repo, base, head}) - .then(data => { - const changedDirectories = data['data']['files'] - .map(file => file['filename']) - .filter(file => reduceFile(file, targetDirectory)) - .map(file => parseDirectories(file, depth, targetDirectory)) - .filter(directory => directory.length > 0); - const result = buildMatrix(changedDirectories) - core.setOutput("is_empty", result['include'].length < 1) - core.setOutput("build_matrix", JSON.stringify(result)); - }).catch(error => { - console.log(error); - core.setFailed(error.message); - }); + if (shouldBuildAll(labels)) { + const directories = targetDirectory => readdirSync(targetDirectory, { withFileTypes: true }) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name) + const result = buildMatrix(directories) + 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 => { + const changedDirectories = data['data']['files'] + .map(file => file['filename']) + .filter(file => reduceFile(file, targetDirectory)) + .map(file => parseDirectories(file, depth, targetDirectory)) + .filter(directory => directory.length > 0); + const result = buildMatrix(changedDirectories) + core.setOutput("is_empty", result['include'].length < 1) + core.setOutput("build_matrix", JSON.stringify(result)); + }).catch(error => { + console.log(error); + core.setFailed(error.message); + }); + } } catch (error) { console.log(error); @@ -70,4 +86,13 @@ function buildMatrix(changedDirectories) { include.push({'directory': directory}); } 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