Skip to content

Commit

Permalink
feat: add support for skipping initial fetch (#1353)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
jackton1 and actions-user committed Jul 9, 2023
1 parent 00b3d3d commit 2f49eb9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 59 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ inputs:
description: "Depth of additional branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history."
required: false
default: "50"
skip_initial_fetch:
description: "Skip the initial fetch to improve performance."
required: false
default: "false"
since_last_remote_commit:
description: "Use the last commit on the remote branch as the `base_sha`. Defaults to the last non-merge commit on the target branch for pull request events and the previous remote commit of the current branch for push events."
required: false
Expand Down
62 changes: 35 additions & 27 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.

4 changes: 2 additions & 2 deletions src/commitSha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const getSHAForPushEvent = async (
const currentBranch = targetBranch
let initialCommit = false

if (isShallow) {
if (isShallow && !inputs.skipInitialFetch) {
core.info('Repository is shallow, fetching more history...')

if (isTag) {
Expand Down Expand Up @@ -271,7 +271,7 @@ export const getSHAForPullRequestEvent = async (
targetBranch = currentBranch
}

if (isShallow) {
if (isShallow && !inputs.skipInitialFetch) {
core.info('Repository is shallow, fetching more history...')

let prFetchExitCode = await gitFetch({
Expand Down
7 changes: 6 additions & 1 deletion src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type Inputs = {
recoverDeletedFilesToDestination: string
token: string
apiUrl: string
skipInitialFetch: boolean
}

export const getInputs = (): Inputs => {
Expand Down Expand Up @@ -154,6 +155,9 @@ export const getInputs = (): Inputs => {
)
const token = core.getInput('token', {required: false})
const apiUrl = core.getInput('api_url', {required: false})
const skipInitialFetch = core.getBooleanInput('skip_initial_fetch', {
required: false
})

const inputs: Inputs = {
files,
Expand Down Expand Up @@ -194,7 +198,8 @@ export const getInputs = (): Inputs => {
outputDir,
outputRenamedFilesAsDeletedAndAdded,
token,
apiUrl
apiUrl,
skipInitialFetch
}

if (fetchDepth) {
Expand Down
69 changes: 41 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ import {
const getChangedFilesFromLocalGit = async ({
inputs,
env,
workingDirectory
workingDirectory,
filePatterns,
yamlFilePatterns
}: {
inputs: Inputs
env: Env
workingDirectory: string
filePatterns: string[]
yamlFilePatterns: Record<string, string[]>
}): Promise<void> => {
await verifyMinimumGitVersion()

Expand Down Expand Up @@ -120,12 +124,6 @@ const getChangedFilesFromLocalGit = async ({
core.info('All Done!')
core.endGroup()

const filePatterns = await getFilePatterns({
inputs,
workingDirectory
})
core.debug(`File patterns: ${filePatterns}`)

if (filePatterns.length > 0) {
core.startGroup('changed-files-patterns')
await setChangedFilesOutput({
Expand All @@ -139,12 +137,6 @@ const getChangedFilesFromLocalGit = async ({
core.endGroup()
}

const yamlFilePatterns = await getYamlFilePatterns({
inputs,
workingDirectory
})
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`)

if (Object.keys(yamlFilePatterns).length > 0) {
for (const key of Object.keys(yamlFilePatterns)) {
core.startGroup(`changed-files-yaml-${key}`)
Expand Down Expand Up @@ -201,11 +193,15 @@ const getChangedFilesFromLocalGit = async ({
const getChangedFilesFromRESTAPI = async ({
inputs,
env,
workingDirectory
workingDirectory,
filePatterns,
yamlFilePatterns
}: {
inputs: Inputs
env: Env
workingDirectory: string
filePatterns: string[]
yamlFilePatterns: Record<string, string[]>
}): Promise<void> => {
const allDiffFiles = await getChangedFilesFromGithubAPI({
inputs,
Expand All @@ -214,12 +210,6 @@ const getChangedFilesFromRESTAPI = async ({
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
core.info('All Done!')

const filePatterns = await getFilePatterns({
inputs,
workingDirectory
})
core.debug(`File patterns: ${filePatterns}`)

if (filePatterns.length > 0) {
core.startGroup('changed-files-patterns')
await setChangedFilesOutput({
Expand All @@ -232,12 +222,6 @@ const getChangedFilesFromRESTAPI = async ({
core.endGroup()
}

const yamlFilePatterns = await getYamlFilePatterns({
inputs,
workingDirectory
})
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`)

if (Object.keys(yamlFilePatterns).length > 0) {
for (const key of Object.keys(yamlFilePatterns)) {
core.startGroup(`changed-files-yaml-${key}`)
Expand Down Expand Up @@ -270,13 +254,30 @@ export async function run(): Promise<void> {

const env = await getEnv()
core.debug(`Env: ${JSON.stringify(env, null, 2)}`)

const inputs = getInputs()
core.debug(`Inputs: ${JSON.stringify(inputs, null, 2)}`)

const workingDirectory = path.resolve(
env.GITHUB_WORKSPACE || process.cwd(),
inputs.path
)
core.debug(`Working directory: ${workingDirectory}`)

const hasGitDirectory = await hasLocalGitDirectory({workingDirectory})
core.debug(`Has git directory: ${hasGitDirectory}`)

const filePatterns = await getFilePatterns({
inputs,
workingDirectory
})
core.debug(`File patterns: ${filePatterns}`)

const yamlFilePatterns = await getYamlFilePatterns({
inputs,
workingDirectory
})
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`)

if (
inputs.token &&
Expand All @@ -302,7 +303,13 @@ export async function run(): Promise<void> {
)
}
}
await getChangedFilesFromRESTAPI({inputs, env, workingDirectory})
await getChangedFilesFromRESTAPI({
inputs,
env,
workingDirectory,
filePatterns,
yamlFilePatterns
})
} else {
if (!hasGitDirectory) {
core.setFailed(
Expand All @@ -312,7 +319,13 @@ export async function run(): Promise<void> {
}

core.info('Using local .git directory')
await getChangedFilesFromLocalGit({inputs, env, workingDirectory})
await getChangedFilesFromLocalGit({
inputs,
env,
workingDirectory,
filePatterns,
yamlFilePatterns
})
}
}

Expand Down

0 comments on commit 2f49eb9

Please sign in to comment.