mirror of
https://github.com/dorny/paths-filter.git
synced 2025-06-08 00:59:04 +00:00
Support workflows triggered by any event (#44)
* Allow change detection on all event types * mention commit SHA in docs for base parameter * improve logging * update CHANGELOG.md
This commit is contained in:
parent
9bd03c0d68
commit
75cbfb4be9
6 changed files with 71 additions and 43 deletions
32
src/main.ts
32
src/main.ts
|
@ -62,19 +62,19 @@ async function getChangedFiles(token: string, base: string, initialFetchDepth: n
|
|||
}
|
||||
core.info('Github token is not available - changes will be detected from PRs merge commit')
|
||||
return await git.getChangesInLastCommit()
|
||||
} else if (github.context.eventName === 'push') {
|
||||
return getChangedFilesFromPush(base, initialFetchDepth)
|
||||
} else {
|
||||
throw new Error('This action can be triggered only by pull_request, pull_request_target or push event')
|
||||
return getChangedFilesFromGit(base, initialFetchDepth)
|
||||
}
|
||||
}
|
||||
|
||||
async function getChangedFilesFromPush(base: string, initialFetchDepth: number): Promise<File[]> {
|
||||
const push = github.context.payload as Webhooks.WebhookPayloadPush
|
||||
const defaultRef = push.repository?.default_branch
|
||||
async function getChangedFilesFromGit(base: string, initialFetchDepth: number): Promise<File[]> {
|
||||
const defaultRef = github.context.payload.repository?.default_branch
|
||||
|
||||
const beforeSha =
|
||||
github.context.eventName === 'push' ? (github.context.payload as Webhooks.WebhookPayloadPush).before : null
|
||||
|
||||
const pushRef =
|
||||
git.getShortName(push.ref) ||
|
||||
git.getShortName(github.context.ref) ||
|
||||
(core.warning(`'ref' field is missing in PUSH event payload - using current branch, tag or commit SHA`),
|
||||
await git.getCurrentRef())
|
||||
|
||||
|
@ -85,17 +85,21 @@ async function getChangedFilesFromPush(base: string, initialFetchDepth: number):
|
|||
)
|
||||
}
|
||||
|
||||
// If base references same branch it was pushed to,
|
||||
// we will do comparison against the previously pushed commit
|
||||
if (baseRef === pushRef) {
|
||||
if (!push.before) {
|
||||
const isBaseRefSha = git.isGitSha(baseRef)
|
||||
const isBaseSameAsPush = baseRef === pushRef
|
||||
|
||||
// If base is commit SHA will do comparison against the referenced commit
|
||||
// Or If base references same branch it was pushed to, we will do comparison against the previously pushed commit
|
||||
if (isBaseRefSha || isBaseSameAsPush) {
|
||||
if (!isBaseRefSha && !beforeSha) {
|
||||
core.warning(`'before' field is missing in PUSH event payload - changes will be detected from last commit`)
|
||||
return await git.getChangesInLastCommit()
|
||||
}
|
||||
|
||||
const baseSha = isBaseRefSha ? baseRef : beforeSha
|
||||
// If there is no previously pushed commit,
|
||||
// we will do comparison against the default branch or return all as added
|
||||
if (push.before === git.NULL_SHA) {
|
||||
if (baseSha === git.NULL_SHA) {
|
||||
if (defaultRef && baseRef !== defaultRef) {
|
||||
core.info(`First push of a branch detected - changes will be detected against the default branch ${defaultRef}`)
|
||||
return await git.getChangesSinceMergeBase(defaultRef, initialFetchDepth)
|
||||
|
@ -105,8 +109,8 @@ async function getChangedFilesFromPush(base: string, initialFetchDepth: number):
|
|||
}
|
||||
}
|
||||
|
||||
core.info(`Changes will be detected against the last previously pushed commit on same branch (${pushRef})`)
|
||||
return await git.getChanges(push.before)
|
||||
core.info(`Changes will be detected against commit (${baseSha})`)
|
||||
return await git.getChanges(baseSha)
|
||||
}
|
||||
|
||||
// Changes introduced by current branch against the base branch
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue