diff --git a/src/server/routers/git.ts b/src/server/routers/git.ts index b7e389f..73cae6e 100644 --- a/src/server/routers/git.ts +++ b/src/server/routers/git.ts @@ -275,9 +275,9 @@ export const gitRouter = router({ data: newRepo.data, } } catch (e) { - // Clean up the repo made + // Clean up the private mirror repo made await privateOctokit.rest.repos.delete({ - owner: orgData.data.login, + owner: privateOrg, repo: opts.input.newRepoName, }) diff --git a/test/server/git.test.ts b/test/server/git.test.ts index 3418a28..1a92fbc 100644 --- a/test/server/git.test.ts +++ b/test/server/git.test.ts @@ -187,6 +187,46 @@ describe('Git router', () => { it('should cleanup repos when there is an error', async () => { const caller = gitRouter.createCaller(createTestContext()) + const configSpy = jest.spyOn(config, 'getConfig').mockResolvedValue({ + publicOrg: 'github', + privateOrg: 'github', + }) + + om.mockFunctions.rest.apps.getOrgInstallation.mockResolvedValue( + fakeOrgInstallation, + ) + om.mockFunctions.rest.orgs.get.mockResolvedValue(fakeOrg) + om.mockFunctions.rest.repos.get.mockResolvedValueOnce(repoNotFound) + om.mockFunctions.rest.repos.get.mockResolvedValueOnce(fakeMirrorRepo) + om.mockFunctions.rest.repos.delete.mockResolvedValue({}) + + stubbedGit.clone.mockRejectedValue(new Error('clone error')) + + await caller + .createMirror({ + forkId: 'test', + orgId: 'test', + forkRepoName: 'fork-test', + forkRepoOwner: 'github', + newBranchName: 'test', + newRepoName: 'test', + }) + .catch((error) => { + expect(error.message).toEqual('clone error') + }) + + expect(configSpy).toHaveBeenCalledTimes(1) + expect(om.mockFunctions.rest.repos.get).toHaveBeenCalledTimes(2) + expect(om.mockFunctions.rest.repos.delete).toHaveBeenCalledWith({ + owner: 'github', + repo: 'test', + }) + expect(stubbedGit.clone).toHaveBeenCalledTimes(1) + }) + + it('dual-org: should cleanup repos when there is an error', async () => { + const caller = gitRouter.createCaller(createTestContext()) + const configSpy = jest.spyOn(config, 'getConfig').mockResolvedValue({ publicOrg: 'github', privateOrg: 'github-test', @@ -217,7 +257,10 @@ describe('Git router', () => { expect(configSpy).toHaveBeenCalledTimes(1) expect(om.mockFunctions.rest.repos.get).toHaveBeenCalledTimes(2) - expect(om.mockFunctions.rest.repos.delete).toHaveBeenCalledTimes(1) + expect(om.mockFunctions.rest.repos.delete).toHaveBeenCalledWith({ + owner: 'github-test', + repo: 'test', + }) expect(stubbedGit.clone).toHaveBeenCalledTimes(1) }) })