Deployment Issues (#583)

* Update git.ts

* Tests

* Update git.ts

* Formatting

* Update src/git.ts

Co-authored-by: Axel Hecht <axel@pike.org>

* TestFlag

* Logging

* Update git.ts

Co-authored-by: Axel Hecht <axel@pike.org>
This commit is contained in:
James Ives 2021-02-03 21:26:28 -05:00 committed by GitHub
parent 64eb7112e4
commit a099e5db8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 5 deletions

View File

@ -40,6 +40,24 @@ describe('git', () => {
})
describe('init', () => {
it('should execute commands', async () => {
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
await init(action)
expect(execute).toBeCalledTimes(4)
})
it('should catch when a function throws an error', async () => {
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
@ -66,6 +84,24 @@ describe('git', () => {
)
}
})
it('should correctly continue when it cannot remove origin', async () => {
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.UNABLE_TO_REMOVE_ORIGIN
})
await init(action)
expect(execute).toBeCalledTimes(4)
})
})
describe('deploy', () => {
@ -75,6 +111,7 @@ describe('git', () => {
folder: 'assets',
branch: 'branch',
token: '123',
repositoryName: 'JamesIves/montezuma',
pusher: {
name: 'asd',
email: 'as@cat'

View File

@ -49,7 +49,7 @@ describe('main', () => {
debug: true
})
await run(action)
expect(execute).toBeCalledTimes(10)
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
expect(exportVariable).toBeCalledTimes(1)
})
@ -68,7 +68,7 @@ describe('main', () => {
isTest: TestFlag.HAS_CHANGED_FILES
})
await run(action)
expect(execute).toBeCalledTimes(13)
expect(execute).toBeCalledTimes(15)
expect(rmRF).toBeCalledTimes(1)
expect(exportVariable).toBeCalledTimes(1)
})

View File

@ -8,7 +8,8 @@ const {pusher, repository} = github.context.payload
export enum TestFlag {
NONE = 0,
HAS_CHANGED_FILES = 1 << 1, // Assume changes to commit
HAS_REMOTE_BRANCH = 1 << 2 // Assume remote repository has existing commits
HAS_REMOTE_BRANCH = 1 << 2, // Assume remote repository has existing commits
UNABLE_TO_REMOVE_ORIGIN = 1 << 3 // Assume we can't remove origin
}
/* For more information please refer to the README: https://github.com/JamesIves/github-pages-deploy-action */

View File

@ -23,6 +23,22 @@ export async function init(action: ActionInterface): Promise<void | Error> {
action.silent
)
try {
await execute(`git remote rm origin`, action.workspace, action.silent)
if (action.isTest === TestFlag.UNABLE_TO_REMOVE_ORIGIN) {
throw new Error()
}
} catch {
info('Attempted to remove origin but failed, continuing…')
}
await execute(
`git remote add origin ${action.repositoryPath}`,
action.workspace,
action.silent
)
info('Git configured… 🔧')
} catch (error) {
throw new Error(
@ -48,7 +64,9 @@ export async function deploy(action: ActionInterface): Promise<Status> {
const commitMessage = !isNullOrUndefined(action.commitMessage)
? (action.commitMessage as string)
: `Deploying to ${action.branch}${
process.env.GITHUB_SHA ? ` from @ ${process.env.GITHUB_SHA}` : ''
process.env.GITHUB_SHA
? ` from @ ${process.env.GITHUB_REPOSITORY}@${process.env.GITHUB_SHA}`
: ''
} 🚀`
// Checks to see if the remote exists prior to deploying.
@ -113,12 +131,13 @@ export async function deploy(action: ActionInterface): Promise<Status> {
branchExists && action.singleCommit
? `git diff origin/${action.branch}`
: `git status --porcelain`
info(`Checking if there are files to commit…`)
const hasFilesToCommit =
action.isTest & TestFlag.HAS_CHANGED_FILES ||
(await execute(
checkGitStatus,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
true // This output is always silenced due to the large output it creates.
))
if (!hasFilesToCommit) {