From 30012e7d8cc9526b756abfa544656ab371a7b87a Mon Sep 17 00:00:00 2001 From: Niya Ma Date: Tue, 19 Jul 2022 15:32:06 -0700 Subject: [PATCH 1/5] 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', }, From afe08f2f1bc45766984cbefedcfea25eb59ebc64 Mon Sep 17 00:00:00 2001 From: Niya Ma Date: Tue, 19 Jul 2022 15:51:22 -0700 Subject: [PATCH 2/5] update public API signature Signed-off-by: Niya Ma --- packages/backend-common/api-report.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index 6059e68792..f2f773d6ff 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -338,6 +338,7 @@ export class Git { dir: string; remote: string; url: string; + force?: boolean; }): Promise; clone(options: { url: string; @@ -363,6 +364,8 @@ export class Git { dir: string; fullName?: boolean; }): Promise; + // (undocumented) + deleteRemote(options: { dir: string; remote: string }): Promise; fetch(options: { dir: string; remote?: string }): Promise; // (undocumented) static fromAuth: (options: { @@ -387,7 +390,12 @@ export class Git { }; }): Promise; // (undocumented) - push(options: { dir: string; remote: string }): Promise; + push(options: { + dir: string; + remote: string; + remoteRef?: string; + force?: boolean; + }): Promise; readCommit(options: { dir: string; sha: string }): Promise; resolveRef(options: { dir: string; ref: string }): Promise; } From a59960bb6a11f46a708af908da329ba8df32d8d4 Mon Sep 17 00:00:00 2001 From: Niya Ma <78030299+niya-ma-1@users.noreply.github.com> Date: Wed, 20 Jul 2022 08:44:40 -0700 Subject: [PATCH 3/5] Update .changeset/khaki-meals-hammer.md Co-authored-by: Ben Lambert Signed-off-by: Niya Ma <78030299+niya-ma-1@users.noreply.github.com> --- .changeset/khaki-meals-hammer.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/khaki-meals-hammer.md b/.changeset/khaki-meals-hammer.md index 000493493b..e8a61fa476 100644 --- a/.changeset/khaki-meals-hammer.md +++ b/.changeset/khaki-meals-hammer.md @@ -2,4 +2,5 @@ '@backstage/backend-common': patch --- -Add force push, force addRemote, and deleteRemote to git wrapper. **_All parameters added are optional_** +- Added `force` and `remoteRef` option to `push` method in `git` actions +- Added `addRemote` and `deleteRemote` methods to `git` actions From 108f0de8df23f91f59f5376a9ba019a57ff5f0cb Mon Sep 17 00:00:00 2001 From: Niya Ma Date: Wed, 20 Jul 2022 09:27:30 -0700 Subject: [PATCH 4/5] fix duplicate variable Signed-off-by: Niya Ma --- packages/backend-common/src/scm/git.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/backend-common/src/scm/git.ts b/packages/backend-common/src/scm/git.ts index 814f5ff422..c8e26420ec 100644 --- a/packages/backend-common/src/scm/git.ts +++ b/packages/backend-common/src/scm/git.ts @@ -225,8 +225,7 @@ export class Git { headers: { 'user-agent': 'git/@isomorphic-git', }, - remote: remote, - remoteRef: remoteRef, + remote, onAuth: this.onAuth, }); } catch (ex) { From f4c1b8e8e9aa971c21b1625d94eda7bcea31e4c0 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Wed, 20 Jul 2022 23:27:59 +0200 Subject: [PATCH 5/5] Update versioning Signed-off-by: Ben Lambert --- .changeset/khaki-meals-hammer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/khaki-meals-hammer.md b/.changeset/khaki-meals-hammer.md index e8a61fa476..1deee2706d 100644 --- a/.changeset/khaki-meals-hammer.md +++ b/.changeset/khaki-meals-hammer.md @@ -1,5 +1,5 @@ --- -'@backstage/backend-common': patch +'@backstage/backend-common': minor --- - Added `force` and `remoteRef` option to `push` method in `git` actions