feat: add dir_names_exclude_current_dir input and cleaned up logic to retrieve the current sha (#1229)

Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: tj-actions[bot] <109116665+tj-actions-bot@users.noreply.github.com>
Co-authored-by: jackton1 <jackton1@users.noreply.github.com>
This commit is contained in:
Tonye Jack 2023-06-06 06:00:56 -06:00 committed by GitHub
parent cdf9afcec8
commit 353ea22e6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 25 deletions

View File

@ -79,15 +79,20 @@ inputs:
default: "true" default: "true"
dir_names: dir_names:
default: "false" default: "false"
description: "Output unique changed directories instead of filenames. **NOTE:** This returns `.` for changed files located in the root of the project." description: "Output unique changed directories instead of filenames. **NOTE:** This returns `.` for changed files located in the current working directory which defaults to `$GITHUB_WORKSPACE`."
required: false required: false
dir_names_max_depth: dir_names_max_depth:
description: "Limit the directory output to a maximum depth e.g `test/test1/test2` with max depth of `2` returns `test/test1`." description: "Limit the directory output to a maximum depth e.g `test/test1/test2` with max depth of `2` returns `test/test1`."
required: false required: false
dir_names_exclude_current_dir:
description: "Exclude the current directory represented by `.` from the output when `dir_names` is set to `true`."
required: false
default: "false"
dir_names_exclude_root: dir_names_exclude_root:
description: "Exclude the root directory represented by `.` from the output when `dir_names`is set to `true`." description: "Exclude the root directory represented by `.` from the output when `dir_names`is set to `true`."
required: false required: false
default: "false" default: "false"
deprecationMessage: "This input is deprecated. Use `dir_names_exclude_current_dir` instead."
json: json:
description: "Output list of changed files in a JSON formatted string which can be used for matrix jobs." description: "Output list of changed files in a JSON formatted string which can be used for matrix jobs."
required: false required: false

33
dist/index.js generated vendored
View File

@ -118,7 +118,7 @@ const getDiffFiles = ({ inputs, workingDirectory, hasSubmodule, diffResult, diff
files = files.map(file => (0, utils_1.getDirnameMaxDepth)({ files = files.map(file => (0, utils_1.getDirnameMaxDepth)({
pathStr: file, pathStr: file,
dirNamesMaxDepth: inputs.dirNamesMaxDepth, dirNamesMaxDepth: inputs.dirNamesMaxDepth,
excludeRoot: inputs.dirNamesExcludeRoot excludeCurrentDir: inputs.dirNamesExcludeRoot || inputs.dirNamesExcludeCurrentDir
})); }));
files = [...new Set(files)]; files = [...new Set(files)];
} }
@ -173,7 +173,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getSHAForPullRequestEvent = exports.getSHAForPushEvent = void 0; exports.getSHAForPullRequestEvent = exports.getSHAForPushEvent = void 0;
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const utils_1 = __nccwpck_require__(918); const utils_1 = __nccwpck_require__(918);
const getCurrentSHA = ({ inputs, workingDirectory }) => __awaiter(void 0, void 0, void 0, function* () { const getCurrentSHA = ({ env, inputs, workingDirectory }) => __awaiter(void 0, void 0, void 0, function* () {
let currentSha = inputs.sha; let currentSha = inputs.sha;
core.debug('Getting current SHA...'); core.debug('Getting current SHA...');
if (inputs.until) { if (inputs.until) {
@ -199,7 +199,17 @@ const getCurrentSHA = ({ inputs, workingDirectory }) => __awaiter(void 0, void 0
} }
else { else {
if (!currentSha) { if (!currentSha) {
currentSha = yield (0, utils_1.getHeadSha)({ cwd: workingDirectory }); if (env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA &&
(yield (0, utils_1.verifyCommitSha)({
sha: env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA,
cwd: workingDirectory,
showAsErrorMessage: false
})) === 0) {
currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA;
}
else {
currentSha = yield (0, utils_1.getHeadSha)({ cwd: workingDirectory });
}
} }
} }
yield (0, utils_1.verifyCommitSha)({ sha: currentSha, cwd: workingDirectory }); yield (0, utils_1.verifyCommitSha)({ sha: currentSha, cwd: workingDirectory });
@ -252,7 +262,7 @@ const getSHAForPushEvent = (inputs, env, workingDirectory, isShallow, hasSubmodu
}); });
} }
} }
const currentSha = yield getCurrentSHA({ inputs, workingDirectory }); const currentSha = yield getCurrentSHA({ env, inputs, workingDirectory });
let previousSha = inputs.baseSha; let previousSha = inputs.baseSha;
const diff = '..'; const diff = '..';
if (previousSha && currentSha && currentBranch && targetBranch) { if (previousSha && currentSha && currentBranch && targetBranch) {
@ -409,7 +419,7 @@ const getSHAForPullRequestEvent = (inputs, env, workingDirectory, isShallow, has
} }
core.info('Completed fetching more history.'); core.info('Completed fetching more history.');
} }
let currentSha = yield getCurrentSHA({ inputs, workingDirectory }); const currentSha = yield getCurrentSHA({ env, inputs, workingDirectory });
let previousSha = inputs.baseSha; let previousSha = inputs.baseSha;
let diff = '...'; let diff = '...';
if (previousSha && currentSha && currentBranch && targetBranch) { if (previousSha && currentSha && currentBranch && targetBranch) {
@ -418,7 +428,7 @@ const getSHAForPullRequestEvent = (inputs, env, workingDirectory, isShallow, has
core.error(`Please verify that both commits are valid, and increase the fetch_depth to a number higher than ${inputs.fetchDepth}.`); core.error(`Please verify that both commits are valid, and increase the fetch_depth to a number higher than ${inputs.fetchDepth}.`);
throw new Error('Similar commit hashes detected.'); throw new Error('Similar commit hashes detected.');
} }
yield (0, utils_1.verifyCommitSha)({ sha: currentSha, cwd: workingDirectory }); yield (0, utils_1.verifyCommitSha)({ sha: previousSha, cwd: workingDirectory });
core.debug(`Previous SHA: ${previousSha}`); core.debug(`Previous SHA: ${previousSha}`);
return { return {
previousSha, previousSha,
@ -485,9 +495,6 @@ const getSHAForPullRequestEvent = (inputs, env, workingDirectory, isShallow, has
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA; previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA;
} }
} }
if (previousSha === currentSha && env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA) {
currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA;
}
if (!(yield (0, utils_1.canDiffCommits)({ if (!(yield (0, utils_1.canDiffCommits)({
cwd: workingDirectory, cwd: workingDirectory,
sha1: previousSha, sha1: previousSha,
@ -676,6 +683,9 @@ const getInputs = () => {
const dirNamesExcludeRoot = core.getBooleanInput('dir_names_exclude_root', { const dirNamesExcludeRoot = core.getBooleanInput('dir_names_exclude_root', {
required: false required: false
}); });
const dirNamesExcludeCurrentDir = core.getBooleanInput('dir_names_exclude_current_dir', {
required: false
});
const json = core.getBooleanInput('json', { required: false }); const json = core.getBooleanInput('json', { required: false });
const escapeJson = core.getBooleanInput('escape_json', { required: false }); const escapeJson = core.getBooleanInput('escape_json', { required: false });
const fetchDepth = core.getInput('fetch_depth', { required: false }); const fetchDepth = core.getInput('fetch_depth', { required: false });
@ -706,6 +716,7 @@ const getInputs = () => {
diffRelative, diffRelative,
dirNames, dirNames,
dirNamesExcludeRoot, dirNamesExcludeRoot,
dirNamesExcludeCurrentDir,
json, json,
escapeJson, escapeJson,
sinceLastRemoteCommit, sinceLastRemoteCommit,
@ -1579,14 +1590,14 @@ const canDiffCommits = ({ cwd, sha1, sha2, diff }) => __awaiter(void 0, void 0,
return true; return true;
}); });
exports.canDiffCommits = canDiffCommits; exports.canDiffCommits = canDiffCommits;
const getDirnameMaxDepth = ({ pathStr, dirNamesMaxDepth, excludeRoot }) => { const getDirnameMaxDepth = ({ pathStr, dirNamesMaxDepth, excludeCurrentDir }) => {
const pathArr = dirname(pathStr).split(path.sep); const pathArr = dirname(pathStr).split(path.sep);
const maxDepth = Math.min(dirNamesMaxDepth || pathArr.length, pathArr.length); const maxDepth = Math.min(dirNamesMaxDepth || pathArr.length, pathArr.length);
let output = pathArr[0]; let output = pathArr[0];
for (let i = 1; i < maxDepth; i++) { for (let i = 1; i < maxDepth; i++) {
output = path.join(output, pathArr[i]); output = path.join(output, pathArr[i]);
} }
if (excludeRoot && output === '.') { if (excludeCurrentDir && output === '.') {
return ''; return '';
} }
return normalizePath(output); return normalizePath(output);

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -130,7 +130,8 @@ export const getDiffFiles = async ({
getDirnameMaxDepth({ getDirnameMaxDepth({
pathStr: file, pathStr: file,
dirNamesMaxDepth: inputs.dirNamesMaxDepth, dirNamesMaxDepth: inputs.dirNamesMaxDepth,
excludeRoot: inputs.dirNamesExcludeRoot excludeCurrentDir:
inputs.dirNamesExcludeRoot || inputs.dirNamesExcludeCurrentDir
}) })
) )
files = [...new Set(files)] files = [...new Set(files)]

View File

@ -15,9 +15,11 @@ import {
} from './utils' } from './utils'
const getCurrentSHA = async ({ const getCurrentSHA = async ({
env,
inputs, inputs,
workingDirectory workingDirectory
}: { }: {
env: Env
inputs: Inputs inputs: Inputs
workingDirectory: string workingDirectory: string
}): Promise<string> => { }): Promise<string> => {
@ -47,7 +49,18 @@ const getCurrentSHA = async ({
} }
} else { } else {
if (!currentSha) { if (!currentSha) {
currentSha = await getHeadSha({cwd: workingDirectory}) if (
env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA &&
(await verifyCommitSha({
sha: env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA,
cwd: workingDirectory,
showAsErrorMessage: false
})) === 0
) {
currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA
} else {
currentSha = await getHeadSha({cwd: workingDirectory})
}
} }
} }
@ -124,7 +137,7 @@ export const getSHAForPushEvent = async (
} }
} }
const currentSha = await getCurrentSHA({inputs, workingDirectory}) const currentSha = await getCurrentSHA({env, inputs, workingDirectory})
let previousSha = inputs.baseSha let previousSha = inputs.baseSha
const diff = '..' const diff = '..'
@ -321,7 +334,7 @@ export const getSHAForPullRequestEvent = async (
core.info('Completed fetching more history.') core.info('Completed fetching more history.')
} }
let currentSha = await getCurrentSHA({inputs, workingDirectory}) const currentSha = await getCurrentSHA({env, inputs, workingDirectory})
let previousSha = inputs.baseSha let previousSha = inputs.baseSha
let diff = '...' let diff = '...'
@ -336,7 +349,7 @@ export const getSHAForPullRequestEvent = async (
throw new Error('Similar commit hashes detected.') throw new Error('Similar commit hashes detected.')
} }
await verifyCommitSha({sha: currentSha, cwd: workingDirectory}) await verifyCommitSha({sha: previousSha, cwd: workingDirectory})
core.debug(`Previous SHA: ${previousSha}`) core.debug(`Previous SHA: ${previousSha}`)
return { return {
@ -425,10 +438,6 @@ export const getSHAForPullRequestEvent = async (
} }
} }
if (previousSha === currentSha && env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA) {
currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA
}
if ( if (
!(await canDiffCommits({ !(await canDiffCommits({
cwd: workingDirectory, cwd: workingDirectory,

View File

@ -23,6 +23,7 @@ export type Inputs = {
dirNames: boolean dirNames: boolean
dirNamesMaxDepth?: number dirNamesMaxDepth?: number
dirNamesExcludeRoot: boolean dirNamesExcludeRoot: boolean
dirNamesExcludeCurrentDir: boolean
json: boolean json: boolean
escapeJson: boolean escapeJson: boolean
fetchDepth?: number fetchDepth?: number
@ -93,6 +94,12 @@ export const getInputs = (): Inputs => {
const dirNamesExcludeRoot = core.getBooleanInput('dir_names_exclude_root', { const dirNamesExcludeRoot = core.getBooleanInput('dir_names_exclude_root', {
required: false required: false
}) })
const dirNamesExcludeCurrentDir = core.getBooleanInput(
'dir_names_exclude_current_dir',
{
required: false
}
)
const json = core.getBooleanInput('json', {required: false}) const json = core.getBooleanInput('json', {required: false})
const escapeJson = core.getBooleanInput('escape_json', {required: false}) const escapeJson = core.getBooleanInput('escape_json', {required: false})
const fetchDepth = core.getInput('fetch_depth', {required: false}) const fetchDepth = core.getInput('fetch_depth', {required: false})
@ -127,6 +134,7 @@ export const getInputs = (): Inputs => {
diffRelative, diffRelative,
dirNames, dirNames,
dirNamesExcludeRoot, dirNamesExcludeRoot,
dirNamesExcludeCurrentDir,
json, json,
escapeJson, escapeJson,
sinceLastRemoteCommit, sinceLastRemoteCommit,

View File

@ -642,11 +642,11 @@ export const canDiffCommits = async ({
export const getDirnameMaxDepth = ({ export const getDirnameMaxDepth = ({
pathStr, pathStr,
dirNamesMaxDepth, dirNamesMaxDepth,
excludeRoot excludeCurrentDir
}: { }: {
pathStr: string pathStr: string
dirNamesMaxDepth?: number dirNamesMaxDepth?: number
excludeRoot?: boolean excludeCurrentDir?: boolean
}): string => { }): string => {
const pathArr = dirname(pathStr).split(path.sep) const pathArr = dirname(pathStr).split(path.sep)
const maxDepth = Math.min(dirNamesMaxDepth || pathArr.length, pathArr.length) const maxDepth = Math.min(dirNamesMaxDepth || pathArr.length, pathArr.length)
@ -656,7 +656,7 @@ export const getDirnameMaxDepth = ({
output = path.join(output, pathArr[i]) output = path.join(output, pathArr[i])
} }
if (excludeRoot && output === '.') { if (excludeCurrentDir && output === '.') {
return '' return ''
} }