From a099e5db8b1ed2751daff5653efcfcc377d918d1 Mon Sep 17 00:00:00 2001 From: James Ives Date: Wed, 3 Feb 2021 21:26:28 -0500 Subject: [PATCH] Deployment Issues (#583) * Update git.ts * Tests * Update git.ts * Formatting * Update src/git.ts Co-authored-by: Axel Hecht * TestFlag * Logging * Update git.ts Co-authored-by: Axel Hecht --- __tests__/git.test.ts | 37 +++++++++++++++++++++++++++++++++++++ __tests__/main.test.ts | 4 ++-- src/constants.ts | 3 ++- src/git.ts | 23 +++++++++++++++++++++-- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index b6d38e76..4b882304 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -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' diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 3f3f499f..87c3a98d 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -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) }) diff --git a/src/constants.ts b/src/constants.ts index 6f3a66ef..648eae36 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 */ diff --git a/src/git.ts b/src/git.ts index ea432fbf..272b9b1e 100644 --- a/src/git.ts +++ b/src/git.ts @@ -23,6 +23,22 @@ export async function init(action: ActionInterface): Promise { 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 { 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 { 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) {