-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9af2bbf
commit a400e80
Showing
28 changed files
with
845 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
module.exports = { | ||
...require('jest-mock-vscode').createVSCodeMock(jest), | ||
env: { | ||
uriScheme: 'vscode' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { CheckoutBranchUriHandlerAction } from './checkoutBranch'; | ||
|
||
describe('CheckoutBranchUriHandlerAction', () => { | ||
const mockAnalyticsApi = { | ||
fireDeepLinkEvent: jest.fn(), | ||
}; | ||
const mockCheckoutHelper = { | ||
checkoutRef: jest.fn().mockResolvedValue(true), | ||
}; | ||
let action: CheckoutBranchUriHandlerAction; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
action = new CheckoutBranchUriHandlerAction(mockCheckoutHelper as any, mockAnalyticsApi as any); | ||
}); | ||
|
||
describe('isAccepted', () => { | ||
it('only accepts URIs ending with checkoutBranch', () => { | ||
expect(action.isAccepted(Uri.parse('https://some-uri/checkoutBranch'))).toBe(true); | ||
expect(action.isAccepted(Uri.parse('https://some-uri/otherThing'))).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('handle', () => { | ||
it('throws if required query params are missing', async () => { | ||
await expect(action.handle(Uri.parse('https://some-uri/checkoutBranch'))).rejects.toThrow(); | ||
await expect( | ||
action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=...&ref=...')), | ||
).rejects.toThrow(); | ||
await expect( | ||
action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=...&refType=...')), | ||
).rejects.toThrow(); | ||
await expect( | ||
action.handle(Uri.parse('https://some-uri/checkoutBranch?ref=...&refType=...')), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('checks out the branch and fires an event on success', async () => { | ||
mockCheckoutHelper.checkoutRef.mockResolvedValue(true); | ||
await action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=one&ref=two&refType=three')); | ||
|
||
expect(mockCheckoutHelper.checkoutRef).toHaveBeenCalledWith('one', 'two', 'three', ''); | ||
expect(mockAnalyticsApi.fireDeepLinkEvent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows an error message on failure', async () => { | ||
mockCheckoutHelper.checkoutRef.mockRejectedValue(new Error('oh no')); | ||
await action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=one&ref=two&refType=three')); | ||
|
||
expect(window.showErrorMessage).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { isAcceptedBySuffix, UriHandlerAction } from '../uriHandlerAction'; | ||
import { CheckoutHelper } from '../../bitbucket/interfaces'; | ||
import { AnalyticsApi } from '../../lib/analyticsApi'; | ||
import { Logger } from '../../logger'; | ||
|
||
export class CheckoutBranchUriHandlerAction implements UriHandlerAction { | ||
constructor( | ||
private bitbucketHelper: CheckoutHelper, | ||
private analyticsApi: AnalyticsApi, | ||
) {} | ||
|
||
isAccepted(uri: Uri): boolean { | ||
return isAcceptedBySuffix(uri, 'checkoutBranch'); | ||
} | ||
|
||
async handle(uri: Uri) { | ||
const query = new URLSearchParams(uri.query); | ||
const cloneUrl = decodeURIComponent(query.get('cloneUrl') || ''); | ||
const sourceCloneUrl = decodeURIComponent(query.get('sourceCloneUrl') || ''); //For branches originating from a forked repo | ||
const ref = query.get('ref'); | ||
const refType = query.get('refType'); | ||
if (!ref || !cloneUrl || !refType) { | ||
throw new Error(`Query params are missing data: ${query}`); | ||
} | ||
|
||
try { | ||
const success = await this.bitbucketHelper.checkoutRef(cloneUrl, ref, refType, sourceCloneUrl); | ||
|
||
if (success) { | ||
this.analyticsApi.fireDeepLinkEvent( | ||
decodeURIComponent(query.get('source') || 'unknown'), | ||
'checkoutBranch', | ||
); | ||
} | ||
} catch (e) { | ||
Logger.debug('error checkout out branch:', e); | ||
window.showErrorMessage('Error checkout out branch (check log for details)'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { CloneRepositoryUriHandlerAction } from './cloneRepository'; | ||
|
||
describe('CloneRepositoryUriHandlerAction', () => { | ||
const mockAnalyticsApi = { | ||
fireDeepLinkEvent: jest.fn(), | ||
}; | ||
const mockCheckoutHelper = { | ||
cloneRepository: jest.fn(), | ||
}; | ||
let action: CloneRepositoryUriHandlerAction; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
action = new CloneRepositoryUriHandlerAction(mockCheckoutHelper as any, mockAnalyticsApi as any); | ||
}); | ||
|
||
describe('isAccepted', () => { | ||
it('only accepts URIs ending with cloneRepository', () => { | ||
expect(action.isAccepted(Uri.parse('https://some-uri/cloneRepository'))).toBe(true); | ||
expect(action.isAccepted(Uri.parse('https://some-uri/otherThing'))).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('handle', () => { | ||
it('throws if required query params are missing', async () => { | ||
await expect(action.handle(Uri.parse('https://some-uri/cloneRepository'))).rejects.toThrow(); | ||
}); | ||
|
||
it('clones the repo and fires an event on success', async () => { | ||
mockCheckoutHelper.cloneRepository.mockResolvedValue(null); | ||
await action.handle(Uri.parse('https://some-uri/cloneRepository?q=one')); | ||
|
||
expect(mockCheckoutHelper.cloneRepository).toHaveBeenCalledWith('one'); | ||
expect(mockAnalyticsApi.fireDeepLinkEvent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows an error message on failure', async () => { | ||
mockCheckoutHelper.cloneRepository.mockRejectedValue(new Error('oh no')); | ||
await action.handle(Uri.parse('https://some-uri/cloneRepository?q=one')); | ||
|
||
expect(window.showErrorMessage).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.