Skip to content

Commit 36e9415

Browse files
committed
Improe coverage
1 parent e71f256 commit 36e9415

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

__tests__/git.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,30 @@ describe('git', () => {
424424
)
425425
}
426426
})
427+
428+
it('should execute commands if force is false and retry until limit is exceeded', async () => {
429+
Object.assign(action, {
430+
hostname: 'github.com',
431+
silent: false,
432+
folder: 'assets',
433+
branch: 'branch',
434+
force: false,
435+
token: '123',
436+
repositoryName: 'JamesIves/montezuma',
437+
pusher: {
438+
name: 'asd',
439+
email: 'as@cat'
440+
},
441+
isTest: TestFlag.HAS_CHANGED_FILES
442+
})
443+
444+
try {
445+
await deploy(action)
446+
} catch (error) {
447+
expect(error instanceof Error && error.message).toBe(
448+
'The deploy step encountered an error: Attempt limit exceeded ❌'
449+
)
450+
}
451+
})
427452
})
428453
})

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export enum TestFlag {
1010
HAS_CHANGED_FILES = 1 << 1, // Assume changes to commit.
1111
HAS_REMOTE_BRANCH = 1 << 2, // Assume remote repository has existing commits.
1212
UNABLE_TO_REMOVE_ORIGIN = 1 << 3, // Assume we can't remove origin.
13-
UNABLE_TO_UNSET_GIT_CONFIG = 1 << 4 // Assume we can't remove previously set git configs.
13+
UNABLE_TO_UNSET_GIT_CONFIG = 1 << 4, // Assume we can't remove previously set git configs.
14+
HAS_REJECTED_COMMIT = 1 << 5 // Assume commit rejection.
1415
}
1516

1617
/* For more information please refer to the README: https://github.com/JamesIves/github-pages-deploy-action */

src/git.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,35 +240,39 @@ export async function deploy(action: ActionInterface): Promise<Status> {
240240
action.silent
241241
)
242242
} else {
243+
const ATTEMPT_LIMIT = 3
243244
// Attempt to push our changes, but fetch + rebase if there were
244245
// other changes added in the meantime
245-
const ATTEMPT_LIMIT = 3
246246
let attempt = 0
247+
247248
// Keep track of whether the most recent attempt was rejected
248249
let rejected = false
250+
249251
do {
250252
attempt++
253+
251254
if (attempt > ATTEMPT_LIMIT) throw new Error(`Attempt limit exceeded`)
252255

253256
// Handle rejection for the previous attempt first such that, on
254257
// the final attempt, time is not wasted rebasing it when it will
255258
// not be pushed
256259
if (rejected) {
257-
info(`Fetching upstream ${action.branch}...`)
260+
info(`Fetching upstream ${action.branch}`)
258261
await execute(
259262
`git fetch ${action.repositoryPath} ${action.branch}:${action.branch}`,
260263
`${action.workspace}/${temporaryDeploymentDirectory}`,
261264
action.silent
262265
)
263-
info(`Rebasing this deployment onto ${action.branch}...`)
266+
info(`Rebasing this deployment onto ${action.branch}`)
264267
await execute(
265268
`git rebase ${action.branch} ${temporaryDeploymentBranch}`,
266269
`${action.workspace}/${temporaryDeploymentDirectory}`,
267270
action.silent
268271
)
269272
}
270273

271-
info(`Pushing changes... (attempt ${attempt} of ${ATTEMPT_LIMIT})`)
274+
info(`Pushing changes… (attempt ${attempt} of ${ATTEMPT_LIMIT})`)
275+
272276
const pushResult = await execute(
273277
`git push --porcelain ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`,
274278
`${action.workspace}/${temporaryDeploymentDirectory}`,
@@ -277,8 +281,10 @@ export async function deploy(action: ActionInterface): Promise<Status> {
277281
)
278282

279283
rejected =
284+
Boolean(action.isTest) ||
280285
pushResult.stdout.includes(`[rejected]`) ||
281286
pushResult.stdout.includes(`[remote rejected]`)
287+
282288
if (rejected) info('Updates were rejected')
283289

284290
// If the push failed for any reason other than being rejected,

0 commit comments

Comments
 (0)