Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -207,7 +207,7 @@ Support this project with a :star:
| diff\_relative | string | false | `"true"` | Exclude changes outside the current <br>directory and show path names <br>relative to it. **NOTE:** This <br>requires you to specify the <br>top level directory via the <br>`path` input. |
| dir\_names | string | false | `"false"` | Output unique changed directories instead <br>of filenames. **NOTE:** This returns <br>`.` for changed files located <br>in the root of the <br>project. |
| dir\_names\_exclude\_root | string | false | `"false"` | Exclude the root directory represented <br>by `.` from the output <br>when `dir_names`is set to `true`. |
| dir\_names\_max\_depth | string | false | | Maximum depth of directories to <br>output. e.g `test/test1/test2` with max <br>depth of `2` returns `test/test1`. |
| dir\_names\_max\_depth | string | false | | Limit the directory output to <br>a maximum depth e.g `test/test1/test2` <br>with max depth of `2` <br>returns `test/test1`. |
| escape\_json | string | false | `"true"` | Escape JSON output. |
| fetch\_depth | string | false | `"50"` | Depth of additional branch history <br>fetched. **NOTE**: This can be <br>adjusted to resolve errors with <br>insufficient history. |
| files | string | false | | File and directory patterns to <br>detect changes using only these <br>list of file(s) (Defaults to the entire repo) **NOTE:** <br>Multiline file/directory patterns should not <br>include quotes. |
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Expand Up @@ -79,15 +79,20 @@ inputs:
default: "true"
dir_names:
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
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`."
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:
description: "Exclude the root directory represented by `.` from the output when `dir_names`is set to `true`."
required: false
default: "false"
deprecationMessage: "This input is deprecated. Use `dir_names_exclude_current_dir` instead."
json:
description: "Output list of changed files in a JSON formatted string which can be used for matrix jobs."
required: false
Expand Down
33 changes: 22 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/changedFiles.ts
Expand Up @@ -130,7 +130,8 @@ export const getDiffFiles = async ({
getDirnameMaxDepth({
pathStr: file,
dirNamesMaxDepth: inputs.dirNamesMaxDepth,
excludeRoot: inputs.dirNamesExcludeRoot
excludeCurrentDir:
inputs.dirNamesExcludeRoot || inputs.dirNamesExcludeCurrentDir
})
)
files = [...new Set(files)]
Expand Down
25 changes: 17 additions & 8 deletions src/commitSha.ts
Expand Up @@ -15,9 +15,11 @@ import {
} from './utils'

const getCurrentSHA = async ({
env,
inputs,
workingDirectory
}: {
env: Env
inputs: Inputs
workingDirectory: string
}): Promise<string> => {
Expand Down Expand Up @@ -47,7 +49,18 @@ const getCurrentSHA = async ({
}
} else {
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})
}
}
}

Expand Down Expand Up @@ -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
const diff = '..'

Expand Down Expand Up @@ -321,7 +334,7 @@ export const getSHAForPullRequestEvent = async (
core.info('Completed fetching more history.')
}

let currentSha = await getCurrentSHA({inputs, workingDirectory})
const currentSha = await getCurrentSHA({env, inputs, workingDirectory})
let previousSha = inputs.baseSha
let diff = '...'

Expand All @@ -336,7 +349,7 @@ export const getSHAForPullRequestEvent = async (
throw new Error('Similar commit hashes detected.')
}

await verifyCommitSha({sha: currentSha, cwd: workingDirectory})
await verifyCommitSha({sha: previousSha, cwd: workingDirectory})
core.debug(`Previous SHA: ${previousSha}`)

return {
Expand Down Expand Up @@ -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 (
!(await canDiffCommits({
cwd: workingDirectory,
Expand Down
8 changes: 8 additions & 0 deletions src/inputs.ts
Expand Up @@ -23,6 +23,7 @@ export type Inputs = {
dirNames: boolean
dirNamesMaxDepth?: number
dirNamesExcludeRoot: boolean
dirNamesExcludeCurrentDir: boolean
json: boolean
escapeJson: boolean
fetchDepth?: number
Expand Down Expand Up @@ -93,6 +94,12 @@ export const getInputs = (): Inputs => {
const dirNamesExcludeRoot = core.getBooleanInput('dir_names_exclude_root', {
required: false
})
const dirNamesExcludeCurrentDir = core.getBooleanInput(
'dir_names_exclude_current_dir',
{
required: false
}
)
const json = core.getBooleanInput('json', {required: false})
const escapeJson = core.getBooleanInput('escape_json', {required: false})
const fetchDepth = core.getInput('fetch_depth', {required: false})
Expand Down Expand Up @@ -127,6 +134,7 @@ export const getInputs = (): Inputs => {
diffRelative,
dirNames,
dirNamesExcludeRoot,
dirNamesExcludeCurrentDir,
json,
escapeJson,
sinceLastRemoteCommit,
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Expand Up @@ -642,11 +642,11 @@ export const canDiffCommits = async ({
export const getDirnameMaxDepth = ({
pathStr,
dirNamesMaxDepth,
excludeRoot
excludeCurrentDir
}: {
pathStr: string
dirNamesMaxDepth?: number
excludeRoot?: boolean
excludeCurrentDir?: boolean
}): string => {
const pathArr = dirname(pathStr).split(path.sep)
const maxDepth = Math.min(dirNamesMaxDepth || pathArr.length, pathArr.length)
Expand All @@ -656,7 +656,7 @@ export const getDirnameMaxDepth = ({
output = path.join(output, pathArr[i])
}

if (excludeRoot && output === '.') {
if (excludeCurrentDir && output === '.') {
return ''
}

Expand Down