From 30012e7d8cc9526b756abfa544656ab371a7b87a Mon Sep 17 00:00:00 2001 From: Niya Ma Date: Tue, 19 Jul 2022 15:32:06 -0700 Subject: [PATCH] Add force options to git wrapper Signed-off-by: Niya Ma --- .changeset/khaki-meals-hammer.md | 5 +++ packages/backend-common/src/scm/git.test.ts | 34 ++++++++++++++++++--- packages/backend-common/src/scm/git.ts | 22 ++++++++++--- 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 .changeset/khaki-meals-hammer.md diff --git a/.changeset/khaki-meals-hammer.md b/.changeset/khaki-meals-hammer.md new file mode 100644 index 0000000000..000493493b --- /dev/null +++ b/.changeset/khaki-meals-hammer.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': patch +--- + +Add force push, force addRemote, and deleteRemote to git wrapper. **_All parameters added are optional_** diff --git a/packages/backend-common/src/scm/git.test.ts b/packages/backend-common/src/scm/git.test.ts index 7b366e01c8..8d3cc05da8 100644 --- a/packages/backend-common/src/scm/git.test.ts +++ b/packages/backend-common/src/scm/git.test.ts @@ -48,14 +48,32 @@ describe('Git', () => { const dir = 'mockdirectory'; const remote = 'origin'; const url = 'git@github.com/something/sads'; + const force = true; - await git.addRemote({ dir, remote, url }); + await git.addRemote({ dir, remote, url, force }); expect(isomorphic.addRemote).toHaveBeenCalledWith({ fs, dir, remote, url, + force, + }); + }); + }); + + describe('deleteRemote', () => { + it('should call isomorphic-git with the correct arguments', async () => { + const git = Git.fromAuth({}); + const dir = 'mockdirectory'; + const remote = 'origin'; + + await git.deleteRemote({ dir, remote }); + + expect(isomorphic.deleteRemote).toHaveBeenCalledWith({ + fs, + dir, + remote, }); }); }); @@ -295,14 +313,18 @@ describe('Git', () => { password: 'hunter2', }; const git = Git.fromAuth(auth); + const remoteRef = 'master'; + const force = true; - await git.push({ dir, remote }); + await git.push({ dir, remote, remoteRef, force }); expect(isomorphic.push).toHaveBeenCalledWith({ fs, http, remote, dir, + remoteRef, + force, onProgress: expect.any(Function), headers: { 'user-agent': 'git/@isomorphic-git', @@ -318,8 +340,10 @@ describe('Git', () => { password: 'hunter2', }; const git = Git.fromAuth(auth); + const remoteRef = 'master'; + const force = true; - await git.push({ remote, dir }); + await git.push({ remote, dir, remoteRef, force }); const { onAuth } = ( isomorphic.push as unknown as jest.Mock @@ -336,6 +360,8 @@ describe('Git', () => { password: 'hunter2', }; const git = Git.fromAuth(auth); + const remoteRef = 'master'; + const force = true; (isomorphic.push as jest.Mock).mockImplementation(() => { const error: Error & { data?: unknown } = new Error('mock error'); @@ -344,7 +370,7 @@ describe('Git', () => { throw error; }); - await expect(git.push({ remote, dir })).rejects.toThrow( + await expect(git.push({ remote, dir, remoteRef, force })).rejects.toThrow( 'more information here', ); }); diff --git a/packages/backend-common/src/scm/git.ts b/packages/backend-common/src/scm/git.ts index e76f675821..d44b22c41c 100644 --- a/packages/backend-common/src/scm/git.ts +++ b/packages/backend-common/src/scm/git.ts @@ -58,12 +58,19 @@ export class Git { dir: string; remote: string; url: string; + force?: boolean; }): Promise { - const { dir, url, remote } = options; + const { dir, url, remote, force } = options; this.config.logger?.info( `Creating new remote {dir=${dir},remote=${remote},url=${url}}`, ); - return git.addRemote({ fs, dir, remote, url }); + return git.addRemote({ fs, dir, remote, url, force }); + } + + async deleteRemote(options: { dir: string; remote: string }): Promise { + const { dir, remote } = options; + this.config.logger?.info(`Deleting remote {dir=${dir},remote=${remote}}`); + return git.deleteRemote({ fs, dir, remote }); } async commit(options: { @@ -190,8 +197,13 @@ export class Git { }); } - async push(options: { dir: string; remote: string }) { - const { dir, remote } = options; + async push(options: { + dir: string; + remote: string; + remoteRef?: string; + force?: boolean; + }) { + const { dir, remote, remoteRef, force } = options; this.config.logger?.info( `Pushing directory to remote {dir=${dir},remote=${remote}}`, ); @@ -201,6 +213,8 @@ export class Git { dir, http, onProgress: this.onProgressHandler(), + remoteRef, + force, headers: { 'user-agent': 'git/@isomorphic-git', },