Skip to content

Commit

Permalink
Merge pull request #109 from github-community-projects/ajhenry/fix-mi…
Browse files Browse the repository at this point in the history
…rror-cleanup

fix: cleanup the private mirror if it fails to create
  • Loading branch information
ajhenry authored Apr 30, 2024
2 parents 28df5e2 + 5fb1814 commit 15c431e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/server/routers/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
45 changes: 44 additions & 1 deletion test/server/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
})
})

0 comments on commit 15c431e

Please sign in to comment.